diff --git a/assets/entities.png b/assets/entities.png index 1961067..eb4ee93 100644 Binary files a/assets/entities.png and b/assets/entities.png differ diff --git a/game/components.h b/game/components.h index dfe7b74..1fd79dd 100644 --- a/game/components.h +++ b/game/components.h @@ -28,6 +28,8 @@ typedef f32 Health; typedef struct TextureRegion { Texture2D texture; Rectangle rec; + bool flipX; + bool flipY; } TextureRegion; typedef enum AnimationType { diff --git a/game/game_state.h b/game/game_state.h index a6bbd9c..cce6212 100644 --- a/game/game_state.h +++ b/game/game_state.h @@ -12,6 +12,8 @@ typedef struct Game { BzTileMap map; int selectedBuilding; ecs_entity_t *entityMap; + f32 frameDuration; + Vector2 targetPos; } Game; extern Game *GAME; diff --git a/game/main.c b/game/main.c index 0c76064..f087411 100644 --- a/game/main.c +++ b/game/main.c @@ -85,6 +85,7 @@ bool init(Game *game) { ECS_SYSTEM(ECS, updateAnimations, EcsOnUpdate, Animation, TextureRegion); ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion); + ECS_SYSTEM(ECS, updatePos, EcsOnUpdate, Position, TextureRegion); return true; } @@ -119,6 +120,8 @@ void update(float dt, Game *game) { game->camera.zoom += ((float) GetMouseWheelMove() * 0.05f); Vector2 worldPos = GetScreenToWorld2D(GetMousePosition(), game->camera); + if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) + game->targetPos = worldPos; int tileX = (int) worldPos.x / 16; int tileY = (int) worldPos.y / 16; @@ -165,7 +168,7 @@ void imguiRender(float dt, Game *game) { } if (igCollapsingHeader_TreeNodeFlags("Entities", 0)) { - + igSliderFloat("Frame duration", &game->frameDuration, 0.0f, 1.0f, NULL, 0); } igEnd(); igShowDemoWindow(NULL); diff --git a/game/map_init.c b/game/map_init.c index a1ed813..d2f7a9d 100644 --- a/game/map_init.c +++ b/game/map_init.c @@ -37,12 +37,12 @@ bool initEntityObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) { ecs_set(ECS, e, Animation, { .firstFrame=(TextureRegion) { objectTileset->tiles, - bzTilesetGetTileRegion(objectTileset, object.gid) + bzTilesetGetTileRegion(objectTileset, object.gid + 1) }, .currAnimation=ANIMATION_IDLE, .currFrame=0, - .frameCount=2, - .frameDuration=0.36f + .frameCount=4, + .frameDuration=0.20f }); } return true; diff --git a/game/systems/animations.c b/game/systems/animations.c index 0669993..8436b5b 100644 --- a/game/systems/animations.c +++ b/game/systems/animations.c @@ -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; diff --git a/game/systems/entity_renderer.c b/game/systems/entity_renderer.c index 1bd1a5b..2cd2bda 100644 --- a/game/systems/entity_renderer.c +++ b/game/systems/entity_renderer.c @@ -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); } } diff --git a/game/systems/systems.h b/game/systems/systems.h index 7759ade..aa0563f 100644 --- a/game/systems/systems.h +++ b/game/systems/systems.h @@ -8,4 +8,38 @@ void renderEntities(ecs_iter_t *it); void updateAnimations(ecs_iter_t *it); +#include "../game_state.h" +#include +#include +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 diff --git a/rawAssets/entities.kra b/rawAssets/entities.kra index 5c20fff..f01f53b 100644 Binary files a/rawAssets/entities.kra and b/rawAssets/entities.kra differ diff --git a/rawAssets/entities.png b/rawAssets/entities.png index 1961067..eb4ee93 100644 Binary files a/rawAssets/entities.png and b/rawAssets/entities.png differ