eco2d/code/modules/source/controllers.c

66 lines
2.0 KiB
C
Raw Normal View History

2021-05-04 17:41:30 +00:00
#include "modules/controllers.h"
#include "modules/general.h"
#include "modules/physics.h"
2021-05-07 14:43:54 +00:00
#include "zpl.h"
2021-05-07 20:48:15 +00:00
#define PLR_MOVE_SPEED 50.0
#define PLR_MOVE_SPEED_MULT 4.0
2021-05-07 14:43:54 +00:00
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++) {
2021-05-07 20:48:15 +00:00
double speed = PLR_MOVE_SPEED * (in[i].sprint ? PLR_MOVE_SPEED_MULT : 1.0);
if (zpl_abs(v[i].x) < speed)
v[i].x = in[i].x*speed;
if (zpl_abs(v[i].y) < speed)
v[i].y = in[i].y*speed;
2021-05-07 14:43:54 +00:00
}
}
2021-05-04 17:41:30 +00:00
2021-05-08 09:05:15 +00:00
#define DEMO_NPC_CHANGEDIR_FACTOR 0.03
#define DEMO_NPC_MOVE_SPEED 100
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);
}
}
2021-05-04 17:41:30 +00:00
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);
2021-05-07 14:43:54 +00:00
2021-05-04 17:41:30 +00:00
ECS_META(ecs, Input);
ECS_TAG(ecs, EcsActor);
ECS_TAG(ecs, EcsPlayer);
ECS_TAG(ecs, EcsBuilder);
2021-05-08 09:05:15 +00:00
ECS_TAG(ecs, EcsDemoNPC);
2021-05-07 14:43:54 +00:00
ECS_SYSTEM(ecs, MovementImpulse, EcsOnUpdate, Input, physics.Velocity);
2021-05-08 09:05:15 +00:00
ECS_SYSTEM(ecs, DemoNPCMoveAround, EcsOnUpdate, physics.Velocity, EcsDemoNPC);
2021-05-04 17:41:30 +00:00
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);
2021-05-08 09:05:15 +00:00
ECS_SET_ENTITY(EcsDemoNPC);
2021-05-04 17:41:30 +00:00
ECS_SET_TYPE(Builder);
ECS_SET_TYPE(Player);
2021-05-07 14:43:54 +00:00
ECS_SET_ENTITY(MovementImpulse);
2021-05-04 17:41:30 +00:00
}