1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
use fxhash::FxHashMap;
use crate::{
config::CHUNK_SIZE,
sim::{
cell::Cell,
cell_manager::{chunk::Chunk, sim::sim_tick},
},
};
pub struct CellManager {
pub seqno: u64,
pub chunks: Vec<Chunk>,
pub chunk_position_to_chunk_idx: FxHashMap<(i32, i32), usize>,
}
impl CellManager {
#[inline]
pub fn split_game_position(x: i32, y: i32) -> ((i32, i32), (u8, u8)) {
(
(x.div_euclid(CHUNK_SIZE), y.div_euclid(CHUNK_SIZE)),
(
x.rem_euclid(CHUNK_SIZE) as u8,
y.rem_euclid(CHUNK_SIZE) as u8,
),
)
}
// VERY EXPENSIVE
pub fn get_cell_from_game_position(&self, x: i32, y: i32) -> Option<Cell> {
let ((cx, cy), (dx, dy)) = CellManager::split_game_position(x, y);
self.chunk_position_to_chunk_idx
.get(&(cx, cy))
.map(|&idx| self.chunks[idx].get_cell_at_local_position(dx, dy))
}
// VERY EXPENSIVE
pub fn set_cell_from_game_position(&mut self, x: i32, y: i32, cell: Cell, sleeping: bool) {
let ((cx, cy), (dx, dy)) = CellManager::split_game_position(x, y);
if let Some(&idx) = self.chunk_position_to_chunk_idx.get(&(cx, cy)) {
self.chunks[idx].set_cell_at_local_position(dx, dy, cell);
// this is a temporary hack
if !sleeping {
self.chunks[idx].sleeping = false;
self.chunks[idx].collider_dirty_seqno = Some(0);
}
self.chunks[idx].needs_texture_update = true;
}
}
pub fn insert(&mut self, x: i32, y: i32, chunk: Chunk) {
self.chunk_position_to_chunk_idx
.insert((x, y), self.chunks.len());
self.chunks.push(chunk);
}
pub fn tick(&mut self, use_threading: bool) {
let seqno = self.seqno;
sim_tick(self, seqno, use_threading);
self.seqno += 1;
}
pub fn from_default_size() -> Self {
let mut world = CellManager {
seqno: 0,
chunks: Vec::new(),
chunk_position_to_chunk_idx: FxHashMap::default(),
};
for y in -10..1 {
for x in -100..100 {
if y == 0 {
world.insert(x, y, Chunk::floor());
} else {
world.insert(x, y, Chunk::void());
}
}
}
world
}
}
|