door opening
This commit is contained in:
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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)),
|
||||||
|
|||||||
@@ -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),
|
)
|
||||||
scene_root.clone(),
|
};
|
||||||
))
|
commands.spawn((
|
||||||
.with_children(|parent| {
|
RigidBody::Fixed,
|
||||||
for (collider, transform) in collider.clone() {
|
Door {
|
||||||
parent.spawn((collider, transform));
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -130,10 +130,10 @@ pub fn init_player(
|
|||||||
..default()
|
..default()
|
||||||
}),
|
}),
|
||||||
DistanceFog {
|
DistanceFog {
|
||||||
color: Color::srgba(0.12, 0.08, 0.08, 0.65),
|
color: Color::srgba(0.12, 0.08, 0.08, 0.65),
|
||||||
falloff: FogFalloff::Linear {
|
falloff: FogFalloff::Linear {
|
||||||
start: 3.0,
|
start: 3.0,
|
||||||
end: 12.0,
|
end: 12.0,
|
||||||
},
|
},
|
||||||
..default()
|
..default()
|
||||||
},
|
},
|
||||||
@@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user