diff --git a/semestralka1/src/render/loop.cpp b/semestralka1/src/render/loop.cpp index e174a0c..4bc8477 100644 --- a/semestralka1/src/render/loop.cpp +++ b/semestralka1/src/render/loop.cpp @@ -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(player_frame)); draw_player(draw_pos.x, draw_pos.y, player_frame); // alternate between frame 0 and 1 when not crawling diff --git a/semestralka1/src/render/player.cpp b/semestralka1/src/render/player.cpp index 8f779bc..2eaf2bb 100644 --- a/semestralka1/src/render/player.cpp +++ b/semestralka1/src/render/player.cpp @@ -1,25 +1,33 @@ // src/render/player.cpp - #include "player.h" #include "player_mask.h" #include -void draw_player(int x, int y, int frame_index) { +void draw_player(int x, int y, PlayerFrame frame) { + int frame_index = static_cast(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); } diff --git a/semestralka1/src/render/player.h b/semestralka1/src/render/player.h index 4201ca2..39172a3 100644 --- a/semestralka1/src/render/player.h +++ b/semestralka1/src/render/player.h @@ -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); diff --git a/semestralka1/src/render/player_mask.h b/semestralka1/src/render/player_mask.h index 9b23672..1b26080 100644 --- a/semestralka1/src/render/player_mask.h +++ b/semestralka1/src/render/player_mask.h @@ -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(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]); diff --git a/semestralka1/src/render/player_positioning.cpp b/semestralka1/src/render/player_positioning.cpp index 2376ea1..4d52773 100644 --- a/semestralka1/src/render/player_positioning.cpp +++ b/semestralka1/src/render/player_positioning.cpp @@ -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; }