From 350c74bc8ef61833be11bbb2bab6747245e3ae0d Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Sun, 23 Aug 2026 00:38:32 -0700 Subject: refactor out content --- src/content/entities/entity_cube.rs | 30 ++++++++ src/content/entities/entity_grenade.rs | 61 ++++++++++++++++ src/content/entities/mod.rs | 2 + src/content/materials/fire.rs | 80 +++++++++++++++++++++ src/content/materials/gas.rs | 24 +++++++ src/content/materials/liquid.rs | 75 ++++++++++++++++++++ src/content/materials/mod.rs | 111 ++++++++++++++++++++++++++++++ src/content/materials/powder.rs | 10 +++ src/content/mod.rs | 2 + src/main.rs | 8 ++- src/renderer/mod.rs | 2 +- src/renderer/ui.rs | 6 +- src/sim/cell/cell.rs | 2 +- src/sim/cell/materials/fire.rs | 77 --------------------- src/sim/cell/materials/gas.rs | 24 ------- src/sim/cell/materials/liquid.rs | 75 -------------------- src/sim/cell/materials/mod.rs | 111 ------------------------------ src/sim/cell/materials/powder.rs | 10 --- src/sim/cell/mod.rs | 1 - src/sim/cell_manager/chunk.rs | 6 +- src/sim/cell_manager/sim.rs | 3 +- src/sim/entity/entities/entity_cube.rs | 27 -------- src/sim/entity/entities/entity_grenade.rs | 57 --------------- src/sim/entity/entities/mod.rs | 2 - src/sim/entity/mod.rs | 7 +- src/sim/lib/force.rs | 10 +-- src/sim/particle_manager/mod.rs | 5 +- src/sim/particle_manager/particle.rs | 2 +- src/sim/sim_manager/mod.rs | 3 +- src/sim/sim_manager/utils.rs | 2 +- 30 files changed, 420 insertions(+), 415 deletions(-) create mode 100644 src/content/entities/entity_cube.rs create mode 100644 src/content/entities/entity_grenade.rs create mode 100644 src/content/entities/mod.rs create mode 100644 src/content/materials/fire.rs create mode 100644 src/content/materials/gas.rs create mode 100644 src/content/materials/liquid.rs create mode 100644 src/content/materials/mod.rs create mode 100644 src/content/materials/powder.rs create mode 100644 src/content/mod.rs delete mode 100644 src/sim/cell/materials/fire.rs delete mode 100644 src/sim/cell/materials/gas.rs delete mode 100644 src/sim/cell/materials/liquid.rs delete mode 100644 src/sim/cell/materials/mod.rs delete mode 100644 src/sim/cell/materials/powder.rs delete mode 100644 src/sim/entity/entities/entity_cube.rs delete mode 100644 src/sim/entity/entities/entity_grenade.rs delete mode 100644 src/sim/entity/entities/mod.rs (limited to 'src') diff --git a/src/content/entities/entity_cube.rs b/src/content/entities/entity_cube.rs new file mode 100644 index 0000000..ab93e5e --- /dev/null +++ b/src/content/entities/entity_cube.rs @@ -0,0 +1,30 @@ +use glam::{IVec2, Vec2}; + +use crate::{ + content::materials::MaterialId, + sim::{ + cell::cell::Cell, + entity::{EntityCells, EntityDef}, + }, +}; + +pub fn entity_cube_def(position: Vec2, material: MaterialId) -> EntityDef { + let w = 10; + let h = 10; + let mut raw_cells = vec![Cell::void(); (w * h) as usize]; + + for x in 0..w { + for y in 0..h { + let cell_idx = x + y * w; + raw_cells[cell_idx as usize] = Cell::from_material(material); + raw_cells[cell_idx as usize].set_entity(true); + } + } + + let entity_cells = EntityCells { + cells: raw_cells, + size: IVec2::new(w, h), + }; + + EntityDef::from_cells(position, entity_cells, None) +} diff --git a/src/content/entities/entity_grenade.rs b/src/content/entities/entity_grenade.rs new file mode 100644 index 0000000..ab7aaa4 --- /dev/null +++ b/src/content/entities/entity_grenade.rs @@ -0,0 +1,61 @@ +use glam::{IVec2, Vec2}; + +use crate::{ + content::materials::MaterialId, + sim::{ + cell::cell::Cell, + entity::{EntityBehaviour, EntityCells, EntityDef, EntityUpdateCtx}, + lib::force::apply_explosion, + sim_manager::SimCtx, + }, +}; + +struct GrenadeEntityBehaviour { + pub fuse: f32, +} + +impl EntityBehaviour for GrenadeEntityBehaviour { + 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, + 15, + Vec2::new(0.0, -0.6), + 200.0, + ); + update_ctx.deferred_destroy(update_ctx.entity_data.id); + } + } +} + +pub fn entity_grenade_def(position: Vec2, fuse: f32) -> EntityDef { + let w = 3; + let h = 3; + let mut raw_cells = vec![Cell::void(); (w * h) as usize]; + + for x in 0..w { + for y in 0..h { + let cell_idx = x + y * w; + raw_cells[cell_idx as usize] = Cell::from_material(MaterialId::Steel); + raw_cells[cell_idx as usize].set_entity(true); + } + } + + let entity_cells = EntityCells { + cells: raw_cells, + size: IVec2::new(w, h), + }; + + EntityDef::from_cells( + position, + entity_cells, + Some(Box::new(GrenadeEntityBehaviour { fuse })), + ) +} diff --git a/src/content/entities/mod.rs b/src/content/entities/mod.rs new file mode 100644 index 0000000..e78eb89 --- /dev/null +++ b/src/content/entities/mod.rs @@ -0,0 +1,2 @@ +pub mod entity_cube; +pub mod entity_grenade; diff --git a/src/content/materials/fire.rs b/src/content/materials/fire.rs new file mode 100644 index 0000000..483afbc --- /dev/null +++ b/src/content/materials/fire.rs @@ -0,0 +1,80 @@ +use rand::RngExt; + +use crate::{ + content::materials::MaterialId, + sim::{ + cell::cell::Cell, + cell_manager::sim::{PostUpdateAction, UpdateCtx}, + }, +}; + +pub trait FireCellView { + fn get_ticks_lived(self) -> u16; + fn set_ticks_lived(&mut self, ticks: u16) -> (); + fn is_flammable(self) -> bool; +} + +impl FireCellView for Cell { + fn get_ticks_lived(self) -> u16 { + self.data + } + fn set_ticks_lived(&mut self, ticks: u16) { + self.data = ticks; + } + // this could be a property of the material def, I think it's better here for now + fn is_flammable(self) -> bool { + [MaterialId::Wood].contains(&self.material) + } +} + +// TODO optimize number of rng calls? +// TODO wtf is fire +#[inline] +pub fn sim_update(ctx: &mut UpdateCtx) -> PostUpdateAction { + let ticks_lived = ctx.cell.get_ticks_lived(); + // 2 seconds + if ticks_lived > 240 { + // we have a chance to live longer--roughly ?% chance of living an extra second + if ctx.rng.random_range(0.0..1.0) > 0.9 { + // kill ourselves, with a chance to turn into ash + ctx.set_cell(0, 0, Cell::void()); + // the updater should take no action since we already killed ourselves + return PostUpdateAction::None; + } + } + + // at an average of 4 times per lifespan, try to spread + // 1/60 * 240 = 4 + if ctx.rng.random_range(0.0..1.0) > (59.0 / 60.0) { + let (dx, dy) = (ctx.rng.random_range(-1..=1), ctx.rng.random_range(-1..=1)); + if let Some(target) = ctx.get_cell(dx, dy) + && target.is_flammable() + { + ctx.set_cell(dx, dy, Cell::from_material(MaterialId::Fire)); + } + } + + // 8 times in our lifespan, emit smoke + if ticks_lived.is_multiple_of(30) + && let Some(target) = ctx.get_cell(0, -1) + && target.material == MaterialId::Void + { + for (dx, dy) in [ + (0, -1), + (1 - ctx.seqno_parity as i32 * 2, 0), + (-1 + ctx.seqno_parity as i32 * 2, 0), + (0, 1), + ] { + if let Some(target) = ctx.get_cell(dx, dy) + && target.material == MaterialId::Void + { + ctx.set_cell(dx, dy, Cell::from_material(MaterialId::Smoke)); + break; + } + } + } + + ctx.cell.set_ticks_lived(ticks_lived + 1); + // apply our new lifespan + PostUpdateAction::Apply +} diff --git a/src/content/materials/gas.rs b/src/content/materials/gas.rs new file mode 100644 index 0000000..229d0e9 --- /dev/null +++ b/src/content/materials/gas.rs @@ -0,0 +1,24 @@ +use rand::RngExt; + +use crate::sim::cell_manager::sim::{PostUpdateAction, UpdateCtx}; + +#[inline] +pub fn sim_update(ctx: &mut UpdateCtx) -> PostUpdateAction { + // only allow upward movement some of the time to limit movement speed + if ctx.rng.random_range(0.0..1.0) > 0.8 + && ctx.swap_or_settle(&[(0, -1)]) == PostUpdateAction::None + { + return PostUpdateAction::None; + } + // same for each horizontal direction + if ctx.rng.random_range(0.0..1.0) > 0.8 + && ctx.swap_or_settle(&[(1 - ctx.seqno_parity as i32 * 2, 0)]) == PostUpdateAction::None + { + return PostUpdateAction::None; + } + if ctx.rng.random_range(0.0..1.0) > 0.8 { + return ctx.swap_or_settle(&[(-1 + ctx.seqno_parity as i32 * 2, 0)]); + } + + PostUpdateAction::Apply +} diff --git a/src/content/materials/liquid.rs b/src/content/materials/liquid.rs new file mode 100644 index 0000000..6c3bdcd --- /dev/null +++ b/src/content/materials/liquid.rs @@ -0,0 +1,75 @@ +use crate::sim::cell_manager::sim::{PostUpdateAction, UpdateCtx}; + +#[inline] +pub fn sim_update(ctx: &mut UpdateCtx) -> PostUpdateAction { + // if the water can fall, do so + if ctx.swap_or_settle(&[ + (0, 1), + (-1 + 2 * ctx.seqno_parity as i32, 1), + (1 - 2 * ctx.seqno_parity as i32, 1), + ]) == PostUpdateAction::None + { + return PostUpdateAction::None; + } + + // if the water can't fall, check if we can move left or right + // these are inverted on parity so that we don't preference a direction + let left_target = ctx.get_cell(-1, 0); + let can_move_left = + left_target.is_some_and(|c| c.material.def().density < ctx.material.density); + let right_target = ctx.get_cell(1, 0); + let can_move_right = + right_target.is_some_and(|c| c.material.def().density < ctx.material.density); + + // we can't move down or to other side, so we're stuck + if !can_move_left && !can_move_right { + return PostUpdateAction::Settle; + } + + // if we can't move left, just move right + if !can_move_left { + return ctx.swap_or_settle(&[(1, 0)]); + } + // and vice versa + if !can_move_right { + return ctx.swap_or_settle(&[(-1, 0)]); + } + + // find the closest hole within 20 pixels (TODO optimize) + // a hole is any space below us with a lesser density + // prevents equidistance stuck state + let starting_side = if ctx.seqno_parity == 0 { 1 } else { -1 }; + for i in 0..20 { + let side = if i % 2 == 0 { + starting_side + } else { + -starting_side + }; + + let offset = side * (1 + i / 2); + let hole_target = ctx.get_cell(offset, 1); + if let Some(target) = hole_target + && target.material.def().density < ctx.material.density + { + // we identified a hole and we know that the space on this side is open + // move toward the hole + return ctx.swap_or_settle(&[(side, 0)]); + } + } + + return PostUpdateAction::Settle; + + // we didn't find a hole, so just move "randomly" on the same surface + // TODO when to settle? + // let target_x = if !can_move_left { + // 1 + // } else if !can_move_right { + // -1 + // } else if ctx.seqno_parity % 2 == 1 { + // 1 + // } else { + // -1 + // }; + + // ctx.candidates_swap(&[(target_x, 0)]); +} diff --git a/src/content/materials/mod.rs b/src/content/materials/mod.rs new file mode 100644 index 0000000..8116565 --- /dev/null +++ b/src/content/materials/mod.rs @@ -0,0 +1,111 @@ +use crate::sim::cell_manager::sim::{PostUpdateAction, UpdateCtx}; + +pub mod fire; +pub mod gas; +pub mod liquid; +pub mod powder; + +#[repr(u8)] +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum MaterialId { + Void = 0, + Sand, + Wood, + Water, + Fire, + Smoke, + Steel, +} + +#[repr(u8)] +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub enum MaterialForm { + Void, + Gas, + Liquid, + Powder, + Solid, +} + +pub struct MaterialDef { + pub name: &'static str, + pub color: (u8, u8, u8, u8), + pub density: u8, + pub form: MaterialForm, + pub sim_update: Option PostUpdateAction>, +} + +static MATERIALS: [MaterialDef; 7] = [ + MaterialDef { + name: "Void", + color: (0x00, 0x00, 0x00, 0x00), + density: 0, + // form usually but not always determines the update fn + form: MaterialForm::Void, + sim_update: None, + }, + MaterialDef { + name: "Sand", + color: (0xDE, 0xCB, 0x85, 0xFF), + density: 50, + form: MaterialForm::Powder, + sim_update: Some(powder::sim_update), + }, + MaterialDef { + name: "Wood", + color: (0x85, 0x56, 0x1D, 0xFF), + density: 50, + form: MaterialForm::Solid, + sim_update: None, + }, + MaterialDef { + name: "Water", + color: (0x38, 0xA9, 0xFF, 0xFF), + density: 40, + form: MaterialForm::Liquid, + sim_update: Some(liquid::sim_update), + }, + // TODO this is not a material + MaterialDef { + name: "Fire", + color: (0xFC, 0x66, 0x00, 0xAA), + // for now this matches wood + density: 50, + form: MaterialForm::Gas, + sim_update: Some(fire::sim_update), + }, + MaterialDef { + name: "Smoke", + color: (0x32, 0x35, 0x36, 0xAA), + density: 15, + form: MaterialForm::Gas, + sim_update: Some(gas::sim_update), + }, + MaterialDef { + name: "Steel", + color: (0x60, 0x60, 0x60, 0xFF), + density: 150, + form: MaterialForm::Solid, + sim_update: None, + }, +]; + +impl MaterialId { + pub const ALL: [MaterialId; 7] = [ + MaterialId::Void, + MaterialId::Sand, + MaterialId::Wood, + MaterialId::Water, + MaterialId::Fire, + MaterialId::Smoke, + MaterialId::Steel, + ]; + #[inline] + pub fn def(self) -> &'static MaterialDef { + &MATERIALS[self as usize] + } + + pub fn from_index(idx: u8) -> MaterialId { + MaterialId::ALL[idx as usize] + } +} diff --git a/src/content/materials/powder.rs b/src/content/materials/powder.rs new file mode 100644 index 0000000..8c916e4 --- /dev/null +++ b/src/content/materials/powder.rs @@ -0,0 +1,10 @@ +use crate::sim::cell_manager::sim::{PostUpdateAction, UpdateCtx}; + +#[inline] +pub fn sim_update(ctx: &mut UpdateCtx) -> PostUpdateAction { + ctx.swap_or_settle(&[ + (0, 1), + (-1 + 2 * ctx.seqno_parity as i32, 1), + (1 - 2 * ctx.seqno_parity as i32, 1), + ]) +} diff --git a/src/content/mod.rs b/src/content/mod.rs new file mode 100644 index 0000000..2044048 --- /dev/null +++ b/src/content/mod.rs @@ -0,0 +1,2 @@ +pub mod entities; +pub mod materials; diff --git a/src/main.rs b/src/main.rs index adb723c..492f0ba 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod camera; mod config; +mod content; mod renderer; mod sim; @@ -21,11 +22,14 @@ use winit::{ use crate::{ camera::Camera, config::WINDOW_TITLE, + content::{ + entities::{entity_cube::entity_cube_def, entity_grenade::entity_grenade_def}, + materials::MaterialId, + }, renderer::RendererState, sim::{ - cell::{cell::Cell, materials::MaterialId}, + cell::cell::Cell, cell_manager::manager::CellManager, - entity::entities::{entity_cube::entity_cube_def, entity_grenade::entity_grenade_def}, lib::force::apply_explosion, particle_manager::particle::Particle, rb_manager::DebugRenderMode, diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 2df15e6..c2e541b 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -9,9 +9,9 @@ use crate::{ Config, Diagnostics, Input, camera::Camera, config::{CELLS_IN_CHUNK, CHUNK_SIZE}, + content::materials::MaterialId, renderer::ui::draw_egui, sim::{ - cell::materials::MaterialId, cell_manager::manager::CellManager, entity::Entity, rb_manager::debug_render::DebugVertex, diff --git a/src/renderer/ui.rs b/src/renderer/ui.rs index 4c6d61c..a7395a0 100644 --- a/src/renderer/ui.rs +++ b/src/renderer/ui.rs @@ -2,10 +2,8 @@ use egui::{Color32, Stroke, Ui, epaint::CircleShape}; use crate::{ Camera, Config, Diagnostics, Input, - sim::{ - cell::materials::MaterialId, cell_manager::manager::CellManager, - rb_manager::DebugRenderMode, sim_manager::SimManager, - }, + content::materials::MaterialId, + sim::{rb_manager::DebugRenderMode, sim_manager::SimManager}, }; const DEBUG_RENDER_MODES: [(&str, DebugRenderMode); 6] = [ diff --git a/src/sim/cell/cell.rs b/src/sim/cell/cell.rs index 2a16d0c..6c6e704 100644 --- a/src/sim/cell/cell.rs +++ b/src/sim/cell/cell.rs @@ -1,4 +1,4 @@ -use crate::{config::SETTLED_THRESOHLD, sim::cell::materials::MaterialId}; +use crate::{config::SETTLED_THRESOHLD, content::materials::MaterialId}; #[derive(Clone, Copy)] pub struct Cell { diff --git a/src/sim/cell/materials/fire.rs b/src/sim/cell/materials/fire.rs deleted file mode 100644 index 8cabe30..0000000 --- a/src/sim/cell/materials/fire.rs +++ /dev/null @@ -1,77 +0,0 @@ -use rand::RngExt; - -use crate::sim::{ - cell::{cell::Cell, materials::MaterialId}, - cell_manager::sim::{PostUpdateAction, UpdateCtx}, -}; - -pub trait FireCellView { - fn get_ticks_lived(self) -> u16; - fn set_ticks_lived(&mut self, ticks: u16) -> (); - fn is_flammable(self) -> bool; -} - -impl FireCellView for Cell { - fn get_ticks_lived(self) -> u16 { - self.data - } - fn set_ticks_lived(&mut self, ticks: u16) { - self.data = ticks; - } - // this could be a property of the material def, I think it's better here for now - fn is_flammable(self) -> bool { - [MaterialId::Wood].contains(&self.material) - } -} - -// TODO optimize number of rng calls? -// TODO wtf is fire -#[inline] -pub fn sim_update(ctx: &mut UpdateCtx) -> PostUpdateAction { - let ticks_lived = ctx.cell.get_ticks_lived(); - // 2 seconds - if ticks_lived > 240 { - // we have a chance to live longer--roughly ?% chance of living an extra second - if ctx.rng.random_range(0.0..1.0) > 0.9 { - // kill ourselves, with a chance to turn into ash - ctx.set_cell(0, 0, Cell::void()); - // the updater should take no action since we already killed ourselves - return PostUpdateAction::None; - } - } - - // at an average of 4 times per lifespan, try to spread - // 1/60 * 240 = 4 - if ctx.rng.random_range(0.0..1.0) > (59.0 / 60.0) { - let (dx, dy) = (ctx.rng.random_range(-1..=1), ctx.rng.random_range(-1..=1)); - if let Some(target) = ctx.get_cell(dx, dy) - && target.is_flammable() - { - ctx.set_cell(dx, dy, Cell::from_material(MaterialId::Fire)); - } - } - - // 8 times in our lifespan, emit smoke - if ticks_lived.is_multiple_of(30) - && let Some(target) = ctx.get_cell(0, -1) - && target.material == MaterialId::Void - { - for (dx, dy) in [ - (0, -1), - (1 - ctx.seqno_parity as i32 * 2, 0), - (-1 + ctx.seqno_parity as i32 * 2, 0), - (0, 1), - ] { - if let Some(target) = ctx.get_cell(dx, dy) - && target.material == MaterialId::Void - { - ctx.set_cell(dx, dy, Cell::from_material(MaterialId::Smoke)); - break; - } - } - } - - ctx.cell.set_ticks_lived(ticks_lived + 1); - // apply our new lifespan - PostUpdateAction::Apply -} diff --git a/src/sim/cell/materials/gas.rs b/src/sim/cell/materials/gas.rs deleted file mode 100644 index 229d0e9..0000000 --- a/src/sim/cell/materials/gas.rs +++ /dev/null @@ -1,24 +0,0 @@ -use rand::RngExt; - -use crate::sim::cell_manager::sim::{PostUpdateAction, UpdateCtx}; - -#[inline] -pub fn sim_update(ctx: &mut UpdateCtx) -> PostUpdateAction { - // only allow upward movement some of the time to limit movement speed - if ctx.rng.random_range(0.0..1.0) > 0.8 - && ctx.swap_or_settle(&[(0, -1)]) == PostUpdateAction::None - { - return PostUpdateAction::None; - } - // same for each horizontal direction - if ctx.rng.random_range(0.0..1.0) > 0.8 - && ctx.swap_or_settle(&[(1 - ctx.seqno_parity as i32 * 2, 0)]) == PostUpdateAction::None - { - return PostUpdateAction::None; - } - if ctx.rng.random_range(0.0..1.0) > 0.8 { - return ctx.swap_or_settle(&[(-1 + ctx.seqno_parity as i32 * 2, 0)]); - } - - PostUpdateAction::Apply -} diff --git a/src/sim/cell/materials/liquid.rs b/src/sim/cell/materials/liquid.rs deleted file mode 100644 index 6c3bdcd..0000000 --- a/src/sim/cell/materials/liquid.rs +++ /dev/null @@ -1,75 +0,0 @@ -use crate::sim::cell_manager::sim::{PostUpdateAction, UpdateCtx}; - -#[inline] -pub fn sim_update(ctx: &mut UpdateCtx) -> PostUpdateAction { - // if the water can fall, do so - if ctx.swap_or_settle(&[ - (0, 1), - (-1 + 2 * ctx.seqno_parity as i32, 1), - (1 - 2 * ctx.seqno_parity as i32, 1), - ]) == PostUpdateAction::None - { - return PostUpdateAction::None; - } - - // if the water can't fall, check if we can move left or right - // these are inverted on parity so that we don't preference a direction - let left_target = ctx.get_cell(-1, 0); - let can_move_left = - left_target.is_some_and(|c| c.material.def().density < ctx.material.density); - let right_target = ctx.get_cell(1, 0); - let can_move_right = - right_target.is_some_and(|c| c.material.def().density < ctx.material.density); - - // we can't move down or to other side, so we're stuck - if !can_move_left && !can_move_right { - return PostUpdateAction::Settle; - } - - // if we can't move left, just move right - if !can_move_left { - return ctx.swap_or_settle(&[(1, 0)]); - } - // and vice versa - if !can_move_right { - return ctx.swap_or_settle(&[(-1, 0)]); - } - - // find the closest hole within 20 pixels (TODO optimize) - // a hole is any space below us with a lesser density - // prevents equidistance stuck state - let starting_side = if ctx.seqno_parity == 0 { 1 } else { -1 }; - for i in 0..20 { - let side = if i % 2 == 0 { - starting_side - } else { - -starting_side - }; - - let offset = side * (1 + i / 2); - let hole_target = ctx.get_cell(offset, 1); - if let Some(target) = hole_target - && target.material.def().density < ctx.material.density - { - // we identified a hole and we know that the space on this side is open - // move toward the hole - return ctx.swap_or_settle(&[(side, 0)]); - } - } - - return PostUpdateAction::Settle; - - // we didn't find a hole, so just move "randomly" on the same surface - // TODO when to settle? - // let target_x = if !can_move_left { - // 1 - // } else if !can_move_right { - // -1 - // } else if ctx.seqno_parity % 2 == 1 { - // 1 - // } else { - // -1 - // }; - - // ctx.candidates_swap(&[(target_x, 0)]); -} diff --git a/src/sim/cell/materials/mod.rs b/src/sim/cell/materials/mod.rs deleted file mode 100644 index 8116565..0000000 --- a/src/sim/cell/materials/mod.rs +++ /dev/null @@ -1,111 +0,0 @@ -use crate::sim::cell_manager::sim::{PostUpdateAction, UpdateCtx}; - -pub mod fire; -pub mod gas; -pub mod liquid; -pub mod powder; - -#[repr(u8)] -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub enum MaterialId { - Void = 0, - Sand, - Wood, - Water, - Fire, - Smoke, - Steel, -} - -#[repr(u8)] -#[derive(Clone, Copy, PartialEq, Eq, Debug)] -pub enum MaterialForm { - Void, - Gas, - Liquid, - Powder, - Solid, -} - -pub struct MaterialDef { - pub name: &'static str, - pub color: (u8, u8, u8, u8), - pub density: u8, - pub form: MaterialForm, - pub sim_update: Option PostUpdateAction>, -} - -static MATERIALS: [MaterialDef; 7] = [ - MaterialDef { - name: "Void", - color: (0x00, 0x00, 0x00, 0x00), - density: 0, - // form usually but not always determines the update fn - form: MaterialForm::Void, - sim_update: None, - }, - MaterialDef { - name: "Sand", - color: (0xDE, 0xCB, 0x85, 0xFF), - density: 50, - form: MaterialForm::Powder, - sim_update: Some(powder::sim_update), - }, - MaterialDef { - name: "Wood", - color: (0x85, 0x56, 0x1D, 0xFF), - density: 50, - form: MaterialForm::Solid, - sim_update: None, - }, - MaterialDef { - name: "Water", - color: (0x38, 0xA9, 0xFF, 0xFF), - density: 40, - form: MaterialForm::Liquid, - sim_update: Some(liquid::sim_update), - }, - // TODO this is not a material - MaterialDef { - name: "Fire", - color: (0xFC, 0x66, 0x00, 0xAA), - // for now this matches wood - density: 50, - form: MaterialForm::Gas, - sim_update: Some(fire::sim_update), - }, - MaterialDef { - name: "Smoke", - color: (0x32, 0x35, 0x36, 0xAA), - density: 15, - form: MaterialForm::Gas, - sim_update: Some(gas::sim_update), - }, - MaterialDef { - name: "Steel", - color: (0x60, 0x60, 0x60, 0xFF), - density: 150, - form: MaterialForm::Solid, - sim_update: None, - }, -]; - -impl MaterialId { - pub const ALL: [MaterialId; 7] = [ - MaterialId::Void, - MaterialId::Sand, - MaterialId::Wood, - MaterialId::Water, - MaterialId::Fire, - MaterialId::Smoke, - MaterialId::Steel, - ]; - #[inline] - pub fn def(self) -> &'static MaterialDef { - &MATERIALS[self as usize] - } - - pub fn from_index(idx: u8) -> MaterialId { - MaterialId::ALL[idx as usize] - } -} diff --git a/src/sim/cell/materials/powder.rs b/src/sim/cell/materials/powder.rs deleted file mode 100644 index 8c916e4..0000000 --- a/src/sim/cell/materials/powder.rs +++ /dev/null @@ -1,10 +0,0 @@ -use crate::sim::cell_manager::sim::{PostUpdateAction, UpdateCtx}; - -#[inline] -pub fn sim_update(ctx: &mut UpdateCtx) -> PostUpdateAction { - ctx.swap_or_settle(&[ - (0, 1), - (-1 + 2 * ctx.seqno_parity as i32, 1), - (1 - 2 * ctx.seqno_parity as i32, 1), - ]) -} diff --git a/src/sim/cell/mod.rs b/src/sim/cell/mod.rs index 2d2175c..48433da 100644 --- a/src/sim/cell/mod.rs +++ b/src/sim/cell/mod.rs @@ -1,2 +1 @@ pub mod cell; -pub mod materials; diff --git a/src/sim/cell_manager/chunk.rs b/src/sim/cell_manager/chunk.rs index 3cf34ff..29e6536 100644 --- a/src/sim/cell_manager/chunk.rs +++ b/src/sim/cell_manager/chunk.rs @@ -2,10 +2,8 @@ use glam::IVec2; use crate::{ config::{CELLS_IN_CHUNK, CHUNK_SIZE}, - sim::{ - cell::{cell::Cell, materials::MaterialForm}, - lib::marching_squares::Marchable, - }, + content::materials::MaterialForm, + sim::{cell::cell::Cell, lib::marching_squares::Marchable}, }; pub struct Chunk { diff --git a/src/sim/cell_manager/sim.rs b/src/sim/cell_manager/sim.rs index b79608e..0e67cad 100644 --- a/src/sim/cell_manager/sim.rs +++ b/src/sim/cell_manager/sim.rs @@ -6,8 +6,9 @@ use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use crate::{ config::{CHUNK_SIZE, SETTLED_THRESOHLD}, + content::materials::MaterialDef, sim::{ - cell::{cell::Cell, materials::MaterialDef}, + cell::cell::Cell, cell_manager::{chunk::Chunk, manager::CellManager}, }, }; diff --git a/src/sim/entity/entities/entity_cube.rs b/src/sim/entity/entities/entity_cube.rs deleted file mode 100644 index 2c79aa5..0000000 --- a/src/sim/entity/entities/entity_cube.rs +++ /dev/null @@ -1,27 +0,0 @@ -use glam::{IVec2, Vec2}; - -use crate::sim::{ - cell::{cell::Cell, materials::MaterialId}, - entity::{EntityCells, EntityDef}, -}; - -pub fn entity_cube_def(position: Vec2, material: MaterialId) -> EntityDef { - let w = 10; - let h = 10; - let mut raw_cells = vec![Cell::void(); (w * h) as usize]; - - for x in 0..w { - for y in 0..h { - let cell_idx = x + y * w; - raw_cells[cell_idx as usize] = Cell::from_material(material); - raw_cells[cell_idx as usize].set_entity(true); - } - } - - let entity_cells = EntityCells { - cells: raw_cells, - size: IVec2::new(w, h), - }; - - EntityDef::from_cells(position, entity_cells, None) -} diff --git a/src/sim/entity/entities/entity_grenade.rs b/src/sim/entity/entities/entity_grenade.rs deleted file mode 100644 index 952fb14..0000000 --- a/src/sim/entity/entities/entity_grenade.rs +++ /dev/null @@ -1,57 +0,0 @@ -use glam::{IVec2, Vec2}; - -use crate::sim::{ - cell::{cell::Cell, materials::MaterialId}, - entity::{EntityBehaviour, EntityCells, EntityDef, EntityUpdateCtx, SimCtx}, - lib::force::apply_explosion, -}; - -struct GrenadeEntityBehaviour { - pub fuse: f32, -} - -impl EntityBehaviour for GrenadeEntityBehaviour { - 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, - 15, - Vec2::new(0.0, -0.6), - 200.0, - ); - update_ctx.deferred_destroy(update_ctx.entity_data.id); - } - } -} - -pub fn entity_grenade_def(position: Vec2, fuse: f32) -> EntityDef { - let w = 3; - let h = 3; - let mut raw_cells = vec![Cell::void(); (w * h) as usize]; - - for x in 0..w { - for y in 0..h { - let cell_idx = x + y * w; - raw_cells[cell_idx as usize] = Cell::from_material(MaterialId::Steel); - raw_cells[cell_idx as usize].set_entity(true); - } - } - - let entity_cells = EntityCells { - cells: raw_cells, - size: IVec2::new(w, h), - }; - - EntityDef::from_cells( - position, - entity_cells, - Some(Box::new(GrenadeEntityBehaviour { fuse })), - ) -} diff --git a/src/sim/entity/entities/mod.rs b/src/sim/entity/entities/mod.rs deleted file mode 100644 index e78eb89..0000000 --- a/src/sim/entity/entities/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub mod entity_cube; -pub mod entity_grenade; diff --git a/src/sim/entity/mod.rs b/src/sim/entity/mod.rs index 0f4ae3c..4a79bf4 100644 --- a/src/sim/entity/mod.rs +++ b/src/sim/entity/mod.rs @@ -6,16 +6,13 @@ use rapier2d::{ use crate::{ config::{CELLS_TO_METRES, MASS_SCALING}, + content::materials::MaterialId, sim::{ - cell::{cell::Cell, materials::MaterialId}, - lib::marching_squares::Marchable, - rb_manager::RbManager, + cell::cell::Cell, lib::marching_squares::Marchable, rb_manager::RbManager, sim_manager::SimCtx, }, }; -pub mod entities; - pub struct EntityCells { pub size: IVec2, pub cells: Vec, diff --git a/src/sim/lib/force.rs b/src/sim/lib/force.rs index ff65fef..39b7904 100644 --- a/src/sim/lib/force.rs +++ b/src/sim/lib/force.rs @@ -1,13 +1,9 @@ use glam::Vec2; use rand::random_range; -use crate::sim::{ - cell::{ - cell::Cell, - materials::{MaterialForm, MaterialId, fire::FireCellView}, - }, - particle_manager::particle::Particle, - sim_manager::SimCtx, +use crate::{ + content::materials::{MaterialForm, MaterialId, fire::FireCellView}, + sim::{cell::cell::Cell, particle_manager::particle::Particle, sim_manager::SimCtx}, }; pub fn apply_explosion( diff --git a/src/sim/particle_manager/mod.rs b/src/sim/particle_manager/mod.rs index 8ecf6f5..052369b 100644 --- a/src/sim/particle_manager/mod.rs +++ b/src/sim/particle_manager/mod.rs @@ -1,9 +1,8 @@ use crate::{ config::CELLS_TO_METRES, + content::materials::MaterialForm, sim::{ - cell::{cell::Cell, materials::MaterialForm}, - cell_manager::manager::CellManager, - lib::ray::AwDda, + cell::cell::Cell, cell_manager::manager::CellManager, lib::ray::AwDda, particle_manager::particle::Particle, }, }; diff --git a/src/sim/particle_manager/particle.rs b/src/sim/particle_manager/particle.rs index cc7dd28..c68d81e 100644 --- a/src/sim/particle_manager/particle.rs +++ b/src/sim/particle_manager/particle.rs @@ -1,6 +1,6 @@ use glam::Vec2; -use crate::sim::cell::materials::MaterialId; +use crate::content::materials::MaterialId; pub struct Particle { pub position: Vec2, diff --git a/src/sim/sim_manager/mod.rs b/src/sim/sim_manager/mod.rs index 2b8bead..8f47761 100644 --- a/src/sim/sim_manager/mod.rs +++ b/src/sim/sim_manager/mod.rs @@ -6,8 +6,9 @@ use glam::IVec2; use crate::{ Config, config::{PHYSICS_DELTA_TIME, PHYSICS_FPS, SIM_FPS}, + content::materials::MaterialId, sim::{ - cell::{cell::Cell, materials::MaterialId}, + cell::cell::Cell, cell_manager::manager::CellManager, entity::{Entity, EntityDef}, particle_manager::ParticleManager, diff --git a/src/sim/sim_manager/utils.rs b/src/sim/sim_manager/utils.rs index 97bc1f3..b668359 100644 --- a/src/sim/sim_manager/utils.rs +++ b/src/sim/sim_manager/utils.rs @@ -1,6 +1,6 @@ use glam::IVec2; -use crate::sim::{cell::materials::MaterialId, sim_manager::SimManager}; +use crate::{content::materials::MaterialId, sim::sim_manager::SimManager}; pub fn write_entity_to_world( sim: &mut SimManager, -- cgit v1.3.1