multiple frames now added and working
This commit is contained in:
@@ -33,3 +33,6 @@ static const char **CHARACTER_CRAWL_FRAMES[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static const int CHARACTER_CRAWL_FRAME_COUNT = sizeof(CHARACTER_CRAWL_FRAMES) / sizeof(CHARACTER_CRAWL_FRAMES[0]);
|
static const int CHARACTER_CRAWL_FRAME_COUNT = sizeof(CHARACTER_CRAWL_FRAMES) / sizeof(CHARACTER_CRAWL_FRAMES[0]);
|
||||||
|
|
||||||
|
// Height (rows per crawl frame)
|
||||||
|
static const int CHARACTER_CRAWL_FRAME_HEIGHT = sizeof(CRAWL_FRAME_1) / sizeof(CRAWL_FRAME_1[0]);
|
||||||
|
|||||||
@@ -1,54 +0,0 @@
|
|||||||
// src/assets/character_frames.h
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
// Player frame 1
|
|
||||||
static const char *CHARACTER_MASK_FRAME_1[] = {
|
|
||||||
".....a@$..",
|
|
||||||
".....7PF..",
|
|
||||||
"..yaM@@__y",
|
|
||||||
"..@.y@FFF~",
|
|
||||||
"..._$M@_..",
|
|
||||||
"yaa@~.`@y.",
|
|
||||||
"........@r"
|
|
||||||
};
|
|
||||||
|
|
||||||
// Player frame 2
|
|
||||||
static const char *CHARACTER_MASK_FRAME_2[] = {
|
|
||||||
".g@$",
|
|
||||||
".7PF",
|
|
||||||
"y@$.",
|
|
||||||
"0@F.",
|
|
||||||
"4@$.",
|
|
||||||
"y$@.",
|
|
||||||
"u@.."
|
|
||||||
};
|
|
||||||
|
|
||||||
// Player frame 3 (triggered)
|
|
||||||
static const char *CHARACTER_MASK_FRAME_3[] = {
|
|
||||||
".._$@.",
|
|
||||||
".$By`.",
|
|
||||||
"4@$@@a",
|
|
||||||
"aa@W@."
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class CharacterFrame {
|
|
||||||
Walk1 = 0,
|
|
||||||
Walk2,
|
|
||||||
Crawl1,
|
|
||||||
COUNT // number of frames, must stay last
|
|
||||||
};
|
|
||||||
|
|
||||||
// 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
|
|
||||||
};
|
|
||||||
|
|
||||||
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]);
|
|
||||||
@@ -92,5 +92,7 @@ static const char **CHARACTER_RUN_FRAMES[] = {
|
|||||||
RUN_FRAME_7,
|
RUN_FRAME_7,
|
||||||
};
|
};
|
||||||
|
|
||||||
static const int CHARACTER_RUN_FRAME_COUNT =
|
static const int CHARACTER_RUN_FRAME_COUNT = sizeof(CHARACTER_RUN_FRAMES) / sizeof(CHARACTER_RUN_FRAMES[0]);
|
||||||
sizeof(CHARACTER_RUN_FRAMES) / sizeof(CHARACTER_RUN_FRAMES[0]);
|
|
||||||
|
// Height (rows per run frame)
|
||||||
|
static const int CHARACTER_RUN_FRAME_HEIGHT = sizeof(RUN_FRAME_1) / sizeof(RUN_FRAME_1[0]);
|
||||||
|
|||||||
@@ -92,5 +92,7 @@ static const char **CHARACTER_WALK_FRAMES[] = {
|
|||||||
WALK_FRAME_7,
|
WALK_FRAME_7,
|
||||||
};
|
};
|
||||||
|
|
||||||
static const int CHARACTER_WALK_FRAME_COUNT =
|
static const int CHARACTER_WALK_FRAME_COUNT = sizeof(CHARACTER_WALK_FRAMES) / sizeof(CHARACTER_WALK_FRAMES[0]);
|
||||||
sizeof(CHARACTER_WALK_FRAMES) / sizeof(CHARACTER_WALK_FRAMES[0]);
|
|
||||||
|
// Height (rows per walk frame)
|
||||||
|
static const int CHARACTER_WALK_FRAME_HEIGHT = sizeof(WALK_FRAME_1) / sizeof(WALK_FRAME_1[0]);
|
||||||
|
|||||||
@@ -1,38 +1,43 @@
|
|||||||
// src/game/state.cpp
|
// src/game/state.cpp
|
||||||
#include "state.h"
|
#include "state.h"
|
||||||
|
#include "../render/player.h"
|
||||||
|
|
||||||
void WalkingState::update() {
|
void WalkingState::update() {
|
||||||
// stop crawling after duration
|
// stop crawling after duration
|
||||||
if (current_state == PlayerState::Crawl1 &&
|
if (current_state == PlayerState::Crawl &&
|
||||||
state_timer.elapsed_time() >= CRAWL_DURATION) {
|
state_timer.elapsed_time() >= CRAWL_DURATION) {
|
||||||
current_state = PlayerState::Walk1;
|
current_state = PlayerState::Walk;
|
||||||
state_timer.stop();
|
state_timer.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WalkingState::start_crawl() {
|
void WalkingState::start_crawl() {
|
||||||
current_state = PlayerState::Crawl1;
|
current_state = PlayerState::Crawl;
|
||||||
state_timer.reset();
|
state_timer.reset();
|
||||||
state_timer.start();
|
state_timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO THIS NEEDS REDESIGN ACCORDING TO SPEED
|
||||||
void WalkingState::toggle_walk_frame() {
|
void WalkingState::toggle_walk_frame() {
|
||||||
if (current_state == PlayerState::Walk1) {
|
if (++walk_index >= 7) walk_index = 0;
|
||||||
current_state = PlayerState::Walk2;
|
|
||||||
} else if (current_state == PlayerState::Walk2) {
|
|
||||||
current_state = PlayerState::Walk1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
CharacterFrame WalkingState::get_character_frame() const {
|
FrameSelection WalkingState::get_frame_selection() const {
|
||||||
|
FrameSelection f{MovementType::Walk, 0};
|
||||||
|
|
||||||
switch (current_state) {
|
switch (current_state) {
|
||||||
case PlayerState::Walk1:
|
case PlayerState::Walk:
|
||||||
return CharacterFrame::Walk1;
|
f.movement = MovementType::Walk;
|
||||||
case PlayerState::Walk2:
|
f.frame_index = walk_index;
|
||||||
return CharacterFrame::Walk2;
|
break;
|
||||||
case PlayerState::Crawl1:
|
case PlayerState::Run:
|
||||||
return CharacterFrame::Crawl1;
|
f.movement = MovementType::Run;
|
||||||
default:
|
f.frame_index = run_index;
|
||||||
return CharacterFrame::Walk1;
|
break;
|
||||||
|
case PlayerState::Crawl:
|
||||||
|
f.movement = MovementType::Crawl;
|
||||||
|
f.frame_index = crawl_index;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
return f;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +1,30 @@
|
|||||||
// src/game/state.h
|
// src/game/state.h
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "mbed.h"
|
#include "mbed.h"
|
||||||
#include "../assets/character_frames.h"
|
#include "../render/player.h"
|
||||||
|
|
||||||
constexpr auto CRAWL_DURATION = 300ms;
|
constexpr auto CRAWL_DURATION = 300ms;
|
||||||
|
|
||||||
enum class PlayerState { Walk1, Walk2, Crawl1 };
|
enum class PlayerState { Walk, Run, Crawl };
|
||||||
|
|
||||||
|
struct FrameSelection {
|
||||||
|
MovementType movement;
|
||||||
|
int frame_index;
|
||||||
|
};
|
||||||
|
|
||||||
class WalkingState {
|
class WalkingState {
|
||||||
private:
|
private:
|
||||||
PlayerState current_state = PlayerState::Walk1;
|
PlayerState current_state = PlayerState::Walk;
|
||||||
Timer state_timer;
|
Timer state_timer;
|
||||||
|
int walk_index = 0; // cycles 0‑6
|
||||||
|
int run_index = 0; // cycles 0‑6
|
||||||
|
int crawl_index = 0; // cycles 0‑1
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// MOVED FROM loop.cpp
|
|
||||||
// Update state (handles crawl timeout)
|
|
||||||
void update();
|
void update();
|
||||||
|
|
||||||
// State transitions
|
|
||||||
void start_crawl();
|
void start_crawl();
|
||||||
void toggle_walk_frame();
|
void toggle_walk_frame();
|
||||||
|
|
||||||
// Getters
|
FrameSelection get_frame_selection() const;
|
||||||
PlayerState get_state() const { return current_state; }
|
PlayerState get_state() const { return current_state; }
|
||||||
CharacterFrame get_character_frame() const;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -22,5 +22,5 @@ int main(void) {
|
|||||||
printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);
|
printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);
|
||||||
|
|
||||||
// Just call into render feature
|
// Just call into render feature
|
||||||
render_loop(4);
|
render_loop(1);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
#include "../game/state.h"
|
#include "../game/state.h"
|
||||||
#include "../game/animation.h"
|
#include "../game/animation.h"
|
||||||
#include "../hardware/uart.h"
|
#include "../hardware/uart.h"
|
||||||
#include "../assets/character_frames.h"
|
#include "../render/player.h"
|
||||||
|
|
||||||
extern BufferedSerial serial_port;
|
extern BufferedSerial serial_port;
|
||||||
extern DigitalOut led;
|
extern DigitalOut led;
|
||||||
@@ -52,13 +52,12 @@ void render_loop(int speed) {
|
|||||||
|
|
||||||
if (need_redraw) {
|
if (need_redraw) {
|
||||||
draw_mask(bg_file, animation.get_shift(), uart.get_message());
|
draw_mask(bg_file, animation.get_shift(), uart.get_message());
|
||||||
CharacterFrame player_frame = player_state.get_character_frame();
|
FrameSelection frame = player_state.get_frame_selection();
|
||||||
|
|
||||||
CharacterPosition draw_pos = get_aligned_frame_position(pos, player_frame);
|
CharacterPosition draw_pos = get_aligned_frame_position(pos, frame.movement, frame.frame_index);
|
||||||
draw_character(draw_pos.x, draw_pos.y, player_frame);
|
draw_character(draw_pos.x, draw_pos.y, frame.movement, frame.frame_index);
|
||||||
|
|
||||||
// alternate between frame 0 and 1 when not crawling
|
if (player_state.get_state() != PlayerState::Crawl) {
|
||||||
if (player_state.get_state() != PlayerState::Crawl1) {
|
|
||||||
player_state.toggle_walk_frame();
|
player_state.toggle_walk_frame();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,28 +1,38 @@
|
|||||||
// src/render/player.cpp
|
// src/render/player.cpp
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "../assets/character_frames.h"
|
#include "../assets/character_walk_frames.h"
|
||||||
|
#include "../assets/character_run_frames.h"
|
||||||
|
#include "../assets/character_crawl_frames.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
void draw_character(int x, int y, CharacterFrame frame) {
|
void draw_character(int x, int y, MovementType movement, int frame_index) {
|
||||||
int frame_index = static_cast<int>(frame);
|
const char **character = nullptr;
|
||||||
if (frame_index < 0 || frame_index >= CHARACTER_FRAME_COUNT)
|
|
||||||
frame_index = 0; // fallback safety
|
|
||||||
|
|
||||||
const char **character = PLAYER_FRAMES[frame_index];
|
|
||||||
|
|
||||||
int character_height = 0;
|
int character_height = 0;
|
||||||
switch (frame) {
|
int total_frames = 0;
|
||||||
case CharacterFrame::Walk1:
|
|
||||||
character_height = CHARACTER_HEIGHT_1;
|
switch (movement) {
|
||||||
|
case MovementType::Walk:
|
||||||
|
total_frames = CHARACTER_WALK_FRAME_COUNT;
|
||||||
|
if (frame_index < 0 || frame_index >= total_frames)
|
||||||
|
frame_index = 0;
|
||||||
|
character = CHARACTER_WALK_FRAMES[frame_index];
|
||||||
|
character_height = CHARACTER_WALK_FRAME_HEIGHT;
|
||||||
break;
|
break;
|
||||||
case CharacterFrame::Walk2:
|
|
||||||
character_height = CHARACTER_HEIGHT_2;
|
case MovementType::Run:
|
||||||
|
total_frames = CHARACTER_RUN_FRAME_COUNT;
|
||||||
|
if (frame_index < 0 || frame_index >= total_frames)
|
||||||
|
frame_index = 0;
|
||||||
|
character = CHARACTER_RUN_FRAMES[frame_index];
|
||||||
|
character_height = CHARACTER_RUN_FRAME_HEIGHT;
|
||||||
break;
|
break;
|
||||||
case CharacterFrame::Crawl1:
|
|
||||||
character_height = CHARACTER_HEIGHT_3;
|
case MovementType::Crawl:
|
||||||
break;
|
total_frames = CHARACTER_CRAWL_FRAME_COUNT;
|
||||||
default:
|
if (frame_index < 0 || frame_index >= total_frames)
|
||||||
character_height = CHARACTER_HEIGHT_1;
|
frame_index = 0;
|
||||||
|
character = CHARACTER_CRAWL_FRAMES[frame_index];
|
||||||
|
character_height = CHARACTER_CRAWL_FRAME_HEIGHT;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,11 @@
|
|||||||
// src/render/player.h
|
// src/render/player.h
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "../assets/character_frames.h"
|
|
||||||
|
enum class MovementType {
|
||||||
|
Walk,
|
||||||
|
Run,
|
||||||
|
Crawl
|
||||||
|
};
|
||||||
|
|
||||||
// Draw the player object starting at given (x, y)
|
// Draw the player object starting at given (x, y)
|
||||||
void draw_character(int x, int y, CharacterFrame frame);
|
void draw_character(int x, int y, MovementType movement, int frame_index);
|
||||||
|
|||||||
@@ -1,25 +1,30 @@
|
|||||||
// src/render/player_positioning.cpp
|
// src/render/player_positioning.cpp
|
||||||
#include "player_positioning.h"
|
#include "player_positioning.h"
|
||||||
#include "../assets/background_frame.h"
|
#include "../assets/background_frame.h"
|
||||||
|
#include "../assets/character_walk_frames.h"
|
||||||
|
#include "../assets/character_run_frames.h"
|
||||||
|
#include "../assets/character_crawl_frames.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
// Convert a pivot coordinate (bottom-left) to the top-left draw position
|
CharacterPosition get_aligned_frame_position(CharacterPosition base,
|
||||||
CharacterPosition get_aligned_frame_position(CharacterPosition base, CharacterFrame frame) {
|
MovementType movement,
|
||||||
int frame_index = static_cast<int>(frame);
|
int /*frame_index*/) {
|
||||||
if (frame_index < 0 || frame_index >= CHARACTER_FRAME_COUNT)
|
|
||||||
frame_index = 0;
|
|
||||||
|
|
||||||
int character_height = 0;
|
int character_height = 0;
|
||||||
if (frame_index == 0)
|
|
||||||
character_height = CHARACTER_HEIGHT_1;
|
|
||||||
else if (frame_index == 1)
|
|
||||||
character_height = CHARACTER_HEIGHT_2;
|
|
||||||
else
|
|
||||||
character_height = CHARACTER_HEIGHT_3;
|
|
||||||
|
|
||||||
CharacterPosition draw_pos;
|
switch (movement) {
|
||||||
|
case MovementType::Walk:
|
||||||
|
character_height = CHARACTER_WALK_FRAME_HEIGHT;
|
||||||
|
break;
|
||||||
|
case MovementType::Run:
|
||||||
|
character_height = CHARACTER_RUN_FRAME_HEIGHT;
|
||||||
|
break;
|
||||||
|
case MovementType::Crawl:
|
||||||
|
character_height = CHARACTER_CRAWL_FRAME_HEIGHT;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
CharacterPosition draw_pos{};
|
||||||
draw_pos.x = base.x;
|
draw_pos.x = base.x;
|
||||||
draw_pos.y = (VIEW_HEIGHT - base.y) - character_height;
|
draw_pos.y = (VIEW_HEIGHT - base.y) - character_height;
|
||||||
|
|
||||||
return draw_pos;
|
return draw_pos;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// src/render/player_positioning.h
|
// src/render/player_positioning.h
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "../assets/background_frame.h"
|
#include "../assets/background_frame.h"
|
||||||
#include "../assets/character_frames.h"
|
#include "player.h"
|
||||||
|
|
||||||
struct CharacterPosition {
|
struct CharacterPosition {
|
||||||
int x;
|
int x;
|
||||||
@@ -9,4 +9,4 @@ struct CharacterPosition {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Calculates drawing position relative to bottom-left pivot
|
// Calculates drawing position relative to bottom-left pivot
|
||||||
CharacterPosition get_aligned_frame_position(CharacterPosition base, CharacterFrame frame);
|
CharacterPosition get_aligned_frame_position(CharacterPosition base, MovementType movement,int frame_index);
|
||||||
|
|||||||
Reference in New Issue
Block a user