eco2d/code/foundation/src/models/components.h

210 lines
4.5 KiB
C
Raw Normal View History

2021-11-01 18:55:13 +00:00
#pragma once
2022-09-29 14:16:06 +00:00
#include "flecs.h"
2022-09-29 18:11:47 +00:00
#include "models/assets.h"
2021-11-01 18:55:13 +00:00
#define ecs_get_mut_ex(world, entity, T) \
2022-10-17 16:28:18 +00:00
(ECS_CAST(T*, world_component_cached(world, entity, ecs_id(T))))
#define ecs_get_if(world, entity, T) \
2022-10-17 16:28:18 +00:00
(world_entity_valid(entity) ? ecs_get(world, entity, T) : NULL)
#define ecs_get_mut_if_ex(world, entity, component) \
2022-10-17 16:28:18 +00:00
(ecs_get_if(world, entity, component) ? ecs_get_mut_ex(world, entity, component) : NULL)
2021-11-01 18:55:13 +00:00
#ifndef ecs_get_mut_if
#define ecs_get_mut_if(world, entity, component)\
2022-10-17 16:28:18 +00:00
(ecs_get(world, entity, component) ? ecs_get_mut(world, entity, component) : NULL)
2021-11-01 18:55:13 +00:00
#endif
#define ITEMS_INVENTORY_SIZE 9
2022-08-09 14:46:23 +00:00
#define ITEMS_CONTAINER_SIZE 16
2021-11-01 18:55:13 +00:00
2022-07-31 14:34:47 +00:00
typedef struct {
float x;
float y;
} Vector2D;
typedef struct {
uint32_t id;
int16_t x;
int16_t y;
uint8_t is_dirty;
} Chunk;
typedef struct {
uint16_t id;
} Drawable;
typedef Vector2D Position;
typedef Vector2D Velocity;
typedef struct {
float x;
float y;
float mx;
float my;
2022-08-09 14:46:23 +00:00
float bx;
float by;
2022-07-31 14:34:47 +00:00
uint8_t use;
uint8_t sprint;
uint8_t ctrl;
2022-08-09 14:46:23 +00:00
uint8_t pick;
2022-07-31 14:34:47 +00:00
uint8_t is_blocked;
2022-08-09 14:46:23 +00:00
ecs_entity_t pick_ent;
ecs_entity_t sel_ent;
2022-10-17 16:28:18 +00:00
2022-07-31 14:34:47 +00:00
// NOTE(zaklaus): inventory
2022-08-09 14:46:23 +00:00
ecs_entity_t storage_ent;
uint8_t storage_action;
2022-07-31 14:34:47 +00:00
uint8_t selected_item;
2022-08-09 14:46:23 +00:00
uint8_t storage_selected_item;
2022-07-31 14:34:47 +00:00
uint8_t drop;
uint8_t swap;
2022-08-09 14:46:23 +00:00
uint8_t swap_storage;
2022-07-31 14:34:47 +00:00
uint8_t swap_from;
uint8_t swap_to;
2022-10-18 17:37:56 +00:00
asset_id craft_item;
2022-10-17 16:28:18 +00:00
2022-07-31 14:34:47 +00:00
// NOTE(zaklaus): build mode
uint8_t num_placements;
float placements_x[20];
float placements_y[20];
2022-08-11 10:41:36 +00:00
uint8_t deletion_mode;
2022-07-31 14:34:47 +00:00
} Input;
typedef struct {
uintptr_t peer;
uint16_t view_id;
uint8_t active;
} ClientInfo;
typedef struct {
float hp;
float max_hp;
2022-10-17 16:28:18 +00:00
2022-07-31 14:34:47 +00:00
//NOTE(zaklaus): Intentionally global, to allow for creative use of damage combos
float pain_time;
float heal_time;
} Health;
typedef struct {
uint16_t id;
} Classify;
typedef struct {
uint64_t seats[4];
2022-10-17 16:28:18 +00:00
2022-07-31 14:34:47 +00:00
float force;
float heading;
float steer;
float wheel_base;
2022-10-17 16:28:18 +00:00
2022-07-31 14:34:47 +00:00
float speed;
float reverse_speed;
2022-09-29 15:35:43 +00:00
uint8_t veh_kind;
2022-07-31 14:34:47 +00:00
} Vehicle;
2021-11-01 18:55:13 +00:00
typedef struct {
ecs_entity_t veh;
} IsInVehicle;
typedef struct {
uint16_t kind;
uint32_t quantity;
float merger_time;
2022-09-28 13:17:33 +00:00
float durability; // 1.0 - 0.0 (0.0 = broken), we can only ever merge items of the same durability
2022-09-28 05:29:32 +00:00
} Item;
2021-11-01 18:55:13 +00:00
2022-09-28 13:17:33 +00:00
typedef struct {
char _unused;
2022-09-29 15:35:43 +00:00
} BlockHarvest;
2022-09-28 13:17:33 +00:00
2021-11-01 18:55:13 +00:00
typedef struct {
2022-09-28 05:53:52 +00:00
// TODO: we now hold a ref to an item, instead of representing an item slot,
// so that we can let the item entity keep its own components and also handle merging ops on its own.
2022-09-28 13:17:33 +00:00
ecs_entity_t items[ITEMS_INVENTORY_SIZE];
2021-11-01 18:55:13 +00:00
float pickup_time;
} Inventory;
2022-08-09 14:46:23 +00:00
typedef struct {
2022-09-28 05:53:52 +00:00
ecs_entity_t items[ITEMS_CONTAINER_SIZE];
2022-08-09 14:46:23 +00:00
} ItemContainer;
enum {
PRODUCER_PUSH_PRODUCT,
PRODUCER_PUSH_ANY,
PRODUCER_PUSH_NONE,
};
enum {
PRODUCER_CRAFT_WAITING,
PRODUCER_CRAFT_BUSY,
PRODUCER_CRAFT_ENQUEUED,
PRODUCER_CRAFT_AUTO,
};
2022-09-28 05:29:32 +00:00
typedef struct {
asset_id target_item;
2022-09-28 13:17:33 +00:00
asset_id processed_item;
2022-10-18 06:57:43 +00:00
uint32_t processed_item_qty;
2022-09-29 15:35:43 +00:00
float process_time;
float energy_level;
uint8_t pending_task;
uint8_t push_filter;
2022-09-29 15:35:43 +00:00
} Producer;
2022-09-28 05:29:32 +00:00
2022-10-17 17:44:28 +00:00
typedef struct {
2022-10-18 06:57:43 +00:00
uint32_t push_qty;
2022-10-17 17:44:28 +00:00
} ItemRouter;
2022-09-28 13:17:33 +00:00
typedef struct {
asset_id kind;
2022-09-29 15:35:43 +00:00
float energy_level;
} EnergySource;
2022-09-28 05:29:32 +00:00
2022-08-09 15:32:58 +00:00
typedef struct {
uint16_t asset;
2022-10-17 16:28:18 +00:00
2022-09-29 16:06:08 +00:00
// progress bar
uint8_t progress_active;
float progress_value;
2022-08-09 15:32:58 +00:00
} Device;
2022-09-29 17:28:56 +00:00
typedef struct {
uint8_t w;
uint8_t h;
asset_id plan[256];
2022-09-29 17:28:56 +00:00
} Blueprint;
typedef struct {
double last_update;
double tick_delay;
} StreamInfo;
2022-07-31 14:34:47 +00:00
typedef struct {char _unused;} DemoNPC;
extern ECS_COMPONENT_DECLARE(Vector2D);
extern ECS_COMPONENT_DECLARE(Position);
extern ECS_COMPONENT_DECLARE(Velocity);
extern ECS_COMPONENT_DECLARE(Chunk);
extern ECS_COMPONENT_DECLARE(Drawable);
extern ECS_COMPONENT_DECLARE(Input);
extern ECS_COMPONENT_DECLARE(ClientInfo);
extern ECS_COMPONENT_DECLARE(Health);
extern ECS_COMPONENT_DECLARE(Classify);
extern ECS_COMPONENT_DECLARE(Vehicle);
extern ECS_COMPONENT_DECLARE(IsInVehicle);
2022-09-28 05:29:32 +00:00
extern ECS_COMPONENT_DECLARE(Item);
2022-09-29 15:35:43 +00:00
extern ECS_COMPONENT_DECLARE(BlockHarvest);
2022-07-31 14:34:47 +00:00
extern ECS_COMPONENT_DECLARE(Inventory);
2022-08-09 14:46:23 +00:00
extern ECS_COMPONENT_DECLARE(ItemContainer);
2022-09-29 15:35:43 +00:00
extern ECS_COMPONENT_DECLARE(Producer);
extern ECS_COMPONENT_DECLARE(EnergySource);
2022-10-17 17:44:28 +00:00
extern ECS_COMPONENT_DECLARE(ItemRouter);
2022-08-09 15:32:58 +00:00
extern ECS_COMPONENT_DECLARE(Device);
2022-09-29 17:28:56 +00:00
extern ECS_COMPONENT_DECLARE(Blueprint);
2022-07-31 14:34:47 +00:00
extern ECS_COMPONENT_DECLARE(DemoNPC);
extern ECS_COMPONENT_DECLARE(StreamInfo);
2021-11-01 18:55:13 +00:00
void ComponentsImport(ecs_world_t *ecs);