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