New tiles
This commit is contained in:
parent
86b9134156
commit
dea1b8599a
5 changed files with 144 additions and 38 deletions
|
|
@ -1,3 +1,5 @@
|
|||
use std::fmt::Display;
|
||||
|
||||
use crate::MAX_FLUID_AMOUNT;
|
||||
|
||||
#[derive(Default,Clone,Copy)]
|
||||
|
|
@ -6,9 +8,20 @@ pub enum GridElement{
|
|||
Air,
|
||||
Water{amount: u32},
|
||||
Solid,
|
||||
Drain,
|
||||
Source{source_amount: u32},
|
||||
}
|
||||
|
||||
impl GridElement {
|
||||
pub fn can_fill(&self) -> bool {
|
||||
match self {
|
||||
GridElement::Air => true,
|
||||
GridElement::Water { amount: _ } => true,
|
||||
GridElement::Solid => false,
|
||||
GridElement::Drain => true,
|
||||
GridElement::Source { source_amount: _ } => false,
|
||||
}
|
||||
}
|
||||
pub fn try_fill(&self, fill_amount: u32) -> Option<u32> {
|
||||
return match self {
|
||||
GridElement::Water { amount } => {
|
||||
|
|
@ -23,7 +36,20 @@ impl GridElement {
|
|||
}
|
||||
},
|
||||
GridElement::Air => Some(fill_amount),
|
||||
GridElement::Drain => Some(fill_amount),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for GridElement {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f,"{}",match self {
|
||||
GridElement::Air => "Air",
|
||||
GridElement::Water { amount: _ } => "Water",
|
||||
GridElement::Solid => "Solid",
|
||||
GridElement::Drain => "Drain",
|
||||
GridElement::Source { source_amount: _ } => "Source",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,10 +53,11 @@ impl Grid {
|
|||
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::Air,GridElement::Water { amount: water_amount }) => {
|
||||
let moved_amount = water_amount.min(transfer_amount);
|
||||
let remaining_amount = water_amount-moved_amount;
|
||||
to_new_value = Some(GridElement::Water { amount: moved_amount });
|
||||
from_new_value = if remaining_amount == 0 {Some(GridElement::Air)} else {Some(GridElement::Water { amount: remaining_amount })};
|
||||
},
|
||||
(GridElement::Water { amount: mut to_amount }, GridElement::Water { amount: mut from_amount }) => {
|
||||
let transfer_amount = from_amount.min(transfer_amount);
|
||||
|
|
@ -71,7 +72,13 @@ impl Grid {
|
|||
}
|
||||
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 })};
|
||||
}
|
||||
},
|
||||
(GridElement::Drain, GridElement::Water { amount: from_amount }) => {
|
||||
let transfer_amount = from_amount.min(transfer_amount);
|
||||
let new_amount = from_amount - transfer_amount;
|
||||
from_new_value = if new_amount == 0 {Some(GridElement::Air)} else {Some(GridElement::Water { amount: new_amount })};
|
||||
to_new_value = None;
|
||||
},
|
||||
_ => {return;},
|
||||
|
||||
}
|
||||
|
|
@ -84,6 +91,28 @@ impl Grid {
|
|||
}
|
||||
|
||||
}
|
||||
pub fn fill(&mut self, at: usize, amount: u32) -> u32 {
|
||||
let transformed_value;
|
||||
let return_amount;
|
||||
match self.data[at] {
|
||||
GridElement::Air => {
|
||||
transformed_value = GridElement::Water { amount };
|
||||
return_amount = 0;
|
||||
},
|
||||
GridElement::Water { amount: water_amount } => {
|
||||
let diff = MAX_FLUID_AMOUNT - water_amount;
|
||||
let fill_amount = diff.min(amount);
|
||||
return_amount = amount - fill_amount;
|
||||
transformed_value = GridElement::Water { amount: water_amount + fill_amount };
|
||||
},
|
||||
GridElement::Drain => { return 0; },
|
||||
_ => { return amount; }
|
||||
}
|
||||
|
||||
self.data[at] = transformed_value;
|
||||
|
||||
return return_amount;
|
||||
}
|
||||
pub fn manipulate<'a>(&'a mut self) -> GridManipulationTools<'a> {
|
||||
GridManipulationTools::new(self)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue