3 Commits

Author SHA1 Message Date
Priec
4ed4452fae some renames, nothing much 2025-11-14 19:39:19 +01:00
Priec
b9ca8df59b small syntax change 2025-11-14 19:25:25 +01:00
Priec
8fa8bf392b usage of enums for frames 2025-11-14 19:20:14 +01:00
6 changed files with 66 additions and 50 deletions

View File

@@ -19,6 +19,7 @@ constexpr auto MESSAGE_DISPLAY_DURATION = 1s;
constexpr int PLAYER_X = 9; constexpr int PLAYER_X = 9;
constexpr int PLAYER_Y = 6; constexpr int PLAYER_Y = 6;
// PlayerState is what user pressed. We are mapping CharacterState to it
enum class PlayerState { Walk1, Walk2, Crawl1 }; enum class PlayerState { Walk1, Walk2, Crawl1 };
struct WalkingState { struct WalkingState {
@@ -86,16 +87,16 @@ static bool update_animation(Timer &anim_timer, int &shift, int speed) {
return false; return false;
} }
static int get_player_frame(PlayerState state) { static CharacterFrame get_character_frame(PlayerState state) {
switch (state) { switch (state) {
case PlayerState::Walk1: case PlayerState::Walk1:
return 0; return CharacterFrame::Walk1;
case PlayerState::Walk2: case PlayerState::Walk2:
return 1; return CharacterFrame::Walk2;
case PlayerState::Crawl1: case PlayerState::Crawl1:
return 2; return CharacterFrame::Crawl1;
default: default:
return 0; return CharacterFrame::Walk1;
} }
} }
@@ -129,7 +130,7 @@ void render_loop(int speed) {
anim_timer.start(); anim_timer.start();
int shift = 0; int shift = 0;
PlayerPosition pos = {PLAYER_X, PLAYER_Y}; CharacterPosition pos = {PLAYER_X, PLAYER_Y};
const char *bg_file = "background_dark_inverted.txt"; const char *bg_file = "background_dark_inverted.txt";
bool need_redraw = false; bool need_redraw = false;
@@ -157,9 +158,9 @@ 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); CharacterFrame player_frame = get_character_frame(player_state.current_state);
PlayerPosition draw_pos = get_aligned_frame_position(pos, player_frame); CharacterPosition draw_pos = get_aligned_frame_position(pos, player_frame);
draw_player(draw_pos.x, draw_pos.y, player_frame); draw_character(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
if (player_state.current_state != PlayerState::Crawl1) { if (player_state.current_state != PlayerState::Crawl1) {

View File

@@ -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_character(int x, int y, CharacterFrame frame) {
if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT) int frame_index = static_cast<int>(frame);
if (frame_index < 0 || frame_index >= CHARACTER_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 CharacterFrame::Walk1:
character_height = CHARACTER_HEIGHT_1;
break;
case CharacterFrame::Walk2:
character_height = CHARACTER_HEIGHT_2;
break;
case CharacterFrame::Crawl1:
character_height = CHARACTER_HEIGHT_3;
break;
default:
character_height = CHARACTER_HEIGHT_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);
} }

View File

@@ -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_character(int x, int y, CharacterFrame frame);

View File

@@ -3,7 +3,7 @@
#pragma once #pragma once
// Player frame 1 // Player frame 1
static const char *PLAYER_MASK_FRAME_1[] = { static const char *CHARACTER_MASK_FRAME_1[] = {
".....a@$..", ".....a@$..",
".....7PF..", ".....7PF..",
"..yaM@@__y", "..yaM@@__y",
@@ -14,7 +14,7 @@ static const char *PLAYER_MASK_FRAME_1[] = {
}; };
// Player frame 2 // Player frame 2
static const char *PLAYER_MASK_FRAME_2[] = { static const char *CHARACTER_MASK_FRAME_2[] = {
".g@$", ".g@$",
".7PF", ".7PF",
"y@$.", "y@$.",
@@ -25,26 +25,30 @@ static const char *PLAYER_MASK_FRAME_2[] = {
}; };
// Player frame 3 (triggered) // Player frame 3 (triggered)
static const char *PLAYER_MASK_FRAME_3[] = { static const char *CHARACTER_MASK_FRAME_3[] = {
".._$@.", ".._$@.",
".$By`.", ".$By`.",
"4@$@@a", "4@$@@a",
"aa@W@." "aa@W@."
}; };
// Combine frames for easy access enum class CharacterFrame {
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[] = {
CHARACTER_MASK_FRAME_1, // Walk1
CHARACTER_MASK_FRAME_2, // Walk1
CHARACTER_MASK_FRAME_3 // Crawl1
};
// Line counts per frame static const int CHARACTER_FRAME_COUNT = static_cast<int>(CharacterFrame::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 CHARACTER_HEIGHT_1 = sizeof(CHARACTER_MASK_FRAME_1) / sizeof(CHARACTER_MASK_FRAME_1[0]);
sizeof(PLAYER_MASK_FRAME_2) / sizeof(PLAYER_MASK_FRAME_2[0]); static const int CHARACTER_HEIGHT_2 = sizeof(CHARACTER_MASK_FRAME_2) / sizeof(CHARACTER_MASK_FRAME_2[0]);
static const int PLAYER_MASK_LINES_FRAME_3 = static const int CHARACTER_HEIGHT_3 = sizeof(CHARACTER_MASK_FRAME_3) / sizeof(CHARACTER_MASK_FRAME_3[0]);
sizeof(PLAYER_MASK_FRAME_3) / sizeof(PLAYER_MASK_FRAME_3[0]);

View File

@@ -4,21 +4,22 @@
#include <cstdio> #include <cstdio>
// Convert a pivot coordinate (bottom-left) to the top-left draw position // Convert a pivot coordinate (bottom-left) to the top-left draw position
PlayerPosition get_aligned_frame_position(PlayerPosition base, int frame_index) { CharacterPosition get_aligned_frame_position(CharacterPosition base, CharacterFrame frame) {
if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT) int frame_index = static_cast<int>(frame);
if (frame_index < 0 || frame_index >= CHARACTER_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 = CHARACTER_HEIGHT_1;
else if (frame_index == 1) else if (frame_index == 1)
sprite_height = PLAYER_MASK_LINES_FRAME_2; character_height = CHARACTER_HEIGHT_2;
else else
sprite_height = PLAYER_MASK_LINES_FRAME_3; character_height = CHARACTER_HEIGHT_3;
PlayerPosition draw_pos; CharacterPosition 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;
} }

View File

@@ -1,11 +1,12 @@
// src/render/player_positioning.h // src/render/player_positioning.h
#pragma once #pragma once
#include "../background_dark_inverted.h"
#include "player_mask.h" #include "player_mask.h"
struct PlayerPosition { struct CharacterPosition {
int x; int x;
int y; int y;
}; };
// Calculates drawing position relative to bottom-left pivot // Calculates drawing position relative to bottom-left pivot
PlayerPosition get_aligned_frame_position(PlayerPosition base, int frame_index); CharacterPosition get_aligned_frame_position(CharacterPosition base, CharacterFrame frame);