Skip to content

Commit 612e95c

Browse files
committed
move stuff out of cell struct until benchmarks
1 parent 157d2d8 commit 612e95c

5 files changed

Lines changed: 44 additions & 24 deletions

File tree

src/world/cell.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub type CellId = u16;
55
pub struct CellData {
66
pub color: CellColor,
77
pub name: &'static str,
8+
pub density: u8,
89
pub type_data: CellDataType,
910
}
1011
#[derive(Debug)]
@@ -81,22 +82,19 @@ impl CellColor {
8182
}
8283
pub const AIR: Self = Self::new(0x00, 0x00, 0x00, 0x00);
8384
}
84-
#[derive(Clone)]
85+
#[derive(Clone, Debug)]
8586
pub struct Cell {
86-
pub color: CellColor,
8787
pub cell_data: &'static CellData,
88-
pub id: CellId,
89-
pub cell_type: CellType,
9088
pub last_changed: u8,
9189
pub velocity: CellVelocity,
9290
pub friction: CellFriction,
9391
}
94-
#[derive(Clone, Default)]
92+
#[derive(Clone, Default, Debug)]
9593
pub struct CellVelocity {
9694
pub x: i8,
9795
pub y: i8,
9896
}
99-
#[derive(Clone, Default)]
97+
#[derive(Clone, Default, Debug)]
10098
pub struct CellFriction {
10199
pub x: u8,
102100
pub y: u8,
@@ -106,18 +104,15 @@ impl Cell {
106104
pub fn new(id: CellId) -> Self {
107105
let cell_data = &CELLS[id.strict_cast::<usize>()];
108106
Self {
109-
color: cell_data.color,
110107
cell_data,
111-
id,
112-
cell_type: cell_data.type_data.cell_type(),
113108
last_changed: 0,
114109
velocity: CellVelocity::default(),
115110
friction: CellFriction::default(),
116111
}
117112
}
118113
pub fn friction(&mut self, x: u8, y: u8) {
119114
fn run(f: &mut i8, r: &mut u8, i: u8) {
120-
let (n, over) = r.overflowing_shl(i.strict_cast());
115+
let (n, over) = r.overflowing_add(i);
121116
*r = n;
122117
if over {
123118
if f.is_positive() {
@@ -130,26 +125,41 @@ impl Cell {
130125
run(&mut self.velocity.x, &mut self.friction.x, x);
131126
run(&mut self.velocity.y, &mut self.friction.y, y);
132127
}
128+
pub fn gravity(&mut self) {
129+
self.velocity.y -= 1;
130+
}
133131
#[must_use]
134132
pub fn is_air(&self) -> bool {
135-
self.color.is_air()
133+
self.color().is_air()
136134
}
137135
#[must_use]
138136
pub fn is_collider(&self) -> bool {
139-
matches!(self.cell_type, CellType::Static | CellType::Granular)
137+
matches!(self.cell_type(), CellType::Static | CellType::Granular)
138+
}
139+
#[must_use]
140+
pub fn color(&self) -> CellColor {
141+
self.cell_data.color
142+
}
143+
#[must_use]
144+
pub fn cell_type(&self) -> CellType {
145+
self.cell_data.type_data.cell_type()
146+
}
147+
#[must_use]
148+
pub fn id(&self) -> CellId {
149+
((ptr::from_ref(self.cell_data).addr() - ptr::from_ref(&CELLS).addr())
150+
/ size_of::<CellData>())
151+
.strict_cast()
152+
}
153+
#[must_use]
154+
pub fn density(&self) -> u8 {
155+
self.cell_data.density
140156
}
141157
#[must_use]
142158
pub fn can_move(&self, other: &Self) -> bool {
143-
matches!(
144-
other.cell_type,
145-
CellType::Liquid | CellType::Gas | CellType::Air
146-
) && !ptr::eq(self.cell_data, other.cell_data)
159+
self.density() > other.density()
147160
}
148161
pub fn into(&mut self, id: CellId) {
149162
let cell_data = &CELLS[id.strict_cast::<usize>()];
150-
self.id = id;
151163
self.cell_data = cell_data;
152-
self.cell_type = cell_data.type_data.cell_type();
153-
self.color = cell_data.color;
154164
}
155165
}

src/world/cells.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,69 @@ pub static CELLS: [CellData; 10] = [
88
color: CellColor::AIR,
99
name: "air",
1010
type_data: CellDataType::Air,
11+
density: 0,
1112
},
1213
//1
1314
CellData {
1415
color: CellColor::new(0xff, 0x6a, 0x00, 0xff),
1516
name: "fire",
1617
type_data: CellDataType::Fire(CellDataFire {}),
18+
density: 1,
1719
},
1820
//2
1921
CellData {
2022
color: CellColor::new(0x2f, 0x8f, 0xff, 0xff),
2123
name: "water",
2224
type_data: CellDataType::Liquid(CellDataLiquid {}),
25+
density: 64,
2326
},
2427
//3
2528
CellData {
2629
color: CellColor::new(0xd8, 0xd8, 0xd8, 0xff),
2730
name: "steam",
2831
type_data: CellDataType::Gas(CellDataGas {}),
32+
density: 32,
2933
},
3034
//4
3135
CellData {
3236
color: CellColor::new(0xb8, 0xf0, 0xff, 0xff),
3337
name: "ice",
3438
type_data: CellDataType::Static(CellDataStatic {}),
39+
density: 255,
3540
},
3641
//5
3742
CellData {
3843
color: CellColor::new(0x8b, 0x5a, 0x2b, 0xff),
3944
name: "dirt",
4045
type_data: CellDataType::Static(CellDataStatic {}),
46+
density: 255,
4147
},
4248
//6
4349
CellData {
4450
color: CellColor::new(0x7a, 0x7a, 0x7a, 0xff),
4551
name: "stone",
4652
type_data: CellDataType::Static(CellDataStatic {}),
53+
density: 255,
4754
},
4855
//7
4956
CellData {
5057
color: CellColor::new(0xc8, 0xc8, 0xd0, 0xff),
5158
name: "iron",
5259
type_data: CellDataType::Static(CellDataStatic {}),
60+
density: 255,
5361
},
5462
//8
5563
CellData {
5664
color: CellColor::new(0x6b, 0x44, 0x22, 0xff),
5765
name: "wood",
5866
type_data: CellDataType::Static(CellDataStatic {}),
67+
density: 255,
5968
},
6069
//9
6170
CellData {
6271
color: CellColor::new(0xcb, 0xbd, 0x93, 0xff),
6372
name: "sand",
6473
type_data: CellDataType::Granular(CellDataGranular {}),
74+
density: 128,
6575
},
6676
];

src/world/pixel_run.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ impl PixelRunBuilder {
8080
}
8181
pub fn write_chunk(&mut self, chunk: &Chunk) {
8282
let mut iter = chunk.cells.iter();
83-
self.current = iter.next().unwrap().id;
83+
self.current = iter.next().unwrap().id();
8484
for cell in iter {
85-
self.push(cell.id);
85+
self.push(cell.id());
8686
}
8787
}
8888
pub fn write(&self, mut file: File) {

src/world/simulate_world.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ impl World {
200200
return;
201201
}
202202
cell.last_changed = frame;
203-
match cell.cell_type {
203+
match cell.cell_type() {
204204
CellType::Liquid => {
205205
let check = if rand.half() {
206206
[
@@ -335,7 +335,7 @@ impl Chunk {
335335
return;
336336
}
337337
self[index].last_changed = frame;
338-
match self[index].cell_type {
338+
match self[index].cell_type() {
339339
CellType::Liquid => {
340340
let check = if rand.half() {
341341
[

src/world/world_image.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(crate) fn write_data(world: &World, chunks: &mut [[u8; 4]], sx: u16, ex: u16
6262
for (x, c) in (sx..).zip(arr.iter_mut()) {
6363
let idx = FullIndex::from((x, y));
6464
*c = <[u8; 4]>::from(if let Some(cell) = world.get(idx) {
65-
cell.color
65+
cell.color()
6666
} else {
6767
CellColor::AIR
6868
});

0 commit comments

Comments
 (0)