From 98adee996c5db5dae340ea76f746e799fdf41e3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Thu, 9 Sep 2021 09:46:22 +0200 Subject: [PATCH] no item merging for now --- code/game/src/items.c | 1 + code/modules/modules/components.h | 1 + code/modules/modules/systems.c | 1 + code/modules/source/system_items.c | 97 ++++++++++++++++-------------- 4 files changed, 56 insertions(+), 44 deletions(-) diff --git a/code/game/src/items.c b/code/game/src/items.c index d9de061..e372816 100644 --- a/code/game/src/items.c +++ b/code/game/src/items.c @@ -18,6 +18,7 @@ uint64_t item_spawn(item_kind kind, uint32_t qty) { *d = (ItemDrop){ .kind = kind, .quantity = qty, + .merger_time = 0, }; return (uint64_t)e; diff --git a/code/modules/modules/components.h b/code/modules/modules/components.h index d6982c4..828a4e3 100644 --- a/code/modules/modules/components.h +++ b/code/modules/modules/components.h @@ -91,6 +91,7 @@ typedef struct { typedef struct { uint16_t kind; uint32_t quantity; + float merger_time; } ItemDrop; typedef struct { diff --git a/code/modules/modules/systems.c b/code/modules/modules/systems.c index 70373db..f8f4c78 100644 --- a/code/modules/modules/systems.c +++ b/code/modules/modules/systems.c @@ -161,6 +161,7 @@ void SystemsImport(ecs_world_t *ecs) { ECS_SYSTEM(ecs, PickItem, EcsPostUpdate, components.Input, components.Position, components.Inventory, !components.IsInVehicle); ECS_SYSTEM(ecs, DropItem, EcsPostUpdate, components.Input, components.Position, components.Inventory, !components.IsInVehicle); ECS_SYSTEM(ecs, SwapItems, EcsPostUpdate, components.Input, components.Inventory); + //ECS_SYSTEM(ecs, MergeItems, EcsPostUpdate, components.Position, components.ItemDrop); ECS_SYSTEM(ecs, UseItem, EcsPostUpdate, components.Input, components.Position, components.Inventory, !components.IsInVehicle); ECS_SYSTEM(ecs, UpdateTrackerPos, EcsPostUpdate, components.Position, components.Velocity); diff --git a/code/modules/source/system_items.c b/code/modules/source/system_items.c index a545db5..dce0a01 100644 --- a/code/modules/source/system_items.c +++ b/code/modules/source/system_items.c @@ -1,6 +1,7 @@ #include "items.h" #define ITEM_PICK_RADIUS 25.0f +#define ITEM_MERGER_RADIUS 75.0f #define ITEM_ATTRACT_RADIUS 75.0f #define ITEM_ATTRACT_FORCE 0.63f @@ -46,6 +47,7 @@ void PickItem(ecs_iter_t *it) { } #define ITEM_DROP_PICKUP_TIME 2.5f +#define ITEM_DROP_MERGER_TIME 6.5f void DropItem(ecs_iter_t *it) { Input *in = ecs_column(it, Input, 1); @@ -60,62 +62,69 @@ void DropItem(ecs_iter_t *it) { if (item->quantity <= 0) continue; - bool is_item_nearby = false; + uint32_t dropped_count = item->quantity; + if (in[i].sprint) { + dropped_count /= 2; + } else if (in[i].ctrl) { + dropped_count = dropped_count > 0 ? 1 : 0; + } + + if (dropped_count == 0) + continue; + + ecs_entity_t te = item_spawn(item->kind, dropped_count); + item->quantity -= dropped_count; + + ItemDrop *d = ecs_get_mut(world_ecs(), te, ItemDrop, NULL); + *d = (ItemDrop){ + .kind = item->kind, + .quantity = dropped_count, + .merger_time = game_time() + ITEM_DROP_MERGER_TIME, + }; + + Position *ipos = ecs_get_mut(it->world, te, Position, NULL); + *ipos = p[i]; + + Velocity *v = ecs_get_mut(it->world, te, Velocity, NULL); + v->x = in[i].mx * 800.0f; + v->y = in[i].my * 800.0f; + + inv[i].pickup_time = game_time() + ITEM_DROP_PICKUP_TIME; + in[i].drop = false; + } +} + +void MergeItems(ecs_iter_t *it) { + Position *p = ecs_column(it, Position, 1); + ItemDrop *id = ecs_column(it, ItemDrop, 2); + + for (int i = 0; i < it->count; i += 1) { + ItemDrop *item = &id[i]; + + if (item->merger_time < game_time()) + continue; size_t ents_count; - int64_t *ents = world_chunk_query_entities(it->entities[i], &ents_count, 2); + int64_t *ents = world_chunk_query_entities(it->entities[i], &ents_count, 1); for (size_t j = 0; j < ents_count; j++) { ItemDrop *drop = 0; if ((drop = ecs_get_mut_if(it->world, ents[j], ItemDrop))) { + if (drop->kind != item->kind || (ecs_entity_t)ents[j] == it->entities[i] || drop->quantity == 0 || item->quantity == 0) + continue; + Position const* p2 = ecs_get(it->world, ents[j], Position); - float dx = p2->x - p[i].x; - float dy = p2->y - p[i].y; + float dx = p2->x - (p[i].x); + float dy = p2->y - (p[i].y); float range = zpl_sqrt(dx*dx + dy*dy); - if (range <= ITEM_PICK_RADIUS) { - if (item->kind == drop->kind) { - uint32_t dropped_count = item->quantity; - if (in[i].sprint) { - dropped_count /= 2; - } else if (in[i].ctrl) { - dropped_count = 1; - } - drop->quantity += dropped_count; - item->quantity -= dropped_count; - is_item_nearby = true; - inv[i].pickup_time = game_time() + ITEM_DROP_PICKUP_TIME; - break; - } + if (range <= ITEM_MERGER_RADIUS) { + drop->quantity += item->quantity; + item_despawn(it->entities[i]); + break; } } } - - if (!is_item_nearby) { - uint32_t dropped_count = item->quantity; - if (in[i].sprint) { - dropped_count /= 2; - } else if (in[i].ctrl) { - dropped_count = dropped_count > 0 ? 1 : 0; - } - - if (dropped_count == 0) - continue; - - ecs_entity_t te = item_spawn(item->kind, dropped_count); - item->quantity -= dropped_count; - - Position *ipos = ecs_get_mut(it->world, te, Position, NULL); - *ipos = p[i]; - - Velocity *v = ecs_get_mut(it->world, te, Velocity, NULL); - v->x = in[i].mx * 800.0f; - v->y = in[i].my * 800.0f; - - inv[i].pickup_time = game_time() + ITEM_DROP_PICKUP_TIME; - } - - in[i].drop = false; } }