From ed5fd927abc7db9709be39de5bafb33e8761edbf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Thu, 29 Sep 2022 17:35:43 +0200 Subject: [PATCH] add more vehicle types --- code/foundation/src/debug/debug_replay.c | 2 +- code/foundation/src/debug/debug_ui_actions.c | 4 +- code/foundation/src/models/components.c | 12 ++--- code/foundation/src/models/components.h | 29 ++++++------ code/foundation/src/models/items.c | 9 ++-- code/foundation/src/models/items.h | 7 +-- code/foundation/src/models/items_list.c | 2 +- .../src/models/items_list_helpers.h | 9 ++-- code/foundation/src/models/prefabs/furnace.c | 6 +-- code/foundation/src/models/prefabs/vehicle.c | 45 ++++++++++++++++++- code/foundation/src/models/prefabs/vehicle.h | 8 +++- .../{system_furnace.c => system_producer.c} | 27 ++++++----- code/foundation/src/systems/systems.c | 6 +-- code/foundation/src/world/entity_view.c | 1 + code/foundation/src/world/entity_view.h | 19 ++++---- code/foundation/src/world/world.c | 1 + code/games/sandbox/src/renderer.c | 5 ++- code/games/sandbox/src/worldgen.c | 2 +- 18 files changed, 125 insertions(+), 69 deletions(-) rename code/foundation/src/systems/modules/{system_furnace.c => system_producer.c} (57%) diff --git a/code/foundation/src/debug/debug_replay.c b/code/foundation/src/debug/debug_replay.c index 6cd19d8..3d6e151 100644 --- a/code/foundation/src/debug/debug_replay.c +++ b/code/foundation/src/debug/debug_replay.c @@ -208,7 +208,7 @@ void debug_replay_update(void) { } }break; case RPKIND_SPAWN_CAR: { - ecs_entity_t e = vehicle_spawn(); + ecs_entity_t e = vehicle_spawn(EVEH_CAR); Position const *origin = ecs_get(world_ecs(), mime, Position); Position *dest = ecs_get_mut(world_ecs(), e, Position); diff --git a/code/foundation/src/debug/debug_ui_actions.c b/code/foundation/src/debug/debug_ui_actions.c index ab05f8b..3db1789 100644 --- a/code/foundation/src/debug/debug_ui_actions.c +++ b/code/foundation/src/debug/debug_ui_actions.c @@ -10,7 +10,7 @@ ActExitGame(void) { void ActSpawnCar(void) { - ecs_entity_t e = vehicle_spawn(); + ecs_entity_t e = vehicle_spawn(EVEH_CAR); ecs_entity_t plr = camera_get().ent_id; Position const* origin = ecs_get(world_ecs(), plr, Position); @@ -74,7 +74,7 @@ ActSpawnFurnace(void) { void ActSpawnCirclingDriver(void) { ecs_entity_t plr = camera_get().ent_id; - ecs_entity_t ve = vehicle_spawn(); + ecs_entity_t ve = vehicle_spawn(EVEH_CAR); ecs_entity_t e = entity_spawn(EKIND_DEMO_NPC); Position const *origin = ecs_get(world_ecs(), plr, Position); diff --git a/code/foundation/src/models/components.c b/code/foundation/src/models/components.c index f179009..811ca9d 100644 --- a/code/foundation/src/models/components.c +++ b/code/foundation/src/models/components.c @@ -12,11 +12,11 @@ ECS_COMPONENT_DECLARE(Classify); ECS_COMPONENT_DECLARE(Vehicle); ECS_COMPONENT_DECLARE(IsInVehicle); ECS_COMPONENT_DECLARE(Item); -ECS_COMPONENT_DECLARE(ItemAlreadyEdited); +ECS_COMPONENT_DECLARE(BlockHarvest); ECS_COMPONENT_DECLARE(Inventory); ECS_COMPONENT_DECLARE(ItemContainer); -ECS_COMPONENT_DECLARE(Furnace); -ECS_COMPONENT_DECLARE(Fuel); +ECS_COMPONENT_DECLARE(Producer); +ECS_COMPONENT_DECLARE(EnergySource); ECS_COMPONENT_DECLARE(Ingredient); ECS_COMPONENT_DECLARE(Device); ECS_COMPONENT_DECLARE(DemoNPC); @@ -37,11 +37,11 @@ void ComponentsImport(ecs_world_t *ecs) { ECS_COMPONENT_DEFINE(ecs, Vehicle); ECS_COMPONENT_DEFINE(ecs, IsInVehicle); ECS_COMPONENT_DEFINE(ecs, Item); - ECS_COMPONENT_DEFINE(ecs, ItemAlreadyEdited); + ECS_COMPONENT_DEFINE(ecs, BlockHarvest); ECS_COMPONENT_DEFINE(ecs, Inventory); ECS_COMPONENT_DEFINE(ecs, ItemContainer); - ECS_COMPONENT_DEFINE(ecs, Furnace); - ECS_COMPONENT_DEFINE(ecs, Fuel); + ECS_COMPONENT_DEFINE(ecs, Producer); + ECS_COMPONENT_DEFINE(ecs, EnergySource); ECS_COMPONENT_DEFINE(ecs, Ingredient); ECS_COMPONENT_DEFINE(ecs, Device); ECS_COMPONENT_DEFINE(ecs, DemoNPC); diff --git a/code/foundation/src/models/components.h b/code/foundation/src/models/components.h index f04e9fb..3610bb5 100644 --- a/code/foundation/src/models/components.h +++ b/code/foundation/src/models/components.h @@ -52,7 +52,7 @@ typedef struct { uint8_t is_blocked; ecs_entity_t pick_ent; ecs_entity_t sel_ent; - + // NOTE(zaklaus): inventory ecs_entity_t storage_ent; uint8_t storage_action; @@ -63,7 +63,7 @@ typedef struct { uint8_t swap_storage; uint8_t swap_from; uint8_t swap_to; - + // NOTE(zaklaus): build mode uint8_t num_placements; float placements_x[20]; @@ -80,7 +80,7 @@ typedef struct { typedef struct { float hp; float max_hp; - + //NOTE(zaklaus): Intentionally global, to allow for creative use of damage combos float pain_time; float heal_time; @@ -92,14 +92,15 @@ typedef struct { typedef struct { uint64_t seats[4]; - + float force; float heading; float steer; float wheel_base; - + float speed; float reverse_speed; + uint8_t veh_kind; } Vehicle; typedef struct { @@ -115,7 +116,7 @@ typedef struct { typedef struct { char _unused; -} ItemAlreadyEdited; +} BlockHarvest; typedef struct { // TODO: we now hold a ref to an item, instead of representing an item slot, @@ -130,14 +131,14 @@ typedef struct { typedef struct { asset_id processed_item; - float cook_time; - float burn_time; -} Furnace; + float process_time; + float energy_level; +} Producer; typedef struct { asset_id kind; - float burn_time; -} Fuel; + float energy_level; +} EnergySource; typedef struct { asset_id producer; @@ -168,11 +169,11 @@ extern ECS_COMPONENT_DECLARE(Classify); extern ECS_COMPONENT_DECLARE(Vehicle); extern ECS_COMPONENT_DECLARE(IsInVehicle); extern ECS_COMPONENT_DECLARE(Item); -extern ECS_COMPONENT_DECLARE(ItemAlreadyEdited); +extern ECS_COMPONENT_DECLARE(BlockHarvest); extern ECS_COMPONENT_DECLARE(Inventory); extern ECS_COMPONENT_DECLARE(ItemContainer); -extern ECS_COMPONENT_DECLARE(Furnace); -extern ECS_COMPONENT_DECLARE(Fuel); +extern ECS_COMPONENT_DECLARE(Producer); +extern ECS_COMPONENT_DECLARE(EnergySource); extern ECS_COMPONENT_DECLARE(Ingredient); extern ECS_COMPONENT_DECLARE(Device); extern ECS_COMPONENT_DECLARE(DemoNPC); diff --git a/code/foundation/src/models/items.c b/code/foundation/src/models/items.c index ad80d90..9b132b9 100644 --- a/code/foundation/src/models/items.c +++ b/code/foundation/src/models/items.c @@ -44,9 +44,12 @@ uint64_t item_spawn(asset_id kind, uint32_t qty) { item_desc *it = &items[item_find(kind)]; switch (it->attachment) { - case UDATA_FUEL: { - Fuel *f = ecs_get_mut(world_ecs(), e, Fuel); - f->burn_time = it->fuel.burn_time; + case UDATA_ENERGY_SOURCE: { + EnergySource *f = ecs_get_mut(world_ecs(), e, EnergySource); + *f = (EnergySource){ + .kind = it->energy_source.producer, + .energy_level = it->energy_source.energy_level, + }; } break; case UDATA_INGREDIENT: { Ingredient *i = ecs_get_mut(world_ecs(), e, Ingredient); diff --git a/code/foundation/src/models/items.h b/code/foundation/src/models/items.h index 836bba9..6ced2b0 100644 --- a/code/foundation/src/models/items.h +++ b/code/foundation/src/models/items.h @@ -19,7 +19,7 @@ typedef enum { typedef enum { UDATA_NONE, - UDATA_FUEL, + UDATA_ENERGY_SOURCE, UDATA_INGREDIENT, } item_attachment; @@ -47,8 +47,9 @@ typedef struct { union { struct { - float burn_time; - } fuel; + asset_id producer; + float energy_level; + } energy_source; struct { asset_id producer; diff --git a/code/foundation/src/models/items_list.c b/code/foundation/src/models/items_list.c index 389382c..af6f7cb 100644 --- a/code/foundation/src/models/items_list.c +++ b/code/foundation/src/models/items_list.c @@ -6,7 +6,7 @@ static item_desc items[] = { { .kind = 0, .max_quantity = 0, }, ITEM_INGREDIENT(ASSET_DEMO_ICEMAKER, 64, ASSET_FURNACE, ASSET_BELT, 0), ITEM_SELF(ASSET_FENCE, 64), - ITEM_FUEL(ASSET_WOOD, 64, 15.0f), + ITEM_ENERGY(ASSET_WOOD, ASSET_FURNACE, 64, 15.0f), ITEM_HOLD(ASSET_TREE, 64), ITEM_SELF_DIR(ASSET_BELT, 999), diff --git a/code/foundation/src/models/items_list_helpers.h b/code/foundation/src/models/items_list_helpers.h index 14ad689..4c70d75 100644 --- a/code/foundation/src/models/items_list_helpers.h +++ b/code/foundation/src/models/items_list_helpers.h @@ -8,14 +8,15 @@ .max_quantity = qty,\ } -#define ITEM_FUEL(asset, qty, fuel_value)\ +#define ITEM_ENERGY(asset, producer_asset, qty, energy_value)\ {\ .kind = asset,\ .usage = UKIND_HOLD,\ -.attachment = UDATA_FUEL,\ +.attachment = UDATA_ENERGY_SOURCE,\ .max_quantity = qty,\ -.fuel = {\ -.burn_time = fuel_value\ +.energy_source = {\ +.producer = producer_asset,\ +.energy_level = energy_value\ }\ } diff --git a/code/foundation/src/models/prefabs/furnace.c b/code/foundation/src/models/prefabs/furnace.c index 126bd88..437a95e 100644 --- a/code/foundation/src/models/prefabs/furnace.c +++ b/code/foundation/src/models/prefabs/furnace.c @@ -11,9 +11,9 @@ uint64_t furnace_spawn(void) { ItemContainer *storage = ecs_get_mut(world_ecs(), e, ItemContainer); *storage = (ItemContainer){0}; - Furnace *furnace = ecs_get_mut(world_ecs(), e, Furnace); - *furnace = (Furnace){0}; - furnace->burn_time = 69.0f; + Producer *producer = ecs_get_mut(world_ecs(), e, Producer); + *producer = (Producer){0}; + producer->energy_level = 69.0f; return (uint64_t)e; } diff --git a/code/foundation/src/models/prefabs/vehicle.c b/code/foundation/src/models/prefabs/vehicle.c index 03ec0e8..b082528 100644 --- a/code/foundation/src/models/prefabs/vehicle.c +++ b/code/foundation/src/models/prefabs/vehicle.c @@ -6,16 +6,57 @@ #include "models/entity.h" #include "models/components.h" -uint64_t vehicle_spawn(void) { + +uint64_t vehicle_spawn(uint8_t veh_kind) { ecs_entity_t e = entity_spawn(EKIND_VEHICLE); - + Vehicle *veh = ecs_get_mut(world_ecs(), e, Vehicle); *veh = (Vehicle){ .wheel_base = 50.0f, .speed = 50.0f, .reverse_speed = -20.0f, .force = 0.0f, + .veh_kind = veh_kind, }; + + switch (veh_kind) { + case EVEH_CAR: { + veh->wheel_base = 50.0f; + veh->speed = 50.0f; + veh->reverse_speed = -20.0f; + veh->force = 0.0f; + } break; + case EVEH_TRUCK: { + veh->wheel_base = 100.0f; + veh->speed = 30.0f; + veh->reverse_speed = -10.0f; + veh->force = 0.0f; + + ItemContainer *storage = ecs_get_mut(world_ecs(), e, ItemContainer); + *storage = (ItemContainer){0}; + + Device *dev = ecs_get_mut(world_ecs(), e, Device); + dev->asset = ASSET_FURNACE; + } break; + case EVEH_FURNACEMOBILE: { + veh->wheel_base = 100.0f; + veh->speed = 30.0f; + veh->reverse_speed = -10.0f; + veh->force = 0.0f; + + ItemContainer *storage = ecs_get_mut(world_ecs(), e, ItemContainer); + *storage = (ItemContainer){0}; + + Device *dev = ecs_get_mut(world_ecs(), e, Device); + dev->asset = ASSET_FURNACE; + + Producer *producer = ecs_get_mut(world_ecs(), e, Producer); + *producer = (Producer){0}; + producer->energy_level = 69.0f; + } break; + } + + ecs_add(world_ecs(), e, BlockHarvest); return (uint64_t)e; } diff --git a/code/foundation/src/models/prefabs/vehicle.h b/code/foundation/src/models/prefabs/vehicle.h index 44c25cd..a99d155 100644 --- a/code/foundation/src/models/prefabs/vehicle.h +++ b/code/foundation/src/models/prefabs/vehicle.h @@ -1,7 +1,13 @@ #pragma once #include "platform/system.h" -uint64_t vehicle_spawn(void); +enum { + EVEH_CAR, + EVEH_TRUCK, + EVEH_FURNACEMOBILE, +}; + +uint64_t vehicle_spawn(uint8_t veh_kind); void vehicle_despawn(uint64_t id); diff --git a/code/foundation/src/systems/modules/system_furnace.c b/code/foundation/src/systems/modules/system_producer.c similarity index 57% rename from code/foundation/src/systems/modules/system_furnace.c rename to code/foundation/src/systems/modules/system_producer.c index ac9eb2c..50880e4 100644 --- a/code/foundation/src/systems/modules/system_furnace.c +++ b/code/foundation/src/systems/modules/system_producer.c @@ -1,6 +1,6 @@ -void FurnaceCook(ecs_iter_t *it) { +void ProduceItems(ecs_iter_t *it) { ItemContainer *storage = ecs_field(it, ItemContainer, 1); - Furnace *furnace = ecs_field(it, Furnace, 2); + Producer *producer = ecs_field(it, Producer, 2); Position *p = ecs_field(it, Position, 3); Device *d = ecs_field(it, Device, 4); @@ -9,29 +9,28 @@ void FurnaceCook(ecs_iter_t *it) { ecs_entity_t item_slot_ent = storage[i].items[j]; Item *item = item_get_data(item_slot_ent); - const Fuel *fuel = 0; - if ((fuel = ecs_get_if(it->world, item_slot_ent, Fuel))) { - if (fuel->kind == d->asset) { - furnace[i].burn_time += fuel->kind; + const EnergySource *energy_source = 0; + if ((energy_source = ecs_get_if(it->world, item_slot_ent, EnergySource))) { + if (energy_source->kind == d->asset) { + producer[i].energy_level += energy_source->kind; item_despawn(item_slot_ent); storage[i].items[j] = 0; } continue; } - // if (furnace[i].burn_time <= 0.0f) continue; TODO - if (furnace[i].cook_time < game_time()) { - if (furnace[i].processed_item > 0) { - uint64_t e = item_spawn(furnace[i].processed_item, 1); + // if (producer[i].burn_time <= 0.0f) continue; TODO + if (producer[i].process_time < game_time()) { + if (producer[i].processed_item > 0) { + uint64_t e = item_spawn(producer[i].processed_item, 1); entity_set_position(e, p[i].x, p[i].y); - furnace[i].processed_item = 0; + producer[i].processed_item = 0; } else { const Ingredient *ing = 0; if ((ing = ecs_get_if(it->world, item_slot_ent, Ingredient))) { if (ing->producer == d->asset) { - furnace[i].processed_item = ing->product; - furnace[i].cook_time = game_time() + game_rules.furnace_cook_time; - zpl_printf("e_id %llu, qty: %d\n", item_slot_ent, item->quantity); + producer[i].processed_item = ing->product; + producer[i].process_time = game_time() + game_rules.furnace_cook_time; item->quantity--; if (item->quantity <= 0) { item_despawn(item_slot_ent); diff --git a/code/foundation/src/systems/systems.c b/code/foundation/src/systems/systems.c index 16477b7..1402384 100644 --- a/code/foundation/src/systems/systems.c +++ b/code/foundation/src/systems/systems.c @@ -16,7 +16,7 @@ #include "modules/system_demo.c" #include "modules/system_vehicle.c" #include "modules/system_items.c" -#include "modules/system_furnace.c" +#include "modules/system_producer.c" static inline float physics_correction(float x, float vx, float bounce) { float r = (((zpl_max(0.0f, (WORLD_BLOCK_SIZE/2.0f) - zpl_abs(x))*zpl_sign(x)))*(WORLD_BLOCK_SIZE/2.0f)); @@ -217,8 +217,8 @@ void SystemsImport(ecs_world_t *ecs) { //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, InspectContainers, EcsPostUpdate, components.Input, !components.IsInVehicle); - ECS_SYSTEM(ecs, HarvestIntoContainers, EcsPostUpdate, components.ItemContainer, components.Position); - ECS_SYSTEM(ecs, FurnaceCook, EcsPostUpdate, components.ItemContainer, components.Furnace, components.Position, components.Device); + ECS_SYSTEM(ecs, HarvestIntoContainers, EcsPostUpdate, components.ItemContainer, components.Position, [none] !components.BlockHarvest); + ECS_SYSTEM(ecs, ProduceItems, EcsPostUpdate, components.ItemContainer, components.Producer, components.Position, components.Device); ECS_SYSTEM(ecs, ResetActivators, EcsPostUpdate, components.Input); diff --git a/code/foundation/src/world/entity_view.c b/code/foundation/src/world/entity_view.c index ac81bc4..1236842 100644 --- a/code/foundation/src/world/entity_view.c +++ b/code/foundation/src/world/entity_view.c @@ -26,6 +26,7 @@ pkt_desc pkt_entity_view_desc[] = { { PKT_KEEP_IF(entity_view, kind, EKIND_VEHICLE, 1) }, // NOTE(zaklaus): keep for vehicles { PKT_HALF(entity_view, heading) }, { PKT_UINT(entity_view, inside_vehicle) }, + { PKT_UINT(entity_view, veh_kind) }, { PKT_KEEP_IF(entity_view, kind, EKIND_ITEM, 2) }, { PKT_UINT(entity_view, asset) }, diff --git a/code/foundation/src/world/entity_view.h b/code/foundation/src/world/entity_view.h index 9f2e9a6..fa52802 100644 --- a/code/foundation/src/world/entity_view.h +++ b/code/foundation/src/world/entity_view.h @@ -42,10 +42,10 @@ typedef struct entity_view { float vy; float tx; float ty; - + float hp; float max_hp; - + // TODO(zaklaus): Find a way to stream dynamic arrays uint8_t blocks_used; block_id blocks[256]; @@ -53,33 +53,34 @@ typedef struct entity_view { uint32_t color; uint8_t is_dirty; int64_t tex; - + // NOTE(zaklaus): vehicle float heading, theading; bool inside_vehicle; - + uint32_t veh_kind; + // NOTE(zaklaus): items, ... asset_id asset; uint32_t quantity; - + // NOTE(zaklaus): inventory uint8_t has_items; Item items[ITEMS_INVENTORY_SIZE]; uint8_t selected_item; - + // NOTE(zaklaus): storage interface uint8_t has_storage_items; Item storage_items[ITEMS_CONTAINER_SIZE]; uint8_t storage_selected_item; - + // NOTE(zaklaus): entity picking uint64_t pick_ent; uint64_t sel_ent; - + // NOTE(zaklaus): internals uint8_t layer_id; uint64_t last_update; - + // NOTE(zaklaus): fade in-out effect entity_transition_effect tran_effect; float tran_time; diff --git a/code/foundation/src/world/world.c b/code/foundation/src/world/world.c index f93249c..2d8dafb 100644 --- a/code/foundation/src/world/world.c +++ b/code/foundation/src/world/world.c @@ -56,6 +56,7 @@ entity_view *world_build_entity_view(int64_t e) { if (ecs_get(world_ecs(), e, Vehicle)) { Vehicle const* veh = ecs_get(world_ecs(), e, Vehicle); view.heading = veh->heading; + view.veh_kind = veh->veh_kind; } if (ecs_get(world_ecs(), e, Item)) { diff --git a/code/games/sandbox/src/renderer.c b/code/games/sandbox/src/renderer.c index 6e08cb0..45b7dc1 100644 --- a/code/games/sandbox/src/renderer.c +++ b/code/games/sandbox/src/renderer.c @@ -123,9 +123,10 @@ void DEBUG_draw_entities_low(uint64_t key, entity_view * data) { case EKIND_VEHICLE: { float x = data->x; float y = data->y; - float const w = 80; + float const w = (float)(data->veh_kind == 0 ? 80 : data->veh_kind == 1 ? 120 : 135); float const h = 50; - DrawRectanglePro((Rectangle){x,y,w,h}, (Vector2){w/2.0f,h/2.0f}, zpl_to_degrees(data->heading), ColorAlpha(RED, data->tran_time)); + Color color = data->veh_kind == 0 ? RED : data->veh_kind == 1 ? GREEN : BLUE; + DrawRectanglePro((Rectangle){x,y,w,h}, (Vector2){w/2.0f,h/2.0f}, zpl_to_degrees(data->heading), ColorAlpha(color, data->tran_time)); }break; case EKIND_DEVICE:{ float x = data->x - 32.f; diff --git a/code/games/sandbox/src/worldgen.c b/code/games/sandbox/src/worldgen.c index 7914ff8..5106d41 100644 --- a/code/games/sandbox/src/worldgen.c +++ b/code/games/sandbox/src/worldgen.c @@ -88,7 +88,7 @@ int32_t worldgen_build(world_data *wld) { // vehicles #if 1 for (int i=0; ix = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);