From d7975eacb7c9b89c0fad37ec454388d20072f41b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Sat, 8 May 2021 11:22:25 +0200 Subject: [PATCH] introduce HandleCollisions system --- code/modules/modules/physics.h | 2 ++ code/modules/source/controllers.c | 4 ++-- code/modules/source/physics.c | 12 +++++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/code/modules/modules/physics.h b/code/modules/modules/physics.h index 21a0ee2..f41cc29 100644 --- a/code/modules/modules/physics.h +++ b/code/modules/modules/physics.h @@ -13,6 +13,7 @@ typedef struct { ECS_DECLARE_COMPONENT(Velocity); ECS_DECLARE_ENTITY(MoveWalk); ECS_DECLARE_ENTITY(UpdateTrackerPos); + ECS_DECLARE_ENTITY(HandleCollisions); } Physics; #define PhysicsImportHandles(handles)\ @@ -22,5 +23,6 @@ typedef struct { ECS_IMPORT_COMPONENT(handles, Velocity);\ ECS_IMPORT_ENTITY(handles, MoveWalk);\ ECS_IMPORT_ENTITY(handles, UpdateTrackerPos);\ + ECS_IMPORT_ENTITY(handles, HandleCollisions);\ void PhysicsImport(ecs_world_t *ecs); diff --git a/code/modules/source/controllers.c b/code/modules/source/controllers.c index 69bab8a..7890319 100644 --- a/code/modules/source/controllers.c +++ b/code/modules/source/controllers.c @@ -20,8 +20,8 @@ void MovementImpulse(ecs_iter_t *it) { } } -#define DEMO_NPC_CHANGEDIR_FACTOR 0.03 -#define DEMO_NPC_MOVE_SPEED 100 +#define DEMO_NPC_CHANGEDIR_FACTOR 0.01 +#define DEMO_NPC_MOVE_SPEED 1000 void DemoNPCMoveAround(ecs_iter_t *it) { Velocity *v = ecs_column(it, Velocity, 1); diff --git a/code/modules/source/physics.c b/code/modules/source/physics.c index d99a498..ce0c3e4 100644 --- a/code/modules/source/physics.c +++ b/code/modules/source/physics.c @@ -14,7 +14,14 @@ void MoveWalk(ecs_iter_t *it) { p[i].y += v[i].y * it->delta_time; v[i].x = zpl_lerp(v[i].x, 0.0f, PHY_WALK_DRAG); v[i].y = zpl_lerp(v[i].y, 0.0f, PHY_WALK_DRAG); - + } +} + +void HandleCollisions(ecs_iter_t *it) { + Position *p = ecs_column(it, Position, 1); + Velocity *v = ecs_column(it, Velocity, 2); + + for (int i = 0; i < it->count; i++) { // NOTE(zaklaus): world bounds { double w = (double)world_dim()/2.0; @@ -43,6 +50,7 @@ void PhysicsImport(ecs_world_t *ecs) { ECS_META(ecs, Velocity); ECS_SYSTEM(ecs, MoveWalk, EcsOnUpdate, general.Position, Velocity); + ECS_SYSTEM(ecs, HandleCollisions, EcsOnValidate, general.Position, Velocity); ECS_SYSTEM(ecs, UpdateTrackerPos, EcsPostUpdate, general.Position); ECS_SET_TYPE(Movement); @@ -50,4 +58,6 @@ void PhysicsImport(ecs_world_t *ecs) { ECS_SET_ENTITY(Flying); ECS_SET_COMPONENT(Velocity); ECS_SET_ENTITY(MoveWalk); + ECS_SET_ENTITY(UpdateTrackerPos); + ECS_SET_ENTITY(HandleCollisions); }