summaryrefslogtreecommitdiff
path: root/src/game.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/game.c')
-rw-r--r--src/game.c89
1 files changed, 45 insertions, 44 deletions
diff --git a/src/game.c b/src/game.c
index 820939f..8412d83 100644
--- a/src/game.c
+++ b/src/game.c
@@ -17,19 +17,21 @@ void game_init(struct GameState *game_state, int field_width, int field_height)
return;
}
- game_state->player.angle = 0;
- game_state->player.speed = 0;
game_state->player.mass = 10;
game_state->player.size = 30;
- game_state->player.x = field_width / 2;
- game_state->player.y = field_height / 2;
game_state->player.color = 0x888888;
- game_state->thrust_vector01.angle = 0;
- game_state->thrust_vector01.length = 0;
- game_state->thrust_vector02.angle = 0;
- game_state->thrust_vector02.length = 0;
- game_state->thrust_vector_sum.angle = 0;
- game_state->thrust_vector_sum.length = 0;
+ game_state->player.position.x = field_width / 2;
+ game_state->player.position.y = field_height / 2;
+ game_state->player.velocity.x = 0;
+ game_state->player.velocity.y = 0;
+
+ game_state->thrust_vector01.x = 0;
+ game_state->thrust_vector01.y = 0;
+ game_state->thrust_vector02.x = 0;
+ game_state->thrust_vector02.y = 0;
+ game_state->thrust_vector_sum.x = 0;
+ game_state->thrust_vector_sum.y = 0;
+
game_state->view.dx = 0;
game_state->view.dy = 0;
game_state->view.zoom = 1;
@@ -61,35 +63,33 @@ void game_update(struct GameState *game_state, struct GameControllerInput game_i
return;
}
- game_state->thrust_vector01.angle = atan2f(game_input.left_stick_y, game_input.left_stick_x);
- game_state->thrust_vector01.length = hypotf(game_input.left_stick_x, game_input.left_stick_y);
- //printf("(lx: %f, ly: %f, thrust_a: %f, thrust_l: %f)\n",
- // game_input.left_stick_x,
- // game_input.left_stick_y,
- // game_state->thrust_vector01.angle,
- // game_state->thrust_vector01.length);
-
- game_state->thrust_vector02.angle = atan2f(game_input.right_stick_y, game_input.right_stick_x);
- game_state->thrust_vector02.length = hypotf(game_input.right_stick_x, game_input.right_stick_y);
- //printf("(rx: %f, ry: %f, thrust_a: %f, thrust_l: %f)\n",
- // game_input.right_stick_x,
- // game_input.right_stick_y,
- // game_state->thrust_vector02.angle,
- // game_state->thrust_vector02.length);
+ game_state->thrust_vector01.x = game_input.left_stick_x;
+ game_state->thrust_vector01.y = game_input.left_stick_y;
+ //printf("(lx: %f, ly: %f, thrust_x: %f, thrust_y: %f)\n", game_input.left_stick_x, game_input.left_stick_y, game_state->thrust_vector01.x, game_state->thrust_vector01.y);
+
+ game_state->thrust_vector02.x = game_input.right_stick_x;
+ game_state->thrust_vector02.y = game_input.right_stick_y;
+ //printf("(rx: %f, ry: %f, thrust_x: %f, thrust_y: %f)\n", game_input.right_stick_x, game_input.right_stick_y, game_state->thrust_vector02.x, game_state->thrust_vector02.y);
game_state->thrust_vector_sum = vec2d_add(
- game_state->thrust_vector01.angle,
- game_state->thrust_vector01.length,
- game_state->thrust_vector02.angle,
- game_state->thrust_vector02.length);
+ game_state->thrust_vector01.x,
+ game_state->thrust_vector01.y,
+ game_state->thrust_vector02.x,
+ game_state->thrust_vector02.y);
+
+ game_state->thrust_vector_sum = vec2d_scale(
+ game_state->thrust_vector_sum.x,
+ game_state->thrust_vector_sum.y,
+ 0.01f);
+ //printf("(total_thrust_x: %f, total_thrust_y: %f)\n", game_state->thrust_vector_sum.x, game_state->thrust_vector_sum.y);
struct Entity *player = &game_state->player;
- entity_accelerate(player, game_state->thrust_vector_sum.angle, game_state->thrust_vector_sum.length * 0.01f);
+ entity_accelerate(player, &game_state->thrust_vector_sum);
- player->x += player->speed * cosf(player->angle);
- player->y += player->speed * sinf(player->angle);
- player->x = wrap(player->x, 0, field_width);
- player->y = wrap(player->y, 0, field_height);
+ player->position.x += player->velocity.x;
+ player->position.y += player->velocity.y;
+ player->position.x = wrap(player->position.x, 0, field_width);
+ player->position.y = wrap(player->position.y, 0, field_height);
}
@@ -101,14 +101,15 @@ void game_render(struct OffscreenBuffer *buffer, float dt, struct GameState *gam
return;
}
struct Entity player = game_state->player;
- game_render_circle(buffer, player.x, player.y, player.size, player.color);
- game_render_vector(buffer, &game_state->thrust_vector01, player.x, player.y, 5, 0x0000FF);
- game_render_vector(buffer, &game_state->thrust_vector02, player.x, player.y, 5, 0xFF0000);
- game_render_vector(buffer, &game_state->thrust_vector_sum, player.x, player.y, 5, 0xFF00FF);
+ game_render_circle(buffer, player.position.x, player.position.y, player.size, player.color);
+ game_render_vector(buffer, &game_state->thrust_vector01, player.position.x, player.position.y, 5, 0x0000FF);
+ game_render_vector(buffer, &game_state->thrust_vector02, player.position.x, player.position.y, 5, 0xFF0000);
+ game_render_vector(buffer, &game_state->thrust_vector_sum, player.position.x, player.position.y, 5, 0xFF00FF);
+ struct Vec2d player_vec = vec2d_scale(player.velocity.x, player.velocity.y, 100);
game_render_circle(buffer,
- player.x + 100 * (player.speed * cosf(player.angle)),
- player.y + 100 * (player.speed * sinf(player.angle)),
+ player.position.x + player_vec.x,
+ player.position.y + player_vec.y,
5,
0x00FF00);
}
@@ -319,10 +320,10 @@ void game_render_line(struct OffscreenBuffer *buffer, float x0, float y0, float
void game_render_vector(struct OffscreenBuffer *buffer, struct Vec2d *v, float x0, float y0, float radius, uint32_t color)
{
- float x1 = x0 + 100 * (v->length * cosf(v->angle));
- float y1 = y0 + 100 * (v->length * sinf(v->angle));
- game_render_line(buffer, x0, y0, x1, y1, color);
- game_render_circle(buffer, x1, y1, radius, color);
+ struct Vec2d vec = vec2d_scale(v->x, v->y, 100);
+ vec = vec2d_add(x0, y0, vec.x, vec.y);
+ game_render_line(buffer, x0, y0, vec.x, vec.y, color);
+ game_render_circle(buffer, vec.x, vec.y, radius, color);
}