summaryrefslogtreecommitdiff
path: root/src/sim/materials
diff options
context:
space:
mode:
Diffstat (limited to 'src/sim/materials')
-rw-r--r--src/sim/materials/fire.rs53
-rw-r--r--src/sim/materials/mod.rs14
2 files changed, 65 insertions, 2 deletions
diff --git a/src/sim/materials/fire.rs b/src/sim/materials/fire.rs
new file mode 100644
index 0000000..b155986
--- /dev/null
+++ b/src/sim/materials/fire.rs
@@ -0,0 +1,53 @@
+use rand::RngExt;
+
+use crate::sim::{cell::Cell, materials::MaterialId, sim::UpdateCtx};
+
+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)
+ }
+}
+
+#[inline]
+pub fn sim_update(ctx: &mut UpdateCtx) {
+ let ticks_lived = ctx.cell.get_ticks_lived();
+ // 2 seconds
+ if ticks_lived > 240 {
+ // kill ourselves, with a chance to turn into ash
+ if ctx.rng.random_range(0.0..1.0) > 0.8 {
+ // TODO ash material
+ ctx.set_cell(0, 0, Cell::from_material(MaterialId::Sand));
+ } else {
+ ctx.set_cell(0, 0, Cell::void());
+ }
+ return;
+ }
+
+ // 4 times in our lifespan, try to spread in a random direction
+ // since it's random, fire may die out if there's nothing in that direction
+ // a 1-thick line of flammable cells has a 31% chance to die each iteration
+ if ticks_lived % 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));
+ }
+ }
+
+ ctx.cell.set_ticks_lived(ticks_lived + 1);
+ ctx.set_cell(0, 0, *ctx.cell);
+}
diff --git a/src/sim/materials/mod.rs b/src/sim/materials/mod.rs
index d2d0e0e..764318a 100644
--- a/src/sim/materials/mod.rs
+++ b/src/sim/materials/mod.rs
@@ -1,5 +1,6 @@
use crate::sim::sim::UpdateCtx;
+mod fire;
mod gas;
mod sand;
mod water;
@@ -12,6 +13,7 @@ pub enum MaterialId {
Wood,
Water,
Gas,
+ Fire,
}
pub struct MaterialDef {
@@ -21,7 +23,7 @@ pub struct MaterialDef {
pub sim_update: Option<fn(ctx: &mut UpdateCtx) -> ()>,
}
-static MATERIALS: [MaterialDef; 5] = [
+static MATERIALS: [MaterialDef; 6] = [
MaterialDef {
name: "Void",
color: (0x00, 0x00, 0x00, 0x00),
@@ -52,15 +54,23 @@ static MATERIALS: [MaterialDef; 5] = [
density: 10,
sim_update: Some(gas::sim_update),
},
+ MaterialDef {
+ name: "Fire",
+ color: (0xFC, 0x66, 0x00, 0xAA),
+ // for now this matches wood
+ density: 50,
+ sim_update: Some(fire::sim_update),
+ },
];
impl MaterialId {
- pub const ALL: [MaterialId; 5] = [
+ pub const ALL: [MaterialId; 6] = [
MaterialId::Void,
MaterialId::Sand,
MaterialId::Wood,
MaterialId::Water,
MaterialId::Gas,
+ MaterialId::Fire,
];
#[inline]
pub fn def(self) -> &'static MaterialDef {