Compare commits

..

No commits in common. "357ef6c8e23551eb1ba5a63c383ae45c40917c61" and "32d141f848d6c678200d426839f0ab43fdb49dd0" have entirely different histories.

4 changed files with 19 additions and 117 deletions

View file

@ -1,20 +0,0 @@
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum CellType {
Empty = 0,
Sand,
}
#[derive(Debug, Clone, Copy)]
pub enum MovementType {
None = 0b0000_0000,
Down = 0b0000_0001,
Side = 0b0000_0010,
DownSide = 0b0000_0011,
}
#[derive(Debug, Clone, Copy)]
pub struct Cell {
pub cell_type: CellType,
pub movement_type: MovementType,
pub color: u32,
}

View file

@ -5,15 +5,13 @@ use minifb::{Key, Window, WindowOptions};
const WIDTH: usize = 800; const WIDTH: usize = 800;
const HEIGHT: usize = 600; const HEIGHT: usize = 600;
use crate::{materials::*, rand::Random, world::World}; use crate::{materials::*, rand::Random};
pub struct Engine { pub struct Engine {
pub window: Window, pub window: Window,
pub front_buffer: Vec<u32>, pub buffer: Vec<u32>,
pub brush_size: usize, pub brush_size: usize,
pub random: Random, pub random: Random,
pub world: World,
} }
impl Engine { impl Engine {
@ -28,10 +26,9 @@ impl Engine {
.unwrap_or_else(|e| { .unwrap_or_else(|e| {
panic!("{}", e); panic!("{}", e);
}), }),
front_buffer: vec![0; WIDTH * HEIGHT], buffer: vec![0; WIDTH * HEIGHT],
brush_size: 1, brush_size: 1,
random: Random::new(0), random: Random::new(0),
world: World::new(),
} }
} }
@ -86,13 +83,19 @@ impl Engine {
for x in 0..WIDTH { for x in 0..WIDTH {
for y in 0..HEIGHT { for y in 0..HEIGHT {
let index = xy_to_i(x, y); let index = xy_to_i(x, y);
let cell = self.front_buffer[index]; let cell = self.buffer[index];
if cell == SAND { if cell == SAND {
let down = self.is_empty(x, y + 1); let down = self.is_empty(x, y + 1);
let random = self.random.random_bool(); let mut left = self.is_empty(x - 1, y + 1);
let left = self.is_empty(x - 1, y + 1) && random; let mut right = self.is_empty(x + 1, y + 1);
let right = self.is_empty(x + 1, y + 1) ^ left;
if left && right {
// TODO: handle left/right randomization here
let rand = self.random.random_bool();
left = rand;
right = !rand;
}
if down { if down {
self.set_cell(x, y + 1, SAND) self.set_cell(x, y + 1, SAND)
@ -110,7 +113,7 @@ impl Engine {
} }
// TODO Handle this. // TODO Handle this.
self.window self.window
.update_with_buffer(&self.front_buffer, WIDTH, HEIGHT) .update_with_buffer(&self.buffer, WIDTH, HEIGHT)
.unwrap(); .unwrap();
} }
} }
@ -119,18 +122,18 @@ impl Engine {
impl Engine { impl Engine {
fn is_empty(&mut self, x: usize, y: usize) -> bool { fn is_empty(&mut self, x: usize, y: usize) -> bool {
let index = xy_to_i(x, y); let index = xy_to_i(x, y);
if index >= self.front_buffer.len() { if index >= self.buffer.len() {
false false
} else { } else {
self.front_buffer[index] == EMPTY self.buffer[index] == EMPTY
} }
} }
fn set_cell(&mut self, x: usize, y: usize, mat: u32) { fn set_cell(&mut self, x: usize, y: usize, mat: u32) {
let index = xy_to_i(x, y); let index = xy_to_i(x, y);
if index >= self.front_buffer.len() { if index >= self.buffer.len() {
} else { } else {
self.front_buffer[index] = mat; self.buffer[index] = mat;
} }
} }
@ -138,7 +141,7 @@ impl Engine {
for x in 0..WIDTH { for x in 0..WIDTH {
for y in 0..HEIGHT { for y in 0..HEIGHT {
let index = xy_to_i(x, y); let index = xy_to_i(x, y);
self.front_buffer[index] = EMPTY; self.buffer[index] = EMPTY;
} }
} }
} }

View file

@ -1,8 +1,6 @@
mod cell;
mod engine; mod engine;
mod materials; mod materials;
mod rand; mod rand;
mod world;
fn main() { fn main() {
let mut engine = engine::Engine::new(); let mut engine = engine::Engine::new();

View file

@ -1,79 +0,0 @@
use crate::cell::{Cell, MovementType};
use crate::cell::CellType::Empty;
use crate::engine::xy_to_i;
use crate::materials::EMPTY;
const WIDTH: usize = 800;
const HEIGHT: usize = 600;
pub struct Change {
x_from: usize,
x_to: usize,
y_from: usize,
y_to: usize,
}
pub struct World {
pub width: usize,
pub height: usize,
pub cells: Vec<Cell>,
changes: Vec<Change>,
}
impl World {
pub fn new() -> Self {
Self {
width: WIDTH,
height: HEIGHT,
cells: vec![
Cell {
cell_type: Empty,
movement_type: MovementType::None,
color: 0
};
WIDTH * HEIGHT
],
changes: vec![],
}
}
}
impl World {
pub fn get_xy(&mut self, x: usize, y: usize) -> Cell {
let index = x + self.width * y;
self.cells[index]
}
pub fn get_i(&mut self, index: usize) -> Cell {
self.cells[index]
}
pub fn is_empty(&mut self, x: usize, y: usize) -> bool {
in_bounds(x, y) && self.get_xy(x, y).cell_type == Empty
}
pub fn set_xy(&mut self, x: usize, y: usize, cell: Cell) {
self.cells[xy_to_i(x, y)] = cell
}
// Collect all the changes to the world and push them into a changes vec
pub fn move_cell(&mut self, x: usize, y: usize, xto: usize, yto: usize) {
let change = Change {
x_from: x,
x_to: xto,
y_from: y,
y_to: yto,
};
self.changes.push(change);
}
// Takes the changes to the world and applies them.
pub fn commit_cells() {}
}
pub fn in_bounds(x: usize, y: usize) -> bool {
let x_in_bounds = x < WIDTH;
let y_in_bounds = y < HEIGHT;
x_in_bounds && y_in_bounds
}