start turret
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
#include "entity.h"
|
||||
#include "../player/player.h"
|
||||
#include "../util/pathfinding.h"
|
||||
#include "../util/font.h"
|
||||
|
||||
EntityArray entities;
|
||||
|
||||
@@ -26,40 +27,52 @@ void renderEntities(SDL_Renderer *renderer, SDL_Rect playerRect) {
|
||||
EntityTypeReg entType = EntityRegistry[ent->type];
|
||||
char animationFrame = (animationStep / entType.animation.divisor) % entType.animation.frameCount;
|
||||
SDL_RenderCopy(renderer, atlasTexture, &entType.animation.atlasRects[animationFrame], &renderRect);
|
||||
char healthStr[12];
|
||||
snprintf(healthStr, 12, "%d/%d", ent->health, entType.maxHealth);
|
||||
renderText(renderer, fonts[2], healthStr, renderRect.x, renderRect.y);
|
||||
}
|
||||
SDL_SetRenderTarget(renderer, oldTarget);
|
||||
}
|
||||
|
||||
void updateEntities() {
|
||||
for (int i = 0; i < entities.activeCount; i++) {
|
||||
Entity *ent = &entities.entities[i];
|
||||
EntityTypeReg entT = EntityRegistry[ent->type];
|
||||
|
||||
// Step 1: If no path, or we're at the end of a path, check for re-path
|
||||
if (ent->health > entT.maxHealth || ent->health <= 0) {
|
||||
remove_entity(&entities, i);
|
||||
continue;
|
||||
}
|
||||
bool atTargetSnapshot = ent->tileRect.x == ent->targetSnapshot.x &&
|
||||
ent->tileRect.y == ent->targetSnapshot.y;
|
||||
|
||||
bool needsPath = ent->path.length == 0 || ent->path.stepIndex >= ent->path.length;
|
||||
bool atTarget = ent->tileRect.x == ent->target.x &&
|
||||
ent->tileRect.y == ent->target.y;
|
||||
|
||||
if (needsPath && (atTargetSnapshot || ent->path.length == 0)) {
|
||||
if (ent->tileRect.x != ent->target.x || ent->tileRect.y != ent->target.y) {
|
||||
if (isWalkable(ent->target) && find_path(ent->tileRect, ent->target)) {
|
||||
ent->path = reconstruct_path(ent->target);
|
||||
ent->path.stepIndex = 0;
|
||||
ent->targetSnapshot = ent->target; // snapshot the current target
|
||||
ent->fromTile = ent->tileRect;
|
||||
ent->toTile = ent->path.steps[0];
|
||||
ent->interpolateTick = 0;
|
||||
ent->entityNextTick = animationStep + entT.entityTickRate;
|
||||
} else {
|
||||
// No path found — freeze
|
||||
ent->path.length = 0;
|
||||
continue;
|
||||
}
|
||||
// Retry pathfinding every 10 ticks if we don't have a path and aren't at the target
|
||||
bool shouldRetryPathfinding = (
|
||||
ent->path.length == 0 &&
|
||||
!atTarget &&
|
||||
(animationStep % 10 == 0)
|
||||
);
|
||||
|
||||
// Also try pathfinding if we reached the snapshot (end of path)
|
||||
if (atTargetSnapshot || shouldRetryPathfinding) {
|
||||
if (isWalkable(ent->target) && find_path(ent->tileRect, ent->target)) {
|
||||
ent->path = reconstruct_path(ent->target);
|
||||
ent->path.stepIndex = 0;
|
||||
ent->targetSnapshot = ent->target; // snapshot the current target
|
||||
ent->fromTile = ent->tileRect;
|
||||
ent->toTile = ent->path.steps[0];
|
||||
ent->interpolateTick = 0;
|
||||
ent->entityNextTick = animationStep + entT.entityTickRate;
|
||||
} else if (atTargetSnapshot) {
|
||||
// No path found and we finished current one — freeze
|
||||
ent->path.length = 0;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: If it's time to move to the next tile
|
||||
// Step 2: Movement
|
||||
if (ent->path.length > 0 && ent->path.stepIndex < ent->path.length &&
|
||||
animationStep >= ent->entityNextTick) {
|
||||
ent->fromTile = ent->tileRect;
|
||||
@@ -70,7 +83,7 @@ void updateEntities() {
|
||||
ent->path.stepIndex++;
|
||||
}
|
||||
|
||||
// Step 3: Interpolate renderRect between fromTile and toTile
|
||||
// Step 3: Interpolation
|
||||
MiniRect from = {
|
||||
.x = ent->fromTile.x * TILE_SIZE,
|
||||
.y = ent->fromTile.y * TILE_SIZE
|
||||
@@ -93,6 +106,7 @@ void updateEntities() {
|
||||
|
||||
|
||||
|
||||
|
||||
void registerEntity(char fname[20], SDL_Renderer *renderer) {
|
||||
char name[21];
|
||||
|
||||
@@ -117,7 +131,7 @@ void registerEntity(char fname[20], SDL_Renderer *renderer) {
|
||||
|
||||
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_NONE);
|
||||
|
||||
printf("Ent %s to %d\n", fname, indexEntity);
|
||||
//printf("Ent %s to %d\n", fname, indexEntity);
|
||||
|
||||
EntityRegistry[indexEntity].animation.textures[frame] = texture;
|
||||
EntityRegistry[indexEntity].animation.atlasRects[frame] = allocate_32x32(texture, renderer);
|
||||
@@ -126,6 +140,7 @@ void registerEntity(char fname[20], SDL_Renderer *renderer) {
|
||||
EntityRegistry[indexEntity].animation.frameCount = frame + 1;
|
||||
EntityRegistry[indexEntity].animation.divisor = 1;
|
||||
EntityRegistry[indexEntity].entityTickRate = 8;
|
||||
EntityRegistry[indexEntity].maxHealth = 100;
|
||||
|
||||
if (indexEntity + 1 > backgroundTileTypeIndex) {
|
||||
backgroundTileTypeIndex = indexEntity + 1;
|
||||
|
Reference in New Issue
Block a user