Skip to content

Commit

Permalink
Collapse some of the arguments in item collection logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Oct 9, 2024
1 parent c9cae81 commit 9d7769e
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 13 deletions.
4 changes: 2 additions & 2 deletions client.c3
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,8 @@ fn void render_items(SpritePool *sprite_pool, Item[]* items, float time, Image *
}

fn void update_items_offline(Item[]* items) {
foreach (item_index, item: *items) {
if (common::collect_item(me.position.x, me.position.y, items, item_index)) {
foreach (item_index, &item: *items) {
if (common::collect_item(me, item)) {
platform_play_sound(ITEM_PICKUP, me.position.x, me.position.y, item.position.x, item.position.y);
}
}
Expand Down
Binary file modified client.wasm
Binary file not shown.
6 changes: 2 additions & 4 deletions common.c3
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,9 @@ Item[] items = {
},
};

fn bool collect_item(float player_position_x, float player_position_y, Item[] *items, usz item_index) @extern("collect_item") @wasm {
if (item_index >= items.len) return false;
Item *item = &(*items)[item_index];
fn bool collect_item(Player player, Item *item) {
if (!item.alive) return false;
if (Vector2{player_position_x, player_position_y}.distance(item.position) >= PLAYER_RADIUS) return false;
if (player.position.distance(item.position) >= PLAYER_RADIUS) return false;
item.alive = false;
return true;
}
Expand Down
12 changes: 5 additions & 7 deletions server.c3
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ fn void send_message_and_update_stats(uint player_id, void* message) {

List(<usz>) collected_items;

fn void collect_items_by_player_at(float player_position_x, float player_position_y, Item[] *items) {
foreach (item_index, item: *items) {
if (item.alive) {
if (common::collect_item(player_position_x, player_position_y, items, item_index)) {
collected_items.push(item_index);
}
fn void collect_items_by_player(Player player, Item[] *items) {
foreach (index, &item: *items) {
if (common::collect_item(player, item)) {
collected_items.push(index);
}
}
}
Expand Down Expand Up @@ -317,7 +315,7 @@ fn void process_world_simulation(Item[] *items, Scene *scene, Bombs *bombs, floa
// Simulating the world for one server tick.
players.@each_entry(; PlayerOnServerEntry* entry) {
common::update_player(&entry.value.player, scene, delta_time);
collect_items_by_player_at(entry.value.player.position.x, entry.value.player.position.y, items);
collect_items_by_player(entry.value.player, items);
};

ItemsCollectedBatchMessage *items_collected_batch_message = collected_items_as_batch_message(items);
Expand Down
Binary file modified server.wasm
Binary file not shown.

0 comments on commit 9d7769e

Please sign in to comment.