door opening

This commit is contained in:
AmadeusWM
2025-04-06 20:59:58 +02:00
parent 6de69ffa07
commit 749d480b85
4 changed files with 68 additions and 34 deletions

View File

@@ -2,8 +2,7 @@ use bevy::prelude::*;
use bevy_rapier3d::prelude::ColliderDisabled;
use crate::{
player::{Player, PlayerAction, toolbar::Item},
util::single_mut,
level_instantiation::Door, player::{toolbar::Item, Player, PlayerAction}, util::single_mut
};
use super::{Interact, ui::InteractionOpportunity};
@@ -79,3 +78,18 @@ pub fn handle_drop(
*item_transform = *transform;
}
}
pub fn handle_door_interaction(
player_query: Query<&PlayerAction, With<Player>>, mut doors: Query<&mut Door>
) {
let Ok(action) = player_query.get_single() else {
return;
};
if *action != PlayerAction::OpenDoor {
return;
}
for mut door in doors.iter_mut() {
door.is_open = !door.is_open;
}
}

View File

@@ -17,7 +17,7 @@ pub(super) fn plugin(app: &mut App) {
(
update_interaction_opportunities,
display_interaction_prompt,
(objects::handle_pick_up, objects::handle_drop),
(objects::handle_pick_up, objects::handle_drop, objects::handle_door_interaction),
)
.chain()
.run_if(in_state(GameState::Playing)),

View File

@@ -5,10 +5,7 @@ use bevy_rapier3d::prelude::*;
use rand::{Rng, seq::IteratorRandom};
use crate::{
GameState,
asset_loading::{GltfAssets, ImageAssets},
interaction::Interact,
player::toolbar::ItemIcon,
asset_loading::{GltfAssets, ImageAssets}, interaction::Interact, player::{toolbar::ItemIcon, Player, PlayerAction}, GameState
};
pub fn map_plugin(app: &mut App) {
@@ -16,6 +13,7 @@ pub fn map_plugin(app: &mut App) {
OnEnter(GameState::Playing),
(spawn_level, spawn_objects, spawn_doors).chain(),
);
app.add_systems(Update, handle_door_pos);
}
fn spawn_level(
@@ -622,6 +620,13 @@ fn spawn_objects(
}
}
#[derive(Component)]
pub struct Door {
pub is_open: bool,
pub open_direction: (i32, i32),
pub position: (i32, i32)
}
fn spawn_doors(
mut commands: Commands,
levels: Res<GameLevels>,
@@ -641,31 +646,42 @@ fn spawn_doors(
for level in levels.levels.iter() {
let (x, z) = level.end_node;
if x <= z {
commands
.spawn((
RigidBody::Fixed,
Transform::from_xyz(2.0 * x as f32, 0.0, -2.0 * z as f32)
.with_rotation(Quat::from_rotation_y(90.0_f32.to_radians())),
scene_root.clone(),
))
.with_children(|parent| {
for (collider, transform) in collider.clone() {
parent.spawn((collider, transform));
}
});
let (direction, transform) = if x >= z {
(
(0, 1),
Transform::from_xyz(2.0 * x as f32, 0.0, -2.0 * z as f32)
.with_rotation(Quat::from_rotation_y(90.0_f32.to_radians()))
)
} else {
commands
.spawn((
RigidBody::Dynamic,
Transform::from_xyz(2.0 * x as f32, 0.0, -2.0 * z as f32),
scene_root.clone(),
))
.with_children(|parent| {
for (collider, transform) in collider.clone() {
parent.spawn((collider, transform));
}
});
(
(1, 0),
Transform::from_xyz(2.0 * x as f32, 0.0, -2.0 * z as f32)
)
};
commands.spawn((
RigidBody::Fixed,
Door {
is_open: false,
open_direction: direction,
position: (x, z)
},
transform,
scene_root.clone(),
)).with_children(|parent| {
for (collider, transform) in collider.clone() {
parent.spawn((collider, transform));
}
});
}
}
fn handle_door_pos(mut query: Query<(&Door, &mut Transform)>) {
for (door, mut transform) in query.iter_mut() {
if door.is_open {
transform.translation.y = 2.0
}
else {
transform.translation.y = 0.0;
}
}
}

View File

@@ -242,6 +242,7 @@ pub(crate) enum PlayerAction {
Sprint,
Jump,
ToggleFlashlight,
OpenDoor,
// Objects and stuff
PickUp,
Drop,
@@ -290,6 +291,9 @@ pub fn handle_input(
if keyboard_input.just_pressed(KeyCode::KeyF) {
*action = PlayerAction::ToggleFlashlight;
}
if keyboard_input.just_pressed(KeyCode::KeyK) {
*action = PlayerAction::OpenDoor;
}
input.movement_direction = movement_direction.normalize_or_zero();
}