Compare commits
2 Commits
4ed4452fae
...
a59192cf05
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a59192cf05 | ||
|
|
fa7e18e4d5 |
@@ -1,4 +1,4 @@
|
|||||||
// background_dark_inverted.h
|
// src/assets/background_frame.h
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
static const char *BACKGROUND_MASK[] = {
|
static const char *BACKGROUND_MASK[] = {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
// src/render/player_mask.h
|
// src/assets/character_frames.h
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
0
semestralka1/src/game/animation.cpp
Normal file
0
semestralka1/src/game/animation.cpp
Normal file
0
semestralka1/src/game/animation.h
Normal file
0
semestralka1/src/game/animation.h
Normal file
38
semestralka1/src/game/state.cpp
Normal file
38
semestralka1/src/game/state.cpp
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
// src/game/state.cpp
|
||||||
|
#include "state.h"
|
||||||
|
|
||||||
|
void WalkingState::update() {
|
||||||
|
// stop crawling after duration
|
||||||
|
if (current_state == PlayerState::Crawl1 &&
|
||||||
|
state_timer.elapsed_time() >= CRAWL_DURATION) {
|
||||||
|
current_state = PlayerState::Walk1;
|
||||||
|
state_timer.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void WalkingState::start_crawl() {
|
||||||
|
current_state = PlayerState::Crawl1;
|
||||||
|
state_timer.reset();
|
||||||
|
state_timer.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
void WalkingState::toggle_walk_frame() {
|
||||||
|
if (current_state == PlayerState::Walk1) {
|
||||||
|
current_state = PlayerState::Walk2;
|
||||||
|
} else if (current_state == PlayerState::Walk2) {
|
||||||
|
current_state = PlayerState::Walk1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CharacterFrame WalkingState::get_character_frame() const {
|
||||||
|
switch (current_state) {
|
||||||
|
case PlayerState::Walk1:
|
||||||
|
return CharacterFrame::Walk1;
|
||||||
|
case PlayerState::Walk2:
|
||||||
|
return CharacterFrame::Walk2;
|
||||||
|
case PlayerState::Crawl1:
|
||||||
|
return CharacterFrame::Crawl1;
|
||||||
|
default:
|
||||||
|
return CharacterFrame::Walk1;
|
||||||
|
}
|
||||||
|
}
|
||||||
27
semestralka1/src/game/state.h
Normal file
27
semestralka1/src/game/state.h
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
// src/game/state.h
|
||||||
|
#pragma once
|
||||||
|
#include "mbed.h"
|
||||||
|
#include "../assets/character_frames.h"
|
||||||
|
|
||||||
|
constexpr auto CRAWL_DURATION = 300ms;
|
||||||
|
|
||||||
|
enum class PlayerState { Walk1, Walk2, Crawl1 };
|
||||||
|
|
||||||
|
class WalkingState {
|
||||||
|
private:
|
||||||
|
PlayerState current_state = PlayerState::Walk1;
|
||||||
|
Timer state_timer;
|
||||||
|
|
||||||
|
public:
|
||||||
|
// MOVED FROM loop.cpp
|
||||||
|
// Update state (handles crawl timeout)
|
||||||
|
void update();
|
||||||
|
|
||||||
|
// State transitions
|
||||||
|
void start_crawl();
|
||||||
|
void toggle_walk_frame();
|
||||||
|
|
||||||
|
// Getters
|
||||||
|
PlayerState get_state() const { return current_state; }
|
||||||
|
CharacterFrame get_character_frame() const;
|
||||||
|
};
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
// main.cpp
|
// main.cpp
|
||||||
|
|
||||||
#include "mbed.h"
|
#include "mbed.h"
|
||||||
#include "background_dark_inverted.h"
|
#include "assets/background_frame.h"
|
||||||
#include "render/loop.h"
|
#include "render/loop.h"
|
||||||
|
|
||||||
#define TARGET_TX_PIN USBTX
|
#define TARGET_TX_PIN USBTX
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// src/render/background.cpp
|
// src/render/background.cpp
|
||||||
|
|
||||||
#include "../background_dark_inverted.h"
|
#include "../assets/background_frame.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
// src/render/loop.cpp
|
// src/render/loop.cpp
|
||||||
#include "loop.h"
|
#include "loop.h"
|
||||||
#include "../background_dark_inverted.h"
|
#include "../assets/background_frame.h"
|
||||||
#include "player_positioning.h"
|
#include "player_positioning.h"
|
||||||
#include "background.h"
|
#include "background.h"
|
||||||
#include "mbed.h"
|
#include "mbed.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "player_mask.h"
|
#include "../game/state.h"
|
||||||
|
#include "../assets/character_frames.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
extern BufferedSerial serial_port;
|
extern BufferedSerial serial_port;
|
||||||
@@ -13,20 +14,11 @@ extern DigitalOut led;
|
|||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
constexpr size_t BUFFER_SIZE = 64;
|
constexpr size_t BUFFER_SIZE = 64;
|
||||||
constexpr auto CRAWL_DURATION = 300ms;
|
|
||||||
constexpr auto ANIMATION_TICK = 170ms;
|
constexpr auto ANIMATION_TICK = 170ms;
|
||||||
constexpr auto MESSAGE_DISPLAY_DURATION = 1s;
|
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 };
|
|
||||||
|
|
||||||
struct WalkingState {
|
|
||||||
PlayerState current_state = PlayerState::Walk1;
|
|
||||||
Timer state_timer;
|
|
||||||
};
|
|
||||||
|
|
||||||
static char rx_buffer[BUFFER_SIZE];
|
static char rx_buffer[BUFFER_SIZE];
|
||||||
static char message[BUFFER_SIZE];
|
static char message[BUFFER_SIZE];
|
||||||
static bool message_active = false;
|
static bool message_active = false;
|
||||||
@@ -87,42 +79,6 @@ static bool update_animation(Timer &anim_timer, int &shift, int speed) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static CharacterFrame get_character_frame(PlayerState state) {
|
|
||||||
switch (state) {
|
|
||||||
case PlayerState::Walk1:
|
|
||||||
return CharacterFrame::Walk1;
|
|
||||||
case PlayerState::Walk2:
|
|
||||||
return CharacterFrame::Walk2;
|
|
||||||
case PlayerState::Crawl1:
|
|
||||||
return CharacterFrame::Crawl1;
|
|
||||||
default:
|
|
||||||
return CharacterFrame::Walk1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void update_player_state(WalkingState &state) {
|
|
||||||
// stop crawling
|
|
||||||
if (state.current_state == PlayerState::Crawl1 &&
|
|
||||||
state.state_timer.elapsed_time() >= CRAWL_DURATION) {
|
|
||||||
state.current_state = PlayerState::Walk1;
|
|
||||||
state.state_timer.stop();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void toggle_walk_frame(WalkingState &state) {
|
|
||||||
if (state.current_state == PlayerState::Walk1) {
|
|
||||||
state.current_state = PlayerState::Walk2;
|
|
||||||
} else if (state.current_state == PlayerState::Walk2) {
|
|
||||||
state.current_state = PlayerState::Walk1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void start_crawl(WalkingState &state) {
|
|
||||||
state.current_state = PlayerState::Crawl1;
|
|
||||||
state.state_timer.reset();
|
|
||||||
state.state_timer.start();
|
|
||||||
}
|
|
||||||
|
|
||||||
void render_loop(int speed) {
|
void render_loop(int speed) {
|
||||||
WalkingState player_state;
|
WalkingState player_state;
|
||||||
|
|
||||||
@@ -145,12 +101,12 @@ void render_loop(int speed) {
|
|||||||
|
|
||||||
if (uart_state == 2) {
|
if (uart_state == 2) {
|
||||||
// start crawl frame for x time
|
// start crawl frame for x time
|
||||||
start_crawl(player_state);
|
player_state.start_crawl();
|
||||||
need_redraw = true;
|
need_redraw = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if crawl duration expired
|
// Check if crawl duration expired
|
||||||
update_player_state(player_state);
|
player_state.update();
|
||||||
|
|
||||||
if (update_animation(anim_timer, shift, speed)) {
|
if (update_animation(anim_timer, shift, speed)) {
|
||||||
need_redraw = true;
|
need_redraw = true;
|
||||||
@@ -158,13 +114,14 @@ 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);
|
||||||
CharacterFrame player_frame = get_character_frame(player_state.current_state);
|
CharacterFrame player_frame = player_state.get_character_frame();
|
||||||
|
|
||||||
CharacterPosition draw_pos = get_aligned_frame_position(pos, player_frame);
|
CharacterPosition draw_pos = get_aligned_frame_position(pos, player_frame);
|
||||||
draw_character(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.get_state() != PlayerState::Crawl1) {
|
||||||
toggle_walk_frame(player_state);
|
player_state.toggle_walk_frame();
|
||||||
}
|
}
|
||||||
|
|
||||||
ThisThread::sleep_for(50ms);
|
ThisThread::sleep_for(50ms);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// src/render/player.cpp
|
// src/render/player.cpp
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
#include "player_mask.h"
|
#include "../assets/character_frames.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
void draw_character(int x, int y, CharacterFrame frame) {
|
void draw_character(int x, int y, CharacterFrame frame) {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// src/render/player.h
|
// src/render/player.h
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "player_mask.h"
|
#include "../assets/character_frames.h"
|
||||||
|
|
||||||
// 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, CharacterFrame frame);
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
// src/render/player_positioning.cpp
|
// src/render/player_positioning.cpp
|
||||||
#include "player_positioning.h"
|
#include "player_positioning.h"
|
||||||
#include "../background_dark_inverted.h"
|
#include "../assets/background_frame.h"
|
||||||
#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
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
// src/render/player_positioning.h
|
// src/render/player_positioning.h
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "../background_dark_inverted.h"
|
#include "../assets/background_frame.h"
|
||||||
#include "player_mask.h"
|
#include "../assets/character_frames.h"
|
||||||
|
|
||||||
struct CharacterPosition {
|
struct CharacterPosition {
|
||||||
int x;
|
int x;
|
||||||
|
|||||||
Reference in New Issue
Block a user