generated from 2ndbeam/bevy-template
- Updated all assets - Refactored door again - Half of the sprites are visually broken for now
74 lines
2.1 KiB
Rust
74 lines
2.1 KiB
Rust
use bevy::prelude::*;
|
|
use bevy_rapier2d::prelude::*;
|
|
|
|
use crate::{
|
|
GameState,
|
|
meters,
|
|
inventory::{
|
|
ActiveInventory,
|
|
Inventory,
|
|
}
|
|
};
|
|
|
|
use super::*;
|
|
|
|
#[derive(Component, Clone, Copy, Default, Reflect, Debug, PartialEq, Eq)]
|
|
#[reflect(Component, Clone, Default, Debug, PartialEq)]
|
|
#[require(Sprite, InteractiveObject, Inventory)]
|
|
pub struct Container;
|
|
|
|
pub 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(
|
|
textures: &Res<LayoutTextures>,
|
|
position: Vec2,
|
|
inventory_size: UVec2,
|
|
) -> impl Bundle {
|
|
let texture_atlas = TextureAtlas {
|
|
layout: textures.container.atlas.clone(),
|
|
index: textures.container.indices["main"],
|
|
};
|
|
let hl_texture_atlas = texture_atlas.clone().with_index(textures.container.indices["highlighted"]);
|
|
let sprite = Sprite {
|
|
image: textures.container.image.clone(),
|
|
texture_atlas: Some(texture_atlas),
|
|
..default()
|
|
};
|
|
let mut highlight_sprite = sprite.clone();
|
|
highlight_sprite.texture_atlas = Some(hl_texture_atlas);
|
|
(
|
|
Container,
|
|
Transform::from_xyz(position.x, position.y - meters(0.5), 0.),
|
|
sprite,
|
|
Inventory::new(inventory_size),
|
|
Children::spawn((
|
|
Spawn((
|
|
Collider::cuboid(meters(1.), meters(1.)),
|
|
Sensor,
|
|
Transform::from_xyz(0., meters(0.5), 0.),
|
|
)),
|
|
Spawn((
|
|
highlight_sprite,
|
|
Transform::from_xyz(0., 0., 1.),
|
|
Visibility::Hidden,
|
|
)),
|
|
)),
|
|
Name::new(format!("Container {}x{}", inventory_size.x, inventory_size.y)),
|
|
)
|
|
}
|