door opening
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user