50 lines
1.3 KiB
Rust
50 lines
1.3 KiB
Rust
use bevy::prelude::*;
|
|
|
|
#[derive(Component, Debug)]
|
|
pub struct Container {
|
|
pub max_weight: u32,
|
|
pub max_items: Option<u32>,
|
|
pub current_weight: u32,
|
|
pub contents: Vec<Entity>, // TODO - move away from Entity
|
|
pub is_open: bool,
|
|
}
|
|
|
|
impl Container {
|
|
pub fn new(max_weight: u32, max_items: Option<u32>) -> Self {
|
|
Self {
|
|
max_weight,
|
|
max_items,
|
|
current_weight: 0,
|
|
contents: Vec::new(),
|
|
is_open: true,
|
|
}
|
|
}
|
|
|
|
pub fn can_fit(&self, weight: u32) -> bool {
|
|
self.current_weight + weight <= self.max_weight
|
|
&& self
|
|
.max_items
|
|
.map_or(true, |max| self.contents.len() < max as usize)
|
|
}
|
|
|
|
pub fn add_item(&mut self, item_entity: Entity, weight: u32) -> bool {
|
|
if self.can_fit(weight) {
|
|
self.contents.push(item_entity);
|
|
self.current_weight += weight;
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
pub fn remove_item(&mut self, item_entity: Entity, weight: u32) -> bool {
|
|
if let Some(pos) = self.contents.iter().position(|&e| e == item_entity) {
|
|
self.contents.remove(pos);
|
|
self.current_weight = self.current_weight.saturating_sub(weight);
|
|
true
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
}
|