generated from 2ndbeam/bevy-template
refactor!: Split everything into submodules
- Bump version to 0.2.0 - Split every module to systems, observers, plugins, etc - Renamed Crate to Container - Removed Wall component - Removed try_insert_item system - Removed inventory::ui module
This commit is contained in:
parent
3094a8af13
commit
7c386d4128
20 changed files with 830 additions and 772 deletions
|
|
@ -0,0 +1,62 @@
|
|||
use bevy::prelude::*;
|
||||
use bevy_rapier2d::prelude::*;
|
||||
|
||||
use crate::{
|
||||
GameState,
|
||||
PIXELS_PER_METER,
|
||||
inventory::{
|
||||
ActiveInventory,
|
||||
Inventory,
|
||||
item::Item,
|
||||
},
|
||||
};
|
||||
|
||||
const CRATE_CLOSED_ASSET: &'static str = "sprites/interactive/crate_closed.png";
|
||||
|
||||
use super::*;
|
||||
|
||||
#[derive(Component)]
|
||||
#[require(Sprite, InteractiveObject, Inventory)]
|
||||
pub struct Container;
|
||||
|
||||
fn on_container_interact(
|
||||
event: On<InteractionEvent>,
|
||||
mut commands: Commands,
|
||||
locked_query: Query<(), With<Locked>>,
|
||||
crate_query: Query<(), With<Container>>,
|
||||
mut next_state: ResMut<NextState<GameState>>,
|
||||
) {
|
||||
if locked_query.get(event.entity).is_ok() {
|
||||
return;
|
||||
}
|
||||
if crate_query.get(event.entity).is_err() {
|
||||
return;
|
||||
}
|
||||
|
||||
commands.entity(event.entity).insert(ActiveInventory);
|
||||
next_state.set(GameState::Inventory);
|
||||
}
|
||||
|
||||
pub fn container_bundle(
|
||||
asset_server: &Res<AssetServer>,
|
||||
position: Vec2,
|
||||
inventory_size: UVec2,
|
||||
items: Vec<Item>
|
||||
) -> impl Bundle {
|
||||
let image = asset_server.load(CRATE_CLOSED_ASSET);
|
||||
(
|
||||
Container,
|
||||
Transform::from_xyz(position.x, position.y - PIXELS_PER_METER * 0.5, 0.),
|
||||
Sprite::from_image(image),
|
||||
Inventory::new(inventory_size),
|
||||
Observer::new(on_container_interact),
|
||||
Children::spawn((
|
||||
SpawnIter(items.into_iter()),
|
||||
Spawn((
|
||||
Collider::cuboid(PIXELS_PER_METER, PIXELS_PER_METER),
|
||||
Sensor,
|
||||
Transform::from_xyz(0., PIXELS_PER_METER * 0.5, 0.),
|
||||
)),
|
||||
)),
|
||||
)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue