example item

This commit is contained in:
2025-09-12 18:04:03 +01:00
parent ee7ac39ad5
commit 8a5884c6d5
19 changed files with 1009 additions and 53 deletions
+54
View File
@@ -0,0 +1,54 @@
use bevy::prelude::*;
#[derive(Component, Debug)]
pub struct Container {
// Maximum weight this container can hold
pub max_weight: u32,
// Maximum number of items (None = unlimited)
pub max_items: Option<u32>,
// Current weight of contents
pub current_weight: u32,
// List of item entities contained
pub contents: Vec<Entity>, // TODO - move away from Entity
// Whether the container is currently open/accessible
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
}
}
}