refactor: split structures and functions into related modules
This commit is contained in:
parent
1119f7b0a0
commit
4109e29d4d
6 changed files with 223 additions and 370 deletions
29
src/grid/element.rs
Normal file
29
src/grid/element.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
use crate::MAX_FLUID_AMOUNT;
|
||||
|
||||
#[derive(Default,Clone,Copy)]
|
||||
pub enum GridElement{
|
||||
#[default]
|
||||
Air,
|
||||
Water{amount: u32},
|
||||
Solid,
|
||||
}
|
||||
|
||||
impl GridElement {
|
||||
pub fn try_fill(&self, fill_amount: u32) -> Option<u32> {
|
||||
return match self {
|
||||
GridElement::Water { amount } => {
|
||||
if *amount == MAX_FLUID_AMOUNT {
|
||||
return None;
|
||||
}
|
||||
else if MAX_FLUID_AMOUNT - *amount < fill_amount {
|
||||
return Some(MAX_FLUID_AMOUNT - *amount);
|
||||
}
|
||||
else {
|
||||
return Some(fill_amount);
|
||||
}
|
||||
},
|
||||
GridElement::Air => Some(fill_amount),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
31
src/grid/manipulation.rs
Normal file
31
src/grid/manipulation.rs
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
use bevy::math::UVec3;
|
||||
use crate::grid::{Grid, GridElement};
|
||||
|
||||
pub struct GridManipulationTools<'a> {
|
||||
grid: &'a mut Grid
|
||||
}
|
||||
|
||||
impl<'a> GridManipulationTools<'a> {
|
||||
pub fn new(grid: &'a mut Grid) -> Self {
|
||||
Self { grid }
|
||||
}
|
||||
pub fn set_xyz(&mut self,x: u32, y: u32, z: u32, element: GridElement) {
|
||||
let index = self.grid.indexify(UVec3::new(x,y,z));
|
||||
self.grid.data[index] = element;
|
||||
}
|
||||
pub fn fill_xyz(&mut self,x1: u32, y1: u32, z1: u32, x2: u32, y2: u32, z2: u32, element: GridElement) {
|
||||
if x1 > x2 || y1 > y2 || z1 > z2 {
|
||||
return;
|
||||
}
|
||||
|
||||
for x in x1..=x2 {
|
||||
for y in y1..=y2 {
|
||||
for z in z1..=z2 {
|
||||
let index = self.grid.indexify(UVec3::new(x,y,z));
|
||||
|
||||
self.grid.data[index] = element.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
86
src/grid/mod.rs
Normal file
86
src/grid/mod.rs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
use bevy::{math::UVec3,prelude::*};
|
||||
|
||||
use crate::MAX_FLUID_AMOUNT;
|
||||
use manipulation::*;
|
||||
|
||||
pub use element::{GridElement};
|
||||
|
||||
|
||||
pub mod element;
|
||||
pub mod manipulation;
|
||||
|
||||
|
||||
#[derive(Resource,Default,Clone)]
|
||||
pub struct Grid {
|
||||
pub(crate) size: UVec3,
|
||||
pub(crate) data: Vec<GridElement>
|
||||
}
|
||||
|
||||
impl Grid {
|
||||
pub fn cube(side: u32) -> Self {
|
||||
let mut data = Vec::new();
|
||||
let uside = side as usize;
|
||||
data.resize(uside*uside*uside, GridElement::Air);
|
||||
Self {
|
||||
size: UVec3::new(side,side,side),
|
||||
data
|
||||
}
|
||||
}
|
||||
pub fn indexify(&self, vector: UVec3) -> usize{
|
||||
let clamped_vector = vector.min(self.size - UVec3::ONE);
|
||||
return (clamped_vector.x + self.size.x * clamped_vector.y + self.size.x * self.size.y * clamped_vector.z) as usize;
|
||||
}
|
||||
pub fn deindexify(&self, index: usize) -> UVec3 {
|
||||
let undex = index as u32;
|
||||
return UVec3::new(
|
||||
undex % self.size.x,
|
||||
undex / self.size.x % self.size.y,
|
||||
undex / (self.size.x*self.size.y)
|
||||
);
|
||||
}
|
||||
pub fn tranfer_fluid(&mut self,from: usize, to: usize, transfer_amount: u32) {
|
||||
let bounds = self.indexify(self.size);
|
||||
if from >= bounds || to >= bounds {
|
||||
return;
|
||||
}
|
||||
|
||||
let from_new_value;
|
||||
let to_new_value;
|
||||
|
||||
match (self.data[to],self.data[from]) {
|
||||
(GridElement::Air,GridElement::Water { amount: mut water_amount }) => {
|
||||
to_new_value = Some(GridElement::Water { amount: transfer_amount });
|
||||
water_amount = water_amount.checked_sub(transfer_amount).unwrap_or(0);
|
||||
from_new_value = if water_amount == 0 {Some(GridElement::Air)} else {Some(GridElement::Water { amount: water_amount })};
|
||||
},
|
||||
(GridElement::Water { amount: mut to_amount }, GridElement::Water { amount: mut from_amount }) => {
|
||||
let transfer_amount = from_amount.min(transfer_amount);
|
||||
let delta = MAX_FLUID_AMOUNT - to_amount;
|
||||
if delta < transfer_amount {
|
||||
to_amount = MAX_FLUID_AMOUNT;
|
||||
from_amount -= delta;
|
||||
}
|
||||
else {
|
||||
to_amount += transfer_amount;
|
||||
from_amount -= transfer_amount;
|
||||
}
|
||||
to_new_value = Some(GridElement::Water { amount: to_amount });
|
||||
from_new_value = if from_amount == 0 {Some(GridElement::Air)} else {Some(GridElement::Water { amount: from_amount })};
|
||||
}
|
||||
_ => {return;},
|
||||
|
||||
}
|
||||
|
||||
if let Some(value) = to_new_value {
|
||||
self.data[to] = value;
|
||||
}
|
||||
if let Some(value) = from_new_value {
|
||||
self.data[from] = value;
|
||||
}
|
||||
|
||||
}
|
||||
pub fn manipulate<'a>(&'a mut self) -> GridManipulationTools<'a> {
|
||||
GridManipulationTools::new(self)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue