summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/renderer/mod.rs17
-rw-r--r--src/sim/cell_manager/chunk.rs13
-rw-r--r--src/sim/cell_manager/sim.rs12
-rw-r--r--src/sim/sim_manager/mod.rs7
4 files changed, 33 insertions, 16 deletions
diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs
index a07bd5b..127a785 100644
--- a/src/renderer/mod.rs
+++ b/src/renderer/mod.rs
@@ -944,14 +944,15 @@ impl RendererState {
&& let Some((pos, (cos, sin))) = entity.data._transform(&sim.rb_manager)
{
// TODO access this better
- let sleeping = sim
- .rb_manager
- .physics_manager
- .world
- .bodies
- .get(entity.data.rb_h.unwrap())
- .unwrap()
- .is_sleeping();
+ // let sleeping = sim
+ // .rb_manager
+ // .physics_manager
+ // .world
+ // .bodies
+ // .get(entity.data.rb_h.unwrap())
+ // .unwrap()
+ // .is_sleeping();
+ let sleeping = false;
entities += 1;
instances.push(RendererInstance {
diff --git a/src/sim/cell_manager/chunk.rs b/src/sim/cell_manager/chunk.rs
index 17e1551..48e9d16 100644
--- a/src/sim/cell_manager/chunk.rs
+++ b/src/sim/cell_manager/chunk.rs
@@ -1,7 +1,7 @@
use glam::IVec2;
use crate::{
- config::{CELLS_IN_CHUNK, CHUNK_SIZE},
+ config::{CELLS_IN_CHUNK, CHUNK_SIZE, SETTLED_THRESOHLD},
content::materials::{MaterialForm, MaterialId},
sim::{cell::Cell, lib::marching_squares::Marchable},
};
@@ -10,6 +10,7 @@ pub struct Chunk {
pub cells: Box<[Cell; CELLS_IN_CHUNK]>,
pub sleeping: bool,
pub needs_texture_update: bool,
+ pub collider_dirty_seqno: Option<u64>,
}
impl Chunk {
@@ -22,11 +23,18 @@ impl Chunk {
self.cells[x as usize + y as usize * CHUNK_SIZE as usize] = cell;
}
+ pub fn mark_collider_dirty(&mut self, seqno: u64) {
+ if self.collider_dirty_seqno.is_none() {
+ self.collider_dirty_seqno = Some(seqno);
+ }
+ }
+
pub fn void() -> Self {
Chunk {
cells: Box::new([Cell::void(); CELLS_IN_CHUNK]),
sleeping: true,
needs_texture_update: true,
+ collider_dirty_seqno: None,
}
}
@@ -35,6 +43,7 @@ impl Chunk {
cells: Box::new([Cell::void(); CELLS_IN_CHUNK]),
sleeping: true,
needs_texture_update: true,
+ collider_dirty_seqno: Some(0),
};
for dy in 1..5 {
@@ -60,7 +69,7 @@ impl Marchable for Chunk {
let cell = self.get_cell_at_local_position(pos.x as u8, pos.y as u8);
let material = cell.material.def();
material.form == MaterialForm::Solid
- || material.form == MaterialForm::Powder && cell.settled() > 4
+ || material.form == MaterialForm::Powder && cell.settled() >= SETTLED_THRESOHLD
}
}
fn size(&self) -> IVec2 {
diff --git a/src/sim/cell_manager/sim.rs b/src/sim/cell_manager/sim.rs
index ffa253b..6b3a0de 100644
--- a/src/sim/cell_manager/sim.rs
+++ b/src/sim/cell_manager/sim.rs
@@ -128,7 +128,7 @@ fn adjacent_chunks(x: u8, y: u8) -> Vec<usize> {
}
}
-fn internal_set_cell(chunks: &mut [Option<&mut Chunk>; 9], x: i32, y: i32, cell: Cell) {
+fn internal_set_cell(chunks: &mut [Option<&mut Chunk>; 9], x: i32, y: i32, cell: Cell, seqno: u64) {
let cx = x.div_euclid(CHUNK_SIZE);
let cy = y.div_euclid(CHUNK_SIZE);
let lx = x.rem_euclid(CHUNK_SIZE) as u8;
@@ -139,10 +139,12 @@ fn internal_set_cell(chunks: &mut [Option<&mut Chunk>; 9], x: i32, y: i32, cell:
chunk.set_cell_at_local_position(lx, ly, cell);
chunk.needs_texture_update = true;
chunk.sleeping = false;
+ chunk.mark_collider_dirty(seqno);
// if we're at the boundaries of the chunk, wake the adjacent chunk(s)
for idx in adjacent_chunks(lx, ly) {
if let Some(chunk) = chunks[idx].as_mut() {
chunk.sleeping = false;
+ chunk.mark_collider_dirty(seqno);
}
}
}
@@ -151,10 +153,12 @@ fn internal_set_cell(chunks: &mut [Option<&mut Chunk>; 9], x: i32, y: i32, cell:
target.set_cell_at_local_position(x as u8, y as u8, cell);
target.needs_texture_update = true;
target.sleeping = false;
+ target.mark_collider_dirty(seqno);
// if we're at the boundaries of the chunk, wake the adjacent chunk(s)
for idx in adjacent_chunks(lx, ly) {
if let Some(chunk) = chunks[idx].as_mut() {
chunk.sleeping = false;
+ chunk.mark_collider_dirty(seqno);
}
}
}
@@ -200,7 +204,7 @@ impl UpdateCtx<'_, '_, '_> {
debug_assert!(dy > -CHUNK_SIZE + 1 && dy < CHUNK_SIZE - 1);
let x = self.x + dx;
let y = self.y + dy;
- internal_set_cell(self.chunks, x, y, cell);
+ internal_set_cell(self.chunks, x, y, cell, self.seqno);
}
pub fn swap_or_settle(&mut self, candidates: &[(i32, i32)]) -> PostUpdateAction {
@@ -267,12 +271,12 @@ fn sim_tick_chunk(chunks: &mut [Option<&mut Chunk>; 9], seqno: u64) {
if cell.settled() < SETTLED_THRESOHLD {
cell.match_parity(seqno + 1);
cell.increment_settled();
- internal_set_cell(chunks, x, y, cell);
+ internal_set_cell(chunks, x, y, cell, seqno);
}
}
PostUpdateAction::Apply => {
cell.match_parity(seqno + 1);
- internal_set_cell(chunks, x, y, cell);
+ internal_set_cell(chunks, x, y, cell, seqno);
}
}
}
diff --git a/src/sim/sim_manager/mod.rs b/src/sim/sim_manager/mod.rs
index 8849a1d..432ac0b 100644
--- a/src/sim/sim_manager/mod.rs
+++ b/src/sim/sim_manager/mod.rs
@@ -242,10 +242,13 @@ impl SimManager {
.cell_manager
.chunk_position_to_chunk_idx
.get(&(cx, cy))
- .map(|&idx| &self.cell_manager.chunks[idx])
- && !chunk.sleeping
+ .and_then(|&idx| self.cell_manager.chunks.get_mut(idx))
+ && let Some(ds) = chunk.collider_dirty_seqno
+ // debounce
+ && self.cell_manager.seqno - ds > 5
{
self.rb_manager.upsert_chunk_collider(cx, cy, chunk);
+ chunk.collider_dirty_seqno = None;
}
}
}