Skip to content

Commit

Permalink
[common] use vectors in throw_bomb()
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Oct 7, 2024
1 parent 9c1617a commit 3a8796f
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 11 deletions.
2 changes: 1 addition & 1 deletion client.c3
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ fn void key_down(uint key_code) @extern("key_down") @wasm {
message.kind = AMMA_THROWING;
platform_send_message(message);
} else {
common::throw_bomb(me.position.x, me.position.y, me.direction, &common::bombs);
common::throw_bomb(me.position, me.direction, &common::bombs);
}
}
}
Expand Down
Binary file modified client.wasm
Binary file not shown.
16 changes: 7 additions & 9 deletions common.c3
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ fn Scene *allocate_default_scene() {
return scene;
}

fn bool Scene.get_tile(Scene *scene, Vector2 p) {
fn bool Scene.get_tile(&scene, Vector2 p) {
int x = (int)math::floor(p.x);
int y = (int)math::floor(p.y);
if (!(0 <= x && x < scene.width && 0 <= y && y < scene.height)) return false;
return scene.walls[y*scene.width + x];
}

fn bool scene_can_rectangle_fit_here(Scene *scene, float px, float py, float sx, float sy) {
fn bool Scene.can_rectangle_fit_here(&scene, float px, float py, float sx, float sy) {
int x1 = (int)math::floor(px - sx*0.5f);
int x2 = (int)math::floor(px + sx*0.5f);
int y1 = (int)math::floor(py - sy*0.5f);
Expand Down Expand Up @@ -230,15 +230,13 @@ fn Bombs* allocate_bombs() @extern("allocate_bombs") @wasm {
return mem::new(Bombs);
}

fn int throw_bomb(float player_position_x, float player_position_y, float player_direction, Bombs *bombs) {
fn int throw_bomb(Vector2 position, float direction, Bombs *bombs) {
foreach (index, &bomb: *bombs) {
if (bomb.lifetime <= 0) {
bomb.lifetime = BOMB_LIFETIME;
bomb.position.x = player_position_x;
bomb.position.y = player_position_y;
bomb.position = position;
bomb.position_z = 0.6;
bomb.velocity.x = math::cos(player_direction);
bomb.velocity.y = math::sin(player_direction);
bomb.velocity = from_polar(direction, 1.0f);
bomb.velocity_z = 0.5;
bomb.velocity *= BOMB_THROW_VELOCITY;
bomb.velocity_z *= BOMB_THROW_VELOCITY;
Expand Down Expand Up @@ -409,11 +407,11 @@ fn void update_player(Player *player, Scene *scene, float delta_time) {
player.direction = (player.direction + angular_velocity*delta_time)%(2*(float)math::PI);

float nx = player.position.x + control_velocity.x*delta_time;
if (scene_can_rectangle_fit_here(scene, nx, player.position.y, PLAYER_SIZE, PLAYER_SIZE)) {
if (scene.can_rectangle_fit_here(nx, player.position.y, PLAYER_SIZE, PLAYER_SIZE)) {
player.position.x = nx;
}
float ny = player.position.y + control_velocity.y*delta_time;
if (scene_can_rectangle_fit_here(scene, player.position.x, ny, PLAYER_SIZE, PLAYER_SIZE)) {
if (scene.can_rectangle_fit_here(player.position.x, ny, PLAYER_SIZE, PLAYER_SIZE)) {
player.position.y = ny;
}
}
Expand Down
2 changes: 1 addition & 1 deletion server.c3
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ List(<usz>) thrown_bombs;

fn void throw_bomb_on_server_side(uint player_id, Bombs *bombs) {
if (try player = players.get(player_id)) {
int index = common::throw_bomb(player.player.position.x, player.player.position.y, player.player.direction, bombs);
int index = common::throw_bomb(player.player.position, player.player.direction, bombs);
if (index >= 0) thrown_bombs.push(index);
}
}
Expand Down
Binary file modified server.wasm
Binary file not shown.

0 comments on commit 3a8796f

Please sign in to comment.