add more vehicle types

isolation
Dominik Madarász 2022-09-29 17:35:43 +02:00
parent 11929260e2
commit ed5fd927ab
18 changed files with 125 additions and 69 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -100,6 +100,7 @@ typedef struct {
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);

View File

@ -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);

View File

@ -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;

View File

@ -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),

View File

@ -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\
}\
}

View File

@ -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;
}

View File

@ -6,7 +6,8 @@
#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);
@ -15,7 +16,47 @@ uint64_t vehicle_spawn(void) {
.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;
}

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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) },

View File

@ -57,6 +57,7 @@ typedef struct entity_view {
// NOTE(zaklaus): vehicle
float heading, theading;
bool inside_vehicle;
uint32_t veh_kind;
// NOTE(zaklaus): items, ...
asset_id asset;

View File

@ -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)) {

View File

@ -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;

View File

@ -88,7 +88,7 @@ int32_t worldgen_build(world_data *wld) {
// vehicles
#if 1
for (int i=0; i<RAND_RANGE(258, 1124); i++) {
uint64_t e = vehicle_spawn();
uint64_t e = vehicle_spawn(RAND_RANGE(0,2));
Position *dest = ecs_get_mut(world_ecs(), e, Position);
dest->x = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);