Compare commits
9 Commits
9170524d33
...
v1.3.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
11f5eb7fd1 | ||
|
|
285840b521 | ||
|
|
761b86585a | ||
|
|
528e38b428 | ||
|
|
a59192cf05 | ||
|
|
fa7e18e4d5 | ||
|
|
4ed4452fae | ||
|
|
b9ca8df59b | ||
|
|
8fa8bf392b |
BIN
semestralka1/chafa/character/crawl/crawl1.jpg
Normal file
|
After Width: | Height: | Size: 8.1 KiB |
BIN
semestralka1/chafa/character/crawl/crawl2.jpg
Normal file
|
After Width: | Height: | Size: 7.2 KiB |
6
semestralka1/chafa/character/crawl/out_crawl1.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
........
|
||||
........
|
||||
...y4@W.
|
||||
..a@a`..
|
||||
..@@R@=.
|
||||
.aa@s@..
|
||||
6
semestralka1/chafa/character/crawl/out_crawl2.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
........
|
||||
........
|
||||
........
|
||||
.____...
|
||||
.@@@@yg,
|
||||
.a@@PR@L
|
||||
6
semestralka1/chafa/character/run/out_run1.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
....g@w.
|
||||
..._ZF~.
|
||||
..@~@@y=
|
||||
.".g@_`.
|
||||
_ygF.Ry.
|
||||
`~`...4L
|
||||
6
semestralka1/chafa/character/run/out_run2.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
....g@w.
|
||||
..._Z~~.
|
||||
..aM@$y,
|
||||
..~a@_`.
|
||||
.yy@`$..
|
||||
..`..@..
|
||||
6
semestralka1/chafa/character/run/out_run3.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
...a@$..
|
||||
..._Z~..
|
||||
..a@@...
|
||||
..R@@=..
|
||||
.yy@@...
|
||||
.``u@...
|
||||
6
semestralka1/chafa/character/run/out_run4.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
..g@$...
|
||||
..yT`...
|
||||
.g@F....
|
||||
.4@L....
|
||||
._$$....
|
||||
.`@~....
|
||||
6
semestralka1/chafa/character/run/out_run5.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
...@@...
|
||||
.._Z~...
|
||||
..@@....
|
||||
..$@#...
|
||||
..g@L...
|
||||
.yFP....
|
||||
6
semestralka1/chafa/character/run/out_run6.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
....@@..
|
||||
..._Z~..
|
||||
..y@@,_.
|
||||
..@$@~~.
|
||||
..y@~$..
|
||||
.aF~.4L.
|
||||
6
semestralka1/chafa/character/run/out_run7.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
....a@e.
|
||||
..._ZF`.
|
||||
..a~@@y=
|
||||
..'y@L`.
|
||||
_yyP.4y.
|
||||
`~`...4L
|
||||
BIN
semestralka1/chafa/character/run/run1.jpg
Normal file
|
After Width: | Height: | Size: 11 KiB |
BIN
semestralka1/chafa/character/run/run2.jpg
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
semestralka1/chafa/character/run/run3.jpg
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
semestralka1/chafa/character/run/run4.jpg
Normal file
|
After Width: | Height: | Size: 9.2 KiB |
BIN
semestralka1/chafa/character/run/run5.jpg
Normal file
|
After Width: | Height: | Size: 9.1 KiB |
BIN
semestralka1/chafa/character/run/run6.jpg
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
semestralka1/chafa/character/run/run7.jpg
Normal file
|
After Width: | Height: | Size: 11 KiB |
@@ -2,3 +2,4 @@ chafa --symbols ascii-block -c none --fill none --invert --size=13x20 predloha_r
|
||||
|
||||
a = 78x111
|
||||
b = 34x111
|
||||
walk = 60x111
|
||||
|
||||
6
semestralka1/chafa/character/walk/out_walk1.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
..a@w.
|
||||
.._T`.
|
||||
.s@@y.
|
||||
.^$$~T
|
||||
.y@`$.
|
||||
=F..?K
|
||||
6
semestralka1/chafa/character/walk/out_walk2.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
..y@$.
|
||||
.._Z~.
|
||||
.y@@L.
|
||||
.4W@Z!
|
||||
._$~$.
|
||||
~~`.0.
|
||||
6
semestralka1/chafa/character/walk/out_walk3.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
..g@L.
|
||||
.._T~.
|
||||
.g@$..
|
||||
.J@@..
|
||||
_y$4L.
|
||||
~~.4~.
|
||||
6
semestralka1/chafa/character/walk/out_walk4.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
..a@w.
|
||||
..~T~.
|
||||
..@$..
|
||||
..@$..
|
||||
..y@..
|
||||
."~@..
|
||||
6
semestralka1/chafa/character/walk/out_walk5.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
..@@..
|
||||
..Z~..
|
||||
..@L..
|
||||
..@L..
|
||||
..$%..
|
||||
.4F...
|
||||
6
semestralka1/chafa/character/walk/out_walk6.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
.u@$..
|
||||
..Z~..
|
||||
.g@_..
|
||||
.$@5+.
|
||||
._BL..
|
||||
uP.@..
|
||||
6
semestralka1/chafa/character/walk/out_walk7.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
..a@w.
|
||||
.._T`.
|
||||
.s@@y.
|
||||
.^$$~T
|
||||
.yE`$.
|
||||
=F..JK
|
||||
BIN
semestralka1/chafa/character/walk/walk1.jpg
Normal file
|
After Width: | Height: | Size: 8.2 KiB |
BIN
semestralka1/chafa/character/walk/walk2.jpg
Normal file
|
After Width: | Height: | Size: 8.0 KiB |
BIN
semestralka1/chafa/character/walk/walk3.jpg
Normal file
|
After Width: | Height: | Size: 9.3 KiB |
BIN
semestralka1/chafa/character/walk/walk4.jpg
Normal file
|
After Width: | Height: | Size: 9.0 KiB |
BIN
semestralka1/chafa/character/walk/walk5.jpg
Normal file
|
After Width: | Height: | Size: 8.6 KiB |
BIN
semestralka1/chafa/character/walk/walk6.jpg
Normal file
|
After Width: | Height: | Size: 9.6 KiB |
BIN
semestralka1/chafa/character/walk/walk7.jpg
Normal file
|
After Width: | Height: | Size: 10 KiB |
@@ -1,4 +1,4 @@
|
||||
// background_dark_inverted.h
|
||||
// src/assets/background_frame.h
|
||||
#pragma once
|
||||
|
||||
static const char *BACKGROUND_MASK[] = {
|
||||
38
semestralka1/src/assets/character_crawl_frames.h
Normal file
@@ -0,0 +1,38 @@
|
||||
// src/assets/character_crawl_frames.h
|
||||
#pragma once
|
||||
|
||||
// Crawl frame 1
|
||||
static const char *CRAWL_FRAME_1[] = {
|
||||
"........",
|
||||
"........",
|
||||
"...y4@W.",
|
||||
"..a@a`..",
|
||||
"..@@R@=.",
|
||||
".aa@s@.."
|
||||
};
|
||||
|
||||
// Crawl frame 2
|
||||
static const char *CRAWL_FRAME_2[] = {
|
||||
"........",
|
||||
"........",
|
||||
"........",
|
||||
".____...",
|
||||
".@@@@yg,",
|
||||
".a@@PR@L"
|
||||
};
|
||||
|
||||
enum class CrawlFrame {
|
||||
Crawl1 = 0,
|
||||
Crawl2,
|
||||
COUNT
|
||||
};
|
||||
|
||||
static const char **CHARACTER_CRAWL_FRAMES[] = {
|
||||
CRAWL_FRAME_1,
|
||||
CRAWL_FRAME_2
|
||||
};
|
||||
|
||||
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]);
|
||||
98
semestralka1/src/assets/character_run_frames.h
Normal file
@@ -0,0 +1,98 @@
|
||||
// src/assets/character_run_frames.h
|
||||
#pragma once
|
||||
|
||||
// Run frame 1
|
||||
static const char *RUN_FRAME_1[] = {
|
||||
"....g@w.",
|
||||
"..._ZF~.",
|
||||
"..@~@@y=",
|
||||
"\".g@_`.",
|
||||
"_ygF.Ry.",
|
||||
"`~`...4L",
|
||||
};
|
||||
|
||||
// Run frame 2
|
||||
static const char *RUN_FRAME_2[] = {
|
||||
"....g@w.",
|
||||
"..._Z~~.",
|
||||
"..aM@$y,",
|
||||
"..~a@_`.",
|
||||
".yy@`$..",
|
||||
"..`..@..",
|
||||
};
|
||||
|
||||
// Run frame 3
|
||||
static const char *RUN_FRAME_3[] = {
|
||||
"...a@$..",
|
||||
"..._Z~..",
|
||||
"..a@@...",
|
||||
"..R@@=..",
|
||||
".yy@@...",
|
||||
".``u@...",
|
||||
};
|
||||
|
||||
// Run frame 4
|
||||
static const char *RUN_FRAME_4[] = {
|
||||
"..g@$...",
|
||||
"..yT`...",
|
||||
".g@F....",
|
||||
".4@L....",
|
||||
"._$$....",
|
||||
".`@~....",
|
||||
};
|
||||
|
||||
// Run frame 5
|
||||
static const char *RUN_FRAME_5[] = {
|
||||
"...@@...",
|
||||
".._Z~...",
|
||||
"..@@....",
|
||||
"..$@#...",
|
||||
"..g@L...",
|
||||
".yFP....",
|
||||
};
|
||||
|
||||
// Run frame 6
|
||||
static const char *RUN_FRAME_6[] = {
|
||||
"....@@..",
|
||||
"..._Z~..",
|
||||
"..y@@,_.",
|
||||
"..@$@~~.",
|
||||
"..y@~$..",
|
||||
".aF~.4L.",
|
||||
};
|
||||
|
||||
// Run frame 7
|
||||
static const char *RUN_FRAME_7[] = {
|
||||
"....a@e.",
|
||||
"..._ZF`.",
|
||||
"..a~@@y=",
|
||||
"..'y@L`.",
|
||||
"_yyP.4y.",
|
||||
"`~`...4L",
|
||||
};
|
||||
|
||||
enum class RunFrame {
|
||||
Run1 = 0,
|
||||
Run2,
|
||||
Run3,
|
||||
Run4,
|
||||
Run5,
|
||||
Run6,
|
||||
Run7,
|
||||
COUNT
|
||||
};
|
||||
|
||||
static const char **CHARACTER_RUN_FRAMES[] = {
|
||||
RUN_FRAME_1,
|
||||
RUN_FRAME_2,
|
||||
RUN_FRAME_3,
|
||||
RUN_FRAME_4,
|
||||
RUN_FRAME_5,
|
||||
RUN_FRAME_6,
|
||||
RUN_FRAME_7,
|
||||
};
|
||||
|
||||
static const int CHARACTER_RUN_FRAME_COUNT = 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]);
|
||||
98
semestralka1/src/assets/character_walk_frames.h
Normal file
@@ -0,0 +1,98 @@
|
||||
// src/assets/character_walk_frames.h
|
||||
#pragma once
|
||||
|
||||
// Walk frame 1
|
||||
static const char *WALK_FRAME_1[] = {
|
||||
"..a@w.",
|
||||
".._T`.",
|
||||
".s@@y.",
|
||||
".^$$~T",
|
||||
".y@`$.",
|
||||
"=F..?K",
|
||||
};
|
||||
|
||||
// Walk frame 2
|
||||
static const char *WALK_FRAME_2[] = {
|
||||
"..y@$.",
|
||||
".._Z~.",
|
||||
".y@@L.",
|
||||
".4W@Z!",
|
||||
"._$~$.",
|
||||
"~~`.0.",
|
||||
};
|
||||
|
||||
// Walk frame 3
|
||||
static const char *WALK_FRAME_3[] = {
|
||||
"..g@L.",
|
||||
".._T~.",
|
||||
".g@$..",
|
||||
".J@@..",
|
||||
"_y$4L.",
|
||||
"~~.4~.",
|
||||
};
|
||||
|
||||
// Walk frame 4
|
||||
static const char *WALK_FRAME_4[] = {
|
||||
"..a@w.",
|
||||
"..~T~.",
|
||||
"..@$..",
|
||||
"..@$..",
|
||||
"..y@..",
|
||||
".\"~@..",
|
||||
};
|
||||
|
||||
// Walk frame 5
|
||||
static const char *WALK_FRAME_5[] = {
|
||||
"..@@..",
|
||||
"..Z~..",
|
||||
"..@L..",
|
||||
"..@L..",
|
||||
"..$%..",
|
||||
".4F...",
|
||||
};
|
||||
|
||||
// Walk frame 6
|
||||
static const char *WALK_FRAME_6[] = {
|
||||
".u@$..",
|
||||
"..Z~..",
|
||||
".g@_..",
|
||||
".$@5+.",
|
||||
"._BL..",
|
||||
"uP.@..",
|
||||
};
|
||||
|
||||
// Walk frame 7
|
||||
static const char *WALK_FRAME_7[] = {
|
||||
"..a@w.",
|
||||
".._T`.",
|
||||
".s@@y.",
|
||||
".^$$~T",
|
||||
".yE`$.",
|
||||
"=F..JK",
|
||||
};
|
||||
|
||||
enum class WalkFrame {
|
||||
Walk1 = 0,
|
||||
Walk2,
|
||||
Walk3,
|
||||
Walk4,
|
||||
Walk5,
|
||||
Walk6,
|
||||
Walk7,
|
||||
COUNT
|
||||
};
|
||||
|
||||
static const char **CHARACTER_WALK_FRAMES[] = {
|
||||
WALK_FRAME_1,
|
||||
WALK_FRAME_2,
|
||||
WALK_FRAME_3,
|
||||
WALK_FRAME_4,
|
||||
WALK_FRAME_5,
|
||||
WALK_FRAME_6,
|
||||
WALK_FRAME_7,
|
||||
};
|
||||
|
||||
static const int CHARACTER_WALK_FRAME_COUNT = 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]);
|
||||
16
semestralka1/src/game/animation.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
// src/game/animation.cpp
|
||||
#include "animation.h"
|
||||
|
||||
AnimationController::AnimationController() {
|
||||
anim_timer.start();
|
||||
}
|
||||
|
||||
bool AnimationController::tick(int speed) {
|
||||
if (anim_timer.elapsed_time() >= ANIMATION_TICK) {
|
||||
// speed determines scroll steps per tick
|
||||
shift += speed;
|
||||
anim_timer.reset();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
20
semestralka1/src/game/animation.h
Normal file
@@ -0,0 +1,20 @@
|
||||
// src/game/animation.h
|
||||
#pragma once
|
||||
#include "mbed.h"
|
||||
|
||||
constexpr auto ANIMATION_TICK = 170ms;
|
||||
|
||||
class AnimationController {
|
||||
private:
|
||||
Timer anim_timer;
|
||||
int shift = 0;
|
||||
|
||||
public:
|
||||
AnimationController();
|
||||
|
||||
// Update animation, returns true if redraw needed
|
||||
bool tick(int speed);
|
||||
|
||||
// Getters
|
||||
int get_shift() const { return shift; }
|
||||
};
|
||||
43
semestralka1/src/game/state.cpp
Normal file
@@ -0,0 +1,43 @@
|
||||
// src/game/state.cpp
|
||||
#include "state.h"
|
||||
#include "../render/player.h"
|
||||
|
||||
void WalkingState::update() {
|
||||
// stop crawling after duration
|
||||
if (current_state == PlayerState::Crawl &&
|
||||
state_timer.elapsed_time() >= CRAWL_DURATION) {
|
||||
current_state = PlayerState::Walk;
|
||||
state_timer.stop();
|
||||
}
|
||||
}
|
||||
|
||||
void WalkingState::start_crawl() {
|
||||
current_state = PlayerState::Crawl;
|
||||
state_timer.reset();
|
||||
state_timer.start();
|
||||
}
|
||||
|
||||
// TODO THIS NEEDS REDESIGN ACCORDING TO SPEED
|
||||
void WalkingState::toggle_walk_frame() {
|
||||
if (++walk_index >= 7) walk_index = 0;
|
||||
}
|
||||
|
||||
FrameSelection WalkingState::get_frame_selection() const {
|
||||
FrameSelection f{MovementType::Walk, 0};
|
||||
|
||||
switch (current_state) {
|
||||
case PlayerState::Walk:
|
||||
f.movement = MovementType::Walk;
|
||||
f.frame_index = walk_index;
|
||||
break;
|
||||
case PlayerState::Run:
|
||||
f.movement = MovementType::Run;
|
||||
f.frame_index = run_index;
|
||||
break;
|
||||
case PlayerState::Crawl:
|
||||
f.movement = MovementType::Crawl;
|
||||
f.frame_index = crawl_index;
|
||||
break;
|
||||
}
|
||||
return f;
|
||||
}
|
||||
30
semestralka1/src/game/state.h
Normal file
@@ -0,0 +1,30 @@
|
||||
// src/game/state.h
|
||||
#pragma once
|
||||
#include "mbed.h"
|
||||
#include "../render/player.h"
|
||||
|
||||
constexpr auto CRAWL_DURATION = 300ms;
|
||||
|
||||
enum class PlayerState { Walk, Run, Crawl };
|
||||
|
||||
struct FrameSelection {
|
||||
MovementType movement;
|
||||
int frame_index;
|
||||
};
|
||||
|
||||
class WalkingState {
|
||||
private:
|
||||
PlayerState current_state = PlayerState::Walk;
|
||||
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:
|
||||
void update();
|
||||
void start_crawl();
|
||||
void toggle_walk_frame();
|
||||
|
||||
FrameSelection get_frame_selection() const;
|
||||
PlayerState get_state() const { return current_state; }
|
||||
};
|
||||
49
semestralka1/src/hardware/uart.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
// src/hardware/uart.cpp
|
||||
#include "uart.h"
|
||||
#include <cstring>
|
||||
|
||||
UartReader::UartReader(BufferedSerial &serial) : serial_port(serial) {
|
||||
memset(rx_buffer, 0, sizeof(rx_buffer));
|
||||
memset(message, 0, sizeof(message));
|
||||
}
|
||||
|
||||
UartEvent UartReader::poll() {
|
||||
bool changed = false;
|
||||
bool triggered = false;
|
||||
|
||||
// cita spravu z uartu
|
||||
if (serial_port.readable()) {
|
||||
memset(rx_buffer, 0, sizeof(rx_buffer));
|
||||
ssize_t num = serial_port.read(rx_buffer, sizeof(rx_buffer) - 1);
|
||||
if (num > 0) {
|
||||
strncpy(message, rx_buffer, sizeof(message) - 1);
|
||||
message_active = true;
|
||||
changed = true;
|
||||
triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
// casovac na 1s zobrazenia spravy
|
||||
if (!timer_started) {
|
||||
msg_timer.start();
|
||||
timer_started = true;
|
||||
}
|
||||
|
||||
// po jednu sekundu sa sprava zobrazi
|
||||
if (message_active && msg_timer.elapsed_time() > MESSAGE_DISPLAY_DURATION) {
|
||||
message_active = false;
|
||||
memset(message, 0, sizeof(message));
|
||||
msg_timer.reset();
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (triggered)
|
||||
return UartEvent::Triggered;
|
||||
if (changed)
|
||||
return UartEvent::MessageUpdate;
|
||||
return UartEvent::NoChange;
|
||||
}
|
||||
|
||||
const char* UartReader::get_message() const {
|
||||
return message_active ? message : nullptr;
|
||||
}
|
||||
35
semestralka1/src/hardware/uart.h
Normal file
@@ -0,0 +1,35 @@
|
||||
// src/hardware/uart.h
|
||||
#pragma once
|
||||
#include "mbed.h"
|
||||
#include <cstddef>
|
||||
|
||||
constexpr size_t UART_BUFFER_SIZE = 64;
|
||||
constexpr auto MESSAGE_DISPLAY_DURATION = 1s;
|
||||
|
||||
enum class UartEvent {
|
||||
NoChange = 0, // No new data
|
||||
MessageUpdate = 1, // Message changed (received or expired)
|
||||
Triggered = 2 // New message received (trigger action)
|
||||
};
|
||||
|
||||
class UartReader {
|
||||
private:
|
||||
BufferedSerial &serial_port;
|
||||
|
||||
char rx_buffer[UART_BUFFER_SIZE];
|
||||
char message[UART_BUFFER_SIZE];
|
||||
bool message_active = false;
|
||||
|
||||
Timer msg_timer;
|
||||
bool timer_started = false;
|
||||
|
||||
public:
|
||||
UartReader(BufferedSerial &serial);
|
||||
|
||||
// Poll for UART events
|
||||
UartEvent poll();
|
||||
|
||||
// Get current message (nullptr if no active message)
|
||||
const char* get_message() const;
|
||||
bool has_message() const { return message_active; }
|
||||
};
|
||||
@@ -1,7 +1,7 @@
|
||||
// main.cpp
|
||||
|
||||
#include "mbed.h"
|
||||
#include "background_dark_inverted.h"
|
||||
#include "assets/background_frame.h"
|
||||
#include "render/loop.h"
|
||||
|
||||
#define TARGET_TX_PIN USBTX
|
||||
@@ -22,5 +22,5 @@ int main(void) {
|
||||
printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);
|
||||
|
||||
// Just call into render feature
|
||||
render_loop(4);
|
||||
render_loop(1);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// src/render/background.cpp
|
||||
|
||||
#include "../background_dark_inverted.h"
|
||||
#include "../assets/background_frame.h"
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
|
||||
@@ -1,169 +1,64 @@
|
||||
// src/render/loop.cpp
|
||||
#include "loop.h"
|
||||
#include "../background_dark_inverted.h"
|
||||
#include "../assets/background_frame.h"
|
||||
#include "player_positioning.h"
|
||||
#include "background.h"
|
||||
#include "mbed.h"
|
||||
#include "player.h"
|
||||
#include "player_mask.h"
|
||||
#include <cstring>
|
||||
#include "../game/state.h"
|
||||
#include "../game/animation.h"
|
||||
#include "../hardware/uart.h"
|
||||
#include "../render/player.h"
|
||||
|
||||
extern BufferedSerial serial_port;
|
||||
extern DigitalOut led;
|
||||
|
||||
// Constants
|
||||
constexpr size_t BUFFER_SIZE = 64;
|
||||
constexpr auto CRAWL_DURATION = 300ms;
|
||||
constexpr auto ANIMATION_TICK = 170ms;
|
||||
constexpr auto MESSAGE_DISPLAY_DURATION = 1s;
|
||||
constexpr int PLAYER_X = 9;
|
||||
constexpr int PLAYER_Y = 6;
|
||||
|
||||
enum class PlayerState { Walk1, Walk2, Crawl1 };
|
||||
|
||||
struct WalkingState {
|
||||
PlayerState current_state = PlayerState::Walk1;
|
||||
Timer state_timer;
|
||||
};
|
||||
|
||||
static char rx_buffer[BUFFER_SIZE];
|
||||
static char message[BUFFER_SIZE];
|
||||
static bool message_active = false;
|
||||
|
||||
void draw_mask(const char *unused_filename, int shift, const char *text);
|
||||
|
||||
// Returns:
|
||||
// 0 = no change - walk1
|
||||
// 1 = normal redraw - walk2
|
||||
// 2 = button triggered - crawl1
|
||||
static int read_uart() {
|
||||
bool changed = false;
|
||||
bool triggered = false;
|
||||
|
||||
// cita spravu z uartu
|
||||
if (serial_port.readable()) {
|
||||
memset(rx_buffer, 0, sizeof(rx_buffer));
|
||||
ssize_t num = serial_port.read(rx_buffer, sizeof(rx_buffer) - 1);
|
||||
if (num > 0) {
|
||||
led = !led; // toogle ledky
|
||||
strncpy(message, rx_buffer, sizeof(message) - 1);
|
||||
message_active = true;
|
||||
changed = true;
|
||||
triggered = true;
|
||||
}
|
||||
}
|
||||
|
||||
// casovac na 1s zobrazenia spravy
|
||||
static Timer msg_timer;
|
||||
static bool timer_started = false;
|
||||
if (!timer_started) {
|
||||
msg_timer.start();
|
||||
timer_started = true;
|
||||
}
|
||||
|
||||
// po jednu sekundu sa sprava zobrazi
|
||||
if (message_active && msg_timer.elapsed_time() > MESSAGE_DISPLAY_DURATION) {
|
||||
message_active = false;
|
||||
memset(message, 0, sizeof(message));
|
||||
msg_timer.reset();
|
||||
changed = true;
|
||||
}
|
||||
|
||||
if (triggered)
|
||||
return 2; // LED toggled press
|
||||
if (changed)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool update_animation(Timer &anim_timer, int &shift, int speed) {
|
||||
if (anim_timer.elapsed_time() >= ANIMATION_TICK) {
|
||||
// speed determines scroll steps per tick
|
||||
shift += speed;
|
||||
anim_timer.reset();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static int get_player_frame(PlayerState state) {
|
||||
switch (state) {
|
||||
case PlayerState::Walk1:
|
||||
return 0;
|
||||
case PlayerState::Walk2:
|
||||
return 1;
|
||||
case PlayerState::Crawl1:
|
||||
return 2;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
WalkingState player_state;
|
||||
AnimationController animation;
|
||||
UartReader uart(serial_port);
|
||||
|
||||
Timer anim_timer;
|
||||
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;
|
||||
|
||||
while (true) {
|
||||
need_redraw = false;
|
||||
int uart_state = read_uart(); // returns 0/1/2
|
||||
UartEvent uart_event = uart.poll();
|
||||
|
||||
if (uart_state == 1) {
|
||||
if (uart_event == UartEvent::MessageUpdate) {
|
||||
need_redraw = true;
|
||||
}
|
||||
|
||||
if (uart_state == 2) {
|
||||
if (uart_event == UartEvent::Triggered) {
|
||||
// start crawl frame for x time
|
||||
start_crawl(player_state);
|
||||
player_state.start_crawl();
|
||||
need_redraw = true;
|
||||
}
|
||||
|
||||
// Check if crawl duration expired
|
||||
update_player_state(player_state);
|
||||
player_state.update();
|
||||
|
||||
if (update_animation(anim_timer, shift, speed)) {
|
||||
if (animation.tick(speed)) {
|
||||
need_redraw = true;
|
||||
}
|
||||
|
||||
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);
|
||||
draw_mask(bg_file, animation.get_shift(), uart.get_message());
|
||||
FrameSelection frame = player_state.get_frame_selection();
|
||||
|
||||
// alternate between frame 0 and 1 when not crawling
|
||||
if (player_state.current_state != PlayerState::Crawl1) {
|
||||
toggle_walk_frame(player_state);
|
||||
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);
|
||||
|
||||
if (player_state.get_state() != PlayerState::Crawl) {
|
||||
player_state.toggle_walk_frame();
|
||||
}
|
||||
|
||||
ThisThread::sleep_for(50ms);
|
||||
|
||||
@@ -1,25 +1,43 @@
|
||||
// src/render/player.cpp
|
||||
|
||||
#include "player.h"
|
||||
#include "player_mask.h"
|
||||
#include "../assets/character_walk_frames.h"
|
||||
#include "../assets/character_run_frames.h"
|
||||
#include "../assets/character_crawl_frames.h"
|
||||
#include <cstdio>
|
||||
|
||||
void draw_player(int x, int y, int frame_index) {
|
||||
if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT)
|
||||
frame_index = 0; // fallback safety
|
||||
void draw_character(int x, int y, MovementType movement, int frame_index) {
|
||||
const char **character = nullptr;
|
||||
int character_height = 0;
|
||||
int total_frames = 0;
|
||||
|
||||
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;
|
||||
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;
|
||||
|
||||
// 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]);
|
||||
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 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;
|
||||
}
|
||||
|
||||
for (int i = 0; i < character_height; i++) {
|
||||
printf("\033[%d;%dH%s", y + i + 1, x + 1, character[i]);
|
||||
}
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,11 @@
|
||||
// src/render/player.h
|
||||
#pragma once
|
||||
|
||||
enum class MovementType {
|
||||
Walk,
|
||||
Run,
|
||||
Crawl
|
||||
};
|
||||
|
||||
// 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, MovementType movement, int frame_index);
|
||||
|
||||
@@ -1,50 +0,0 @@
|
||||
// src/render/player_mask.h
|
||||
|
||||
#pragma once
|
||||
|
||||
// Player frame 1
|
||||
static const char *PLAYER_MASK_FRAME_1[] = {
|
||||
".....a@$..",
|
||||
".....7PF..",
|
||||
"..yaM@@__y",
|
||||
"..@.y@FFF~",
|
||||
"..._$M@_..",
|
||||
"yaa@~.`@y.",
|
||||
"........@r"
|
||||
};
|
||||
|
||||
// Player frame 2
|
||||
static const char *PLAYER_MASK_FRAME_2[] = {
|
||||
".g@$",
|
||||
".7PF",
|
||||
"y@$.",
|
||||
"0@F.",
|
||||
"4@$.",
|
||||
"y$@.",
|
||||
"u@.."
|
||||
};
|
||||
|
||||
// Player frame 3 (triggered)
|
||||
static const char *PLAYER_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)
|
||||
};
|
||||
|
||||
static const int PLAYER_FRAME_COUNT = sizeof(PLAYER_FRAMES) / sizeof(PLAYER_FRAMES[0]);
|
||||
|
||||
// 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]);
|
||||
@@ -1,24 +1,30 @@
|
||||
// src/render/player_positioning.cpp
|
||||
#include "player_positioning.h"
|
||||
#include "../background_dark_inverted.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
|
||||
PlayerPosition get_aligned_frame_position(PlayerPosition base, int frame_index) {
|
||||
if (frame_index < 0 || frame_index >= PLAYER_FRAME_COUNT)
|
||||
frame_index = 0;
|
||||
CharacterPosition get_aligned_frame_position(CharacterPosition base,
|
||||
MovementType movement,
|
||||
int /*frame_index*/) {
|
||||
int character_height = 0;
|
||||
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
// src/render/player_positioning.h
|
||||
#pragma once
|
||||
#include "player_mask.h"
|
||||
#include "../assets/background_frame.h"
|
||||
#include "player.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, MovementType movement,int frame_index);
|
||||
|
||||