Skip to content

Commit b1f6e3b

Browse files
committed
esp32-s3: fix build for embedded
1 parent 8eb8d47 commit b1f6e3b

File tree

4 files changed

+42
-16
lines changed

4 files changed

+42
-16
lines changed

esp32-s3/src/embedded_systems/player_input.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use spooky_core::systems::player_input::PlayerInputEvent;
1+
use spooky_core::events::player::PlayerInputEvent;
22
use embedded_hal::i2c::I2c;
33
use bevy_ecs::prelude::*;
44
use core::fmt::Debug;

esp32-s3/src/embedded_systems/render.rs

+33-13
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ use spooky_core::systems::setup::TextureAssets;
1616
/// Render the maze tile map, coins, and the player ghost into the off‑screen framebuffer,
1717
/// then flush it to the physical display.
1818
///
19-
/// Drawing order is:
19+
/// The camera is simulated by calculating an offset so that the player (whose world
20+
/// coordinates are in PlayerPosition) is always centered on the display.
21+
/// The drawing order is:
2022
/// 1. Maze tile map (background)
21-
/// 2. Coins (drawn so that they’re centered on the tile)
22-
/// 3. (Other elements, such as the player, might be drawn separately)
23+
/// 2. Coins (drawn using the tile’s top‑left as anchor)
24+
/// 3. The player ghost (drawn using the tile’s top‑left as anchor)
2325
pub fn render_system(
2426
mut display_res: NonSendMut<crate::DisplayResource>,
2527
mut fb_res: ResMut<crate::FrameBufferResource>,
@@ -35,11 +37,16 @@ pub fn render_system(
3537
let tile_w = maze.tile_width as i32;
3638
let tile_h = maze.tile_height as i32;
3739

40+
// Compute the camera offset so that the player is centered.
41+
// For example, if the display is 320x240 then the center is (160,120).
42+
let display_center_x = (crate::LCD_H_RES as i32) / 2;
43+
let display_center_y = (crate::LCD_V_RES as i32) / 2;
44+
let offset_x = player_pos.x as i32 - display_center_x;
45+
let offset_y = player_pos.y as i32 - display_center_y;
46+
3847
// --- Render the maze tile map (background) ---
3948
for ty in 0..maze.height as i32 {
4049
for tx in 0..maze.width as i32 {
41-
// Since our maze data is stored bottom‑up, we can use the current (tx, ty)
42-
// directly if valid_coordinates are computed that way.
4350
let tile_index = (ty * maze.width as i32 + tx) as usize;
4451
let bmp_opt = match maze.data[tile_index] {
4552
1 => texture_assets.wall.as_ref(),
@@ -49,10 +56,13 @@ pub fn render_system(
4956
};
5057

5158
if let Some(bmp) = bmp_opt {
52-
// Compute the top‑left pixel coordinate of this tile.
53-
let x = left + tx * tile_w;
54-
let y = bottom + ty * tile_h;
55-
let pos = Point::new(x, y);
59+
// Compute the tile's world coordinate (top‑left of the tile)
60+
let world_x = left + tx * tile_w;
61+
let world_y = bottom + ty * tile_h;
62+
// Compute the screen coordinate by subtracting the camera offset.
63+
let screen_x = world_x - offset_x;
64+
let screen_y = world_y - offset_y;
65+
let pos = Point::new(screen_x, screen_y);
5666
Image::new(bmp, pos)
5767
.draw(&mut fb_res.frame_buf)
5868
.unwrap();
@@ -64,17 +74,27 @@ pub fn render_system(
6474
for coin in &maze.coins {
6575
if coin.x != -1 && coin.y != -1 {
6676
if let Some(bmp) = texture_assets.coin.as_ref() {
67-
// The valid coordinate is the center of the tile.
68-
// Subtract half the tile dimensions so the coin is centered.
69-
let pos = Point::new(coin.x - tile_w / 2, coin.y - tile_h / 2);
77+
// Use the coin's coordinate as the top‑left corner.
78+
let screen_x = coin.x - offset_x;
79+
let screen_y = coin.y - offset_y;
80+
let pos = Point::new(screen_x, screen_y);
7081
Image::new(bmp, pos)
7182
.draw(&mut fb_res.frame_buf)
7283
.unwrap();
7384
}
7485
}
7586
}
7687

77-
// (You can add additional drawing for the player ghost here if needed.)
88+
// --- Render the player ghost ---
89+
if let Some(bmp) = texture_assets.ghost.as_ref() {
90+
// Use the player position as the top‑left coordinate.
91+
let screen_x = player_pos.x as i32 - offset_x;
92+
let screen_y = player_pos.y as i32 - offset_y;
93+
let pos = Point::new(screen_x, screen_y);
94+
Image::new(bmp, pos)
95+
.draw(&mut fb_res.frame_buf)
96+
.unwrap();
97+
}
7898

7999
// Flush the completed framebuffer to the physical display.
80100
let area = Rectangle::new(Point::zero(), fb_res.frame_buf.size());

esp32-s3/src/main.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#![no_main]
33

44
extern crate alloc;
5-
use spooky_core::systems::player_input::PlayerInputEvent;
5+
use spooky_core::systems::process_player_input::process_player_input;
6+
use spooky_core::events::player::PlayerInputEvent;
67
use alloc::boxed::Box;
78

89
use core::fmt::Write;
@@ -196,11 +197,15 @@ fn main() -> ! {
196197
app.insert_resource(FrameBufferResource::new());
197198
// Register the custom event.
198199
app.add_event::<PlayerInputEvent>();
200+
app.add_event::<spooky_core::events::coin::CoinCollisionEvent>();
201+
199202

200203
// Use spooky_core's setup system to spawn the maze, coins, and player.
201204
app.add_systems(Startup, systems::setup::setup);
202205
// Add game logic system.
203206
app.add_systems(Update, spooky_core::systems::game_logic::update_game);
207+
app.add_systems(Update, spooky_core::systems::coin_collision::detect_coin_collision);
208+
app.add_systems(Update, spooky_core::systems::coin_collision::remove_coin_on_collision);
204209
// Add the embedded render system.
205210
app.add_systems(Update, render_system);
206211
// Add the accelerometer dispatch system (from the embedded module).
@@ -210,7 +215,7 @@ fn main() -> ! {
210215
);
211216

212217
// Add the common event processing system.
213-
app.add_systems(Update, spooky_core::systems::player_input::process_player_input);
218+
app.add_systems(Update, process_player_input);
214219

215220
let mut loop_delay = Delay::new();
216221
loop {

spooky-core/src/systems/process_player_input.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::events::player::PlayerInputEvent;
55

66
// Use our unified transform type alias.
77
use crate::transform::SpookyTransform;
8+
use log::info;
89

910
#[cfg(feature = "std")]
1011
type SpTransform = SpookyTransform;

0 commit comments

Comments
 (0)