diff --git a/assets/img/smiley.bmp b/assets/img/smiley.bmp new file mode 100644 index 0000000..d998496 Binary files /dev/null and b/assets/img/smiley.bmp differ diff --git a/desktop/src/main.rs b/desktop/src/main.rs index 4cef0ae..69c94fd 100644 --- a/desktop/src/main.rs +++ b/desktop/src/main.rs @@ -13,7 +13,7 @@ use embedded_graphics_framebuf::{FrameBuf}; use std::time::Duration; -use spooky_core::{ spritebuf::SpriteBuf, engine::Engine }; +use spooky_core::{ spritebuf::SpriteBuf, engine::Engine, engine::Action::{ Up, Down, Left, Right, Teleport, PlaceDynamite } }; pub struct Universe { engine: Engine, @@ -34,38 +34,37 @@ impl > Universe } pub fn move_up(&mut self) { - self.engine.move_up(); + self.engine.action(Up); } pub fn move_down(&mut self) { - self.engine.move_down(); + self.engine.action(Down); } pub fn move_left(&mut self) { - self.engine.move_left(); + self.engine.action(Left); } pub fn move_right(&mut self) { - self.engine.move_right(); + self.engine.action(Right); } pub fn teleport(&mut self) { - self.engine.teleport(); + self.engine.action(Teleport); } pub fn initialize(&mut self) { self.engine.initialize(); + self.engine.start(); } pub fn place_dynamite(&mut self) { - self.engine.place_dynamite(); + self.engine.action(PlaceDynamite); } pub fn render_frame(&mut self) -> &D { self.engine.tick(); self.engine.draw() - // display.flush().unwrap(); - } } diff --git a/esp-wrover-kit/src/main.rs b/esp-wrover-kit/src/main.rs index c25c08b..f2590b2 100644 --- a/esp-wrover-kit/src/main.rs +++ b/esp-wrover-kit/src/main.rs @@ -14,12 +14,6 @@ use embedded_graphics::{ #[cfg(feature = "esp32")] use esp32_hal as hal; -#[cfg(feature = "esp32c3")] -use esp32c3_hal as hal; -#[cfg(feature = "esp32s2")] -use esp32s2_hal as hal; -#[cfg(feature = "esp32s3")] -use esp32s3_hal as hal; use hal::{ clock::{ClockControl, CpuClock}, @@ -40,7 +34,7 @@ use xtensa_lx_rt::entry; use embedded_graphics::pixelcolor::Rgb565; -use spooky_core::{engine::Engine, spritebuf::SpriteBuf}; +use spooky_core::{engine::Engine, spritebuf::SpriteBuf, engine::Action::{ Up, Down, Left, Right, Teleport, PlaceDynamite }}; #[cfg(any(feature = "imu_controls"))] use shared_bus::BusManagerSimple; @@ -59,30 +53,31 @@ impl> Universe pub fn initialize(&mut self) { self.engine.initialize(); + self.engine.start(); } pub fn move_up(&mut self) { - self.engine.move_up(); + self.engine.action(Up); } pub fn move_down(&mut self) { - self.engine.move_down(); + self.engine.action(Down); } pub fn move_left(&mut self) { - self.engine.move_left(); + self.engine.action(Left); } pub fn move_right(&mut self) { - self.engine.move_right(); + self.engine.action(Right); } pub fn teleport(&mut self) { - self.engine.teleport(); + self.engine.action(Teleport); } pub fn place_dynamite(&mut self) { - self.engine.place_dynamite(); + self.engine.action(PlaceDynamite); } pub fn render_frame(&mut self) -> &D { diff --git a/esp32-c3-devkit-rust/src/main.rs b/esp32-c3-devkit-rust/src/main.rs index 97c15ab..db80290 100644 --- a/esp32-c3-devkit-rust/src/main.rs +++ b/esp32-c3-devkit-rust/src/main.rs @@ -55,7 +55,7 @@ use xtensa_lx_rt::entry; use embedded_graphics::pixelcolor::Rgb565; // use esp32s2_hal::Rng; -use spooky_core::{engine::Engine, spritebuf::SpriteBuf}; +use spooky_core::{engine::Engine, spritebuf::SpriteBuf, engine::Action::{ Up, Down, Left, Right, Teleport, PlaceDynamite }}; #[cfg(any(feature = "imu_controls"))] use icm42670::{accelerometer::Accelerometer, Address, Icm42670}; @@ -67,10 +67,7 @@ use embedded_hal::digital::v2::OutputPin; pub struct Universe { pub engine: Engine, - // #[cfg(any(feature = "imu_controls"))] icm: I, - // icm: Option>>>> - // delay: Some(Delay), } impl> @@ -79,14 +76,13 @@ impl, engine: Engine) -> Universe { Universe { engine, - // #[cfg(any(feature = "imu_controls"))] icm, - // delay: None, } } pub fn initialize(&mut self) { self.engine.initialize(); + self.engine.start(); } pub fn render_frame(&mut self) -> &D { @@ -96,27 +92,27 @@ impl accel_threshold { - self.engine.move_left(); + self.engine.action(Left); } if accel_norm.y < -accel_threshold { - self.engine.move_right(); + self.engine.action(Right); } if accel_norm.x > accel_threshold { - self.engine.move_down(); + self.engine.action(Down); } if accel_norm.x < -accel_threshold { - self.engine.move_up(); + self.engine.action(Up); } // Quickly move up to teleport // Quickly move down to place dynamite if accel_norm.z < -1.2 { - self.engine.teleport(); + self.engine.action(Teleport); } else if accel_norm.z > 1.5 { - self.engine.place_dynamite(); + self.engine.action(PlaceDynamite); } } diff --git a/esp32-s2-kaluga/src/main.rs b/esp32-s2-kaluga/src/main.rs index 7fbe111..6d500d5 100644 --- a/esp32-s2-kaluga/src/main.rs +++ b/esp32-s2-kaluga/src/main.rs @@ -60,10 +60,7 @@ use riscv_rt::entry; use embedded_graphics::{pixelcolor::Rgb565}; // use esp32s2_hal::Rng; -#[cfg(any(feature = "esp32s2_ili9341", feature = "esp32_wrover_kit", feature = "esp32c3_ili9341"))] -use ili9341::{DisplaySize240x320, Ili9341, Orientation}; - -use spooky_core::{spritebuf::SpriteBuf, engine::Engine}; +use spooky_core::{spritebuf::SpriteBuf, engine::Engine, engine::Action::{ Up, Down, Left, Right, Teleport, PlaceDynamite }}; use embedded_hal::digital::v2::OutputPin; use embedded_graphics_framebuf::{FrameBuf}; @@ -82,22 +79,23 @@ impl > Universe pub fn initialize(&mut self) { self.engine.initialize(); + self.engine.start(); } pub fn move_up(&mut self) { - self.engine.move_up(); + self.engine.action(Up); } pub fn move_down(&mut self) { - self.engine.move_down(); + self.engine.action(Down); } pub fn move_left(&mut self) { - self.engine.move_left(); + self.engine.action(Left); } pub fn move_right(&mut self) { - self.engine.move_right(); + self.engine.action(Right); } pub fn render_frame(&mut self) -> &D { @@ -205,13 +203,13 @@ fn main() -> ! { let button_value: u16 = nb::block!(adc1.read(&mut button_ladder_pin)).unwrap(); // Based on https://github.com/espressif/esp-bsp/blob/master/esp32_s2_kaluga_kit/include/bsp/esp32_s2_kaluga_kit.h#L299 if button_value > 4000 && button_value < 5000 { - universe.engine.move_right(); + universe.move_right(); } else if button_value >= 5000 && button_value < 6000 { - universe.engine.move_left(); + universe.move_left(); } else if button_value >= 6000 && button_value < 7000 { - universe.engine.move_down(); + universe.move_down(); } else if button_value >= 7000 && button_value < 8180 { - universe.engine.move_up(); + universe.move_up(); } display.draw_iter(universe.render_frame().into_iter()).unwrap(); diff --git a/esp32-s2-usb-otg/src/main.rs b/esp32-s2-usb-otg/src/main.rs index 951f55d..dfcd31f 100644 --- a/esp32-s2-usb-otg/src/main.rs +++ b/esp32-s2-usb-otg/src/main.rs @@ -53,10 +53,7 @@ use riscv_rt::entry; use embedded_graphics::{pixelcolor::Rgb565}; // use esp32s2_hal::Rng; -#[cfg(any(feature = "esp32s2_ili9341", feature = "esp32_wrover_kit", feature = "esp32c3_ili9341"))] -use ili9341::{DisplaySize240x320, Ili9341, Orientation}; - -use spooky_core::{spritebuf::SpriteBuf, engine::Engine}; +use spooky_core::{spritebuf::SpriteBuf, engine::Engine, engine::Action::{ Up, Down, Left, Right, Teleport, PlaceDynamite }}; use embedded_hal::digital::v2::OutputPin; use embedded_graphics_framebuf::{FrameBuf}; @@ -75,22 +72,31 @@ impl > Universe pub fn initialize(&mut self) { self.engine.initialize(); + self.engine.start() } pub fn move_up(&mut self) { - self.engine.move_up(); + self.engine.action(Up); } pub fn move_down(&mut self) { - self.engine.move_down(); + self.engine.action(Down); } pub fn move_left(&mut self) { - self.engine.move_left(); + self.engine.action(Left); } pub fn move_right(&mut self) { - self.engine.move_right(); + self.engine.action(Right); + } + + pub fn teleport(&mut self) { + self.engine.action(Teleport); + } + + pub fn place_dynamite(&mut self) { + self.engine.action(PlaceDynamite); } pub fn render_frame(&mut self) -> &D { @@ -243,11 +249,11 @@ fn main() -> ! { let button_menu = button_menu_pin.is_low().unwrap(); if button_up && button_down { - universe.engine.teleport(); + universe.teleport(); } else if button_menu && button_ok { - universe.engine.place_dynamite(); + universe.place_dynamite(); } else if button_down { - universe.engine.move_down(); + universe.move_down(); } else if button_up { universe.move_up(); } else if button_menu { diff --git a/esp32-s3-box/src/main.rs b/esp32-s3-box/src/main.rs index 0bfd7ca..a7b4158 100644 --- a/esp32-s3-box/src/main.rs +++ b/esp32-s3-box/src/main.rs @@ -51,16 +51,8 @@ use riscv_rt::entry; use xtensa_lx_rt::entry; use embedded_graphics::pixelcolor::Rgb565; -// use esp32s2_hal::Rng; -#[cfg(any( - feature = "esp32s2_ili9341", - feature = "esp32_wrover_kit", - feature = "esp32c3_ili9341" -))] -use ili9341::{DisplaySize240x320, Ili9341, Orientation}; - -use spooky_core::{engine::Engine, spritebuf::SpriteBuf}; +use spooky_core::{engine::Engine, spritebuf::SpriteBuf, engine::Action::{ Up, Down, Left, Right, Teleport, PlaceDynamite } }; #[cfg(any(feature = "imu_controls"))] use icm42670::{accelerometer::Accelerometer, Address, Icm42670}; @@ -72,10 +64,7 @@ use embedded_hal::digital::v2::OutputPin; pub struct Universe { pub engine: Engine, - // #[cfg(any(feature = "imu_controls"))] icm: I, - // icm: Option>>>> - // delay: Some(Delay), } impl> @@ -84,14 +73,13 @@ impl, engine: Engine) -> Universe { Universe { engine, - // #[cfg(any(feature = "imu_controls"))] icm, - // delay: None, } } pub fn initialize(&mut self) { self.engine.initialize(); + self.engine.start(); } pub fn render_frame(&mut self) -> &D { @@ -101,27 +89,27 @@ impl accel_threshold { - self.engine.move_left(); + self.engine.action(Left); } if accel_norm.y < -accel_threshold { - self.engine.move_right(); + self.engine.action(Right); } if accel_norm.x > accel_threshold { - self.engine.move_down(); + self.engine.action(Down); } if accel_norm.x < -accel_threshold { - self.engine.move_up(); + self.engine.action(Up); } // Quickly move up to teleport // Quickly move down to place dynamite if accel_norm.z < -1.2 { - self.engine.teleport(); + self.engine.action(Teleport); } else if accel_norm.z > 1.5 { - self.engine.place_dynamite(); + self.engine.action(PlaceDynamite); } } @@ -165,62 +153,12 @@ fn main() -> ! { println!("About to initialize the SPI LED driver"); let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); - // https://espressif-docs.readthedocs-hosted.com/projects/espressif-esp-dev-kits/en/latest/esp32s3/esp32-s3-usb-otg/user_guide.html - // let button_up = button::Button::new(); - - #[cfg(any(feature = "esp32s2_usb_otg", feature = "esp32s3_usb_otg"))] - let button_ok_pin = io.pins.gpio0.into_pull_up_input(); - #[cfg(any(feature = "esp32s2_usb_otg", feature = "esp32s3_usb_otg"))] - let button_menu_pin = io.pins.gpio14.into_pull_up_input(); - #[cfg(any(feature = "esp32s2_usb_otg", feature = "esp32s3_usb_otg"))] - let button_up_pin = io.pins.gpio10.into_pull_up_input(); - #[cfg(any(feature = "esp32s2_usb_otg", feature = "esp32s3_usb_otg"))] - let button_down_pin = io.pins.gpio11.into_pull_up_input(); - - #[cfg(feature = "esp32")] - let mut backlight = io.pins.gpio5.into_push_pull_output(); - #[cfg(any(feature = "esp32s2", feature = "esp32s3_usb_otg"))] - let mut backlight = io.pins.gpio9.into_push_pull_output(); - #[cfg(feature = "esp32c3")] - let mut backlight = io.pins.gpio0.into_push_pull_output(); - - #[cfg(feature = "esp32")] - let spi = spi::Spi::new( - peripherals.SPI2, - io.pins.gpio19, - io.pins.gpio23, - io.pins.gpio25, - io.pins.gpio22, - 100u32.MHz(), - spi::SpiMode::Mode0, - &mut system.peripheral_clock_control, - &mut clocks, - ); - - #[cfg(any(feature = "esp32s2", feature = "esp32s3_usb_otg"))] - let spi = spi::Spi::new( - peripherals.SPI3, - io.pins.gpio6, - io.pins.gpio7, - io.pins.gpio12, - io.pins.gpio5, - 100u32.MHz(), - spi::SpiMode::Mode0, - &mut system.peripheral_clock_control, - &mut clocks, - ); #[cfg(any(feature = "esp32s3_box"))] let sclk = io.pins.gpio7; #[cfg(any(feature = "esp32s3_box"))] let mosi = io.pins.gpio6; - // let dma = Gdma::new(peripherals.DMA, &mut system.peripheral_clock_control); - // let dma_channel = dma.channel0; - - // let mut descriptors = [0u32; 8 * 3]; - // let mut rx_descriptors = [0u32; 8 * 3]; - #[cfg(any(feature = "esp32s3_box"))] let spi = spi::Spi::new_no_cs_no_miso( peripherals.SPI2, @@ -231,12 +169,6 @@ fn main() -> ! { &mut system.peripheral_clock_control, &clocks, ); - // .with_dma(dma_channel.configure( - // false, - // &mut descriptors, - // &mut rx_descriptors, - // DmaPriority::Priority0, - // )); #[cfg(any(feature = "esp32s3_box"))] let mut backlight = io.pins.gpio45.into_push_pull_output(); @@ -246,41 +178,12 @@ fn main() -> ! { #[cfg(any(feature = "esp32s2", feature = "esp32s3", feature = "esp32c3"))] backlight.set_high().unwrap(); - #[cfg(feature = "esp32c3")] - let spi = spi::Spi::new( - peripherals.SPI2, - io.pins.gpio6, - io.pins.gpio7, - io.pins.gpio12, - io.pins.gpio20, - 100u32.MHz(), - spi::SpiMode::Mode0, - &mut system.peripheral_clock_control, - &mut clocks, - ); - - #[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3_usb_otg"))] - let reset = io.pins.gpio18.into_push_pull_output(); #[cfg(any(feature = "esp32s3_box"))] let reset = io.pins.gpio48.into_push_pull_output(); - #[cfg(any(feature = "esp32c3"))] - let reset = io.pins.gpio9.into_push_pull_output(); - #[cfg(any(feature = "esp32", feature = "esp32c3"))] - let di = SPIInterfaceNoCS::new(spi, io.pins.gpio21.into_push_pull_output()); #[cfg(any(feature = "esp32s2", feature = "esp32s3"))] let di = SPIInterfaceNoCS::new(spi, io.pins.gpio4.into_push_pull_output()); - #[cfg(any( - feature = "esp32s2_ili9341", - feature = "esp32_wrover_kit", - feature = "esp32c3_ili9341" - ))] - let mut delay = Delay::new(&clocks); - - #[cfg(any(feature = "esp32s2_usb_otg", feature = "esp32s3_usb_otg"))] - let mut display = mipidsi::Display::st7789(di, reset); - //https://github.com/espressif/esp-box/blob/master/components/bsp/src/boards/esp32_s3_box.c #[cfg(any(feature = "esp32s3_box"))] @@ -290,32 +193,6 @@ fn main() -> ! { .with_color_order(mipidsi::ColorOrder::Rgb) .init(&mut delay, Some(reset)) .unwrap(); - // let mut display = mipidsi::Display::ili9342c_rgb565(di, core::prelude::v1::Some(reset), display_options); - #[cfg(any( - feature = "esp32s2_ili9341", - feature = "esp32_wrover_kit", - feature = "esp32c3_ili9341" - ))] - let mut display = Ili9341::new( - di, - reset, - &mut delay, - Orientation::Portrait, - DisplaySize240x320, - ) - .unwrap(); - - #[cfg(any(feature = "esp32s2_usb_otg", feature = "esp32s3_usb_otg"))] - display - .init( - &mut delay, - DisplayOptions { - ..DisplayOptions::default() - }, - ) - .unwrap(); - - // display.clear(RgbColor::WHITE).unwrap(); Text::new( "Initializing...", @@ -358,13 +235,9 @@ fn main() -> ! { let mut universe = Universe::new(icm, Some(seed_buffer), engine); universe.initialize(); - // #[cfg(any(feature = "imu_controls"))] - // let accel_threshold = 0.20; - loop { display .draw_iter(universe.render_frame().into_iter()) .unwrap(); - // delay.delay_ms(300u32); } } diff --git a/esp32-s3-usb-otg/src/main.rs b/esp32-s3-usb-otg/src/main.rs index 3d1b07a..cdbc46f 100644 --- a/esp32-s3-usb-otg/src/main.rs +++ b/esp32-s3-usb-otg/src/main.rs @@ -59,7 +59,7 @@ use embedded_graphics::{pixelcolor::Rgb565}; #[cfg(any(feature = "esp32s2_ili9341", feature = "esp32_wrover_kit", feature = "esp32c3_ili9341"))] use ili9341::{DisplaySize240x320, Ili9341, Orientation}; -use spooky_core::{spritebuf::SpriteBuf, engine::Engine}; +use spooky_core::{spritebuf::SpriteBuf, engine::Engine, engine::Action::{ Up, Down, Left, Right, Teleport, PlaceDynamite }}; use embedded_hal::digital::v2::OutputPin; use embedded_graphics_framebuf::{FrameBuf}; @@ -78,22 +78,31 @@ impl > Universe pub fn initialize(&mut self) { self.engine.initialize(); + self.engine.start(); } pub fn move_up(&mut self) { - self.engine.move_up(); + self.engine.action(Up); } pub fn move_down(&mut self) { - self.engine.move_down(); + self.engine.action(Down); } pub fn move_left(&mut self) { - self.engine.move_left(); + self.engine.action(Left); } pub fn move_right(&mut self) { - self.engine.move_right(); + self.engine.action(Right); + } + + pub fn teleport(&mut self) { + self.engine.action(Teleport); + } + + pub fn place_dynamite(&mut self) { + self.engine.action(PlaceDynamite); } pub fn render_frame(&mut self) -> &D { @@ -262,11 +271,11 @@ fn main() -> ! { let button_menu = button_menu_pin.is_low().unwrap(); if button_up && button_down { - universe.engine.teleport(); + universe.teleport(); } else if button_menu && button_ok { - universe.engine.place_dynamite(); + universe.place_dynamite(); } else if button_down { - universe.engine.move_down(); + universe.move_down(); } else if button_up { universe.move_up(); } else if button_menu { diff --git a/m5core-fire/src/main.rs b/m5core-fire/src/main.rs index 679d519..728af7d 100644 --- a/m5core-fire/src/main.rs +++ b/m5core-fire/src/main.rs @@ -48,7 +48,7 @@ use xtensa_lx_rt::entry; use embedded_graphics::pixelcolor::Rgb565; -use spooky_core::{engine::Engine, spritebuf::SpriteBuf}; +use spooky_core::{engine::Engine, spritebuf::SpriteBuf, engine::Action::{ Up, Down, Left, Right, Teleport, PlaceDynamite }}; #[cfg(any(feature = "imu_controls"))] use shared_bus::BusManagerSimple; @@ -67,30 +67,31 @@ impl> Universe pub fn initialize(&mut self) { self.engine.initialize(); + self.engine.start() } pub fn move_up(&mut self) { - self.engine.move_up(); + self.engine.action(Up); } pub fn move_down(&mut self) { - self.engine.move_down(); + self.engine.action(Down); } pub fn move_left(&mut self) { - self.engine.move_left(); + self.engine.action(Left); } pub fn move_right(&mut self) { - self.engine.move_right(); + self.engine.action(Right); } pub fn teleport(&mut self) { - self.engine.teleport(); + self.engine.action(Teleport) } pub fn place_dynamite(&mut self) { - self.engine.place_dynamite(); + self.engine.action(PlaceDynamite); } pub fn render_frame(&mut self) -> &D { diff --git a/spooky-core/src/assets.rs b/spooky-core/src/assets.rs index 1d03a9e..f3be05d 100644 --- a/spooky-core/src/assets.rs +++ b/spooky-core/src/assets.rs @@ -18,6 +18,7 @@ pub struct Assets<'a> { pub npc: Option>, pub teleport: Option>, pub walker: Option>, + pub smiley: Option>, } impl Assets<'static> { @@ -36,6 +37,7 @@ impl Assets<'static> { npc: None, teleport: None, walker: None, + smiley: None, } } @@ -53,5 +55,6 @@ impl Assets<'static> { self.npc = Some(Bmp::::from_slice(include_bytes!("../../assets/img/npc.bmp")).unwrap()); self.teleport = Some(Bmp::::from_slice(include_bytes!("../../assets/img/teleport.bmp")).unwrap()); self.walker = Some(Bmp::::from_slice(include_bytes!("../../assets/img/walker.bmp")).unwrap()); + self.smiley = Some(Bmp::::from_slice(include_bytes!("../../assets/img/smiley.bmp")).unwrap()); } } diff --git a/spooky-core/src/engine.rs b/spooky-core/src/engine.rs index 7b1fc85..5bbf052 100644 --- a/spooky-core/src/engine.rs +++ b/spooky-core/src/engine.rs @@ -16,6 +16,23 @@ use crate::{assets::Assets, maze::Maze}; use heapless::String; use tinybmp::Bmp; +pub enum GameState { + Start, + Playing, + GameOver, + Outro, +} + +pub enum Action { + None, + Up, + Down, + Left, + Right, + Teleport, + PlaceDynamite, +} + pub struct Engine { pub start_time: u64, pub ghost_x: i32, @@ -31,6 +48,8 @@ pub struct Engine { teleport_counter: u32, walker_counter: u32, dynamite_counter: u32, + game_state: GameState, + outro_counter: u32, } @@ -52,6 +71,8 @@ impl > Engine { teleport_counter: 100, walker_counter: 0, dynamite_counter: 1, + game_state: GameState::Start, + outro_counter: 0, } } @@ -63,6 +84,9 @@ impl > Engine { match self.maze.get_coin_at(x, y) { Some(coin) => { self.maze.remove_coin(coin); + if self.maze.coin_counter == 0 { + self.game_state = GameState::Outro; + } }, None => {} } @@ -121,7 +145,31 @@ impl > Engine { } } - pub fn move_right(&mut self) { + pub fn action(&mut self, action: Action) { + match self.game_state { + GameState::Playing => { + match action { + Action::None => {} + Action::Up => self.move_up(), + Action::Down => self.move_down(), + Action::Left => self.move_left(), + Action::Right => self.move_right(), + Action::Teleport => self.teleport(), + Action::PlaceDynamite => self.place_dynamite(), + } + }, + GameState::Outro => { + if self.outro_counter > 30 { + self.game_state = GameState::Start; + self.outro_counter = 0; + self.start(); + } + }, + _ => {} + } + } + + fn move_right(&mut self) { let new_camera_x = self.camera_x + self.step_size_x as i32; if self.is_walkable(new_camera_x + self.ghost_x, self.camera_y + self.ghost_y) { self.camera_x = new_camera_x; @@ -129,7 +177,7 @@ impl > Engine { } } - pub fn move_left(&mut self) { + fn move_left(&mut self) { let new_camera_x = self.camera_x - self.step_size_x as i32; if self.is_walkable(new_camera_x + self.ghost_x, self.camera_y + self.ghost_y) { self.camera_x = new_camera_x; @@ -137,7 +185,7 @@ impl > Engine { } } - pub fn move_up(&mut self) { + fn move_up(&mut self) { let new_camera_y = self.camera_y - self.step_size_y as i32; if self.is_walkable(self.camera_x + self.ghost_x, new_camera_y + self.ghost_y) { self.camera_y = new_camera_y; @@ -145,7 +193,7 @@ impl > Engine { } } - pub fn move_down(&mut self) { + fn move_down(&mut self) { let new_camera_y = self.camera_y + self.step_size_y as i32; if self.is_walkable(self.camera_x + self.ghost_x, new_camera_y + self.ghost_y) { self.camera_y = new_camera_y; @@ -153,14 +201,14 @@ impl > Engine { } } - pub fn teleport(&mut self) { + fn teleport(&mut self) { if self.teleport_counter == 100 { self.relocate_avatar(); self.teleport_counter = 0; } } - pub fn place_dynamite(&mut self) { + fn place_dynamite(&mut self) { if self.dynamite_counter == 0 { return; } @@ -213,30 +261,43 @@ impl > Engine { pub fn tick(&mut self) { - self.animation_step += 1; - if self.animation_step > 1 { - self.animation_step = 0; - } + match self.game_state { + GameState::Playing => { - // Recharge teleport - if self.teleport_counter < 100 { - self.teleport_counter += 1; - } + self.animation_step += 1; + if self.animation_step > 1 { + self.animation_step = 0; + } - // Decrement remaining time when Walker is active - if self.walker_counter > 0 { - self.walker_counter -= 1; - } + // Recharge teleport + if self.teleport_counter < 100 { + self.teleport_counter += 1; + } + + // Decrement remaining time when Walker is active + if self.walker_counter > 0 { + self.walker_counter -= 1; + } - self.maze.move_npcs(); - self.check_npc_collision(); + self.maze.move_npcs(); + self.check_npc_collision(); + }, + GameState::Outro => { + if self.outro_counter < 1000 { + self.outro_counter += 1; + } + }, + _ => {} + } } pub fn initialize(&mut self) { let mut assets = Assets::new(); assets.load(); self.assets = Some(assets); + } + pub fn start(&mut self) { self.maze.generate_maze(32, 32); self.relocate_avatar(); self.maze.generate_coins(); @@ -244,7 +305,7 @@ impl > Engine { self.maze.generate_walkers(); self.maze.generate_dynamites(); self.draw_maze(self.camera_x,self.camera_y); - + self.game_state = GameState::Playing; } fn draw_status_number(&mut self, value: u32, x: i32, y: i32) { @@ -253,7 +314,7 @@ impl > Engine { .draw(&mut self.display); } - pub fn draw(&mut self) -> &mut D { + pub fn draw_main_scene(&mut self) -> &mut D { self.draw_maze(self.camera_x,self.camera_y); @@ -374,6 +435,29 @@ impl > Engine { &mut self.display } + pub fn draw_outro_scene(&mut self) -> &mut D { + let assets = self.assets.as_ref().unwrap(); + let bmp:Bmp = assets.smiley.unwrap(); + let (x,y) = (self.maze.get_rand() + self.maze.get_rand() % 70, self.maze.get_rand() % 240); + let outro = Image::new(&bmp, Point::new(x, y)); + outro.draw(&mut self.display); + &mut self.display + } + + pub fn draw(&mut self) -> &mut D { + match self.game_state { + GameState::Playing => { + self.draw_main_scene() + }, + GameState::Outro => { + self.draw_outro_scene() + }, + _ => { + &mut self.display + } + } + } + } diff --git a/spooky-core/src/maze.rs b/spooky-core/src/maze.rs index 5c52c1c..ecc1466 100644 --- a/spooky-core/src/maze.rs +++ b/spooky-core/src/maze.rs @@ -136,7 +136,7 @@ impl Maze { } } - fn get_rand(&mut self) -> i32 { + pub fn get_rand(&mut self) -> i32 { self.rng.gen_range(0..255) } @@ -252,8 +252,6 @@ impl Maze { self.coins[index].y = -1; if self.coin_counter > 0 { self.coin_counter -= 1; - } else { - self.generate_coins(); } } } diff --git a/wasm/index.html b/wasm/index.html index de9b88a..beda264 100644 --- a/wasm/index.html +++ b/wasm/index.html @@ -9,7 +9,7 @@ + content="#000000"> @@ -19,10 +19,10 @@ Spooky Maze Game
- - - - + + + +
@@ -34,7 +34,7 @@ - \ No newline at end of file + diff --git a/wasm/index.js b/wasm/index.js index 78a5bd2..3496076 100644 --- a/wasm/index.js +++ b/wasm/index.js @@ -54,6 +54,7 @@ const rust = import('./pkg') }, true); document.addEventListener('keydown', (event) => { + var isKnownKey = true; if ((event.key === "Up") || (event.key == "ArrowUp")) { universe.move_up(); } else if ((event.key === "Down") || (event.key === "ArrowDown")) { @@ -62,6 +63,16 @@ const rust = import('./pkg') universe.move_left(); } else if ((event.key === "Right") || (event.key === "ArrowRight")) { universe.move_right(); + } else if ((event.key === " ") || (event.key === "d")) { + universe.place_dynamite(); + } else if ((event.key === "t") || (event.key === "Enter")){ + universe.teleport(); + } else { + isKnownKey = false; + } + + if (isKnownKey) { + event.preventDefault(); } }); diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index f031ef4..1aed469 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -11,7 +11,7 @@ use embedded_graphics_web_simulator::{ use wasm_bindgen::prelude::*; use web_sys::{console}; -use spooky_core::{ engine::Engine }; +use spooky_core::{ engine::Engine, engine::Action::{ Up, Down, Left, Right, Teleport, PlaceDynamite } }; #[wasm_bindgen] pub struct Universe { @@ -50,31 +50,32 @@ impl Universe { } pub fn move_up(&mut self) { - self.engine.move_up(); + self.engine.action(Up); } pub fn move_down(&mut self) { - self.engine.move_down(); + self.engine.action(Down); } pub fn move_left(&mut self) { - self.engine.move_left(); + self.engine.action(Left); } pub fn move_right(&mut self) { - self.engine.move_right(); + self.engine.action(Right); } pub fn teleport(&mut self) { - self.engine.teleport(); + self.engine.action(Teleport); } pub fn place_dynamite(&mut self) { - self.engine.place_dynamite(); + self.engine.action(PlaceDynamite); } pub fn initialize(&mut self) { self.engine.initialize(); + self.engine.start(); } pub fn render_frame(&mut self) {