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 bevy_rapier3d::prelude::ColliderDisabled;
use crate::{ use crate::{
player::{Player, PlayerAction, toolbar::Item}, level_instantiation::Door, player::{toolbar::Item, Player, PlayerAction}, util::single_mut
util::single_mut,
}; };
use super::{Interact, ui::InteractionOpportunity}; use super::{Interact, ui::InteractionOpportunity};
@@ -79,3 +78,18 @@ pub fn handle_drop(
*item_transform = *transform; *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, update_interaction_opportunities,
display_interaction_prompt, display_interaction_prompt,
(objects::handle_pick_up, objects::handle_drop), (objects::handle_pick_up, objects::handle_drop, objects::handle_door_interaction),
) )
.chain() .chain()
.run_if(in_state(GameState::Playing)), .run_if(in_state(GameState::Playing)),

View File

@@ -5,10 +5,7 @@ use bevy_rapier3d::prelude::*;
use rand::{Rng, seq::IteratorRandom}; use rand::{Rng, seq::IteratorRandom};
use crate::{ use crate::{
GameState, asset_loading::{GltfAssets, ImageAssets}, interaction::Interact, player::{toolbar::ItemIcon, Player, PlayerAction}, GameState
asset_loading::{GltfAssets, ImageAssets},
interaction::Interact,
player::toolbar::ItemIcon,
}; };
pub fn map_plugin(app: &mut App) { pub fn map_plugin(app: &mut App) {
@@ -16,6 +13,7 @@ pub fn map_plugin(app: &mut App) {
OnEnter(GameState::Playing), OnEnter(GameState::Playing),
(spawn_level, spawn_objects, spawn_doors).chain(), (spawn_level, spawn_objects, spawn_doors).chain(),
); );
app.add_systems(Update, handle_door_pos);
} }
fn spawn_level( 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( fn spawn_doors(
mut commands: Commands, mut commands: Commands,
levels: Res<GameLevels>, levels: Res<GameLevels>,
@@ -641,31 +646,42 @@ fn spawn_doors(
for level in levels.levels.iter() { for level in levels.levels.iter() {
let (x, z) = level.end_node; let (x, z) = level.end_node;
if x <= z { let (direction, transform) = if x >= z {
commands (
.spawn(( (0, 1),
RigidBody::Fixed,
Transform::from_xyz(2.0 * x as f32, 0.0, -2.0 * z as f32) 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())), .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));
}
});
} else { } else {
commands (
.spawn(( (1, 0),
RigidBody::Dynamic, Transform::from_xyz(2.0 * x as f32, 0.0, -2.0 * z as f32)
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(), scene_root.clone(),
)) )).with_children(|parent| {
.with_children(|parent| {
for (collider, transform) in collider.clone() { for (collider, transform) in collider.clone() {
parent.spawn((collider, transform)); 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, Sprint,
Jump, Jump,
ToggleFlashlight, ToggleFlashlight,
OpenDoor,
// Objects and stuff // Objects and stuff
PickUp, PickUp,
Drop, Drop,
@@ -290,6 +291,9 @@ pub fn handle_input(
if keyboard_input.just_pressed(KeyCode::KeyF) { if keyboard_input.just_pressed(KeyCode::KeyF) {
*action = PlayerAction::ToggleFlashlight; *action = PlayerAction::ToggleFlashlight;
} }
if keyboard_input.just_pressed(KeyCode::KeyK) {
*action = PlayerAction::OpenDoor;
}
input.movement_direction = movement_direction.normalize_or_zero(); input.movement_direction = movement_direction.normalize_or_zero();
} }