no item merging for now

isolation_bkp/dynres
Dominik Madarász 2021-09-09 09:46:22 +02:00
parent 9ed12e35f6
commit 98adee996c
4 changed files with 56 additions and 44 deletions

View File

@ -18,6 +18,7 @@ uint64_t item_spawn(item_kind kind, uint32_t qty) {
*d = (ItemDrop){ *d = (ItemDrop){
.kind = kind, .kind = kind,
.quantity = qty, .quantity = qty,
.merger_time = 0,
}; };
return (uint64_t)e; return (uint64_t)e;

View File

@ -91,6 +91,7 @@ typedef struct {
typedef struct { typedef struct {
uint16_t kind; uint16_t kind;
uint32_t quantity; uint32_t quantity;
float merger_time;
} ItemDrop; } ItemDrop;
typedef struct { typedef struct {

View File

@ -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, 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, DropItem, EcsPostUpdate, components.Input, components.Position, components.Inventory, !components.IsInVehicle);
ECS_SYSTEM(ecs, SwapItems, EcsPostUpdate, components.Input, components.Inventory); 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, UseItem, EcsPostUpdate, components.Input, components.Position, components.Inventory, !components.IsInVehicle);
ECS_SYSTEM(ecs, UpdateTrackerPos, EcsPostUpdate, components.Position, components.Velocity); ECS_SYSTEM(ecs, UpdateTrackerPos, EcsPostUpdate, components.Position, components.Velocity);

View File

@ -1,6 +1,7 @@
#include "items.h" #include "items.h"
#define ITEM_PICK_RADIUS 25.0f #define ITEM_PICK_RADIUS 25.0f
#define ITEM_MERGER_RADIUS 75.0f
#define ITEM_ATTRACT_RADIUS 75.0f #define ITEM_ATTRACT_RADIUS 75.0f
#define ITEM_ATTRACT_FORCE 0.63f #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_PICKUP_TIME 2.5f
#define ITEM_DROP_MERGER_TIME 6.5f
void DropItem(ecs_iter_t *it) { void DropItem(ecs_iter_t *it) {
Input *in = ecs_column(it, Input, 1); Input *in = ecs_column(it, Input, 1);
@ -60,38 +62,6 @@ void DropItem(ecs_iter_t *it) {
if (item->quantity <= 0) if (item->quantity <= 0)
continue; continue;
bool is_item_nearby = false;
size_t ents_count;
int64_t *ents = world_chunk_query_entities(it->entities[i], &ents_count, 2);
for (size_t j = 0; j < ents_count; j++) {
ItemDrop *drop = 0;
if ((drop = ecs_get_mut_if(it->world, ents[j], ItemDrop))) {
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 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 (!is_item_nearby) {
uint32_t dropped_count = item->quantity; uint32_t dropped_count = item->quantity;
if (in[i].sprint) { if (in[i].sprint) {
dropped_count /= 2; dropped_count /= 2;
@ -105,6 +75,13 @@ void DropItem(ecs_iter_t *it) {
ecs_entity_t te = item_spawn(item->kind, dropped_count); ecs_entity_t te = item_spawn(item->kind, dropped_count);
item->quantity -= 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); Position *ipos = ecs_get_mut(it->world, te, Position, NULL);
*ipos = p[i]; *ipos = p[i];
@ -113,12 +90,44 @@ void DropItem(ecs_iter_t *it) {
v->y = in[i].my * 800.0f; v->y = in[i].my * 800.0f;
inv[i].pickup_time = game_time() + ITEM_DROP_PICKUP_TIME; inv[i].pickup_time = game_time() + ITEM_DROP_PICKUP_TIME;
}
in[i].drop = false; 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, 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 range = zpl_sqrt(dx*dx + dy*dy);
if (range <= ITEM_MERGER_RADIUS) {
drop->quantity += item->quantity;
item_despawn(it->entities[i]);
break;
}
}
}
}
}
void SwapItems(ecs_iter_t *it) { void SwapItems(ecs_iter_t *it) {
Input *in = ecs_column(it, Input, 1); Input *in = ecs_column(it, Input, 1);
Inventory *inv = ecs_column(it, Inventory, 2); Inventory *inv = ecs_column(it, Inventory, 2);