160 lines
5.0 KiB
C
160 lines
5.0 KiB
C
#include "systems.h"
|
|
|
|
|
|
#include "game_state.h"
|
|
#include <math.h>
|
|
#include <raymath.h>
|
|
|
|
void entityRemoved(ecs_iter_t *it) {
|
|
|
|
}
|
|
void entityAdded(ecs_iter_t *it) {
|
|
|
|
}
|
|
void entityUpdatePhysics(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);
|
|
|
|
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]);
|
|
bzSpatialGridRemove(game->entityGrid, spatialID[i]);
|
|
spatialID[i] = bzSpatialGridInsert(game->entityGrid, &(it->entities[i]), pos[i].x, pos[i].y, size[i].x, size[i].y);
|
|
}
|
|
|
|
}
|
|
|
|
void updateAnimations(ecs_iter_t *it) {
|
|
Game *game = ecs_singleton_get_mut(ECS, Game);
|
|
Animation *anim = ecs_field(it, Animation, 1);
|
|
TextureRegion *t = ecs_field(it, TextureRegion, 2);
|
|
|
|
float dt = GetFrameTime();
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
anim[i].frameDuration = game->frameDuration;
|
|
anim[i].elapsed += dt;
|
|
if (anim[i].elapsed < anim[i].frameDuration) continue;
|
|
|
|
anim[i].currFrame = (anim[i].currFrame + 1) % anim[i].frameCount;
|
|
anim[i].elapsed = 0.0f;
|
|
t[i].rec.x = anim[i].firstFrame.rec.x + anim[i].currFrame * t[i].rec.width;
|
|
}
|
|
}
|
|
|
|
static void render(ecs_iter_t *it) {
|
|
Position *p = ecs_field(it, Position, 1);
|
|
Size *s = ecs_field(it, Size, 2);
|
|
Rotation *r = ecs_field(it, Rotation, 3);
|
|
TextureRegion *t = ecs_field(it, TextureRegion, 4);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
Rectangle dst = {p[i].x, p[i].y, s[i].x, s[i].y};
|
|
Vector2 origin = {dst.width * 0.5f, dst.height * 0.5f};
|
|
Rectangle src = t[i].rec;
|
|
if (t[i].flipX) src.width *= -1.0f;
|
|
if (t[i].flipY) src.height *= -1.0f;
|
|
DrawTexturePro(t[i].texture, src, dst, origin, r[i], WHITE);
|
|
//DrawRectangleLines(dst.x - dst.width * 0.5f, dst.y - dst.height * 0.5f, dst.width, dst.height, RED);
|
|
}
|
|
}
|
|
|
|
void renderTerrain(ecs_iter_t *it) {
|
|
render(it);
|
|
}
|
|
void renderBuildings(ecs_iter_t *it) {
|
|
render(it);
|
|
}
|
|
void renderEntities(ecs_iter_t *it) {
|
|
render(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) {
|
|
Path *path = ecs_field(it, Path, 1);
|
|
|
|
for (i32 i = 0; i < it->count; i++) {
|
|
PathData *pathData = path->paths;
|
|
bool first = true;
|
|
while (pathData) {
|
|
for (i32 iPath = 0; iPath < pathData->numWaypoints; iPath++) {
|
|
Color color = RED;
|
|
if (first && iPath < path[i].curWaypoint - 1)
|
|
color = GREEN;
|
|
else if (first && iPath == path[i].curWaypoint - 1)
|
|
color = ORANGE;
|
|
color.a = 180;
|
|
|
|
Position pos = pathData->waypoints[iPath];
|
|
DrawCircle(pos.x, pos.y, 3, color);
|
|
}
|
|
first = false;
|
|
pathData = pathData->next;
|
|
}
|
|
}
|
|
}
|