Move flecs out of engine, reorganize systems

This commit is contained in:
2023-11-17 20:29:51 +01:00
parent 2167d10501
commit 5cbb7b6c94
7 changed files with 130 additions and 157 deletions

View File

@@ -5,30 +5,53 @@
#include <math.h>
#include <raymath.h>
void entityRemoved(ecs_iter_t *it) {
static Position getBottomLeftPos(Position pos, Size size) {
return (Position) {pos.x - size.x * 0.5f, pos.y - size.y * 0.5f};
}
void entityAdded(ecs_iter_t *it) {
}
void entityUpdatePhysics(ecs_iter_t *it) {
void entitySpatialRemoved(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
Position *pos = ecs_field(it, Position, 1);
Size *size = ecs_field(it, Size, 2);
//MoveForce *force = ecs_field(it, MoveForce, 3);
SpatialGridID *spatialID = ecs_field(it, SpatialGridID, 3);
SpatialGridID *spatialID = ecs_field(it, SpatialGridID , 2);
for (i32 i = 0; i < it->count; i++) {
//bzSpatialGridUpdate(game->entityGrid, spatialID[i], pos[i].x, pos[i].y, size[i].x, size[i].y);
ecs_entity_t *e = bzSpatialGridGetData(game->entityGrid, spatialID[i]);
BZ_ASSERT(*e == it->entities[i]);
f32 posX = pos[i].x - size[i].x * 0.5f;
f32 posY = pos[i].y - size[i].y * 0.5f;
bzSpatialGridUpdate(game->entityGrid, spatialID[i], posX, posY, size[i].x, size[i].y);
bzSpatialGridRemove(game->entityGrid, spatialID[i]);
}
}
void pathRemoved(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
BzObjectPool *pool = game->pools.pathData;
Path *path = ecs_field(it, Path, 1);
for (i32 i = 0; i < it->count; i++) {
PathData *cur = path[i].paths;
while (cur) {
bzObjectPoolRelease(pool, cur);
cur = cur->next;
}
}
}
void entityUpdateKinematic(ecs_iter_t *it) {
Position *pos = ecs_field(it, Position, 1);
Rotation *rotation = ecs_field(it, Rotation, 2);
Velocity *velocity = ecs_field(it, Velocity, 3);
AngularVelocity *angularVelocity = ecs_field(it, Rotation, 4);
SteeringOutput *steeringOutput = ecs_field(it, SteeringOutput, 5);
}
void entityFollowPath(ecs_iter_t *it) {
Position *pos = ecs_field(it, Position, 1);
Rotation *rotation = ecs_field(it, Rotation, 2);
Velocity *velocity = ecs_field(it, Velocity, 3);
AngularVelocity *angularVelocity = ecs_field(it, Rotation, 4);
SteeringOutput *steeringOutput = ecs_field(it, SteeringOutput, 5);
Path *path = ecs_field(it, Path, 6);
}
void updateAnimations(ecs_iter_t *it) {
Game *game = ecs_singleton_get_mut(ECS, Game);
Animation *anim = ecs_field(it, Animation, 1);
@@ -66,9 +89,11 @@ static void render(ecs_iter_t *it) {
void renderTerrain(ecs_iter_t *it) {
render(it);
}
void renderBuildings(ecs_iter_t *it) {
render(it);
}
void renderEntities(ecs_iter_t *it) {
render(it);
}
@@ -84,68 +109,8 @@ void renderColliders(ecs_iter_t *it) {
}
}
void updatePos(ecs_iter_t *it) {
Position *pos = ecs_field(it, Position, 1);
TargetPosition *target = ecs_field(it, TargetPosition, 2);
TextureRegion *t = ecs_field(it, TextureRegion, 3);
for (i32 i = 0; i < it->count; i++) {
Vector2 d = Vector2Subtract(target[i], pos[i]);
if (Vector2LengthSqr(d) < 1) {
ecs_remove(ECS, it->entities[i], TargetPosition);
}
Vector2 dN = Vector2Normalize(d);
dN = Vector2Scale(dN, 20);
t[i].flipX = dN.x < 0;
pos[i].x += dN.x * it->delta_time;
pos[i].y += dN.y * it->delta_time;
}
}
#include <stdlib.h>
void targetFinish(ecs_iter_t *it) {
const Game *game = ecs_singleton_get(ECS, Game);
if (game == NULL) return;
for (i32 i = 0; i < it->count; i++) {
ecs_entity_t e = it->entities[i];
Path *path = ecs_get_mut(it->world, e, Path);
if (path->curWaypoint >= path->paths->numWaypoints) {
PathData *finished = path->paths;
path->paths = finished->next;
path->curWaypoint = 0;
bzObjectPoolRelease(game->pools.pathData, finished);
// Finished
if (path->paths == NULL) {
ecs_remove(it->world, e, Path);
continue;
}
}
TargetPosition target = path->paths->waypoints[path->curWaypoint];
path->curWaypoint++;
target.x += rand() % (4 + 1 + 2) -2;
ecs_set(it->world, e, TargetPosition, {target.x, target.y});
}
}
void startPath(ecs_iter_t *it) {
Path *path = ecs_field(it, Path, 1);
for (i32 i = 0; i < it->count; i++) {
ecs_entity_t e = it->entities[i];
if (path->paths == NULL) {
ecs_remove(it->world, e, Path);
continue;
}
ecs_set(it->world, e, TargetPosition, {path[i].paths->waypoints[0].x, path[i].paths->waypoints[0].y});
path[i].curWaypoint = 1;
}
}
void drawDebugPath(ecs_iter_t *it) {
void renderDebugPath(ecs_iter_t *it) {
Path *path = ecs_field(it, Path, 1);
for (i32 i = 0; i < it->count; i++) {