From e51d9410104ccf18c07fce6221e9b91328a5493c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Thu, 29 Sep 2022 20:49:00 +0200 Subject: [PATCH] items can have storage now --- code/foundation/src/core/rules.h | 1 + code/foundation/src/core/rules_default.c | 1 + code/foundation/src/lists/blocks_list.c | 4 +- .../foundation/src/lists/items_list_helpers.h | 42 +++++++++++-------- code/foundation/src/models/items.c | 6 +++ code/foundation/src/models/items.h | 1 + .../src/systems/modules/system_items.c | 16 +++++++ code/foundation/src/systems/systems.c | 3 +- code/foundation/src/world/blocks.h | 2 +- code/foundation/src/world/world.c | 6 +-- code/games/sandbox/src/platform.c | 2 + code/games/sandbox/src/renderer.c | 2 +- 12 files changed, 61 insertions(+), 25 deletions(-) diff --git a/code/foundation/src/core/rules.h b/code/foundation/src/core/rules.h index 71cd870..fa9d647 100644 --- a/code/foundation/src/core/rules.h +++ b/code/foundation/src/core/rules.h @@ -24,6 +24,7 @@ typedef struct { float vehicle_power; float vehicle_brake_force; float veh_enter_radius; + float blueprint_build_time; } game_rulesdef; extern game_rulesdef game_rules; diff --git a/code/foundation/src/core/rules_default.c b/code/foundation/src/core/rules_default.c index 7a18a15..feeb7d5 100644 --- a/code/foundation/src/core/rules_default.c +++ b/code/foundation/src/core/rules_default.c @@ -23,4 +23,5 @@ game_rulesdef game_rules = { .vehicle_power = 97.89f, .vehicle_brake_force = 0.84f, .veh_enter_radius = 45.0f, + .blueprint_build_time = 1.5f, }; diff --git a/code/foundation/src/lists/blocks_list.c b/code/foundation/src/lists/blocks_list.c index 54310aa..4de963f 100644 --- a/code/foundation/src/lists/blocks_list.c +++ b/code/foundation/src/lists/blocks_list.c @@ -17,8 +17,8 @@ static block blocks[] = { BLOCK(ASSET_FENCE, BLOCK_FLAG_COLLISION, '[', .drag = 1.0f , .friction = 1.0f, .bounce = 1.0f), BLOCK(ASSET_WOOD, BLOCK_FLAG_COLLISION, ']', .drag = 1.0f , .friction = 1.0f, .bounce = 0.0f), BLOCK(ASSET_TREE, BLOCK_FLAG_COLLISION|BLOCK_FLAG_DESTROY_ON_COLLISION, '@', .drag = 1.0f , .friction = 1.0f, .bounce = 0.0f), - BLOCK(ASSET_CHEST, BLOCK_FLAG_DEVICE, 'C'), - BLOCK(ASSET_FURNACE, BLOCK_FLAG_DEVICE, 'F'), + BLOCK(ASSET_CHEST, BLOCK_FLAG_ENTITY, 'C'), + BLOCK(ASSET_FURNACE, BLOCK_FLAG_ENTITY, 'F'), BLOCK(ASSET_BELT_LEFT, 0, '@', .drag = 1.0f , .friction = 1.0f, .velx = -150.0f), BLOCK(ASSET_BELT_RIGHT, 0, '@', .drag = 1.0f , .friction = 1.0f, .velx = 150.0f), diff --git a/code/foundation/src/lists/items_list_helpers.h b/code/foundation/src/lists/items_list_helpers.h index fe00b85..40719ce 100644 --- a/code/foundation/src/lists/items_list_helpers.h +++ b/code/foundation/src/lists/items_list_helpers.h @@ -1,14 +1,15 @@ #pragma once -#define ITEM_HOLD(asset, qty)\ +#define ITEM_HOLD(asset, qty, ...)\ {\ .kind = asset,\ .usage = UKIND_HOLD,\ .attachment = UDATA_NONE,\ .max_quantity = qty,\ +__VA_ARGS__\ } -#define ITEM_ENERGY(asset, producer_asset, qty, energy_value)\ +#define ITEM_ENERGY(asset, producer_asset, qty, energy_value, ...)\ {\ .kind = asset,\ .usage = UKIND_HOLD,\ @@ -17,10 +18,11 @@ .energy_source = {\ .producer = producer_asset,\ .energy_level = energy_value\ -}\ +},\ +__VA_ARGS__\ } -#define ITEM_BLUEPRINT(asset, qty, w_, h_, plan_)\ +#define ITEM_BLUEPRINT(asset, qty, w_, h_, plan_, ...)\ {\ .kind = asset,\ .usage = UKIND_PLACE_ITEM_DATA,\ @@ -33,10 +35,11 @@ },\ .place_item = {\ .id = asset\ -}\ +},\ +__VA_ARGS__\ } -#define ITEM_INGREDIENT(asset, qty, _producer, _product, _additional)\ +#define ITEM_INGREDIENT(asset, qty, _producer, _product, _additional, ...)\ {\ .kind = asset,\ .usage = UKIND_HOLD,\ @@ -46,10 +49,11 @@ .producer = _producer,\ .product = _product,\ .additional_ingredient = _additional,\ -}\ +},\ +__VA_ARGS__\ } -#define ITEM_BLOCK(asset, qty, build_asset)\ +#define ITEM_BLOCK(asset, qty, build_asset, ...)\ {\ .kind = asset,\ .usage = UKIND_PLACE,\ @@ -57,10 +61,11 @@ .max_quantity = qty,\ .place = {\ .kind = build_asset,\ -}\ +},\ +__VA_ARGS__\ } -#define ITEM_BLOCK_DIR(asset, qty, build_asset)\ +#define ITEM_BLOCK_DIR(asset, qty, build_asset, ...)\ {\ .kind = asset,\ .usage = UKIND_PLACE,\ @@ -69,20 +74,22 @@ .place = {\ .kind = build_asset,\ .directional = true,\ -}\ +},\ +__VA_ARGS__\ } -#define ITEM_PROXY(asset, proxy_id)\ +#define ITEM_PROXY(asset, proxy_id, ...)\ {\ .kind = asset,\ .usage = UKIND_PROXY,\ .attachment = UDATA_NONE,\ .proxy = {\ .id = proxy_id,\ -}\ +},\ +__VA_ARGS__\ } -#define ITEM_ENT(asset, qty, eid)\ +#define ITEM_ENT(asset, qty, eid, ...)\ {\ .kind = asset,\ .usage = UKIND_PLACE_ITEM,\ @@ -90,8 +97,9 @@ .max_quantity = qty,\ .place_item = {\ .id = eid\ -}\ +},\ +__VA_ARGS__\ } -#define ITEM_SELF(asset, qty) ITEM_BLOCK(asset, qty, asset) -#define ITEM_SELF_DIR(asset, qty) ITEM_BLOCK_DIR(asset, qty, asset) +#define ITEM_SELF(asset, qty, ...) ITEM_BLOCK(asset, qty, asset, __VA_ARGS__) +#define ITEM_SELF_DIR(asset, qty, ...) ITEM_BLOCK_DIR(asset, qty, asset, __VA_ARGS__) diff --git a/code/foundation/src/models/items.c b/code/foundation/src/models/items.c index 5379172..9e102b7 100644 --- a/code/foundation/src/models/items.c +++ b/code/foundation/src/models/items.c @@ -43,6 +43,12 @@ uint64_t item_spawn(asset_id kind, uint32_t qty) { item_desc *it = &items[item_find(kind)]; + if (it->has_storage) { + ecs_add(world_ecs(), e, BlockHarvest); + ItemContainer *storage = ecs_get_mut(world_ecs(), e, ItemContainer); + *storage = (ItemContainer){0}; + } + switch (it->attachment) { case UDATA_ENERGY_SOURCE: { EnergySource *f = ecs_get_mut(world_ecs(), e, EnergySource); diff --git a/code/foundation/src/models/items.h b/code/foundation/src/models/items.h index da58730..b6a74d2 100644 --- a/code/foundation/src/models/items.h +++ b/code/foundation/src/models/items.h @@ -29,6 +29,7 @@ typedef struct { item_usage usage; item_attachment attachment; uint32_t max_quantity; + uint8_t has_storage; // NOTE(zaklaus): usage data union { diff --git a/code/foundation/src/systems/modules/system_items.c b/code/foundation/src/systems/modules/system_items.c index 127d46d..217a3cb 100644 --- a/code/foundation/src/systems/modules/system_items.c +++ b/code/foundation/src/systems/modules/system_items.c @@ -382,3 +382,19 @@ void HarvestIntoContainers(ecs_iter_t *it) { } } } + +void ThrowItemsOut(ecs_iter_t *it) { + ItemContainer *storage = ecs_field(it, ItemContainer, 1); + Position *p = ecs_field(it, Position, 2); + + for (int i = 0; i < it->count; i++) { + for (int j = 0; j < ITEMS_CONTAINER_SIZE; j++) { + ecs_entity_t item_slot_ent = storage[i].items[j]; + Item *item = item_get_data(item_slot_ent); + if (!item) continue; + + item_show(item_slot_ent, true); + entity_set_position(item_slot_ent, p[i].x, p[i].y); + } + } +} diff --git a/code/foundation/src/systems/systems.c b/code/foundation/src/systems/systems.c index c2905e6..997b2df 100644 --- a/code/foundation/src/systems/systems.c +++ b/code/foundation/src/systems/systems.c @@ -160,7 +160,7 @@ void PlayerClosestInteractable(ecs_iter_t *it){ for (int i = 0; i < it->count; ++i) { size_t ents_count; - int64_t *ents = world_chunk_fetch_entities_realpos(in[i].bx, in[i].by, &ents_count); + int64_t *ents = world_chunk_query_entities(it->entities[i], &ents_count, 2); ecs_entity_t closest_pick = 0; float min_pick = ZPL_F32_MAX; @@ -225,6 +225,7 @@ void SystemsImport(ecs_world_t *ecs) { ECS_SYSTEM(ecs, ResetActivators, EcsPostUpdate, components.Input); ECS_SYSTEM(ecs, ClearVehicle, EcsUnSet, components.Vehicle); + ECS_SYSTEM(ecs, ThrowItemsOut, EcsUnSet, components.ItemContainer, components.Position); ECS_SYSTEM(ecs, DisableWorldEdit, EcsPostUpdate); diff --git a/code/foundation/src/world/blocks.h b/code/foundation/src/world/blocks.h index e35a91a..f70cdb5 100644 --- a/code/foundation/src/world/blocks.h +++ b/code/foundation/src/world/blocks.h @@ -7,7 +7,7 @@ typedef enum { BLOCK_FLAG_HAZARD = (1 << 2), BLOCK_FLAG_ESSENTIAL = (1 << 3), BLOCK_FLAG_DESTROY_ON_COLLISION = (1 << 4), - BLOCK_FLAG_DEVICE = (1 << 5), + BLOCK_FLAG_ENTITY = (1 << 5), } block_flags; typedef uint16_t block_id; diff --git a/code/foundation/src/world/world.c b/code/foundation/src/world/world.c index d024c7a..56457f2 100644 --- a/code/foundation/src/world/world.c +++ b/code/foundation/src/world/world.c @@ -559,14 +559,14 @@ int64_t world_chunk_from_entity(ecs_entity_t id) { void world_chunk_replace_worldgen_block(int64_t id, uint16_t block_idx, block_id bid) { ZPL_ASSERT(block_idx >= 0 && block_idx < zpl_square(world.chunk_size)); - ZPL_ASSERT(!(blocks_get_flags(bid) & BLOCK_FLAG_DEVICE)); + ZPL_ASSERT(!(blocks_get_flags(bid) & BLOCK_FLAG_ENTITY)); world.block_mapping[id][block_idx] = bid; world_chunk_mark_dirty(world.chunk_mapping[id]); } void world_chunk_replace_block(int64_t id, uint16_t block_idx, block_id bid) { ZPL_ASSERT(block_idx >= 0 && block_idx < zpl_square(world.chunk_size)); - if (blocks_get_flags(bid) & BLOCK_FLAG_DEVICE) { + if (blocks_get_flags(bid) & BLOCK_FLAG_ENTITY) { ecs_entity_t e = entity_spawn_id(blocks_get_asset(bid)); world_block_lookup l = world_block_from_index(id, block_idx); entity_set_position(e, l.ox, l.oy); @@ -579,7 +579,7 @@ void world_chunk_replace_block(int64_t id, uint16_t block_idx, block_id bid) { bool world_chunk_place_block(int64_t id, uint16_t block_idx, block_id bid) { ZPL_ASSERT(block_idx >= 0 && block_idx < zpl_square(world.chunk_size)); if (world.outer_block_mapping[id][block_idx] != 0 && bid != 0) return false; - if (blocks_get_flags(bid) & BLOCK_FLAG_DEVICE) { + if (blocks_get_flags(bid) & BLOCK_FLAG_ENTITY) { ecs_entity_t e = entity_spawn_id(blocks_get_asset(bid)); world_block_lookup l = world_block_from_index(id, block_idx); entity_set_position(e, l.ox, l.oy); diff --git a/code/games/sandbox/src/platform.c b/code/games/sandbox/src/platform.c index 5b54703..bdade5b 100644 --- a/code/games/sandbox/src/platform.c +++ b/code/games/sandbox/src/platform.c @@ -27,6 +27,8 @@ void platform_init() { platform_create_window("eco2d"); renderer_init(); + + target_zoom = 2.70f; } inline static diff --git a/code/games/sandbox/src/renderer.c b/code/games/sandbox/src/renderer.c index 76ec259..c02fcfe 100644 --- a/code/games/sandbox/src/renderer.c +++ b/code/games/sandbox/src/renderer.c @@ -177,7 +177,7 @@ void renderer_init(void) { render_camera.target = (Vector2){0.0f,0.0f}; render_camera.offset = (Vector2){(float)(screenWidth >> 1), (float)(screenHeight >> 1)}; render_camera.rotation = 0.0f; - render_camera.zoom = 1.5f; + render_camera.zoom = 2.9f; // NOTE(zaklaus): Paint the screen before we load the game // TODO(zaklaus): Render a cool loading screen background maybe? :wink: :wink: