Most texure work

This commit is contained in:
2025-06-10 21:59:51 +02:00
parent a17e3abbff
commit 39c31f0042
103 changed files with 666 additions and 246 deletions

View File

@@ -8,6 +8,7 @@
#include "../player/player.h"
#include "../util/pathfinding.h"
#include "../util/font.h"
#include "../util/audio.h"
EntityArray entities;
@@ -29,11 +30,12 @@ void renderEntities(SDL_Renderer *renderer, SDL_Rect playerRect) {
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);
renderText(renderer, fonts[3], healthStr, renderRect.x, renderRect.y);
}
SDL_SetRenderTarget(renderer, oldTarget);
}
void updateEntities() {
void updateEntities(Player *plr) {
for (int i = 0; i < entities.activeCount; i++) {
Entity *ent = &entities.entities[i];
EntityTypeReg entT = EntityRegistry[ent->type];
@@ -48,6 +50,35 @@ void updateEntities() {
bool atTarget = ent->tileRect.x == ent->target.x &&
ent->tileRect.y == ent->target.y;
if (animationStep >= ent->entityNextTick) {
if (sqrt(pow(abs(plr->tileRect.x - ent->tileRect.x), 2) + pow(abs(plr->tileRect.y - ent->tileRect.y), 2)) < ENEMY_RANGE) {
plr->health -= ENEMY_DAMAGE;
}
if (plr->tileRect.x)
for (int y = ent->tileRect.y - ENEMY_RANGE; y < ent->tileRect.y + ENEMY_RANGE; y++) {
if (y < 0 || y >= MAP_HEIGHT) {
continue;
}
for (int x = ent->tileRect.x - ENEMY_RANGE; x < ent->tileRect.x + ENEMY_RANGE; x++) {
if (x < 0 || x >= MAP_WIDTH) {
continue;
}
Tile *targTile = &tileMap[y][x];
if (targTile->type == TYPE_AIR) {
continue;
}
targTile->health -= ENEMY_DAMAGE;
if (targTile->health <= 0) {
if (targTile->audioCh < NUM_SYNTH_VOICES) {
audioData.synthVoices[targTile->audioCh].volume = 0;
}
memset(targTile->items, 0, sizeof(targTile->items));
targTile->type = TYPE_AIR;
}
}
}
}
// Retry pathfinding every 10 ticks if we don't have a path and aren't at the target
bool shouldRetryPathfinding = (
ent->path.length == 0 &&
@@ -93,9 +124,9 @@ void updateEntities() {
.y = ent->toTile.y * TILE_SIZE
};
float t = (float)ent->interpolateTick / entT.entityTickRate;
ent->renderRect.x = (int)(from.x + (to.x - from.x) * t);
ent->renderRect.y = (int)(from.y + (to.y - from.y) * t);
float t = (float) ent->interpolateTick / entT.entityTickRate;
ent->renderRect.x = (int) (from.x + (to.x - from.x) * t);
ent->renderRect.y = (int) (from.y + (to.y - from.y) * t);
if (ent->interpolateTick < entT.entityTickRate) {
ent->interpolateTick++;
@@ -104,9 +135,6 @@ void updateEntities() {
}
void registerEntity(char fname[20], SDL_Renderer *renderer) {
char name[21];
@@ -133,7 +161,6 @@ void registerEntity(char fname[20], SDL_Renderer *renderer) {
//printf("Ent %s to %d\n", fname, indexEntity);
EntityRegistry[indexEntity].animation.textures[frame] = texture;
EntityRegistry[indexEntity].animation.atlasRects[frame] = allocate_32x32(texture, renderer);
EntityRegistry[indexEntity].type = indexEntity;

View File

@@ -7,9 +7,13 @@
#include "../tiles/tile.h"
#include "../util/pathfinding.h"
#include "../player/player.h"
#define ENTITY_MAX_COUNT 1024
#define ENEMY_DAMAGE 2
#define ENEMY_RANGE 3
typedef enum EntityType {
GHOST,
} EntityType;
@@ -49,7 +53,7 @@ void remove_entity(EntityArray *arr, int index);
int add_entity(EntityArray *arr, Entity t);
void renderEntities(SDL_Renderer *renderer, SDL_Rect playerRect);
void updateEntities();
void updateEntities(Player * plr);
void registerEntity(char fname[20], SDL_Renderer *renderer);
void loadEntities(SDL_Renderer *renderer);