From a272aa9d9b2c642871abe2dc3379e6ffcce8bf56 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Wed, 26 Aug 2026 23:54:09 -0700 Subject: sprite loader --- src/content/entities/entity_tnt.rs | 44 ++++++++++++++++++++++++++++++++++++++ src/content/entities/mod.rs | 1 + src/content/materials/mod.rs | 21 +++++++++++++----- 3 files changed, 61 insertions(+), 5 deletions(-) create mode 100644 src/content/entities/entity_tnt.rs (limited to 'src/content') diff --git a/src/content/entities/entity_tnt.rs b/src/content/entities/entity_tnt.rs new file mode 100644 index 0000000..2e4cef5 --- /dev/null +++ b/src/content/entities/entity_tnt.rs @@ -0,0 +1,44 @@ +use glam::{IVec2, Vec2}; + +use crate::{ + sim::{ + entity::{EntityBehaviour, EntityCells, EntityDef, EntityUpdateCtx}, + lib::force::apply_explosion, + sim_manager::SimCtx, + }, + sprite_loader::load_sprite_to_cells, +}; + +struct TntEntityBehaviour { + pub fuse: f32, +} + +impl EntityBehaviour for TntEntityBehaviour { + fn update(&mut self, update_ctx: &mut EntityUpdateCtx, ctx: &mut SimCtx, delta_time: f32) { + self.fuse -= delta_time; + if self.fuse <= 0.0 { + apply_explosion( + ctx, + update_ctx.entity_data.transform(ctx).unwrap().0, + 35, + Vec2::new(0.0, -0.6), + 400.0, + ); + update_ctx.deferred_destroy(update_ctx.entity_data.id); + } + } +} + +pub fn entity_tnt_def(position: Vec2) -> EntityDef { + let sprite_cells = load_sprite_to_cells("assets/sprites/tnt"); + let entity_cells = EntityCells { + cells: sprite_cells.cells, + size: IVec2::new(sprite_cells.width as i32, sprite_cells.height as i32), + }; + + EntityDef::from_cells( + position, + entity_cells, + Some(Box::new(TntEntityBehaviour { fuse: 3.0 })), + ) +} diff --git a/src/content/entities/mod.rs b/src/content/entities/mod.rs index 652ee6d..0ec45ce 100644 --- a/src/content/entities/mod.rs +++ b/src/content/entities/mod.rs @@ -1,3 +1,4 @@ pub mod entity_bullet_emitter; pub mod entity_cube; pub mod entity_grenade; +pub mod entity_tnt; diff --git a/src/content/materials/mod.rs b/src/content/materials/mod.rs index 8116565..819ec2a 100644 --- a/src/content/materials/mod.rs +++ b/src/content/materials/mod.rs @@ -1,3 +1,5 @@ +use serde::Deserialize; + use crate::sim::cell_manager::sim::{PostUpdateAction, UpdateCtx}; pub mod fire; @@ -6,7 +8,7 @@ pub mod liquid; pub mod powder; #[repr(u8)] -#[derive(Clone, Copy, PartialEq, Eq, Debug)] +#[derive(Clone, Copy, PartialEq, Eq, Debug, Deserialize)] pub enum MaterialId { Void = 0, Sand, @@ -15,6 +17,7 @@ pub enum MaterialId { Fire, Smoke, Steel, + Tnt, } #[repr(u8)] @@ -35,7 +38,7 @@ pub struct MaterialDef { pub sim_update: Option PostUpdateAction>, } -static MATERIALS: [MaterialDef; 7] = [ +static MATERIALS: [MaterialDef; 8] = [ MaterialDef { name: "Void", color: (0x00, 0x00, 0x00, 0x00), @@ -53,8 +56,8 @@ static MATERIALS: [MaterialDef; 7] = [ }, MaterialDef { name: "Wood", - color: (0x85, 0x56, 0x1D, 0xFF), - density: 50, + color: (0x66, 0x39, 0x31, 0xFF), + density: 60, form: MaterialForm::Solid, sim_update: None, }, @@ -88,10 +91,17 @@ static MATERIALS: [MaterialDef; 7] = [ form: MaterialForm::Solid, sim_update: None, }, + MaterialDef { + name: "TNT", + color: (0xA6, 0x34, 0x23, 0xFF), + density: 55, + form: MaterialForm::Solid, + sim_update: None, + }, ]; impl MaterialId { - pub const ALL: [MaterialId; 7] = [ + pub const ALL: [MaterialId; 8] = [ MaterialId::Void, MaterialId::Sand, MaterialId::Wood, @@ -99,6 +109,7 @@ impl MaterialId { MaterialId::Fire, MaterialId::Smoke, MaterialId::Steel, + MaterialId::Tnt, ]; #[inline] pub fn def(self) -> &'static MaterialDef { -- cgit v1.3.1