usage of enums for frames
This commit is contained in:
@@ -86,16 +86,16 @@ static bool update_animation(Timer &anim_timer, int &shift, int speed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
static int get_player_frame(PlayerState state) {
|
||||
static PlayerFrame get_player_frame(PlayerState state) {
|
||||
switch (state) {
|
||||
case PlayerState::Walk1:
|
||||
return 0;
|
||||
return PlayerFrame::Walk1;
|
||||
case PlayerState::Walk2:
|
||||
return 1;
|
||||
return PlayerFrame::Walk2;
|
||||
case PlayerState::Crawl1:
|
||||
return 2;
|
||||
return PlayerFrame::Crawl1;
|
||||
default:
|
||||
return 0;
|
||||
return PlayerFrame::Walk1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,8 +157,8 @@ void render_loop(int speed) {
|
||||
|
||||
if (need_redraw) {
|
||||
draw_mask(bg_file, shift, message_active ? message : nullptr);
|
||||
int player_frame = get_player_frame(player_state.current_state);
|
||||
PlayerPosition draw_pos = get_aligned_frame_position(pos, player_frame);
|
||||
PlayerFrame player_frame = get_player_frame(player_state.current_state);
|
||||
PlayerPosition draw_pos = get_aligned_frame_position(pos, static_cast<int>(player_frame));
|
||||
draw_player(draw_pos.x, draw_pos.y, player_frame);
|
||||
|
||||
// alternate between frame 0 and 1 when not crawling
|
||||
|
||||
@@ -1,25 +1,33 @@
|
||||
// src/render/player.cpp
|
||||
|
||||
#include "player.h"
|
||||
#include "player_mask.h"
|
||||
#include <cstdio>
|
||||
|
||||
void draw_player(int x, int y, int frame_index) {
|
||||
void draw_player(int x, int y, PlayerFrame frame) {
|
||||
int frame_index = static_cast<int>(frame);
|
||||
if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT)
|
||||
frame_index = 0; // fallback safety
|
||||
|
||||
const char **sprite = PLAYER_FRAMES[frame_index];
|
||||
int sprite_height = 0;
|
||||
if (frame_index == 0)
|
||||
sprite_height = PLAYER_MASK_LINES_FRAME_1;
|
||||
else if (frame_index == 1)
|
||||
sprite_height = PLAYER_MASK_LINES_FRAME_2;
|
||||
else
|
||||
sprite_height = PLAYER_MASK_LINES_FRAME_3;
|
||||
const char **character = PLAYER_FRAMES[frame_index];
|
||||
|
||||
// Draw the sprite starting from (x, y)
|
||||
for (int i = 0; i < sprite_height; i++) {
|
||||
printf("\033[%d;%dH%s", y + i + 1, x + 1, sprite[i]);
|
||||
int character_height = 0;
|
||||
switch (frame) {
|
||||
case PlayerFrame::Walk1:
|
||||
character_height = PLAYER_MASK_LINES_FRAME_1;
|
||||
break;
|
||||
case PlayerFrame::Walk2:
|
||||
character_height = PLAYER_MASK_LINES_FRAME_2;
|
||||
break;
|
||||
case PlayerFrame::Crawl1:
|
||||
character_height = PLAYER_MASK_LINES_FRAME_3;
|
||||
break;
|
||||
default:
|
||||
character_height = PLAYER_MASK_LINES_FRAME_1;
|
||||
break;
|
||||
}
|
||||
|
||||
for (int i = 0; i < character_height; i++) {
|
||||
printf("\033[%d;%dH%s", y + i + 1, x + 1, character[i]);
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// src/render/player.h
|
||||
#pragma once
|
||||
#include "player_mask.h"
|
||||
|
||||
// Draw the player object starting at given (x, y)
|
||||
void draw_player(int x, int y, int frame_index);
|
||||
void draw_player(int x, int y, PlayerFrame frame);
|
||||
|
||||
@@ -32,19 +32,23 @@ static const char *PLAYER_MASK_FRAME_3[] = {
|
||||
"aa@W@."
|
||||
};
|
||||
|
||||
// Combine frames for easy access
|
||||
static const char **PLAYER_FRAMES[] = {
|
||||
PLAYER_MASK_FRAME_1, // 0
|
||||
PLAYER_MASK_FRAME_2, // 1
|
||||
PLAYER_MASK_FRAME_3 // 2 (triggered)
|
||||
enum class PlayerFrame {
|
||||
Walk1 = 0,
|
||||
Walk2,
|
||||
Crawl1,
|
||||
COUNT // number of frames, must stay last
|
||||
};
|
||||
|
||||
static const int PLAYER_FRAME_COUNT = sizeof(PLAYER_FRAMES) / sizeof(PLAYER_FRAMES[0]);
|
||||
// Combine frames for easy access
|
||||
static const char **PLAYER_FRAMES[] = {
|
||||
PLAYER_MASK_FRAME_1, // Walk1
|
||||
PLAYER_MASK_FRAME_2, // Walk1
|
||||
PLAYER_MASK_FRAME_3 // Crawl1
|
||||
};
|
||||
|
||||
// Line counts per frame
|
||||
static const int PLAYER_MASK_LINES_FRAME_1 =
|
||||
sizeof(PLAYER_MASK_FRAME_1) / sizeof(PLAYER_MASK_FRAME_1[0]);
|
||||
static const int PLAYER_MASK_LINES_FRAME_2 =
|
||||
sizeof(PLAYER_MASK_FRAME_2) / sizeof(PLAYER_MASK_FRAME_2[0]);
|
||||
static const int PLAYER_MASK_LINES_FRAME_3 =
|
||||
sizeof(PLAYER_MASK_FRAME_3) / sizeof(PLAYER_MASK_FRAME_3[0]);
|
||||
static const int PLAYER_FRAME_COUNT = static_cast<int>(PlayerFrame::COUNT);
|
||||
|
||||
// Height per frame
|
||||
static const int PLAYER_MASK_LINES_FRAME_1 = sizeof(PLAYER_MASK_FRAME_1) / sizeof(PLAYER_MASK_FRAME_1[0]);
|
||||
static const int PLAYER_MASK_LINES_FRAME_2 = sizeof(PLAYER_MASK_FRAME_2) / sizeof(PLAYER_MASK_FRAME_2[0]);
|
||||
static const int PLAYER_MASK_LINES_FRAME_3 = sizeof(PLAYER_MASK_FRAME_3) / sizeof(PLAYER_MASK_FRAME_3[0]);
|
||||
|
||||
@@ -8,17 +8,17 @@ PlayerPosition get_aligned_frame_position(PlayerPosition base, int frame_index)
|
||||
if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT)
|
||||
frame_index = 0;
|
||||
|
||||
int sprite_height = 0;
|
||||
int character_height = 0;
|
||||
if (frame_index == 0)
|
||||
sprite_height = PLAYER_MASK_LINES_FRAME_1;
|
||||
character_height = PLAYER_MASK_LINES_FRAME_1;
|
||||
else if (frame_index == 1)
|
||||
sprite_height = PLAYER_MASK_LINES_FRAME_2;
|
||||
character_height = PLAYER_MASK_LINES_FRAME_2;
|
||||
else
|
||||
sprite_height = PLAYER_MASK_LINES_FRAME_3;
|
||||
character_height = PLAYER_MASK_LINES_FRAME_3;
|
||||
|
||||
PlayerPosition draw_pos;
|
||||
draw_pos.x = base.x;
|
||||
draw_pos.y = (VIEW_HEIGHT - base.y) - sprite_height;
|
||||
draw_pos.y = (VIEW_HEIGHT - base.y) - character_height;
|
||||
|
||||
return draw_pos;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user