Compare commits
23 Commits
v1.2.0
...
8382d1caec
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8382d1caec | ||
|
|
165540588f | ||
|
|
0083fff73a | ||
|
|
ca7eac70e4 | ||
|
|
3e47f855ed | ||
|
|
dd410c6f6c | ||
|
|
967b377e76 | ||
|
|
cf30946c0d | ||
|
|
2af725bfe7 | ||
|
|
c56dac3b6a | ||
|
|
ae2b8b91aa | ||
|
|
34c9d76389 | ||
|
|
09454e3ab4 | ||
|
|
07e295f2d9 | ||
|
|
42ad5ee7a2 | ||
|
|
cf07c9c47a | ||
|
|
ace0e2d706 | ||
|
|
8cb6892d30 | ||
|
|
de6facff03 | ||
|
|
2753aed573 | ||
|
|
0ff2be6564 | ||
|
|
11f5eb7fd1 | ||
|
|
285840b521 |
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
|
a = 78x111
|
||||||
b = 34x111
|
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 |
57
semestralka1/src/assets/character_crawl_frames.h
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
// src/assets/character_crawl_frames.h
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
// Crawl1 frame 1
|
||||||
|
static const char *CRAWL1_FRAME_1[] = {
|
||||||
|
"........",
|
||||||
|
"........",
|
||||||
|
"...y4@W.",
|
||||||
|
"..a@a`..",
|
||||||
|
"..@@R@=.",
|
||||||
|
".aa@s@.."
|
||||||
|
};
|
||||||
|
|
||||||
|
// Crawl2 frame 1
|
||||||
|
static const char *CRAWL2_FRAME_1[] = {
|
||||||
|
"........",
|
||||||
|
"........",
|
||||||
|
"........",
|
||||||
|
".____...",
|
||||||
|
".@@@@yg,",
|
||||||
|
".a@@PR@L"
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class CrawlType {
|
||||||
|
Crawl1 = 0,
|
||||||
|
Crawl2,
|
||||||
|
COUNT
|
||||||
|
};
|
||||||
|
|
||||||
|
static const char **CHARACTER_CRAWL1_FRAMES[] = {
|
||||||
|
CRAWL1_FRAME_1,
|
||||||
|
};
|
||||||
|
static const char **CHARACTER_CRAWL2_FRAMES[] = {
|
||||||
|
CRAWL2_FRAME_1,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const int CHARACTER_CRAWL1_FRAME_COUNT = sizeof(CHARACTER_CRAWL1_FRAMES) / sizeof(CHARACTER_CRAWL1_FRAMES[0]);
|
||||||
|
static const int CHARACTER_CRAWL2_FRAME_COUNT = sizeof(CHARACTER_CRAWL2_FRAMES) / sizeof(CHARACTER_CRAWL2_FRAMES[0]);
|
||||||
|
|
||||||
|
// Height (rows per crawl frame)
|
||||||
|
static const int CHARACTER_CRAWL1_FRAME_HEIGHT = sizeof(CRAWL1_FRAME_1) / sizeof(CRAWL1_FRAME_1[0]);
|
||||||
|
static const int CHARACTER_CRAWL2_FRAME_HEIGHT = sizeof(CRAWL2_FRAME_1) / sizeof(CRAWL2_FRAME_1[0]);
|
||||||
|
|
||||||
|
|
||||||
|
// Crawl1 collision box
|
||||||
|
static const int CHARACTER_CRAWL1_FRAME_WIDTH = 8;
|
||||||
|
static const int CHARACTER_CRAWL1_COLLISION_LEFT_OFFSET = 1;
|
||||||
|
static const int CHARACTER_CRAWL1_COLLISION_RIGHT_OFFSET = 1;
|
||||||
|
static const int CHARACTER_CRAWL1_COLLISION_WIDTH = CHARACTER_CRAWL1_FRAME_WIDTH - (CHARACTER_CRAWL1_COLLISION_LEFT_OFFSET + CHARACTER_CRAWL1_COLLISION_RIGHT_OFFSET);
|
||||||
|
static const int CHARACTER_CRAWL1_COLLISION_HEIGHT = CHARACTER_CRAWL1_FRAME_HEIGHT - 2;
|
||||||
|
|
||||||
|
// Crawl2 collision box (lower posture - smaller collision height)
|
||||||
|
static const int CHARACTER_CRAWL2_FRAME_WIDTH = 8;
|
||||||
|
static const int CHARACTER_CRAWL2_COLLISION_LEFT_OFFSET = 1;
|
||||||
|
static const int CHARACTER_CRAWL2_COLLISION_RIGHT_OFFSET = 0;
|
||||||
|
static const int CHARACTER_CRAWL2_COLLISION_WIDTH = CHARACTER_CRAWL2_FRAME_WIDTH - (CHARACTER_CRAWL2_COLLISION_LEFT_OFFSET + CHARACTER_CRAWL2_COLLISION_RIGHT_OFFSET);
|
||||||
|
static const int CHARACTER_CRAWL2_COLLISION_HEIGHT = CHARACTER_CRAWL2_FRAME_HEIGHT - 4;
|
||||||
@@ -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]);
|
|
||||||
106
semestralka1/src/assets/character_run_frames.h
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
// 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]);
|
||||||
|
|
||||||
|
static const int CHARACTER_RUN_COLLISION_HEIGHT = CHARACTER_RUN_FRAME_HEIGHT;
|
||||||
|
|
||||||
|
static const int CHARACTER_RUN_FRAME_WIDTH = 8;
|
||||||
|
static const int CHARACTER_RUN_COLLISION_LEFT_OFFSET = 2;
|
||||||
|
static const int CHARACTER_RUN_COLLISION_RIGHT_OFFSET = 2;
|
||||||
|
// 8 je width a 2 zlava a 2 zprava
|
||||||
|
static const int CHARACTER_RUN_COLLISION_WIDTH = CHARACTER_RUN_FRAME_WIDTH - (CHARACTER_RUN_COLLISION_LEFT_OFFSET + CHARACTER_RUN_COLLISION_RIGHT_OFFSET);
|
||||||
104
semestralka1/src/assets/character_walk_frames.h
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
// 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]);
|
||||||
|
|
||||||
|
static const int CHARACTER_WALK_FRAME_WIDTH = 6;
|
||||||
|
static const int CHARACTER_WALK_COLLISION_LEFT_OFFSET = 1;
|
||||||
|
static const int CHARACTER_WALK_COLLISION_RIGHT_OFFSET = 1;
|
||||||
|
// 8 je width a 2 zlava a 2 zprava
|
||||||
|
static const int CHARACTER_WALK_COLLISION_WIDTH = CHARACTER_WALK_FRAME_WIDTH - (CHARACTER_WALK_COLLISION_LEFT_OFFSET + CHARACTER_WALK_COLLISION_RIGHT_OFFSET);
|
||||||
47
semestralka1/src/assets/obstacle_crawl_frames.h
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
// src/assets/obstacle_crawl_frames.h
|
||||||
|
#pragma once
|
||||||
|
#include <cstring>
|
||||||
|
|
||||||
|
// Crawl obstacle for CRAWL1 (2 lines tall)
|
||||||
|
static const char *OBSTACLE_CRAWL1_FRAME[] = {
|
||||||
|
"#########",
|
||||||
|
".######",
|
||||||
|
"..#####",
|
||||||
|
"..####",
|
||||||
|
"...###",
|
||||||
|
"...###"
|
||||||
|
};
|
||||||
|
|
||||||
|
// Crawl obstacle for CRAWL2 (4 lines tall)
|
||||||
|
static const char *OBSTACLE_CRAWL2_FRAME[] = {
|
||||||
|
"###",
|
||||||
|
"###",
|
||||||
|
"###",
|
||||||
|
"###",
|
||||||
|
"###",
|
||||||
|
"###",
|
||||||
|
"###",
|
||||||
|
"###"
|
||||||
|
};
|
||||||
|
|
||||||
|
enum class CrawlObstacleType { Crawl1 = 0, Crawl2, COUNT };
|
||||||
|
|
||||||
|
static const char **OBSTACLE_CRAWL_FRAMES[] = { OBSTACLE_CRAWL1_FRAME, OBSTACLE_CRAWL2_FRAME };
|
||||||
|
|
||||||
|
// Crawl1 dimensions and collision
|
||||||
|
static const int OBSTACLE_CRAWL1_FRAME_HEIGHT = sizeof(OBSTACLE_CRAWL1_FRAME) / sizeof(OBSTACLE_CRAWL1_FRAME[0]);
|
||||||
|
static const int OBSTACLE_CRAWL1_FRAME_WIDTH = std::strlen(OBSTACLE_CRAWL1_FRAME[0]);
|
||||||
|
static const int OBSTACLE_CRAWL1_COLLISION_LEFT_OFFSET = 3;
|
||||||
|
static const int OBSTACLE_CRAWL1_COLLISION_RIGHT_OFFSET = 3;
|
||||||
|
static const int OBSTACLE_CRAWL1_COLLISION_WIDTH = OBSTACLE_CRAWL1_FRAME_WIDTH - (OBSTACLE_CRAWL1_COLLISION_LEFT_OFFSET + OBSTACLE_CRAWL1_COLLISION_RIGHT_OFFSET);
|
||||||
|
static const int OBSTACLE_CRAWL1_COLLISION_TOP_OFFSET = 4; // first 4 rows are just empty ('.')
|
||||||
|
// collision box height = bottom 2 rows
|
||||||
|
static const int OBSTACLE_CRAWL1_COLLISION_HEIGHT = 2;
|
||||||
|
|
||||||
|
// Crawl2 dimensions and collision
|
||||||
|
static const int OBSTACLE_CRAWL2_FRAME_HEIGHT = sizeof(OBSTACLE_CRAWL2_FRAME) / sizeof(OBSTACLE_CRAWL2_FRAME[0]);
|
||||||
|
static const int OBSTACLE_CRAWL2_FRAME_WIDTH = std::strlen(OBSTACLE_CRAWL2_FRAME[0]);
|
||||||
|
static const int OBSTACLE_CRAWL2_COLLISION_LEFT_OFFSET = 0;
|
||||||
|
static const int OBSTACLE_CRAWL2_COLLISION_RIGHT_OFFSET = 0;
|
||||||
|
static const int OBSTACLE_CRAWL2_COLLISION_WIDTH = OBSTACLE_CRAWL2_FRAME_WIDTH - (OBSTACLE_CRAWL2_COLLISION_LEFT_OFFSET + OBSTACLE_CRAWL2_COLLISION_RIGHT_OFFSET);
|
||||||
|
static const int OBSTACLE_CRAWL2_COLLISION_HEIGHT = OBSTACLE_CRAWL2_FRAME_HEIGHT;
|
||||||
65
semestralka1/src/game/collision.h
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
// src/game/collision.h
|
||||||
|
#pragma once
|
||||||
|
#include "../render/player_positioning.h"
|
||||||
|
#include "../assets/character_walk_frames.h"
|
||||||
|
#include "../assets/character_run_frames.h"
|
||||||
|
#include "../assets/character_crawl_frames.h"
|
||||||
|
|
||||||
|
// Obstacle representation
|
||||||
|
struct Obstacle {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline bool check_collision(const CharacterPosition& player,
|
||||||
|
MovementType movement,
|
||||||
|
const Obstacle& obs) {
|
||||||
|
|
||||||
|
// Player is given in bottom-based world coordinates (0 = bottom line)
|
||||||
|
const int player_x = player.x;
|
||||||
|
const int player_bottom = player.y;
|
||||||
|
int box_x = 0;
|
||||||
|
int box_w = 0;
|
||||||
|
int box_h = 0;
|
||||||
|
|
||||||
|
switch (movement) {
|
||||||
|
case MovementType::Walk:
|
||||||
|
box_x = player_x + CHARACTER_WALK_COLLISION_LEFT_OFFSET;
|
||||||
|
box_w = CHARACTER_WALK_COLLISION_WIDTH;
|
||||||
|
box_h = CHARACTER_WALK_FRAME_HEIGHT;
|
||||||
|
break;
|
||||||
|
case MovementType::Run:
|
||||||
|
box_x = player_x + CHARACTER_RUN_COLLISION_LEFT_OFFSET;
|
||||||
|
box_w = CHARACTER_RUN_COLLISION_WIDTH;
|
||||||
|
box_h = CHARACTER_RUN_COLLISION_HEIGHT;
|
||||||
|
break;
|
||||||
|
case MovementType::Crawl1:
|
||||||
|
box_x = player_x + CHARACTER_CRAWL1_COLLISION_LEFT_OFFSET;
|
||||||
|
box_w = CHARACTER_CRAWL1_COLLISION_WIDTH;
|
||||||
|
box_h = CHARACTER_CRAWL1_COLLISION_HEIGHT;
|
||||||
|
break;
|
||||||
|
case MovementType::Crawl2:
|
||||||
|
box_x = player_x + CHARACTER_CRAWL2_COLLISION_LEFT_OFFSET;
|
||||||
|
box_w = CHARACTER_CRAWL2_COLLISION_WIDTH;
|
||||||
|
box_h = CHARACTER_CRAWL2_COLLISION_HEIGHT;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Player bounding box
|
||||||
|
const int player_left = box_x;
|
||||||
|
const int player_right = player_left + box_w;
|
||||||
|
const int player_top = player_bottom + box_h;
|
||||||
|
|
||||||
|
// Obstacle bounding box
|
||||||
|
const int obs_left = obs.x;
|
||||||
|
const int obs_right = obs.x + obs.width;
|
||||||
|
const int obs_bottom = obs.y;
|
||||||
|
const int obs_top = obs_bottom + obs.height;
|
||||||
|
|
||||||
|
// Overlap check
|
||||||
|
bool horizontal = player_left < obs_right && player_right > obs_left;
|
||||||
|
bool vertical = player_bottom < obs_top && player_top > obs_bottom;
|
||||||
|
return horizontal && vertical;
|
||||||
|
}
|
||||||
56
semestralka1/src/game/obstacle_manager.h
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
// src/game/obstacle_manager.h
|
||||||
|
#pragma once
|
||||||
|
#include "../game/collision.h"
|
||||||
|
#include "../assets/obstacle_crawl_frames.h"
|
||||||
|
#include "../assets/background_frame.h"
|
||||||
|
|
||||||
|
// Movable obstacle
|
||||||
|
struct MovingObstacle {
|
||||||
|
Obstacle data;
|
||||||
|
CrawlObstacleType type;
|
||||||
|
bool active;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Fixed obstacle pool
|
||||||
|
constexpr int MAX_OBSTACLES = 6;
|
||||||
|
static MovingObstacle obstacle_pool[MAX_OBSTACLES];
|
||||||
|
|
||||||
|
// Create / reset obstacle in one slot
|
||||||
|
inline int spawn_obstacle(CrawlObstacleType type, int x_start) {
|
||||||
|
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
|
if (!obstacle_pool[i].active) {
|
||||||
|
obstacle_pool[i].active = true;
|
||||||
|
obstacle_pool[i].type = type;
|
||||||
|
obstacle_pool[i].data.x = x_start;
|
||||||
|
|
||||||
|
obstacle_pool[i].data.width = (type == CrawlObstacleType::Crawl1) ? OBSTACLE_CRAWL1_COLLISION_WIDTH : OBSTACLE_CRAWL2_COLLISION_WIDTH;
|
||||||
|
obstacle_pool[i].data.height = (type == CrawlObstacleType::Crawl1) ? OBSTACLE_CRAWL1_COLLISION_HEIGHT : OBSTACLE_CRAWL2_COLLISION_HEIGHT;
|
||||||
|
|
||||||
|
// Default Y so that the character touches the top of the screen.
|
||||||
|
// We store Y as the bottom of the COLLISION BOX in bottom-based world coords.
|
||||||
|
int top_offset = 0;
|
||||||
|
if (type == CrawlObstacleType::Crawl1) {
|
||||||
|
top_offset = OBSTACLE_CRAWL1_COLLISION_TOP_OFFSET;
|
||||||
|
} else {
|
||||||
|
top_offset = 0;
|
||||||
|
}
|
||||||
|
const int i_bottom_of_collision = top_offset + obstacle_pool[i].data.height - 1;
|
||||||
|
// Top of screen in world coords is VIEW_HEIGHT - 1
|
||||||
|
const int collision_bottom_world_y = (VIEW_HEIGHT - 1) - i_bottom_of_collision;
|
||||||
|
obstacle_pool[i].data.y = collision_bottom_world_y;
|
||||||
|
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update X positions; mark inactive if offscreen
|
||||||
|
inline void update_obstacles(int shift_speed) {
|
||||||
|
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
|
if (!obstacle_pool[i].active) continue;
|
||||||
|
obstacle_pool[i].data.x -= shift_speed;
|
||||||
|
if (obstacle_pool[i].data.x + obstacle_pool[i].data.width < 0)
|
||||||
|
obstacle_pool[i].active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
73
semestralka1/src/game/obstacle_system.h
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
// src/game/obstacle_system.h
|
||||||
|
#pragma once
|
||||||
|
#include "obstacle_manager.h"
|
||||||
|
#include "../render/player_positioning.h"
|
||||||
|
#include "../assets/obstacle_crawl_frames.h"
|
||||||
|
#include "collision.h"
|
||||||
|
#include "mbed.h"
|
||||||
|
|
||||||
|
inline int clamp_min(int a, int b) { return (a > b) ? a : b; }
|
||||||
|
inline int clamp_max(int a, int b) { return (a < b) ? a : b; }
|
||||||
|
|
||||||
|
// Handles spawning, movement, rendering, and collision of obstacles
|
||||||
|
class ObstacleSystem {
|
||||||
|
public:
|
||||||
|
ObstacleSystem() : tick_counter_(0), spawn_index_(0) {}
|
||||||
|
|
||||||
|
// Called once per frame
|
||||||
|
bool update_and_draw(const CharacterPosition &player_pos,
|
||||||
|
MovementType player_movement) {
|
||||||
|
// Update timers, move existing obstacles
|
||||||
|
tick_counter_++;
|
||||||
|
if (tick_counter_ % UPDATE_INTERVAL == 0) {
|
||||||
|
update_obstacles(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Periodically spawn new obstacle
|
||||||
|
if (tick_counter_ % SPAWN_EVERY_TICKS == 0) {
|
||||||
|
auto type = (spawn_index_++ % 2 == 0) ? CrawlObstacleType::Crawl1 : CrawlObstacleType::Crawl2;
|
||||||
|
const int w = (type == CrawlObstacleType::Crawl1) ? OBSTACLE_CRAWL1_FRAME_WIDTH : OBSTACLE_CRAWL2_FRAME_WIDTH;
|
||||||
|
spawn_obstacle(type, VIEW_WIDTH - w - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Draw & collision check
|
||||||
|
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
|
if (!obstacle_pool[i].active)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
const char **frame = nullptr;
|
||||||
|
int height = 0;
|
||||||
|
if (obstacle_pool[i].type == CrawlObstacleType::Crawl1) {
|
||||||
|
frame = OBSTACLE_CRAWL1_FRAME;
|
||||||
|
height = OBSTACLE_CRAWL1_FRAME_HEIGHT;
|
||||||
|
} else {
|
||||||
|
frame = OBSTACLE_CRAWL2_FRAME;
|
||||||
|
height = OBSTACLE_CRAWL2_FRAME_HEIGHT;
|
||||||
|
}
|
||||||
|
|
||||||
|
draw_clipped_obstacle(obstacle_pool[i], frame, height);
|
||||||
|
|
||||||
|
// Collision check
|
||||||
|
if (check_collision(player_pos, player_movement,
|
||||||
|
obstacle_pool[i].data)) {
|
||||||
|
printf("\033[2J\033[H");
|
||||||
|
printf("GAME OVER\r\n");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static constexpr int SPAWN_EVERY_TICKS = 35;
|
||||||
|
static constexpr int UPDATE_INTERVAL = 1; // move obstacles each frame
|
||||||
|
int tick_counter_;
|
||||||
|
int spawn_index_;
|
||||||
|
|
||||||
|
static void draw_clipped_obstacle(const MovingObstacle &obs,
|
||||||
|
const char ** /*frame*/,
|
||||||
|
int /*frame_height*/) {
|
||||||
|
draw_obstacle(obs.data.x, obs.data.y, obs.type);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,38 +1,179 @@
|
|||||||
// src/game/state.cpp
|
// src/game/state.cpp
|
||||||
#include "state.h"
|
#include "state.h"
|
||||||
|
#include "../render/player.h"
|
||||||
|
#include "../timing/speed_controller.h"
|
||||||
|
#include "../assets/character_run_frames.h"
|
||||||
|
#include "../assets/character_walk_frames.h"
|
||||||
|
#include "../assets/character_crawl_frames.h"
|
||||||
|
#include <cmath>
|
||||||
|
|
||||||
void WalkingState::update() {
|
namespace {
|
||||||
|
// Vygenerovane AI, proste to nahodne vymiena obrazky - assests
|
||||||
|
// Frame selector with two regimes:
|
||||||
|
// 1) Normal (speed <= 6): ping‑pong with holds at both ends
|
||||||
|
// 2) High‑speed (speed >= 7): 0, random(1..last-1), last, random(1..last-1)...
|
||||||
|
static inline int compute_frame_index(int frame_count,
|
||||||
|
float player_speed,
|
||||||
|
int tick_counter) {
|
||||||
|
if (frame_count <= 1) return 0;
|
||||||
|
if (player_speed <= 0.0f) return 0;
|
||||||
|
|
||||||
|
const int last = frame_count - 1;
|
||||||
|
|
||||||
|
// High-speed pattern: 0, rand(1..last-1), last, rand(1..last-1), ...
|
||||||
|
if (player_speed >= 7.0f) {
|
||||||
|
// Special-case 2-frame animations
|
||||||
|
if (frame_count <= 2) {
|
||||||
|
return (tick_counter & 1) ? 1 : 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Even ticks → extremes (0 or last), alternating each time.
|
||||||
|
// Odd ticks → pseudo-random mid in [1..last-1].
|
||||||
|
if ((tick_counter & 1) == 0) {
|
||||||
|
// Alternate 0 and last on even ticks
|
||||||
|
bool choose_zero = ((tick_counter >> 1) & 1) == 0;
|
||||||
|
return choose_zero ? 0 : last;
|
||||||
|
} else {
|
||||||
|
// Simple deterministic PRNG from tick_counter and speed
|
||||||
|
unsigned int seed =
|
||||||
|
static_cast<unsigned int>(tick_counter) * 1664525u + 1013904223u;
|
||||||
|
seed ^= static_cast<unsigned int>(frame_count) * 2654435761u;
|
||||||
|
seed ^= static_cast<unsigned int>(player_speed * 997.0f);
|
||||||
|
int range = last - 1; // size of 1..last-1
|
||||||
|
if (range <= 0) return 0; // safety
|
||||||
|
int mid = 1 + static_cast<int>(seed % static_cast<unsigned int>(range));
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normal-speed regime: discrete ping‑pong with holds at both ends.
|
||||||
|
// - speed 1 => 0,1,2,...,last, last, ..., 2,1, 0,0 ...
|
||||||
|
// - speed 2 => advances 2 steps per tick (skipping every second timeline entry)
|
||||||
|
// - holds at ends make 0 and last appear more frequently
|
||||||
|
const int hold = 2; // how many extra steps to hold at each end
|
||||||
|
|
||||||
|
// One full ping‑pong cycle length:
|
||||||
|
// [hold at 0] + [ascend 1..last] + [hold at last] + [descend last-1..0]
|
||||||
|
const int ascend_len = last; // produces 1..last (length = last)
|
||||||
|
const int descend_len = last; // produces last-1..0 (length = last)
|
||||||
|
const int cycle_len = hold + ascend_len + hold + descend_len;
|
||||||
|
|
||||||
|
// Advance by an integer number of steps per tick based on speed
|
||||||
|
int step = static_cast<int>(floorf(player_speed + 0.5f));
|
||||||
|
if (step < 1) step = 1;
|
||||||
|
|
||||||
|
// Use 64-bit for safety, then fold into cycle length
|
||||||
|
const long long pos64 =
|
||||||
|
(static_cast<long long>(tick_counter) *
|
||||||
|
static_cast<long long>(step)) %
|
||||||
|
static_cast<long long>(cycle_len);
|
||||||
|
int pos = static_cast<int>(pos64);
|
||||||
|
|
||||||
|
// Map timeline position to frame index
|
||||||
|
if (pos < hold) {
|
||||||
|
return 0; // hold at 0
|
||||||
|
}
|
||||||
|
pos -= hold;
|
||||||
|
|
||||||
|
if (pos < ascend_len) {
|
||||||
|
// ascend: 1..last
|
||||||
|
return (pos + 1);
|
||||||
|
}
|
||||||
|
pos -= ascend_len;
|
||||||
|
|
||||||
|
if (pos < hold) {
|
||||||
|
return last; // hold at last
|
||||||
|
}
|
||||||
|
pos -= hold;
|
||||||
|
|
||||||
|
// descend: last-1 .. 0
|
||||||
|
return last - 1 - pos;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void MovementState::update() {
|
||||||
// stop crawling after duration
|
// stop crawling after duration
|
||||||
if (current_state == PlayerState::Crawl1 &&
|
if ((current_state == PlayerState::Crawl1 ||
|
||||||
|
current_state == PlayerState::Crawl2) &&
|
||||||
state_timer.elapsed_time() >= CRAWL_DURATION) {
|
state_timer.elapsed_time() >= CRAWL_DURATION) {
|
||||||
current_state = PlayerState::Walk1;
|
current_state = previous_state;
|
||||||
state_timer.stop();
|
state_timer.stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WalkingState::start_crawl() {
|
void MovementState::start_crawl(PlayerState crawl_type) {
|
||||||
current_state = PlayerState::Crawl1;
|
if (current_state != PlayerState::Crawl1 && current_state != PlayerState::Crawl2) {
|
||||||
|
previous_state = current_state;
|
||||||
|
}
|
||||||
|
|
||||||
|
current_state = crawl_type;
|
||||||
|
state_timer.stop();
|
||||||
state_timer.reset();
|
state_timer.reset();
|
||||||
state_timer.start();
|
state_timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WalkingState::toggle_walk_frame() {
|
void MovementState::set_state(PlayerState s) {
|
||||||
if (current_state == PlayerState::Walk1) {
|
if (current_state != s) {
|
||||||
current_state = PlayerState::Walk2;
|
walk_index = run_index = crawl1_index = crawl2_index = 0;
|
||||||
} else if (current_state == PlayerState::Walk2) {
|
phase = 0.0f;
|
||||||
current_state = PlayerState::Walk1;
|
}
|
||||||
|
current_state = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MovementState::set_motion_state_for_speed(int player_speed, int ground_speed) {
|
||||||
|
int relative = player_speed - ground_speed;
|
||||||
|
PlayerState new_state = (relative > 1) ? PlayerState::Run : PlayerState::Walk;
|
||||||
|
if (new_state != current_state) {
|
||||||
|
run_index = 0;
|
||||||
|
walk_index = 0;
|
||||||
|
}
|
||||||
|
current_state = new_state;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO THIS NEEDS REDESIGN FOR WALK/RUN
|
||||||
|
void MovementState::toggle_walk_frame(float player_speed, int tick_counter) {
|
||||||
|
switch (current_state) {
|
||||||
|
case PlayerState::Run:
|
||||||
|
run_index = compute_frame_index(CHARACTER_RUN_FRAME_COUNT, player_speed, tick_counter);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PlayerState::Walk:
|
||||||
|
walk_index = compute_frame_index(CHARACTER_WALK_FRAME_COUNT, player_speed, tick_counter);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PlayerState::Crawl1:
|
||||||
|
crawl1_index = compute_frame_index(CHARACTER_CRAWL1_FRAME_COUNT, player_speed, tick_counter);
|
||||||
|
break;
|
||||||
|
|
||||||
|
case PlayerState::Crawl2:
|
||||||
|
crawl2_index = compute_frame_index(CHARACTER_CRAWL2_FRAME_COUNT, player_speed, tick_counter);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CharacterFrame WalkingState::get_character_frame() const {
|
FrameSelection MovementState::get_frame_selection() const {
|
||||||
switch (current_state) {
|
FrameSelection f{};
|
||||||
case PlayerState::Walk1:
|
switch (current_state) {
|
||||||
return CharacterFrame::Walk1;
|
case PlayerState::Walk:
|
||||||
case PlayerState::Walk2:
|
f.movement = MovementType::Walk;
|
||||||
return CharacterFrame::Walk2;
|
f.frame_index = walk_index;
|
||||||
case PlayerState::Crawl1:
|
break;
|
||||||
return CharacterFrame::Crawl1;
|
case PlayerState::Run:
|
||||||
default:
|
f.movement = MovementType::Run;
|
||||||
return CharacterFrame::Walk1;
|
f.frame_index = run_index;
|
||||||
}
|
break;
|
||||||
|
case PlayerState::Crawl1:
|
||||||
|
f.movement = MovementType::Crawl1;
|
||||||
|
f.frame_index = crawl1_index;
|
||||||
|
break;
|
||||||
|
case PlayerState::Crawl2:
|
||||||
|
f.movement = MovementType::Crawl2;
|
||||||
|
f.frame_index = crawl2_index;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
f.movement = MovementType::Walk;
|
||||||
|
f.frame_index = walk_index;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return f;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,27 +1,37 @@
|
|||||||
// 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, Crawl1, Crawl2 };
|
||||||
|
|
||||||
class WalkingState {
|
struct FrameSelection {
|
||||||
|
MovementType movement;
|
||||||
|
int frame_index;
|
||||||
|
};
|
||||||
|
|
||||||
|
class MovementState {
|
||||||
private:
|
private:
|
||||||
PlayerState current_state = PlayerState::Walk1;
|
PlayerState current_state = PlayerState::Walk;
|
||||||
|
PlayerState previous_state = PlayerState::Walk;
|
||||||
Timer state_timer;
|
Timer state_timer;
|
||||||
|
int walk_index = 0; // cycles 0‑6
|
||||||
|
int run_index = 0; // cycles 0‑6
|
||||||
|
int crawl1_index = 0; // cycles 0‑1
|
||||||
|
int crawl2_index = 0; // cycles 0‑1
|
||||||
|
float phase = 0.0f;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// MOVED FROM loop.cpp
|
|
||||||
// Update state (handles crawl timeout)
|
|
||||||
void update();
|
void update();
|
||||||
|
void start_crawl(PlayerState crawl_type);
|
||||||
// State transitions
|
|
||||||
void start_crawl();
|
void set_motion_state_for_speed(int player_speed, int ground_speed);
|
||||||
void toggle_walk_frame();
|
void toggle_walk_frame(float player_speed, int tick_counter);
|
||||||
|
|
||||||
// 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;
|
|
||||||
|
void set_state(PlayerState s);
|
||||||
};
|
};
|
||||||
|
|||||||
33
semestralka1/src/hardware/button.cpp
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
// src/hardware/button.cpp
|
||||||
|
#include "button.h"
|
||||||
|
|
||||||
|
ButtonHandler::ButtonHandler(PinName pin, PinName led_pin) : button(pin), led(led_pin, 0) {
|
||||||
|
// ISR for button press (falling edge)
|
||||||
|
button.fall(callback(this, &ButtonHandler::on_pressed));
|
||||||
|
}
|
||||||
|
|
||||||
|
void ButtonHandler::on_pressed() {
|
||||||
|
pressed_flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ButtonEvent ButtonHandler::poll() {
|
||||||
|
static Timer blink_timer;
|
||||||
|
static bool blinking = false;
|
||||||
|
|
||||||
|
if (pressed_flag) {
|
||||||
|
pressed_flag = false;
|
||||||
|
led = 1;
|
||||||
|
blink_timer.reset();
|
||||||
|
blink_timer.start();
|
||||||
|
blinking = true;
|
||||||
|
return ButtonEvent::Pressed;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (blinking && blink_timer.elapsed_time() >= 500ms) {
|
||||||
|
led = 0;
|
||||||
|
blink_timer.stop();
|
||||||
|
blinking = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ButtonEvent::None;
|
||||||
|
}
|
||||||
23
semestralka1/src/hardware/button.h
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
// src/hardware/button.h
|
||||||
|
#pragma once
|
||||||
|
#include "mbed.h"
|
||||||
|
|
||||||
|
enum class ButtonEvent {
|
||||||
|
None = 0,
|
||||||
|
Pressed = 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
class ButtonHandler {
|
||||||
|
private:
|
||||||
|
InterruptIn button;
|
||||||
|
DigitalOut led;
|
||||||
|
volatile bool pressed_flag = false;
|
||||||
|
volatile bool blink_flag = false;
|
||||||
|
|
||||||
|
void on_pressed();
|
||||||
|
|
||||||
|
public:
|
||||||
|
ButtonHandler(PinName pin = BUTTON1, PinName led_pin = LED1);
|
||||||
|
|
||||||
|
ButtonEvent poll();
|
||||||
|
};
|
||||||
@@ -1,49 +0,0 @@
|
|||||||
// 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;
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
// 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; }
|
|
||||||
};
|
|
||||||
@@ -21,6 +21,5 @@ int main(void) {
|
|||||||
serial_port.set_format(8, BufferedSerial::None, 1);
|
serial_port.set_format(8, BufferedSerial::None, 1);
|
||||||
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
|
render_loop(6);
|
||||||
render_loop(4);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,15 @@
|
|||||||
// src/render/background.cpp
|
// src/render/background.cpp
|
||||||
|
|
||||||
|
#include "background.h"
|
||||||
#include "../assets/background_frame.h"
|
#include "../assets/background_frame.h"
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
void draw_mask(const char *unused_filename, int shift, const char *text) {
|
void draw_mask(const char *unused_filename, int shift) {
|
||||||
const int view_width = 90;
|
|
||||||
const int view_height = 16;
|
|
||||||
|
|
||||||
// Terminal clear + home
|
// Terminal clear + home
|
||||||
printf("\033[2J\033[H");
|
printf("\033[2J\033[H");
|
||||||
|
|
||||||
for (int i = 0; i < view_height && i < VIEW_HEIGHT; i++) {
|
for (int i = 0; i < VIEW_HEIGHT && i < VIEW_HEIGHT; i++) {
|
||||||
const char *row = BACKGROUND_MASK[i];
|
const char *row = BACKGROUND_MASK[i];
|
||||||
int width = strlen(row);
|
int width = strlen(row);
|
||||||
if (width == 0) {
|
if (width == 0) {
|
||||||
@@ -21,15 +19,11 @@ void draw_mask(const char *unused_filename, int shift, const char *text) {
|
|||||||
|
|
||||||
int start = shift % width;
|
int start = shift % width;
|
||||||
|
|
||||||
for (int j = 0; j < view_width; j++) {
|
for (int j = 0; j < VIEW_WIDTH; j++) {
|
||||||
printf("%c", row[(start + j) % width]);
|
printf("%c", row[(start + j) % width]);
|
||||||
}
|
}
|
||||||
printf("\r\n");
|
printf("\r\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (text && text[0] != '\0') {
|
|
||||||
printf("\r\n[RX] %s\r\n", text);
|
|
||||||
}
|
|
||||||
|
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
// src/render/background.h
|
// src/render/background.h
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
constexpr int VIEW_WIDTH = 90;
|
||||||
|
|
||||||
// Draws the ASCII art background with optional message
|
// Draws the ASCII art background with optional message
|
||||||
void draw_mask(const char *unused_filename, int shift, const char *text = nullptr);
|
void draw_mask(const char *unused_filename, int shift);
|
||||||
|
|||||||
@@ -5,41 +5,79 @@
|
|||||||
#include "background.h"
|
#include "background.h"
|
||||||
#include "mbed.h"
|
#include "mbed.h"
|
||||||
#include "player.h"
|
#include "player.h"
|
||||||
|
#include "background.h"
|
||||||
#include "../game/state.h"
|
#include "../game/state.h"
|
||||||
#include "../game/animation.h"
|
#include "../game/animation.h"
|
||||||
#include "../hardware/uart.h"
|
#include "../game/collision.h"
|
||||||
#include "../assets/character_frames.h"
|
#include "../hardware/button.h"
|
||||||
|
#include "../render/player.h"
|
||||||
|
#include "../render/obstacle.h"
|
||||||
|
#include "../timing/speed_controller.h"
|
||||||
|
#include "../timing/movement_controller.h"
|
||||||
|
#include "../game/obstacle_system.h"
|
||||||
|
|
||||||
extern BufferedSerial serial_port;
|
|
||||||
extern DigitalOut led;
|
extern DigitalOut led;
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
constexpr int PLAYER_X = 9;
|
// constexpr int PLAYER_X = 9;
|
||||||
|
constexpr int PLAYER_X = 29;
|
||||||
constexpr int PLAYER_Y = 6;
|
constexpr int PLAYER_Y = 6;
|
||||||
|
|
||||||
void draw_mask(const char *unused_filename, int shift, const char *text);
|
|
||||||
|
|
||||||
void render_loop(int speed) {
|
void render_loop(int speed) {
|
||||||
WalkingState player_state;
|
MovementState player_state;
|
||||||
AnimationController animation;
|
AnimationController animation;
|
||||||
UartReader uart(serial_port);
|
ButtonHandler button;
|
||||||
|
MovementController mover(PLAYER_X, VIEW_WIDTH);
|
||||||
|
SpeedController timing;
|
||||||
|
timing.set_ground_speed(speed);
|
||||||
|
|
||||||
CharacterPosition pos = {PLAYER_X, PLAYER_Y};
|
CharacterPosition pos = {PLAYER_X, PLAYER_Y};
|
||||||
|
|
||||||
const char *bg_file = "background_dark_inverted.txt";
|
const char *bg_file = "background_dark_inverted.txt";
|
||||||
bool need_redraw = false;
|
bool need_redraw = false;
|
||||||
|
|
||||||
while (true) {
|
int anim_tick_counter = 0;
|
||||||
need_redraw = false;
|
int tick_counter = 0;
|
||||||
UartEvent uart_event = uart.poll();
|
int player_speed = 6;
|
||||||
|
bool game_over = false;
|
||||||
|
|
||||||
if (uart_event == UartEvent::MessageUpdate) {
|
player_state.set_state(PlayerState::Walk);
|
||||||
need_redraw = true;
|
|
||||||
|
CrawlObstacleType type = CrawlObstacleType::Crawl1;
|
||||||
|
int start_x = VIEW_WIDTH + 10;
|
||||||
|
spawn_obstacle(type, start_x);
|
||||||
|
|
||||||
|
Timer spawn_timer;
|
||||||
|
spawn_timer.start();
|
||||||
|
|
||||||
|
while (!game_over) {
|
||||||
|
tick_counter++;
|
||||||
|
|
||||||
|
mover.update_position(player_speed, timing.get_ground_speed());
|
||||||
|
pos.x = mover.get_position();
|
||||||
|
|
||||||
|
if (spawn_timer.elapsed_time() > 2s) {
|
||||||
|
spawn_timer.reset();
|
||||||
|
CrawlObstacleType s_type = CrawlObstacleType::Crawl1;
|
||||||
|
int spawn_x = VIEW_WIDTH;
|
||||||
|
int idx = spawn_obstacle(s_type, spawn_x);
|
||||||
|
if (idx >= 0) {
|
||||||
|
obstacle_pool[idx].active = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (uart_event == UartEvent::Triggered) {
|
need_redraw = false;
|
||||||
// start crawl frame for x time
|
ButtonEvent button_event = button.poll();
|
||||||
player_state.start_crawl();
|
|
||||||
|
if (button_event == ButtonEvent::Pressed) {
|
||||||
|
PlayerState current = player_state.get_state();
|
||||||
|
|
||||||
|
if (current == PlayerState::Walk || current == PlayerState::Run) {
|
||||||
|
player_state.start_crawl(PlayerState::Crawl1);
|
||||||
|
} else if (current == PlayerState::Crawl1) {
|
||||||
|
player_state.start_crawl(PlayerState::Crawl2);
|
||||||
|
}
|
||||||
|
|
||||||
need_redraw = true;
|
need_redraw = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -47,20 +85,45 @@ void render_loop(int speed) {
|
|||||||
player_state.update();
|
player_state.update();
|
||||||
|
|
||||||
if (animation.tick(speed)) {
|
if (animation.tick(speed)) {
|
||||||
|
anim_tick_counter++;
|
||||||
|
int ground_speed = timing.get_ground_speed();
|
||||||
|
for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
|
if (obstacle_pool[i].active) {
|
||||||
|
obstacle_pool[i].data.x -= ground_speed;
|
||||||
|
// deactivate when offscreen
|
||||||
|
if (obstacle_pool[i].data.x + obstacle_pool[i].data.width < 0)
|
||||||
|
obstacle_pool[i].active = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
need_redraw = true;
|
need_redraw = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (need_redraw) {
|
if (need_redraw) {
|
||||||
draw_mask(bg_file, animation.get_shift(), uart.get_message());
|
player_state.toggle_walk_frame(player_speed, anim_tick_counter);
|
||||||
CharacterFrame player_frame = player_state.get_character_frame();
|
draw_mask(bg_file, animation.get_shift());
|
||||||
|
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
|
// for (int i = 0; i < MAX_OBSTACLES; i++) {
|
||||||
if (player_state.get_state() != PlayerState::Crawl1) {
|
// if (!obstacle_pool[i].active)
|
||||||
player_state.toggle_walk_frame();
|
// continue;
|
||||||
}
|
|
||||||
|
// draw_obstacle(obstacle_pool[i].data.x, obstacle_pool[i].data.y,
|
||||||
|
// obstacle_pool[i].type);
|
||||||
|
|
||||||
|
// if (check_collision(pos, frame.movement, obstacle_pool[i].data)) {
|
||||||
|
// printf("\033[2J\033[H");
|
||||||
|
// printf("GAME OVER\r\n");
|
||||||
|
// game_over = true;
|
||||||
|
// break;
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (game_over)
|
||||||
|
break;
|
||||||
|
|
||||||
ThisThread::sleep_for(50ms);
|
ThisThread::sleep_for(50ms);
|
||||||
}
|
}
|
||||||
|
|||||||
56
semestralka1/src/render/obstacle.cpp
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
// src/render/obstacle.cpp
|
||||||
|
#include "obstacle.h"
|
||||||
|
#include "../assets/obstacle_crawl_frames.h"
|
||||||
|
#include "../assets/background_frame.h"
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
// Draws an obstacle using bottom‑based world coordinates.
|
||||||
|
// x = left edge of the collision box in world space
|
||||||
|
// y = bottom edge of the collision box in world space (0 = ground)
|
||||||
|
void draw_obstacle(int x, int y, CrawlObstacleType type) {
|
||||||
|
const char **obstacle_frame = nullptr;
|
||||||
|
int frame_total_height = 0;
|
||||||
|
|
||||||
|
// Collision‑model parameters for this obstacle
|
||||||
|
int collision_top_offset = 0; // rows from visual top to top of collision box
|
||||||
|
int collision_height = 0; // rows tall
|
||||||
|
int collision_left_offset = 0; // columns from visual left to collision left
|
||||||
|
|
||||||
|
switch (type) {
|
||||||
|
case CrawlObstacleType::Crawl1:
|
||||||
|
obstacle_frame = OBSTACLE_CRAWL1_FRAME;
|
||||||
|
frame_total_height = OBSTACLE_CRAWL1_FRAME_HEIGHT;
|
||||||
|
collision_top_offset = OBSTACLE_CRAWL1_COLLISION_TOP_OFFSET;
|
||||||
|
collision_height = OBSTACLE_CRAWL1_COLLISION_HEIGHT;
|
||||||
|
collision_left_offset = OBSTACLE_CRAWL1_COLLISION_LEFT_OFFSET;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case CrawlObstacleType::Crawl2:
|
||||||
|
obstacle_frame = OBSTACLE_CRAWL2_FRAME;
|
||||||
|
frame_total_height = OBSTACLE_CRAWL2_FRAME_HEIGHT;
|
||||||
|
collision_top_offset = 0;
|
||||||
|
collision_height = OBSTACLE_CRAWL2_COLLISION_HEIGHT;
|
||||||
|
collision_left_offset = OBSTACLE_CRAWL2_COLLISION_LEFT_OFFSET;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
return; // no rendering for invalid type
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert bottom‑based (world) coordinates to screen coordinates.
|
||||||
|
// Y of the collision bottom is given; translate to where to start drawing the visual frame.
|
||||||
|
const int frame_row_index_bottom = collision_top_offset + collision_height - 1;
|
||||||
|
const int frame_top_world_y = y + frame_row_index_bottom;
|
||||||
|
const int frame_top_screen_row = VIEW_HEIGHT - frame_top_world_y;
|
||||||
|
|
||||||
|
// X of the collision box is given; shift left to the visual frame's starting column.
|
||||||
|
const int frame_screen_x = x - collision_left_offset;
|
||||||
|
|
||||||
|
for (int i = 0; i < frame_total_height; i++) {
|
||||||
|
const int screen_row = frame_top_screen_row + i;
|
||||||
|
if (screen_row < 1 || screen_row > VIEW_HEIGHT) continue;
|
||||||
|
printf("\033[%d;%dH%s", screen_row, frame_screen_x + 1, obstacle_frame[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
fflush(stdout);
|
||||||
|
}
|
||||||
7
semestralka1/src/render/obstacle.h
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
// src/render/obstacle.h
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../assets/obstacle_crawl_frames.h"
|
||||||
|
|
||||||
|
// Draw the obstacle ASCII block starting at given (x, y)
|
||||||
|
void draw_obstacle(int x, int y, CrawlObstacleType type);
|
||||||
@@ -1,28 +1,46 @@
|
|||||||
// 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::Crawl1:
|
||||||
|
total_frames = CHARACTER_CRAWL1_FRAME_COUNT;
|
||||||
|
if (frame_index < 0 || frame_index >= total_frames)
|
||||||
|
frame_index = 0;
|
||||||
|
character = CHARACTER_CRAWL1_FRAMES[frame_index];
|
||||||
|
character_height = CHARACTER_CRAWL1_FRAME_HEIGHT;
|
||||||
break;
|
break;
|
||||||
default:
|
|
||||||
character_height = CHARACTER_HEIGHT_1;
|
case MovementType::Crawl2:
|
||||||
|
total_frames = CHARACTER_CRAWL2_FRAME_COUNT;
|
||||||
|
if (frame_index < 0 || frame_index >= total_frames)
|
||||||
|
frame_index = 0;
|
||||||
|
character = CHARACTER_CRAWL2_FRAMES[frame_index];
|
||||||
|
character_height = CHARACTER_CRAWL2_FRAME_HEIGHT;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,12 @@
|
|||||||
// src/render/player.h
|
// src/render/player.h
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "../assets/character_frames.h"
|
|
||||||
|
enum class MovementType {
|
||||||
|
Walk,
|
||||||
|
Run,
|
||||||
|
Crawl1,
|
||||||
|
Crawl2
|
||||||
|
};
|
||||||
|
|
||||||
// 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,33 @@
|
|||||||
// 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::Crawl1:
|
||||||
|
character_height = CHARACTER_CRAWL1_FRAME_HEIGHT;
|
||||||
|
break;
|
||||||
|
case MovementType::Crawl2:
|
||||||
|
character_height = CHARACTER_CRAWL2_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);
|
||||||
|
|||||||
29
semestralka1/src/timing/movement_controller.h
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
// src/timing/movement_controller.h
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
#include "mbed.h"
|
||||||
|
|
||||||
|
// Handles position of the player relative to ground movement
|
||||||
|
class MovementController {
|
||||||
|
private:
|
||||||
|
float x_pos;
|
||||||
|
const int view_width;
|
||||||
|
|
||||||
|
// Scale factor for translating speed difference to pixel movement
|
||||||
|
const float motion_scale = 0.2f;
|
||||||
|
|
||||||
|
public:
|
||||||
|
MovementController(int start_x, int width) : x_pos(static_cast<float>(start_x)), view_width(width) {}
|
||||||
|
|
||||||
|
void reset(int start_x) { x_pos = static_cast<float>(start_x); }
|
||||||
|
|
||||||
|
void update_position(int player_speed, int ground_speed) {
|
||||||
|
int delta = player_speed - ground_speed;
|
||||||
|
x_pos += static_cast<float>(delta) * motion_scale;
|
||||||
|
|
||||||
|
if (x_pos < 0) x_pos = 0;
|
||||||
|
if (x_pos > (VIEW_WIDTH - 10)) x_pos = static_cast<float>(VIEW_WIDTH - 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_position() const { return static_cast<int>(x_pos); }
|
||||||
|
};
|
||||||
34
semestralka1/src/timing/speed_controller.cpp
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
// src/timing/speed_controller.cpp
|
||||||
|
#include "speed_controller.h"
|
||||||
|
|
||||||
|
void SpeedController::set_ground_speed(int spd) {
|
||||||
|
if (spd < 0) spd = 0;
|
||||||
|
if (spd > 30) spd = 30;
|
||||||
|
ground_speed = spd;
|
||||||
|
}
|
||||||
|
|
||||||
|
int SpeedController::frame_advance_for(int object_speed, int tick_counter) const {
|
||||||
|
if (object_speed <= 0) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ground_speed == 0) {
|
||||||
|
return 1 + object_speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
const int delta = object_speed - ground_speed;
|
||||||
|
|
||||||
|
if (delta > 0) {
|
||||||
|
return 1 + delta;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (delta == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
float ratio_f = static_cast<float>(ground_speed) / object_speed;
|
||||||
|
int ratio = static_cast<int>(ratio_f + 0.3f);
|
||||||
|
if (ratio < 1) ratio = 1;
|
||||||
|
|
||||||
|
return (tick_counter % ratio == 0) ? 1 : 0;
|
||||||
|
}
|
||||||
17
semestralka1/src/timing/speed_controller.h
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
// src/timing/speed_controller.h
|
||||||
|
#pragma once
|
||||||
|
#include "mbed.h"
|
||||||
|
|
||||||
|
class SpeedController {
|
||||||
|
private:
|
||||||
|
int ground_speed = 1;
|
||||||
|
|
||||||
|
public:
|
||||||
|
SpeedController() = default;
|
||||||
|
|
||||||
|
void set_ground_speed(int spd);
|
||||||
|
int get_ground_speed() const { return ground_speed; }
|
||||||
|
|
||||||
|
// Calculates how many frames to advance for current tick.
|
||||||
|
int frame_advance_for(int object_speed, int tick_counter) const;
|
||||||
|
};
|
||||||