Add walk animation

This commit is contained in:
2025-10-28 21:46:00 -05:00
parent 300cd7b811
commit cf1dcd3637
4 changed files with 65 additions and 4 deletions

View File

@@ -17,6 +17,19 @@ void entityInit(entity_t *entity, const entitytype_t type) {
void entityTick(entity_t *entity) { void entityTick(entity_t *entity) {
if(entity->type == ENTITY_TYPE_NULL) return; if(entity->type == ENTITY_TYPE_NULL) return;
// Handle animation
switch(entity->animation) {
case ENTITY_ANIM_WALK:
entity->animWalk--;
if(entity->animWalk == 0) {
entity->animation = ENTITY_ANIM_NONE;
}
return;
default:
break;
}
// Let entities move (if they do) // Let entities move (if they do)
if(entity->type == ENTITY_TYPE_PLAYER) { if(entity->type == ENTITY_TYPE_PLAYER) {
playerTickInput(entity); playerTickInput(entity);
@@ -76,6 +89,9 @@ void entityWalk(entity_t *entity, const direction_t direction) {
// Move. // Move.
entity->position.x = newX; entity->position.x = newX;
entity->position.y = newY; entity->position.y = newY;
entity->animation = ENTITY_ANIM_WALK;
entity->animWalk = ENTITY_ANIM_WALK_DURATION;// TODO: Running vs walking
} }
void entityInteract(entity_t *entity) { void entityInteract(entity_t *entity) {

View File

@@ -11,6 +11,11 @@
#include "player.h" #include "player.h"
#include "sign.h" #include "sign.h"
#define ENTITY_ANIM_NONE 0
#define ENTITY_ANIM_WALK 1
#define ENTITY_ANIM_WALK_DURATION 8
typedef struct entity_s { typedef struct entity_s {
struct { struct {
// Relative to top-left position of the top-left chunk of the map. // Relative to top-left position of the top-left chunk of the map.
@@ -20,6 +25,11 @@ typedef struct entity_s {
direction_t direction; direction_t direction;
entitytype_t type; entitytype_t type;
uint8_t animation;
union {
uint8_t animWalk;
};
union { union {
sign_t sign; sign_t sign;
}; };

View File

@@ -3,6 +3,16 @@
# This software is released under the MIT License. # This software is released under the MIT License.
# https://opensource.org/licenses/MIT # https://opensource.org/licenses/MIT
# add_subdirectory(raylib) if(NOT DEFINED PLATFORM)
add_subdirectory(term) set(PLATFORM "raylib" CACHE STRING "Select the platform to build for (options: raylib, term, tty)")
add_subdirectory(tty) endif()
if(PLATFORM STREQUAL "tty")
add_subdirectory(term)
add_subdirectory(tty)
elseif(PLATFORM STREQUAL "raylib")
add_subdirectory(raylib)
add_subdirectory(term)
else()
message(FATAL_ERROR "Unknown PLATFORM: ${PLATFORM}.")
endif()

View File

@@ -24,6 +24,12 @@ void termDrawOverworld(
// Draw entities // Draw entities
termDrawEntity(&GAME.player, chars, colors, w, h); termDrawEntity(&GAME.player, chars, colors, w, h);
entity_t *start = GAME.overworld.map.entities;
while(start != GAME.overworld.map.entities + MAP_ENTITY_COUNT) {
termDrawEntity(start, chars, colors, w, h);
start++;
}
} }
void termDrawEntity( void termDrawEntity(
@@ -40,7 +46,26 @@ void termDrawEntity(
int32_t index = ent->position.y * w + ent->position.x; int32_t index = ent->position.y * w + ent->position.x;
if(ent->type == ENTITY_TYPE_PLAYER) { if(ent->type == ENTITY_TYPE_PLAYER) {
switch(ent->direction) {
case DIRECTION_UP:
chars[index] = '^';
break;
case DIRECTION_DOWN:
chars[index] = 'v';
break;
case DIRECTION_LEFT:
chars[index] = '<';
break;
case DIRECTION_RIGHT:
chars[index] = '>';
break;
default:
chars[index] = '@'; chars[index] = '@';
break;
}
colors[index] = TERM_COLOR_WHITE;
} else if(ent->type == ENTITY_TYPE_SIGN) {
chars[index] = '!';
colors[index] = TERM_COLOR_YELLOW; colors[index] = TERM_COLOR_YELLOW;
} }
} }