diff --git a/game/Cargo.lock b/game/Cargo.lock index 67f5061..1564d79 100644 --- a/game/Cargo.lock +++ b/game/Cargo.lock @@ -22,10 +22,20 @@ checksum = "96e613fca5924ccab8ab8369ab292a1b4720b125e98007fbda163f4ce7176472" dependencies = [ "bitflags", "libc", + "sdl3-image-sys", "sdl3-sys", "sdl3-ttf-sys", ] +[[package]] +name = "sdl3-image-sys" +version = "0.6.2+SDL-image-3.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64a0c5e6f5874aef7cdc76ff36855bffd98f52feb9df3f5b7f443e0beb4472fe" +dependencies = [ + "sdl3-sys", +] + [[package]] name = "sdl3-sys" version = "0.6.3+SDL-3.4.4" diff --git a/game/Cargo.toml b/game/Cargo.toml index a663c89..728863c 100644 --- a/game/Cargo.toml +++ b/game/Cargo.toml @@ -4,4 +4,4 @@ version = "0.1.0" edition = "2021" [dependencies] -sdl3 = { version = "0.18.0", features = ["ttf"] } +sdl3 = { version = "0.18.0", features = ["ttf", "image"] } diff --git a/game/assets/explosion/0.png b/game/assets/explosion/0.png new file mode 100644 index 0000000..5105699 Binary files /dev/null and b/game/assets/explosion/0.png differ diff --git a/game/assets/explosion/1.png b/game/assets/explosion/1.png new file mode 100644 index 0000000..cd76f2d Binary files /dev/null and b/game/assets/explosion/1.png differ diff --git a/game/assets/explosion/10.png b/game/assets/explosion/10.png new file mode 100644 index 0000000..4d66657 Binary files /dev/null and b/game/assets/explosion/10.png differ diff --git a/game/assets/explosion/11.png b/game/assets/explosion/11.png new file mode 100644 index 0000000..1abc691 Binary files /dev/null and b/game/assets/explosion/11.png differ diff --git a/game/assets/explosion/12.png b/game/assets/explosion/12.png new file mode 100644 index 0000000..7cba5ac Binary files /dev/null and b/game/assets/explosion/12.png differ diff --git a/game/assets/explosion/13.png b/game/assets/explosion/13.png new file mode 100644 index 0000000..2f00667 Binary files /dev/null and b/game/assets/explosion/13.png differ diff --git a/game/assets/explosion/14.png b/game/assets/explosion/14.png new file mode 100644 index 0000000..c04de9e Binary files /dev/null and b/game/assets/explosion/14.png differ diff --git a/game/assets/explosion/15.png b/game/assets/explosion/15.png new file mode 100644 index 0000000..f24f442 Binary files /dev/null and b/game/assets/explosion/15.png differ diff --git a/game/assets/explosion/2.png b/game/assets/explosion/2.png new file mode 100644 index 0000000..00e0f2a Binary files /dev/null and b/game/assets/explosion/2.png differ diff --git a/game/assets/explosion/3.png b/game/assets/explosion/3.png new file mode 100644 index 0000000..2aae67b Binary files /dev/null and b/game/assets/explosion/3.png differ diff --git a/game/assets/explosion/4.png b/game/assets/explosion/4.png new file mode 100644 index 0000000..b6b0025 Binary files /dev/null and b/game/assets/explosion/4.png differ diff --git a/game/assets/explosion/5.png b/game/assets/explosion/5.png new file mode 100644 index 0000000..20f250e Binary files /dev/null and b/game/assets/explosion/5.png differ diff --git a/game/assets/explosion/6.png b/game/assets/explosion/6.png new file mode 100644 index 0000000..dd4aa04 Binary files /dev/null and b/game/assets/explosion/6.png differ diff --git a/game/assets/explosion/7.png b/game/assets/explosion/7.png new file mode 100644 index 0000000..15441c1 Binary files /dev/null and b/game/assets/explosion/7.png differ diff --git a/game/assets/explosion/8.png b/game/assets/explosion/8.png new file mode 100644 index 0000000..3043533 Binary files /dev/null and b/game/assets/explosion/8.png differ diff --git a/game/assets/explosion/9.png b/game/assets/explosion/9.png new file mode 100644 index 0000000..f95d285 Binary files /dev/null and b/game/assets/explosion/9.png differ diff --git a/game/src/engine/game.rs b/game/src/engine/game.rs index 1525dbd..2ad5c72 100644 --- a/game/src/engine/game.rs +++ b/game/src/engine/game.rs @@ -12,6 +12,7 @@ pub trait Io> { pub trait Renderer { fn load_text(&mut self, text: &str, size: f64, color: Color) -> u32; + fn load_image(&mut self, path: &str) -> u32; fn draw_texture(&mut self, id: u32, pos: V2); fn query_texture(&mut self, id: u32) -> V2; fn draw_rect(&mut self, pos: V2, size: V2, color: Color); diff --git a/game/src/engine/sdl_io.rs b/game/src/engine/sdl_io.rs index 6fd012f..5345cc7 100644 --- a/game/src/engine/sdl_io.rs +++ b/game/src/engine/sdl_io.rs @@ -33,6 +33,7 @@ mod textures { use std::collections::HashMap; use sdl3::{ + image::LoadTexture, render::{Texture, TextureCreator}, ttf::Font, video::WindowContext, @@ -54,6 +55,19 @@ mod textures { id_counter: 0, } } + pub fn render_image(&mut self, path: &str) -> u32 { + let texture = unsafe { + let static_texture_creator: *const TextureCreator = + &self.texture_creator as *const _; + let texture = (&*static_texture_creator).load_texture(path).unwrap(); + texture + }; + let id = self.id_counter; + self.textures.insert(id, texture); + self.id_counter += 1; + id + } + pub fn render_font(&mut self, font: Font, text: &str, color: Color) -> u32 { let surface = font.render(text).blended(color).unwrap(); let texture = unsafe { @@ -276,6 +290,11 @@ impl Renderer for SdlIo<'_> { let (_, size) = self.canvas.window().size(); size as f64 } + + fn load_image(&mut self, path: &str) -> u32 { + let id = self.texture_handler.render_image(path); + id + } } impl From for FPoint { diff --git a/game/src/main.rs b/game/src/main.rs index 165317e..69723fb 100644 --- a/game/src/main.rs +++ b/game/src/main.rs @@ -375,10 +375,11 @@ struct Game { keys_pressed: HashSet, event_queue: Arc>>, ground: GroundManager, - joever_timer: Option, + game_over_timer: Option, } impl Game { + const RESTART_TIMER: Duration = Duration::from_secs(5); fn new() -> Self { Self { skateboard: Skateboard { @@ -395,27 +396,28 @@ impl Game { segment_manager: SegmentManager::new(V3(0.0, -0.24, 1.6), 4, 2.0, 1.0), keys_pressed: HashSet::new(), ground: GroundManager::new(V3(0.0, -0.25, -0.4), 0.1, 10, 20, 4), - joever_timer: None, + game_over_timer: None, } } fn restart(&mut self) { - *self = Self::new(); + let mut new = Self::new(); + std::mem::swap(&mut new.event_queue, &mut self.event_queue); + *self = new; } } impl engine::Game for Game { fn update(&mut self, delta_time: Duration) { - if let Some(joever_timer) = self.joever_timer.as_mut() { - *joever_timer -= delta_time.as_secs_f64(); - if *joever_timer <= 0.0 { - self.restart(); + if let Some(timer) = self.game_over_timer.as_mut() { + match timer.checked_sub(delta_time) { + Some(result) => *timer = result, + None => self.restart(), } return; } if let UpdateResult::GameOver = self.skateboard.update(delta_time) { - self.joever_timer = Some(5.0); - return; + self.game_over_timer = Some(Self::RESTART_TIMER); } self.camera_pos = V3( self.skateboard.pos.0, @@ -457,8 +459,7 @@ impl engine::Game for Game { }; if let UpdateResult::GameOver = self.segment_manager.update(&mut cx, delta_time) { - self.joever_timer = Some(5.0); - return; + self.game_over_timer = Some(Self::RESTART_TIMER); } if self @@ -485,9 +486,26 @@ impl engine::Game for Game { let V2(width, _height) = r.query_texture(id); r.draw_texture(id, V2(r.screen_width() / 2.0 - width / 2.0, 50.0)); } - if let Some(joever_timer) = self.joever_timer { + if let Some(timer) = self.game_over_timer { + { + let percentage = + (Self::RESTART_TIMER - timer).as_secs_f64() / Self::RESTART_TIMER.as_secs_f64(); + let percentage = percentage * 4.0; + if percentage < 1.0 { + let picked_texture = (percentage * 16.0).floor() as u32; + let id = r.load_image(&format!("assets/explosion/{picked_texture}.png")); + let V2(width, height) = r.query_texture(id); + r.draw_texture( + id, + V2( + r.screen_width() / 2.0 - width / 2.0, + r.screen_height() * 0.8 - height / 2.0, + ), + ); + } + } let id = r.load_text( - &format!("{}", joever_timer.ceil() as u32), + &format!("{}", timer.as_secs_f64().ceil() as u32), 96.0, Color::Green, );