Compare commits
6 Commits
v1.3.0
...
cf07c9c47a
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf07c9c47a | ||
|
|
ace0e2d706 | ||
|
|
8cb6892d30 | ||
|
|
de6facff03 | ||
|
|
2753aed573 | ||
|
|
0ff2be6564 |
@@ -1,6 +1,94 @@
|
|||||||
// src/game/state.cpp
|
// src/game/state.cpp
|
||||||
#include "state.h"
|
#include "state.h"
|
||||||
#include "../render/player.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>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
// 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 WalkingState::update() {
|
void WalkingState::update() {
|
||||||
// stop crawling after duration
|
// stop crawling after duration
|
||||||
@@ -17,27 +105,63 @@ void WalkingState::start_crawl() {
|
|||||||
state_timer.start();
|
state_timer.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO THIS NEEDS REDESIGN ACCORDING TO SPEED
|
void WalkingState::set_state(PlayerState s) {
|
||||||
void WalkingState::toggle_walk_frame() {
|
if (current_state != s) {
|
||||||
if (++walk_index >= 7) walk_index = 0;
|
walk_index = run_index = crawl_index = 0;
|
||||||
|
phase = 0.0f;
|
||||||
|
}
|
||||||
|
current_state = s;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WalkingState::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 WalkingState::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::Crawl:
|
||||||
|
crawl_index =
|
||||||
|
compute_frame_index(CHARACTER_CRAWL_FRAME_COUNT, player_speed, tick_counter);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
FrameSelection WalkingState::get_frame_selection() const {
|
FrameSelection WalkingState::get_frame_selection() const {
|
||||||
FrameSelection f{MovementType::Walk, 0};
|
FrameSelection f{};
|
||||||
|
switch (current_state) {
|
||||||
switch (current_state) {
|
case PlayerState::Walk:
|
||||||
case PlayerState::Walk:
|
f.movement = MovementType::Walk;
|
||||||
f.movement = MovementType::Walk;
|
f.frame_index = walk_index;
|
||||||
f.frame_index = walk_index;
|
break;
|
||||||
break;
|
case PlayerState::Run:
|
||||||
case PlayerState::Run:
|
f.movement = MovementType::Run;
|
||||||
f.movement = MovementType::Run;
|
f.frame_index = run_index;
|
||||||
f.frame_index = run_index;
|
break;
|
||||||
break;
|
case PlayerState::Crawl:
|
||||||
case PlayerState::Crawl:
|
f.movement = MovementType::Crawl;
|
||||||
f.movement = MovementType::Crawl;
|
f.frame_index = crawl_index;
|
||||||
f.frame_index = crawl_index;
|
break;
|
||||||
break;
|
default:
|
||||||
}
|
f.movement = MovementType::Walk;
|
||||||
return f;
|
f.frame_index = walk_index;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return f;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,12 +19,17 @@ private:
|
|||||||
int walk_index = 0; // cycles 0‑6
|
int walk_index = 0; // cycles 0‑6
|
||||||
int run_index = 0; // cycles 0‑6
|
int run_index = 0; // cycles 0‑6
|
||||||
int crawl_index = 0; // cycles 0‑1
|
int crawl_index = 0; // cycles 0‑1
|
||||||
|
float phase = 0.0f;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void update();
|
void update();
|
||||||
void start_crawl();
|
void start_crawl();
|
||||||
void toggle_walk_frame();
|
|
||||||
|
void set_motion_state_for_speed(int player_speed, int ground_speed);
|
||||||
|
void toggle_walk_frame(float player_speed, int tick_counter);
|
||||||
|
|
||||||
FrameSelection get_frame_selection() const;
|
FrameSelection get_frame_selection() const;
|
||||||
PlayerState get_state() const { return current_state; }
|
PlayerState get_state() const { return current_state; }
|
||||||
|
|
||||||
|
void set_state(PlayerState s);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -22,5 +22,5 @@ int main(void) {
|
|||||||
printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);
|
printf("Baud: %d, Format: 8-N-1\r\n", BAUD_RATE);
|
||||||
|
|
||||||
// Just call into render feature
|
// Just call into render feature
|
||||||
render_loop(1);
|
render_loop(6);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 char *text) {
|
||||||
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,7 +19,7 @@ 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");
|
||||||
|
|||||||
@@ -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, const char *text = nullptr);
|
||||||
|
|||||||
@@ -9,12 +9,15 @@
|
|||||||
#include "../game/animation.h"
|
#include "../game/animation.h"
|
||||||
#include "../hardware/uart.h"
|
#include "../hardware/uart.h"
|
||||||
#include "../render/player.h"
|
#include "../render/player.h"
|
||||||
|
#include "../timing/speed_controller.h"
|
||||||
|
#include "../timing/movement_controller.h"
|
||||||
|
|
||||||
extern BufferedSerial serial_port;
|
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 draw_mask(const char *unused_filename, int shift, const char *text);
|
||||||
@@ -23,13 +26,26 @@ void render_loop(int speed) {
|
|||||||
WalkingState player_state;
|
WalkingState player_state;
|
||||||
AnimationController animation;
|
AnimationController animation;
|
||||||
UartReader uart(serial_port);
|
UartReader uart(serial_port);
|
||||||
|
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;
|
||||||
|
|
||||||
|
int anim_tick_counter = 0;
|
||||||
|
int tick_counter = 0;
|
||||||
|
int player_speed = 6;
|
||||||
|
|
||||||
|
player_state.set_state(PlayerState::Run);
|
||||||
while (true) {
|
while (true) {
|
||||||
|
tick_counter++;
|
||||||
|
|
||||||
|
mover.update_position(player_speed, timing.get_ground_speed());
|
||||||
|
pos.x = mover.get_position();
|
||||||
|
|
||||||
need_redraw = false;
|
need_redraw = false;
|
||||||
UartEvent uart_event = uart.poll();
|
UartEvent uart_event = uart.poll();
|
||||||
|
|
||||||
@@ -47,20 +63,18 @@ void render_loop(int speed) {
|
|||||||
player_state.update();
|
player_state.update();
|
||||||
|
|
||||||
if (animation.tick(speed)) {
|
if (animation.tick(speed)) {
|
||||||
|
anim_tick_counter++;
|
||||||
need_redraw = true;
|
need_redraw = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (need_redraw) {
|
if (need_redraw) {
|
||||||
|
player_state.toggle_walk_frame(player_speed, anim_tick_counter);
|
||||||
draw_mask(bg_file, animation.get_shift(), uart.get_message());
|
draw_mask(bg_file, animation.get_shift(), uart.get_message());
|
||||||
FrameSelection frame = player_state.get_frame_selection();
|
FrameSelection frame = player_state.get_frame_selection();
|
||||||
|
|
||||||
CharacterPosition draw_pos = get_aligned_frame_position(pos, frame.movement, frame.frame_index);
|
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);
|
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);
|
ThisThread::sleep_for(50ms);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
29
semestralka1/src/timing/movement_controller.h
Normal file
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
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;
|
||||||
|
}
|
||||||
19
semestralka1/src/timing/speed_controller.h
Normal file
19
semestralka1/src/timing/speed_controller.h
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
// 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.
|
||||||
|
// Takes object’s current speed and a global tick counter
|
||||||
|
// to automatically handle slower "wait" behavior.
|
||||||
|
int frame_advance_for(int object_speed, int tick_counter) const;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user