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

View File

@@ -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) {
if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT)
void draw_character(int x, int y, CharacterFrame frame) {
int frame_index = static_cast<int>(frame);
if (frame_index < 0 || frame_index >= CHARACTER_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 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);
}

View File

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

View File

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

View File

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

View File

@@ -1,11 +1,12 @@
// src/render/player_positioning.h
#pragma once
#include "../background_dark_inverted.h"
#include "player_mask.h"
struct PlayerPosition {
struct CharacterPosition {
int x;
int y;
};
// 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);