calculations
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// src/render/loop.cpp
|
||||
#include "loop.h"
|
||||
#include "../background_dark_inverted.h"
|
||||
#include "player_positioning.h"
|
||||
#include "background.h"
|
||||
#include "mbed.h"
|
||||
#include "player.h"
|
||||
@@ -25,11 +26,6 @@ struct WalkingState {
|
||||
Timer state_timer;
|
||||
};
|
||||
|
||||
struct PlayerPosition {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
static char rx_buffer[BUFFER_SIZE];
|
||||
static char message[BUFFER_SIZE];
|
||||
static bool message_active = false;
|
||||
@@ -133,7 +129,7 @@ void render_loop(int speed) {
|
||||
anim_timer.start();
|
||||
int shift = 0;
|
||||
|
||||
PlayerPosition pos = {9, 3};
|
||||
PlayerPosition pos = {9, 9};
|
||||
|
||||
const char *bg_file = "background_dark_inverted.txt";
|
||||
bool need_redraw = false;
|
||||
@@ -162,7 +158,8 @@ void render_loop(int speed) {
|
||||
if (need_redraw) {
|
||||
draw_mask(bg_file, shift, message_active ? message : nullptr);
|
||||
int player_frame = get_player_frame(player_state.current_state);
|
||||
draw_player(pos.x, pos.y, player_frame);
|
||||
PlayerPosition draw_pos = get_aligned_frame_position(pos, player_frame);
|
||||
draw_player(draw_pos.x, draw_pos.y, player_frame);
|
||||
|
||||
// alternate between frame 0 and 1 when not crawling
|
||||
if (player_state.current_state != PlayerState::Crawl1) {
|
||||
|
||||
25
semestralka1/src/render/player_positioning.cpp
Normal file
25
semestralka1/src/render/player_positioning.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
// src/render/player_positioning.cpp
|
||||
#include "player_positioning.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;
|
||||
|
||||
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;
|
||||
|
||||
// The X coordinate stays the same (left edge aligned)
|
||||
// The Y coordinate moves upward by the sprite height,
|
||||
// because the pivot is bottom-left, but rendering starts from top-left
|
||||
PlayerPosition draw_pos;
|
||||
draw_pos.x = base.x;
|
||||
draw_pos.y = base.y - sprite_height + 1; // align to feet baseline
|
||||
return draw_pos;
|
||||
}
|
||||
11
semestralka1/src/render/player_positioning.h
Normal file
11
semestralka1/src/render/player_positioning.h
Normal file
@@ -0,0 +1,11 @@
|
||||
// src/render/player_positioning.h
|
||||
#pragma once
|
||||
#include "player_mask.h"
|
||||
|
||||
struct PlayerPosition {
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
|
||||
// Calculates drawing position relative to bottom-left pivot
|
||||
PlayerPosition get_aligned_frame_position(PlayerPosition base, int frame_index);
|
||||
Reference in New Issue
Block a user