Add basic movement
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#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);
|
||||
@@ -7,6 +9,7 @@ void updateAnimations(ecs_iter_t *it) {
|
||||
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;
|
||||
|
||||
|
||||
@@ -9,6 +9,9 @@ void renderEntities(ecs_iter_t *it) {
|
||||
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};
|
||||
DrawTexturePro(t[i].texture, t[i].rec, dst, origin, r[i], WHITE);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,4 +8,38 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
#endif //PIXELDEFENSE_SYSTEMS_H
|
||||
|
||||
Reference in New Issue
Block a user