ecs: refactor modules

isolation_bkp/dynres
Dominik Madarász 2021-07-27 14:43:26 +02:00
parent 3e59e59881
commit 022f3870b8
19 changed files with 236 additions and 299 deletions

View File

@ -4,15 +4,12 @@
#include "librg.h"
#include "world/world.h"
#include "modules/general.h"
#include "modules/controllers.h"
#include "modules/net.h"
#include "modules/physics.h"
#include "modules/components.h"
#include "modules/systems.h"
#include "zpl.h"
uint64_t entity_spawn(char *name) {
ECS_IMPORT(world_ecs(), General);
ECS_IMPORT(world_ecs(), Physics);
ECS_IMPORT(world_ecs(), Components);
ecs_entity_t e = ecs_new(world_ecs(), 0);

View File

@ -16,9 +16,8 @@
#include "flecs/flecs_systems_civetweb.h"
#include "flecs/flecs_os_api_stdcpp.h"
#include "modules/general.h"
#include "modules/physics.h"
#include "modules/controllers.h"
#include "modules/components.h"
#include "modules/systems.h"
#include "packets/pkt_00_init.h"
#include "packets/pkt_01_welcome.h"

View File

@ -12,9 +12,8 @@
#include "flecs/flecs_systems_civetweb.h"
#include "flecs/flecs_os_api_stdcpp.h"
#include "modules/general.h"
#include "modules/physics.h"
#include "modules/controllers.h"
#include "modules/components.h"
#include "modules/systems.h"
#define DEFAULT_WORLD_SEED 302097
#define DEFAULT_CHUNK_SIZE 16 /* amount of blocks within a chunk (single axis) */
@ -68,9 +67,7 @@ int main(int argc, char** argv) {
// TODO(zaklaus): VERY TEMPORARY -- SPAWN SOME NPCS THAT RANDOMLY MOVE
{
ECS_IMPORT(world_ecs(), General);
ECS_IMPORT(world_ecs(), Controllers);
ECS_IMPORT(world_ecs(), Physics);
ECS_IMPORT(world_ecs(), Components);
for (uint32_t i = 0; i < npc_count; i++) {
uint64_t e = entity_spawn(NULL);
ecs_add(world_ecs(), e, EcsDemoNPC);

View File

@ -7,7 +7,8 @@
#include "camera.h"
#include "player.h"
#include "modules/net.h"
#include "modules/components.h"
#include "modules/systems.h"
pkt_desc pkt_00_init_desc[] = {
{ PKT_FIELD(CWP_ITEM_POSITIVE_INTEGER, pkt_00_init, view_id) },
@ -27,7 +28,7 @@ size_t pkt_00_init_send(uint16_t view_id) {
}
int32_t pkt_00_init_handler(pkt_header *header) {
ECS_IMPORT(world_ecs(), Net);
ECS_IMPORT(world_ecs(), Components);
pkt_00_init table;
PKT_IF(pkt_msg_decode(header, pkt_00_init_desc, pkt_pack_desc_args(pkt_00_init_desc), PKT_STRUCT_PTR(&table)));

View File

@ -1,6 +1,7 @@
#include "packet_utils.h"
#include "packets/pkt_send_keystate.h"
#include "modules/controllers.h"
#include "modules/components.h"
#include "modules/systems.h"
#include "world/world.h"
pkt_desc pkt_send_keystate_desc[] = {
@ -33,7 +34,7 @@ int32_t pkt_send_keystate_handler(pkt_header *header) {
ecs_entity_t e = PKT_GET_ENT(header);
ecs_world_t *ecs = world_ecs();
ECS_IMPORT(ecs, Controllers);
ECS_IMPORT(ecs, Components);
Input *i = ecs_get_mut(world_ecs(), e, Input, NULL);
if (i) {
i->x = table.x;
@ -41,6 +42,6 @@ int32_t pkt_send_keystate_handler(pkt_header *header) {
i->use = table.use;
i->sprint = table.sprint;
}
return 0;
}

View File

@ -5,10 +5,8 @@
#include "librg.h"
#include "world/world.h"
#include "modules/general.h"
#include "modules/controllers.h"
#include "modules/net.h"
#include "modules/physics.h"
#include "modules/components.h"
#include "modules/systems.h"
#include "zpl.h"
uint64_t player_spawn(char *name) {
@ -18,9 +16,7 @@ uint64_t player_spawn(char *name) {
name = zpl_bprintf("player_%d", e);
}
ECS_IMPORT(world_ecs(), General);
ECS_IMPORT(world_ecs(), Controllers);
ECS_IMPORT(world_ecs(), Net);
ECS_IMPORT(world_ecs(), Components);
ecs_set(world_ecs(), e, EcsName, {.alloc_value = name });
ecs_add(world_ecs(), e, EcsClient);

View File

@ -1,8 +1,7 @@
#include "zpl.h"
#include "librg.h"
#include "modules/general.h"
#include "modules/net.h"
#include "modules/physics.h"
#include "modules/components.h"
#include "modules/systems.h"
#include "world/world.h"
#include "entity_view.h"
#include "world/worldgen/worldgen.h"
@ -14,12 +13,9 @@
static world_data world = {0};
entity_view world_build_entity_view(int64_t e) {
ECS_IMPORT(world_ecs(), General);
ECS_IMPORT(world_ecs(), Physics);
ECS_IMPORT(world_ecs(), Net);
ECS_IMPORT(world_ecs(), Components);
entity_view view = {0};
// TODO(zaklaus): branch out based on ECS tags
const Position *pos = ecs_get(world_ecs(), e, Position);
if (pos) {
@ -140,9 +136,9 @@ int32_t world_init(int32_t seed, uint16_t chunk_size, uint16_t chunk_amount) {
world.ecs = ecs_init();
ecs_set_entity_range(world.ecs, 0, UINT32_MAX);
ECS_IMPORT(world.ecs, General);
ECS_IMPORT(world.ecs, Net);
world.ecs_update = ecs_query_new(world.ecs, "net.ClientInfo, general.Position");
ECS_IMPORT(world.ecs, Components);
ECS_IMPORT(world.ecs, Systems);
world.ecs_update = ecs_query_new(world.ecs, "components.ClientInfo, components.Position");
world.chunk_mapping = zpl_malloc(sizeof(ecs_entity_t)*zpl_square(chunk_amount));
world.block_mapping = zpl_malloc(sizeof(uint8_t*)*zpl_square(chunk_amount));
world.chunk_handle = ecs_typeid(Chunk);
@ -197,8 +193,7 @@ static void world_tracker_update(uint8_t ticker, uint32_t freq, uint8_t radius)
world.tracker_update[ticker] = zpl_time_rel_ms() + freq;
profile(PROF_WORLD_WRITE) {
ECS_IMPORT(world.ecs, General);
ECS_IMPORT(world.ecs, Net);
ECS_IMPORT(world.ecs, Components);
ecs_iter_t it = ecs_query_iter(world.ecs_update);
static char buffer[WORLD_LIBRG_BUFSIZ] = {0};
@ -214,7 +209,6 @@ static void world_tracker_update(uint8_t ticker, uint32_t freq, uint8_t radius)
{
librg_entity_radius_set(world_tracker(), p[i].peer, radius);
}
// TODO(zaklaus): push radius once librg patch comes in
int32_t result = librg_world_write(world_tracker(), p[i].peer, buffer, &datalen, NULL);
@ -232,7 +226,7 @@ static void world_tracker_update(uint8_t ticker, uint32_t freq, uint8_t radius)
int32_t world_update() {
ECS_IMPORT(world.ecs, General);
ECS_IMPORT(world.ecs, Components);
profile (PROF_UPDATE_SYSTEMS) {
ecs_progress(world.ecs, 0.0f);
@ -350,7 +344,7 @@ int64_t world_chunk_from_entity(ecs_entity_t id) {
void world_chunk_replace_block(int64_t id, uint16_t block_idx, uint8_t block_id) {
assert(block_idx >= 0 && block_idx < zpl_square(world.chunk_size));
world.block_mapping[id][block_idx] = block_id;
world_chunk_mark_dirty(id);
world_chunk_mark_dirty(world.chunk_mapping[id]);
}
uint8_t *world_chunk_get_blocks(int64_t id) {
@ -358,12 +352,16 @@ uint8_t *world_chunk_get_blocks(int64_t id) {
}
void world_chunk_mark_dirty(ecs_entity_t e) {
Chunk *chunk = (Chunk *)ecs_get_mut_w_entity(world.ecs, e, world.chunk_handle, NULL);
bool was_added=false;
Chunk *chunk = (Chunk *)ecs_get_mut_w_entity(world.ecs, e, world.chunk_handle, &was_added);
assert(!was_added);
if (chunk) chunk->is_dirty = true;
}
uint8_t world_chunk_is_dirty(ecs_entity_t e) {
Chunk *chunk = (Chunk *)ecs_get_mut_w_entity(world.ecs, e, world.chunk_handle, NULL);
bool was_added=false;
Chunk *chunk = (Chunk *)ecs_get_mut_w_entity(world.ecs, e, world.chunk_handle, &was_added);
assert(!was_added);
if (chunk) return chunk->is_dirty;
return false;
}

View File

@ -1,3 +1,5 @@
file(GLOB MODULES modules/*.h source/*.c)
add_library(eco2d-modules STATIC ${MODULES})
add_library(eco2d-modules STATIC
source/systems.c
source/components.c
)
include_directories(. ../game/header)

View File

@ -0,0 +1,76 @@
#pragma once
#include "flecs/flecs.h"
#include "flecs/flecs_meta.h"
#include "world/world.h"
ECS_STRUCT(Vector2D, {
float x;
float y;
});
ECS_STRUCT(Chunk, {
uint32_t id;
int16_t x;
int16_t y;
uint8_t is_dirty;
});
ECS_STRUCT(Drawable, {
uint16_t id;
});
ECS_ALIAS(Vector2D, Position);
ECS_ALIAS(Vector2D, Velocity);
ECS_STRUCT(Input, {
float x;
float y;
uint8_t use;
uint8_t sprint;
});
ECS_STRUCT(ClientInfo, {
uintptr_t peer;
uint16_t view_id;
});
typedef struct {
ECS_DECLARE_COMPONENT(Chunk);
ECS_DECLARE_COMPONENT(Position);
ECS_DECLARE_COMPONENT(Vector2D);
ECS_DECLARE_COMPONENT(Drawable);
ECS_DECLARE_COMPONENT(Input);
ECS_DECLARE_ENTITY(EcsActor);
ECS_DECLARE_ENTITY(EcsPlayer);
ECS_DECLARE_ENTITY(EcsBuilder);
ECS_DECLARE_ENTITY(EcsDemoNPC);
ECS_DECLARE_TYPE(Player);
ECS_DECLARE_TYPE(Builder);
ECS_DECLARE_TYPE(Movement);
ECS_DECLARE_ENTITY(Walking);
ECS_DECLARE_ENTITY(Flying);
ECS_DECLARE_COMPONENT(Velocity);
ECS_DECLARE_ENTITY(EcsClient);
ECS_DECLARE_COMPONENT(ClientInfo);
} Components;
#define ComponentsImportHandles(handles)\
ECS_IMPORT_COMPONENT(handles, Chunk);\
ECS_IMPORT_COMPONENT(handles, Vector2D);\
ECS_IMPORT_COMPONENT(handles, Position);\
ECS_IMPORT_COMPONENT(handles, Drawable);\
ECS_IMPORT_COMPONENT(handles, Input);\
ECS_IMPORT_TYPE(handles, Player);\
ECS_IMPORT_TYPE(handles, Builder);\
ECS_IMPORT_ENTITY(handles, EcsActor);\
ECS_IMPORT_ENTITY(handles, EcsPlayer);\
ECS_IMPORT_ENTITY(handles, EcsBuilder);\
ECS_IMPORT_ENTITY(handles, EcsDemoNPC);\
ECS_IMPORT_TYPE(handles, Movement);\
ECS_IMPORT_ENTITY(handles, Walking);\
ECS_IMPORT_ENTITY(handles, Flying);\
ECS_IMPORT_COMPONENT(handles, Velocity);\
ECS_IMPORT_ENTITY(handles, EcsClient);\
ECS_IMPORT_COMPONENT(handles, ClientInfo);\
void ComponentsImport(ecs_world_t *ecs);

View File

@ -1,35 +0,0 @@
#pragma once
#include "flecs/flecs.h"
#include "flecs/flecs_meta.h"
ECS_STRUCT(Input, {
float x;
float y;
uint8_t use;
uint8_t sprint;
});
typedef struct {
ECS_DECLARE_COMPONENT(Input);
ECS_DECLARE_ENTITY(EcsActor);
ECS_DECLARE_ENTITY(EcsPlayer);
ECS_DECLARE_ENTITY(EcsBuilder);
ECS_DECLARE_ENTITY(EcsDemoNPC);
ECS_DECLARE_TYPE(Player);
ECS_DECLARE_TYPE(Builder);
ECS_DECLARE_ENTITY(MovementImpulse);
ECS_DECLARE_ENTITY(DemoNPCMoveAround);
} Controllers;
#define ControllersImportHandles(handles)\
ECS_IMPORT_COMPONENT(handles, Input);\
ECS_IMPORT_TYPE(handles, Player);\
ECS_IMPORT_TYPE(handles, Builder);\
ECS_IMPORT_ENTITY(handles, EcsActor);\
ECS_IMPORT_ENTITY(handles, EcsPlayer);\
ECS_IMPORT_ENTITY(handles, EcsBuilder);\
ECS_IMPORT_ENTITY(handles, EcsDemoNPC);\
ECS_IMPORT_ENTITY(handles, MovementImpulse);\
ECS_IMPORT_ENTITY(handles, DemoNPCMoveAround);\
void ControllersImport(ecs_world_t *ecs);

View File

@ -1,37 +0,0 @@
#pragma once
#include "flecs/flecs.h"
#include "flecs/flecs_meta.h"
#include "world/world.h"
ECS_STRUCT(Vector2D, {
float x;
float y;
});
ECS_STRUCT(Chunk, {
uint32_t id;
int16_t x;
int16_t y;
uint8_t is_dirty;
});
ECS_STRUCT(Drawable, {
uint16_t id;
});
ECS_ALIAS(Vector2D, Position);
typedef struct {
ECS_DECLARE_COMPONENT(Chunk);
ECS_DECLARE_COMPONENT(Position);
ECS_DECLARE_COMPONENT(Vector2D);
ECS_DECLARE_COMPONENT(Drawable);
} General;
#define GeneralImportHandles(handles)\
ECS_IMPORT_COMPONENT(handles, Chunk);\
ECS_IMPORT_COMPONENT(handles, Vector2D);\
ECS_IMPORT_COMPONENT(handles, Position);\
ECS_IMPORT_COMPONENT(handles, Drawable);\
void GeneralImport(ecs_world_t *ecs);

View File

@ -1,19 +0,0 @@
#pragma once
#include "flecs/flecs.h"
#include "flecs/flecs_meta.h"
ECS_STRUCT(ClientInfo, {
uintptr_t peer;
uint16_t view_id;
});
typedef struct {
ECS_DECLARE_ENTITY(EcsClient);
ECS_DECLARE_COMPONENT(ClientInfo);
} Net;
#define NetImportHandles(handles)\
ECS_IMPORT_ENTITY(handles, EcsClient);\
ECS_IMPORT_COMPONENT(handles, ClientInfo);\
void NetImport(ecs_world_t *ecs);

View File

@ -1,30 +0,0 @@
#pragma once
#include "flecs/flecs.h"
#include "flecs/flecs_meta.h"
#include "modules/general.h"
ECS_ALIAS(Vector2D, Velocity);
typedef struct {
ECS_DECLARE_TYPE(Movement);
ECS_DECLARE_ENTITY(Walking);
ECS_DECLARE_ENTITY(Flying);
ECS_DECLARE_COMPONENT(Velocity);
ECS_DECLARE_ENTITY(MoveWalk);
ECS_DECLARE_ENTITY(UpdateTrackerPos);
ECS_DECLARE_ENTITY(IntegratePositions);
ECS_DECLARE_ENTITY(PushOutOverlappingEntities);
} Physics;
#define PhysicsImportHandles(handles)\
ECS_IMPORT_TYPE(handles, Movement);\
ECS_IMPORT_ENTITY(handles, Walking);\
ECS_IMPORT_ENTITY(handles, Flying);\
ECS_IMPORT_COMPONENT(handles, Velocity);\
ECS_IMPORT_ENTITY(handles, MoveWalk);\
ECS_IMPORT_ENTITY(handles, UpdateTrackerPos);\
ECS_IMPORT_ENTITY(handles, IntegratePositions);\
ECS_IMPORT_ENTITY(handles, PushOutOverlappingEntities);\
void PhysicsImport(ecs_world_t *ecs);

View File

@ -0,0 +1,17 @@
#pragma once
#include "flecs/flecs.h"
typedef struct {
ECS_DECLARE_ENTITY(MoveWalk);
ECS_DECLARE_ENTITY(UpdateTrackerPos);
ECS_DECLARE_ENTITY(IntegratePositions);
ECS_DECLARE_ENTITY(PushOutOverlappingEntities);
} Systems;
#define SystemsImportHandles(handles)\
ECS_IMPORT_ENTITY(handles, MoveWalk);\
ECS_IMPORT_ENTITY(handles, UpdateTrackerPos);\
ECS_IMPORT_ENTITY(handles, IntegratePositions);\
ECS_IMPORT_ENTITY(handles, PushOutOverlappingEntities);\
void SystemsImport(ecs_world_t *ecs);

View File

@ -0,0 +1,48 @@
#include "modules/components.h"
void ComponentsImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Components);
ecs_set_name_prefix(ecs, "Components");
ECS_IMPORT(ecs, FlecsMeta);
ECS_META(ecs, Position);
ECS_META(ecs, Chunk);
ECS_META(ecs, Vector2D);
ECS_META(ecs, Drawable);
ECS_META(ecs, ClientInfo);
ECS_META(ecs, Velocity);
ECS_META(ecs, Input);
ECS_TAG(ecs, Walking);
ECS_TAG(ecs, Flying);
ECS_TAG(ecs, EcsClient);
ECS_TAG(ecs, EcsActor);
ECS_TAG(ecs, EcsPlayer);
ECS_TAG(ecs, EcsBuilder);
ECS_TAG(ecs, EcsDemoNPC);
ECS_PREFAB(ecs, Base, Position, Velocity, Input, EcsActor);
ECS_TYPE(ecs, Movement, Walking, Flying);
ECS_TYPE(ecs, Player, INSTANCEOF | Base, SWITCH | Movement, CASE | Walking, EcsActor, EcsPlayer);
ECS_TYPE(ecs, Builder, INSTANCEOF | Base, SWITCH | Movement, CASE | Flying, EcsActor, EcsBuilder);
ECS_SET_COMPONENT(Chunk);
ECS_SET_COMPONENT(Vector2D);
ECS_SET_COMPONENT(Position);
ECS_SET_COMPONENT(Drawable);
ECS_SET_COMPONENT(Velocity);
ECS_SET_COMPONENT(ClientInfo);
ECS_SET_COMPONENT(Input);
ECS_SET_ENTITY(EcsClient);
ECS_SET_ENTITY(Walking);
ECS_SET_ENTITY(Flying);
ECS_SET_ENTITY(EcsActor);
ECS_SET_ENTITY(EcsPlayer);
ECS_SET_ENTITY(EcsBuilder);
ECS_SET_ENTITY(EcsDemoNPC);
ECS_SET_TYPE(Movement);
ECS_SET_TYPE(Builder);
ECS_SET_TYPE(Player);
}

View File

@ -1,80 +0,0 @@
#include "zpl.h"
#include "modules/controllers.h"
#include "modules/general.h"
#include "modules/physics.h"
#include "world/blocks.h"
#define PLR_MOVE_SPEED 50.0
#define PLR_MOVE_SPEED_MULT 4.0
void MovementImpulse(ecs_iter_t *it) {
Input *in = ecs_column(it, Input, 1);
Velocity *v = ecs_column(it, Velocity, 2);
for (int i = 0; i < it->count; i++) {
double speed = PLR_MOVE_SPEED * (in[i].sprint ? PLR_MOVE_SPEED_MULT : 1.0);
if (zpl_abs(v[i].x) < speed && in[i].x)
v[i].x = in[i].x*speed;
if (zpl_abs(v[i].y) < speed && in[i].y)
v[i].y = in[i].y*speed;
}
}
#define DEMO_NPC_CHANGEDIR_FACTOR 0.1
#define DEMO_NPC_MOVE_SPEED 1500
void DemoNPCMoveAround(ecs_iter_t *it) {
Velocity *v = ecs_column(it, Velocity, 1);
for (int i = 0; i < it->count; i++) {
v[i].x = zpl_lerp(v[i].x, (rand()%3-1)*DEMO_NPC_MOVE_SPEED, DEMO_NPC_CHANGEDIR_FACTOR);
v[i].y = zpl_lerp(v[i].y, (rand()%3-1)*DEMO_NPC_MOVE_SPEED, DEMO_NPC_CHANGEDIR_FACTOR);
}
}
void DemoPlaceIceBlock(ecs_iter_t *it) {
Input *in = ecs_column(it, Input, 1);
Position *p = ecs_column(it, Position, 2);
uint8_t watr_id = blocks_find(BLOCK_BIOME_DEV, BLOCK_KIND_WATER);
for (int i = 0; i < it->count; i++) {
if (in[i].use) {
world_block_lookup l = world_block_from_realpos(p[i].x, p[i].y);
world_chunk_replace_block(l.chunk_id, l.id, watr_id);
}
}
}
void ControllersImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Controllers);
ecs_set_name_prefix(ecs, "Controllers");
ECS_IMPORT(ecs, General);
ECS_IMPORT(ecs, Physics);
ECS_IMPORT(ecs, FlecsMeta);
ECS_META(ecs, Input);
ECS_TAG(ecs, EcsActor);
ECS_TAG(ecs, EcsPlayer);
ECS_TAG(ecs, EcsBuilder);
ECS_TAG(ecs, EcsDemoNPC);
ECS_SYSTEM(ecs, MovementImpulse, EcsOnLoad, Input, physics.Velocity);
ECS_SYSTEM(ecs, DemoPlaceIceBlock, EcsOnLoad, Input, general.Position);
ECS_SYSTEM(ecs, DemoNPCMoveAround, EcsOnLoad, physics.Velocity, EcsDemoNPC);
ECS_PREFAB(ecs, Base, general.Position, physics.Velocity, Input, EcsActor);
ECS_TYPE(ecs, Player, INSTANCEOF | Base, SWITCH | physics.Movement, CASE | physics.Walking, EcsActor, EcsPlayer);
ECS_TYPE(ecs, Builder, INSTANCEOF | Base, SWITCH | physics.Movement, CASE | physics.Flying, EcsActor, EcsBuilder);
ECS_SET_COMPONENT(Input);
ECS_SET_ENTITY(EcsActor);
ECS_SET_ENTITY(EcsPlayer);
ECS_SET_ENTITY(EcsBuilder);
ECS_SET_ENTITY(EcsDemoNPC);
ECS_SET_TYPE(Builder);
ECS_SET_TYPE(Player);
}

View File

@ -1,18 +0,0 @@
#include "modules/general.h"
void GeneralImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, General);
ecs_set_name_prefix(ecs, "General");
ECS_IMPORT(ecs, FlecsMeta);
ECS_META(ecs, Position);
ECS_META(ecs, Chunk);
ECS_META(ecs, Vector2D);
ECS_META(ecs, Drawable);
ECS_SET_COMPONENT(Chunk);
ECS_SET_COMPONENT(Vector2D);
ECS_SET_COMPONENT(Position);
ECS_SET_COMPONENT(Drawable);
}

View File

@ -1,16 +0,0 @@
#include "modules/net.h"
void NetImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Net);
ecs_set_name_prefix(ecs, "Net");
ECS_TAG(ecs, EcsClient);
ECS_IMPORT(ecs, FlecsMeta);
ECS_META(ecs, ClientInfo);
ECS_EXPORT_ENTITY(EcsClient);
ECS_EXPORT_COMPONENT(ClientInfo);
}

View File

@ -1,6 +1,6 @@
#include "zpl.h"
#include "modules/physics.h"
#include "modules/systems.h"
#include "modules/components.h"
#include "world/world.h"
#include "world/blocks.h"
#include "profiler.h"
@ -127,27 +127,67 @@ void UpdateTrackerPos(ecs_iter_t *it) {
}
}
void PhysicsImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Physics);
ecs_set_name_prefix(ecs, "Physics");
#define PLR_MOVE_SPEED 50.0
#define PLR_MOVE_SPEED_MULT 4.0
void MovementImpulse(ecs_iter_t *it) {
Input *in = ecs_column(it, Input, 1);
Velocity *v = ecs_column(it, Velocity, 2);
ECS_TAG(ecs, Walking);
ECS_TAG(ecs, Flying);
ECS_TYPE(ecs, Movement, Walking, Flying);
for (int i = 0; i < it->count; i++) {
double speed = PLR_MOVE_SPEED * (in[i].sprint ? PLR_MOVE_SPEED_MULT : 1.0);
if (zpl_abs(v[i].x) < speed && in[i].x)
v[i].x = in[i].x*speed;
if (zpl_abs(v[i].y) < speed && in[i].y)
v[i].y = in[i].y*speed;
}
}
#define DEMO_NPC_CHANGEDIR_FACTOR 0.1
#define DEMO_NPC_MOVE_SPEED 1500
void DemoNPCMoveAround(ecs_iter_t *it) {
Velocity *v = ecs_column(it, Velocity, 1);
ECS_META(ecs, Velocity);
for (int i = 0; i < it->count; i++) {
v[i].x = zpl_lerp(v[i].x, (rand()%3-1)*DEMO_NPC_MOVE_SPEED, DEMO_NPC_CHANGEDIR_FACTOR);
v[i].y = zpl_lerp(v[i].y, (rand()%3-1)*DEMO_NPC_MOVE_SPEED, DEMO_NPC_CHANGEDIR_FACTOR);
}
}
void DemoPlaceIceBlock(ecs_iter_t *it) {
Input *in = ecs_column(it, Input, 1);
Position *p = ecs_column(it, Position, 2);
uint8_t watr_id = blocks_find(BLOCK_BIOME_DEV, BLOCK_KIND_WATER);
for (int i = 0; i < it->count; i++) {
if (in[i].use) {
world_block_lookup l = world_block_from_realpos(p[i].x, p[i].y);
world_chunk_replace_block(l.chunk_id, l.id, watr_id);
}
}
}
void SystemsImport(ecs_world_t *ecs) {
ECS_MODULE(ecs, Systems);
ecs_set_name_prefix(ecs, "Systems");
ECS_IMPORT(ecs, Components);
ECS_SYSTEM(ecs, MovementImpulse, EcsOnLoad, components.Input, components.Velocity);
ECS_SYSTEM(ecs, DemoPlaceIceBlock, EcsOnLoad, components.Input, components.Position);
ECS_SYSTEM(ecs, DemoNPCMoveAround, EcsOnLoad, components.Velocity, components.EcsDemoNPC);
ECS_SYSTEM(ecs, MoveWalk, EcsOnUpdate, components.Position, components.Velocity);
ECS_SYSTEM(ecs, IntegratePositions, EcsOnValidate, components.Position, components.Velocity);
//ECS_SYSTEM(ecs, PushOutOverlappingEntities, EcsOnValidate, components.Position, Velocity);
ECS_SYSTEM(ecs, UpdateTrackerPos, EcsPostUpdate, components.Position);
ECS_SYSTEM(ecs, MoveWalk, EcsOnUpdate, general.Position, Velocity);
ECS_SYSTEM(ecs, IntegratePositions, EcsOnValidate, general.Position, Velocity);
//ECS_SYSTEM(ecs, PushOutOverlappingEntities, EcsOnValidate, general.Position, Velocity);
ECS_SYSTEM(ecs, UpdateTrackerPos, EcsPostUpdate, general.Position);
ECS_SET_TYPE(Movement);
ECS_SET_ENTITY(Walking);
ECS_SET_ENTITY(Flying);
ECS_SET_COMPONENT(Velocity);
ECS_SET_ENTITY(MoveWalk);
ECS_SET_ENTITY(UpdateTrackerPos);
ECS_SET_ENTITY(IntegratePositions);
ECS_SET_ENTITY(PushOutOverlappingEntities);
//ECS_SET_ENTITY(PushOutOverlappingEntities);
}