multiple frames now added and working
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
#include "../game/state.h"
|
||||
#include "../game/animation.h"
|
||||
#include "../hardware/uart.h"
|
||||
#include "../assets/character_frames.h"
|
||||
#include "../render/player.h"
|
||||
|
||||
extern BufferedSerial serial_port;
|
||||
extern DigitalOut led;
|
||||
@@ -52,13 +52,12 @@ void render_loop(int speed) {
|
||||
|
||||
if (need_redraw) {
|
||||
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);
|
||||
draw_character(draw_pos.x, draw_pos.y, player_frame);
|
||||
CharacterPosition draw_pos = get_aligned_frame_position(pos, frame.movement, frame.frame_index);
|
||||
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::Crawl1) {
|
||||
if (player_state.get_state() != PlayerState::Crawl) {
|
||||
player_state.toggle_walk_frame();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,28 +1,38 @@
|
||||
// src/render/player.cpp
|
||||
#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>
|
||||
|
||||
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 **character = PLAYER_FRAMES[frame_index];
|
||||
|
||||
void draw_character(int x, int y, MovementType movement, int frame_index) {
|
||||
const char **character = nullptr;
|
||||
int character_height = 0;
|
||||
switch (frame) {
|
||||
case CharacterFrame::Walk1:
|
||||
character_height = CHARACTER_HEIGHT_1;
|
||||
int total_frames = 0;
|
||||
|
||||
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;
|
||||
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;
|
||||
case CharacterFrame::Crawl1:
|
||||
character_height = CHARACTER_HEIGHT_3;
|
||||
break;
|
||||
default:
|
||||
character_height = CHARACTER_HEIGHT_1;
|
||||
|
||||
case MovementType::Crawl:
|
||||
total_frames = CHARACTER_CRAWL_FRAME_COUNT;
|
||||
if (frame_index < 0 || frame_index >= total_frames)
|
||||
frame_index = 0;
|
||||
character = CHARACTER_CRAWL_FRAMES[frame_index];
|
||||
character_height = CHARACTER_CRAWL_FRAME_HEIGHT;
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
// src/render/player.h
|
||||
#pragma once
|
||||
#include "../assets/character_frames.h"
|
||||
|
||||
enum class MovementType {
|
||||
Walk,
|
||||
Run,
|
||||
Crawl
|
||||
};
|
||||
|
||||
// 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
|
||||
#include "player_positioning.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>
|
||||
|
||||
// Convert a pivot coordinate (bottom-left) to the top-left draw position
|
||||
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;
|
||||
|
||||
CharacterPosition get_aligned_frame_position(CharacterPosition base,
|
||||
MovementType movement,
|
||||
int /*frame_index*/) {
|
||||
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.y = (VIEW_HEIGHT - base.y) - character_height;
|
||||
|
||||
return draw_pos;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// src/render/player_positioning.h
|
||||
#pragma once
|
||||
#include "../assets/background_frame.h"
|
||||
#include "../assets/character_frames.h"
|
||||
#include "player.h"
|
||||
|
||||
struct CharacterPosition {
|
||||
int x;
|
||||
@@ -9,4 +9,4 @@ struct CharacterPosition {
|
||||
};
|
||||
|
||||
// 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