Skip to content

Commit

Permalink
Initial container UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Alainx277 committed Feb 1, 2024
1 parent dfc3668 commit 3798091
Show file tree
Hide file tree
Showing 6 changed files with 477 additions and 9 deletions.
3 changes: 2 additions & 1 deletion assets/items/enforcer.scn.ron
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
id: "models/items/enforcer.glb#Mesh0/Primitive0"
),
"ssnt::items::Item": (
name: "Enforcer Handgun"
name: "Enforcer Handgun",
size: (x: 3, y: 2),
),
"ssnt::combat::ranged::Gun": (
),
Expand Down
2 changes: 1 addition & 1 deletion assets/items/gray_backpack.scn.ron
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
clothing_type: "back",
),
"ssnt::items::containers::Container": (
size: (x: 10, y: 10),
size: (x: 6, y: 5),
),
"physics::RigidBody": (
kind: Dynamic
Expand Down
4 changes: 3 additions & 1 deletion src/interaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,9 @@ pub enum InteractionStatus {
pub enum InteractionSpecificity {
/// The interaction is available on a limited set of specific objects.
Specific,
// The interaction is available on many objects.
// The interaction is available on a class of objects.
Common,
// The interaction is available on most objects.
Generic,
}

Expand Down
44 changes: 38 additions & 6 deletions src/items/containers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use utils::task::{Task, Tasks};

use super::{Item, StoredItem};

mod ui;

pub struct ContainerPlugin;

impl Plugin for ContainerPlugin {
Expand All @@ -31,6 +33,8 @@ impl Plugin for ContainerPlugin {
)
.add_systems(Update, (cleanup_deleted_entities, do_item_move).chain());
}

app.add_plugins(ui::ContainerUiPlugin);
}
}

Expand Down Expand Up @@ -82,11 +86,12 @@ impl Container {
for (&other_position, &entity) in self.items.iter() {
let other_item = items_query.get(entity).unwrap();

let x_overlap = (position.x as i32 - other_position.x as i32).unsigned_abs() * 2
< item.size.x + other_item.size.x;
let y_overlap = (position.y as i32 - other_position.y as i32).unsigned_abs() * 2
< item.size.y + other_item.size.y;

let x_overlap = (other_position.x..(other_position.x + other_item.size.x))
.contains(&position.x)
|| (position.x..(position.x + item.size.x)).contains(&other_position.x);
let y_overlap = (other_position.y..(other_position.y + other_item.size.y))
.contains(&position.y)
|| (position.y..(position.y + item.size.y)).contains(&other_position.y);
if x_overlap && y_overlap {
return false;
}
Expand All @@ -95,6 +100,24 @@ impl Container {
true
}

fn find_space(&self, items_query: &Query<&Item>, item: &Item) -> Option<UVec2> {
let mut current = UVec2::ZERO;
while current.x < self.size.x && current.y < self.size.y {
// TODO: This is very inefficient <3
if self.can_fit(items_query, item, current) {
return Some(current);
}

current += UVec2::X;
if current.x >= self.size.x {
current.x = 0;
current.y += 1;
}
}

None
}

pub fn iter(&self) -> impl Iterator<Item = (&UVec2, &Entity)> {
self.items.iter()
}
Expand Down Expand Up @@ -149,6 +172,11 @@ fn do_item_move(
return MoveItemResult { success: false };
};

if data.container == Some(data.item) {
error!(task = ?data, "Tried to store a container inside itself");
return MoveItemResult { success: false };
}

// Remove from old container if it exists
if let Some(stored) = stored.as_mut() {
let mut container = containers.get_mut(*stored.container).unwrap();
Expand Down Expand Up @@ -186,7 +214,9 @@ fn do_item_move(
return MoveItemResult { success: false };
};

let position = data.position.expect("Automatic position not supported yet");
let position = data
.position
.unwrap_or_else(|| container.find_space(&only_items, item).unwrap_or_default());
if !container.can_fit(&only_items, item, position) {
warn!(task = ?data, "Failed to move item because it does not fit in the container");
return MoveItemResult { success: false };
Expand All @@ -195,10 +225,12 @@ fn do_item_move(
container.insert_item_unchecked(data.item, position);
if let Some(stored) = stored.as_mut() {
*stored.container = container_entity;
*stored.slot = position;
*stored.visible = container.items_visible;
} else {
commands.entity(data.item).insert(StoredItem {
container: container_entity.into(),
slot: position.into(),
visible: container.items_visible.into(),
});
}
Expand Down
Loading

0 comments on commit 3798091

Please sign in to comment.