Path following
This commit is contained in:
@@ -1,20 +0,0 @@
|
||||
#include "systems.h"
|
||||
|
||||
#include "../game_state.h"
|
||||
|
||||
void updateAnimations(ecs_iter_t *it) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
#include "systems.h"
|
||||
|
||||
void renderEntities(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);
|
||||
}
|
||||
}
|
||||
95
game/systems/entity_systems.c
Normal file
95
game/systems/entity_systems.c
Normal file
@@ -0,0 +1,95 @@
|
||||
#include "systems.h"
|
||||
|
||||
|
||||
#include "../game_state.h"
|
||||
#include <math.h>
|
||||
#include <raymath.h>
|
||||
|
||||
|
||||
void updateAnimations(ecs_iter_t *it) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void renderEntities(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);
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
bzLogInfo("Done");
|
||||
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) {
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
ecs_entity_t e = it->entities[i];
|
||||
Path *path = ecs_get(it->world, e, Path);
|
||||
if (!path) continue;
|
||||
path->curWaypoint++;
|
||||
if (path->curWaypoint >= path->numWaypoints) {
|
||||
// Finished
|
||||
ecs_remove(it->world, e, Path);
|
||||
continue;
|
||||
}
|
||||
TargetPosition target = path->waypoints[path->curWaypoint - 1];
|
||||
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->numWaypoints == 0) {
|
||||
ecs_remove(it->world, e, Path);
|
||||
continue;
|
||||
}
|
||||
ecs_set(it->world, e, TargetPosition, {path[i].waypoints[0].x, path[i].waypoints[0].y});
|
||||
path[i].curWaypoint++;
|
||||
}
|
||||
}
|
||||
@@ -7,39 +7,8 @@
|
||||
|
||||
void renderEntities(ecs_iter_t *it);
|
||||
void updateAnimations(ecs_iter_t *it);
|
||||
|
||||
#include "../game_state.h"
|
||||
#include <math.h>
|
||||
#include <raymath.h>
|
||||
static void updatePos(ecs_iter_t *it) {
|
||||
Position *pos = ecs_field(it, Position, 1);
|
||||
TextureRegion *t = ecs_field(it, TextureRegion, 2);
|
||||
|
||||
Vector2 target = GAME->targetPos;
|
||||
if (target.x == 0 && target.y == 0) return;
|
||||
|
||||
for (i32 i = 0; i < it->count; i++) {
|
||||
target = Vector2Subtract(target, pos[i]);
|
||||
float dX = 0, dY = 0;
|
||||
if (target.x > 0) dX = 1;
|
||||
else if (target.x < 0) dX = -1;
|
||||
if (target.y > 0) dY = 1;
|
||||
else if (target.y < 0) dY = -1;
|
||||
|
||||
dX *= 20;
|
||||
dY *= 20;
|
||||
|
||||
if (Vector2Length(target) < 1) continue;
|
||||
|
||||
pos[i].x += dX * it->delta_time;
|
||||
pos[i].y += dY * it->delta_time;
|
||||
|
||||
f32 ddx = GAME->targetPos.x - pos[i].x;
|
||||
if (ddx < 0) ddx *= -1.0f;
|
||||
|
||||
if (ddx > 5)
|
||||
t[i].flipX = dX < 0;
|
||||
}
|
||||
}
|
||||
void updatePos(ecs_iter_t *it);
|
||||
void targetFinish(ecs_iter_t *it);
|
||||
void startPath(ecs_iter_t *it);
|
||||
|
||||
#endif //PIXELDEFENSE_SYSTEMS_H
|
||||
|
||||
Reference in New Issue
Block a user