Added NPC animation.

This commit is contained in:
mibi88 2024-08-01 17:37:20 +02:00
parent a8294d3542
commit 6c5641e5e9
9 changed files with 29 additions and 18 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 221 B

After

Width:  |  Height:  |  Size: 382 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 206 B

After

Width:  |  Height:  |  Size: 327 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 222 B

After

Width:  |  Height:  |  Size: 343 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 220 B

After

Width:  |  Height:  |  Size: 344 B

View file

@ -4,14 +4,14 @@
#include "config.h" #include "config.h"
typedef struct { typedef struct {
unsigned char frame; unsigned char frame : 8;
bopti_image_t *image; bopti_image_t *image;
unsigned char len; unsigned char len : 8;
unsigned short frame_ms; unsigned short frame_ms : 16;
int current_ms; int current_ms : 32;
unsigned short width; unsigned short width : 16;
unsigned short height; unsigned short height : 16;
unsigned short wrap_dest; unsigned char wrap_dest : 8;
} Animation; } Animation;
/* TODO: Doc! */ /* TODO: Doc! */

View file

@ -213,5 +213,6 @@ void game_get_inputs(Game *game) {
} }
void game_update_animations(Game *game, unsigned char ms) { void game_update_animations(Game *game, unsigned char ms) {
animation_update(&game->npc_animation, ms);
animation_update(&game->player.animation, ms); animation_update(&game->player.animation, ms);
} }

View file

@ -175,6 +175,8 @@ typedef struct {
bool debug_player; bool debug_player;
bool debug_extra; bool debug_extra;
Animation npc_animation;
int mana; /* Only for testing events TODO: Remove this! */ int mana; /* Only for testing events TODO: Remove this! */
} Game; } Game;

View file

@ -29,6 +29,7 @@
extern bopti_image_t player_face_img; extern bopti_image_t player_face_img;
extern bopti_image_t player_male_img; extern bopti_image_t player_male_img;
extern bopti_image_t tiny_npc_male;
extern Map *worldRPG[]; extern Map *worldRPG[];
@ -47,6 +48,7 @@ Game game = {
false, false,
false, false,
false, false,
{},
100}; 100};
/* screen capture management code. TODO: Clean this up! */ /* screen capture management code. TODO: Clean this up! */
@ -109,6 +111,9 @@ int main(void) {
timer_start(timer); timer_start(timer);
game.map_level = worldRPG[0]; game.map_level = worldRPG[0];
/* NPC animation */
animation_new(&game.npc_animation, &tiny_npc_male, 3, 250);
/* Player animation */
animation_new(&game.player.animation, &player_male_img, 3, 250); animation_new(&game.player.animation, &player_male_img, 3, 250);
/* Wrapping back to 1 to skip the idle frame. */ /* Wrapping back to 1 to skip the idle frame. */
game.player.animation.wrap_dest = 1; game.player.animation.wrap_dest = 1;

View file

@ -260,8 +260,9 @@ void update_npc(NPC *npc) {
bopti_image_t *npc_sprites[FACES] = {&tiny_npc_male, &tiny_npc_female, bopti_image_t *npc_sprites[FACES] = {&tiny_npc_male, &tiny_npc_female,
&tiny_npc_milkman, &tiny_npc_police}; &tiny_npc_milkman, &tiny_npc_police};
void npc_draw_single(NPC *npc, Player *pl) { void npc_draw_single(NPC *npc, Game *game) {
Player *pl = &game->player;
/* Render the path if in debug and it has one*/ /* Render the path if in debug and it has one*/
#if DEBUGMODE #if DEBUGMODE
if(npc->hasPath) { if(npc->hasPath) {
@ -270,16 +271,16 @@ void npc_draw_single(NPC *npc, Player *pl) {
int16_t deltaX1 = int16_t deltaX1 =
((int16_t)(npc->x + npc->xpath[v % NbPoints]) * PXSIZE) - ((int16_t)(npc->x + npc->xpath[v % NbPoints]) * PXSIZE) -
(int16_t)pl->wx; (int16_t)pl->x;
int16_t deltaY1 = int16_t deltaY1 =
((int16_t)(npc->y + npc->ypath[v % NbPoints]) * PXSIZE) - ((int16_t)(npc->y + npc->ypath[v % NbPoints]) * PXSIZE) -
(int16_t)pl->wy; (int16_t)pl->y;
int16_t deltaX2 = int16_t deltaX2 =
((int16_t)(npc->x + npc->xpath[(v + 1) % NbPoints]) * PXSIZE) - ((int16_t)(npc->x + npc->xpath[(v + 1) % NbPoints]) * PXSIZE) -
(int16_t)pl->wx; (int16_t)pl->x;
int16_t deltaY2 = int16_t deltaY2 =
((int16_t)(npc->y + npc->ypath[(v + 1) % NbPoints]) * PXSIZE) - ((int16_t)(npc->y + npc->ypath[(v + 1) % NbPoints]) * PXSIZE) -
(int16_t)pl->wy; (int16_t)pl->y;
dline(pl->px + deltaX1, pl->py + deltaY1, pl->px + deltaX2, dline(pl->px + deltaX1, pl->py + deltaY1, pl->px + deltaX2,
pl->py + deltaY2, PATH_COLOR); pl->py + deltaY2, PATH_COLOR);
@ -289,19 +290,21 @@ void npc_draw_single(NPC *npc, Player *pl) {
int16_t delX = ((npc->curx * PXSIZE) >> PRECISION) - (int16_t)pl->x; int16_t delX = ((npc->curx * PXSIZE) >> PRECISION) - (int16_t)pl->x;
int16_t delY = ((npc->cury * PXSIZE) >> PRECISION) - (int16_t)pl->y; int16_t delY = ((npc->cury * PXSIZE) >> PRECISION) - (int16_t)pl->y;
bopti_image_t *face = npc_sprites[npc->face]; game->npc_animation.image = npc_sprites[npc->face];
dimage(pl->px - P_WIDTH / 2 + delX, pl->py - P_HEIGHT / 2 + delY, face); unsigned char frame = game->npc_animation.frame;
if(npc->paused || !npc->hasPath) game->npc_animation.frame = 0;
animation_draw(&game->npc_animation, pl->px - P_WIDTH / 2 + delX,
pl->py - P_HEIGHT / 2 + delY);
game->npc_animation.frame = frame;
} }
void npc_draw(Game *game) { void npc_draw(Game *game) {
Player *pl = &game->player;
uint32_t u; uint32_t u;
for(u = 0; u < game->map_level->nbNPC; u++) { for(u = 0; u < game->map_level->nbNPC; u++) {
npc_draw_single(&game->map_level->npcs[u], pl); npc_draw_single(&game->map_level->npcs[u], game);
} }
for(u = 0; u < npc_count; u++) { for(u = 0; u < npc_count; u++) {
npc_draw_single(&npc_stack[u], pl); npc_draw_single(&npc_stack[u], game);
} }
} }