summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/camera.rs10
-rw-r--r--src/main.rs45
-rw-r--r--src/renderer/mod.rs3
-rw-r--r--src/sim/chunk.rs2
-rw-r--r--src/sim/materials/fire.rs33
-rw-r--r--src/sim/materials/smoke.rs24
-rw-r--r--src/sim/sim.rs52
-rw-r--r--src/sim/world.rs10
8 files changed, 76 insertions, 103 deletions
diff --git a/src/camera.rs b/src/camera.rs
index d14adbd..bd4f290 100644
--- a/src/camera.rs
+++ b/src/camera.rs
@@ -50,11 +50,11 @@ impl Camera {
}
let magnitude = (x.powi(2) + y.powi(2)).sqrt();
- let adjusted_x = x / magnitude * self.zoom as f32 * CAMERA_MOVEMENT_SPEED * delta_time;
- let adjusted_y = y / magnitude * self.zoom as f32 * CAMERA_MOVEMENT_SPEED * delta_time;
+ let adjusted_x = x / magnitude * self.zoom * CAMERA_MOVEMENT_SPEED * delta_time;
+ let adjusted_y = y / magnitude * self.zoom * CAMERA_MOVEMENT_SPEED * delta_time;
- self.centre.0 += adjusted_x as f32;
- self.centre.1 += adjusted_y as f32;
+ self.centre.0 += adjusted_x;
+ self.centre.1 += adjusted_y;
}
pub fn screen_position_to_world(&self, x: f32, y: f32) -> (f32, f32) {
@@ -76,7 +76,7 @@ impl Camera {
}
}
- pub fn resize(&mut self, screen_size: (i32, i32)) -> () {
+ pub fn resize(&mut self, screen_size: (i32, i32)) {
self.screen_size = screen_size;
}
diff --git a/src/main.rs b/src/main.rs
index 7cac1f3..bb4cb27 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -81,11 +81,10 @@ struct App {
impl App {
// called as often as possible
// delta time is the real seconds elapsed since the last time this was called
- fn update(&mut self, delta_time: f32) -> () {
+ fn update(&mut self, delta_time: f32) {
// apply inputs
- match &mut self.camera {
- Some(camera) => camera.handle_camera_input(&self.input, delta_time),
- _ => {}
+ if let Some(camera) = &mut self.camera {
+ camera.handle_camera_input(&self.input, delta_time)
}
// // --TEST DRAWING--
@@ -110,11 +109,10 @@ impl App {
let mut cell = Cell::from_material(self.config.brush_material);
// ensure we simulate on the first tick
cell.flags = (self.sim_seqno as u8) & 0b1;
- match &mut self.world {
- Some(world) => world.set_cell_from_game_position(
+ if let Some(world) = &mut self.world {
+ world.set_cell_from_game_position(
x, y, cell, false, // wake the chunk
- ),
- _ => {}
+ )
}
}
}
@@ -125,20 +123,17 @@ impl App {
// will be called before the render and before the physics update(s)
// may be called multiple times if the sim time is behind
// sim_delta_time is statically 1/SIM_FPS
- fn sim_update(&mut self, sim_delta_time: f32) -> () {
- match &mut self.world {
- Some(world) => {
- sim_tick(world, self.sim_seqno, self.config.use_threading);
- self.sim_seqno += 1;
- }
- _ => {}
+ fn sim_update(&mut self, _sim_delta_time: f32) {
+ if let Some(world) = &mut self.world {
+ sim_tick(world, self.sim_seqno, self.config.use_threading);
+ self.sim_seqno += 1;
}
}
// called PHYSICS_FPS times per second
// will be called before the render
// may be called multiple times if the physics time is behind
// physics_delta_time is statically 1/PHYSICS_FPS
- fn physics_update(&mut self, physics_delta_time: f32) -> () {}
+ fn physics_update(&mut self, _physics_delta_time: f32) {}
}
impl Default for App {
@@ -216,7 +211,7 @@ impl ApplicationHandler for App {
if let Some(renderer_state) = &mut self.renderer_state
&& let Some(window) = &mut self.window
{
- let egui_response = renderer_state.egui_state.on_window_event(&window, &event);
+ let egui_response = renderer_state.egui_state.on_window_event(window, &event);
// if egui consumed the event, it means we shouldn't treat any e.g., mouse clicks
if egui_response.consumed {
return;
@@ -245,21 +240,15 @@ impl ApplicationHandler for App {
self.sim_paused = !self.sim_paused
}
}
- KeyCode::KeyX => {
- if pressed {
- self.ignore_pause_next_tick = true
- }
- }
+ KeyCode::KeyX if pressed => self.ignore_pause_next_tick = true,
_ => {}
}
}
WindowEvent::CursorMoved { position, .. } => {
self.input.last_mouse_pos_on_screen = Some((position.x as f32, position.y as f32));
- self.input.last_mouse_world_pos = if let Some(camera) = &mut self.camera {
- Some(camera.screen_position_to_world(position.x as f32, position.y as f32))
- } else {
- None
- }
+ self.input.last_mouse_world_pos = self.camera.as_mut().map(|camera| {
+ camera.screen_position_to_world(position.x as f32, position.y as f32)
+ })
}
WindowEvent::MouseInput { state, button, .. } => {
if button == MouseButton::Left {
@@ -351,7 +340,7 @@ impl ApplicationHandler for App {
camera,
&mut self.config,
&self.diagnostics,
- &mut self.input,
+ &self.input,
);
}
}
diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs
index 1ce461b..5231bc3 100644
--- a/src/renderer/mod.rs
+++ b/src/renderer/mod.rs
@@ -376,8 +376,7 @@ impl RendererState {
);
// write the chunk textures
- let mut chunk_buffer: [u8; (CELLS_IN_CHUNK * 4) as usize] =
- [0; (CELLS_IN_CHUNK * 4) as usize];
+ let mut chunk_buffer: [u8; CELLS_IN_CHUNK * 4] = [0; (CELLS_IN_CHUNK * 4)];
{
puffin::profile_scope!("Upload chunk textures");
diff --git a/src/sim/chunk.rs b/src/sim/chunk.rs
index 1980932..478af12 100644
--- a/src/sim/chunk.rs
+++ b/src/sim/chunk.rs
@@ -4,7 +4,7 @@ use crate::{
};
pub struct Chunk {
- pub cells: Box<[Cell; CELLS_IN_CHUNK as usize]>,
+ pub cells: Box<[Cell; CELLS_IN_CHUNK]>,
pub sleeping: bool,
pub needs_texture_update: bool,
}
diff --git a/src/sim/materials/fire.rs b/src/sim/materials/fire.rs
index 3c686fe..97027d5 100644
--- a/src/sim/materials/fire.rs
+++ b/src/sim/materials/fire.rs
@@ -12,7 +12,7 @@ impl FireCellView for Cell {
fn get_ticks_lived(self) -> u16 {
self.data
}
- fn set_ticks_lived(&mut self, ticks: u16) -> () {
+ 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
@@ -52,22 +52,21 @@ pub fn sim_update(ctx: &mut UpdateCtx) {
}
// 8 times in our lifespan, emit smoke
- if ticks_lived % 30 == 0 {
- if 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;
- }
+ 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;
}
}
}
diff --git a/src/sim/materials/smoke.rs b/src/sim/materials/smoke.rs
index 68f77ca..5240bc9 100644
--- a/src/sim/materials/smoke.rs
+++ b/src/sim/materials/smoke.rs
@@ -1,24 +1,20 @@
use rand::RngExt;
-use crate::sim::{cell::Cell, materials::MaterialId, sim::UpdateCtx};
+use crate::sim::sim::UpdateCtx;
#[inline]
pub fn sim_update(ctx: &mut UpdateCtx) {
// only allow upward movement some of the time to limit movement speed
- if ctx.rng.random_range(0.0..1.0) > 0.8 {
- if ctx.candidates_swap(&[(0, -1)]) {
- return;
- }
+ if ctx.rng.random_range(0.0..1.0) > 0.8 && ctx.candidates_swap(&[(0, -1)]) {
+ return;
}
// same for each horizontal direction
- if ctx.rng.random_range(0.0..1.0) > 0.8 {
- if ctx.candidates_swap(&[(1 - ctx.seqno_parity as i32 * 2, 0)]) {
- return;
- }
- }
- if ctx.rng.random_range(0.0..1.0) > 0.8 {
- if ctx.candidates_swap(&[(-1 + ctx.seqno_parity as i32 * 2, 0)]) {
- return;
- }
+ if ctx.rng.random_range(0.0..1.0) > 0.8
+ && ctx.candidates_swap(&[(1 - ctx.seqno_parity as i32 * 2, 0)])
+ {
+ return;
}
+ if ctx.rng.random_range(0.0..1.0) > 0.8
+ && ctx.candidates_swap(&[(-1 + ctx.seqno_parity as i32 * 2, 0)])
+ {}
}
diff --git a/src/sim/sim.rs b/src/sim/sim.rs
index 84c3126..b9f4b76 100644
--- a/src/sim/sim.rs
+++ b/src/sim/sim.rs
@@ -39,17 +39,13 @@ fn get_cell(chunks: &[Option<&mut Chunk>; 9], x: i32, y: i32) -> Option<Cell> {
let nc_x = x.rem_euclid(CHUNK_SIZE) as u8;
let nc_y = y.rem_euclid(CHUNK_SIZE) as u8;
- return if let Some(chunk) = &chunks[(dcx + 1 + (dcy + 1) * 3) as usize] {
- Some(chunk.get_cell_at_local_position(nc_x, nc_y))
- } else {
- None
- };
+ chunks[(dcx + 1 + (dcy + 1) * 3) as usize]
+ .as_ref()
+ .map(|chunk| chunk.get_cell_at_local_position(nc_x, nc_y))
} else {
- if let Some(target) = &chunks[4] {
- Some(target.get_cell_at_local_position(x as u8, y as u8))
- } else {
- None
- }
+ chunks[4]
+ .as_ref()
+ .map(|target| target.get_cell_at_local_position(x as u8, y as u8))
}
}
@@ -138,28 +134,28 @@ pub fn sim_tick_chunk(chunks: &mut [Option<&mut Chunk>; 9], seqno: u64) {
let mut cell = get_cell(chunks, x, y).unwrap();
let material = cell.material.def();
- if let Some(update) = material.sim_update {
- if cell.parity() == seqno_parity {
- cell.flip_parity();
- // apply the flipped parity in case the sim target doesn't
- set_cell(chunks, x, y, cell);
+ if let Some(update) = material.sim_update
+ && cell.parity() == seqno_parity
+ {
+ cell.flip_parity();
+ // apply the flipped parity in case the sim target doesn't
+ set_cell(chunks, x, y, cell);
- let mut update_ctx = UpdateCtx {
- chunks,
- seqno,
- seqno_parity,
+ let mut update_ctx = UpdateCtx {
+ chunks,
+ seqno,
+ seqno_parity,
- x,
- y,
- cell: &mut cell,
- material,
+ x,
+ y,
+ cell: &mut cell,
+ material,
- // TODO this is platform-dependent, will break for multiplayer
- rng: &mut rng,
- };
+ // TODO this is platform-dependent, will break for multiplayer
+ rng: &mut rng,
+ };
- update(&mut update_ctx);
- }
+ update(&mut update_ctx);
}
}
}
diff --git a/src/sim/world.rs b/src/sim/world.rs
index 5993b10..d54906c 100644
--- a/src/sim/world.rs
+++ b/src/sim/world.rs
@@ -36,13 +36,7 @@ impl World {
}
// VERY EXPENSIVE
- pub fn set_cell_from_game_position(
- &mut self,
- x: i32,
- y: i32,
- cell: Cell,
- sleeping: bool,
- ) -> () {
+ pub fn set_cell_from_game_position(&mut self, x: i32, y: i32, cell: Cell, sleeping: bool) {
let ((cx, cy), (dx, dy)) = World::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);
@@ -52,7 +46,7 @@ impl World {
}
}
- pub fn insert(&mut self, x: i32, y: i32, chunk: Chunk) -> () {
+ 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);