done
This commit is contained in:
@@ -2,7 +2,6 @@ use bevy::prelude::*;
|
||||
use bevy_rapier3d::prelude::ColliderDisabled;
|
||||
|
||||
use crate::{
|
||||
level_instantiation::Door,
|
||||
player::{Player, PlayerAction, toolbar::Item},
|
||||
util::single_mut,
|
||||
};
|
||||
@@ -17,13 +16,18 @@ pub struct KeyCardId(pub usize);
|
||||
#[derive(Component, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
|
||||
pub struct OpenedByKey(pub KeyCardId);
|
||||
|
||||
#[derive(Resource, Default, Clone, Copy)]
|
||||
pub struct WrongKey(pub bool);
|
||||
|
||||
impl OpenedByKey {
|
||||
pub fn can_be_opened_by(&self, card: KeyCardId) -> bool {
|
||||
self.0 == card
|
||||
}
|
||||
}
|
||||
|
||||
pub fn plugin(_app: &mut App) {}
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.init_resource::<WrongKey>();
|
||||
}
|
||||
|
||||
pub fn handle_pick_up(
|
||||
mut commands: Commands,
|
||||
@@ -103,6 +107,7 @@ pub fn handle_door_interaction(
|
||||
mut commands: Commands,
|
||||
mut player_query: Query<(&PlayerAction, &mut Item), With<Player>>,
|
||||
card_query: Query<&KeyCardId>,
|
||||
mut wrong_key: ResMut<WrongKey>,
|
||||
// current interactable
|
||||
interaction_opportunity: ResMut<InteractionOpportunity>,
|
||||
door_query: Query<(Entity, &OpenedByKey), With<Interact>>,
|
||||
@@ -128,5 +133,8 @@ pub fn handle_door_interaction(
|
||||
// unwrap is safe because of the item.inner().and_then
|
||||
commands.entity(item.inner().unwrap()).despawn_recursive();
|
||||
item.take();
|
||||
} else {
|
||||
// wrong key :)
|
||||
wrong_key.0 = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ use bevy_rapier3d::prelude::*;
|
||||
use bevy_rapier3d::rapier::prelude::CollisionEventFlags;
|
||||
use std::iter;
|
||||
|
||||
use super::objects::WrongKey;
|
||||
use super::{Interact, objects};
|
||||
|
||||
pub(super) fn plugin(app: &mut App) {
|
||||
@@ -35,6 +36,7 @@ fn update_interaction_opportunities(
|
||||
mut collision_events: EventReader<CollisionEvent>,
|
||||
player_query: Query<Entity, With<Player>>,
|
||||
parents: Query<&Parent>,
|
||||
mut wrong_key: ResMut<WrongKey>,
|
||||
target_query: Query<Entity, (Without<Player>, With<Interact>)>,
|
||||
mut interaction_opportunity: ResMut<InteractionOpportunity>,
|
||||
) {
|
||||
@@ -63,6 +65,8 @@ fn update_interaction_opportunities(
|
||||
if started {
|
||||
interaction_opportunity.0.replace(interactable);
|
||||
} else {
|
||||
// reset when gone
|
||||
wrong_key.0 = false;
|
||||
interaction_opportunity.0.take_if(|t| *t == interactable);
|
||||
}
|
||||
}
|
||||
@@ -72,6 +76,7 @@ fn display_interaction_prompt(
|
||||
interaction_opportunity: Res<InteractionOpportunity>,
|
||||
mut egui_contexts: EguiContexts,
|
||||
primary_windows: Query<&Window, With<PrimaryWindow>>,
|
||||
wrong_key: Res<WrongKey>,
|
||||
names: Query<(&Name, Option<&Door>), With<Interact>>, // only the interactables ofcourse
|
||||
) {
|
||||
let Some(opportunity) = interaction_opportunity.0 else {
|
||||
@@ -94,5 +99,8 @@ fn display_interaction_prompt(
|
||||
.fixed_pos(egui::Pos2::new(window.width() / 2., window.height() / 2.))
|
||||
.show(egui_contexts.ctx_mut(), |ui| {
|
||||
ui.label(interaction_str);
|
||||
if wrong_key.0 {
|
||||
ui.label("Wrong Key :)");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -597,6 +597,7 @@ fn spawn_objects(
|
||||
} else {
|
||||
Transform::from_xyz(2.0 * x as f32, 0.5, -2.0 * z as f32)
|
||||
};
|
||||
// Correct Key Card
|
||||
commands
|
||||
.spawn((
|
||||
transform,
|
||||
@@ -627,6 +628,51 @@ fn spawn_objects(
|
||||
Sensor,
|
||||
));
|
||||
});
|
||||
|
||||
// wrong key card
|
||||
let (x, z) = loop {
|
||||
let positions = level.nodes.keys();
|
||||
let random_pos = positions.choose(&mut rand::rng()).unwrap().clone();
|
||||
if random_pos != begin_node && random_pos != end_node {
|
||||
break random_pos;
|
||||
}
|
||||
};
|
||||
let transform = if x <= z {
|
||||
Transform::from_xyz(2.0 * x as f32, 0.5, -2.0 * z as f32)
|
||||
.with_rotation(Quat::from_rotation_y(90.0_f32.to_radians()))
|
||||
} else {
|
||||
Transform::from_xyz(2.0 * x as f32, 0.5, -2.0 * z as f32)
|
||||
};
|
||||
commands
|
||||
.spawn((
|
||||
transform,
|
||||
Interact,
|
||||
PickUpAble,
|
||||
RigidBody::Dynamic,
|
||||
KeyCardId(usize::MAX),
|
||||
Name::new(format!("Id Card {i}")),
|
||||
Visibility::Visible,
|
||||
ItemIcon::new(image_assets.id_card.clone()),
|
||||
SceneRoot(asset.clone()),
|
||||
))
|
||||
.with_children(|parent| {
|
||||
parent.spawn((
|
||||
ColliderMassProperties::Mass(10.0),
|
||||
Collider::cuboid(0.05, 0.05, 0.01),
|
||||
Transform::from_rotation(Quat::from_euler(
|
||||
EulerRot::XYZ,
|
||||
-10.0f32.to_radians(), // X-axis rotation (tilt)
|
||||
-5.0f32.to_radians(), // Y-axis rotation
|
||||
10.0f32.to_radians(), // Z-axis rotation
|
||||
)),
|
||||
));
|
||||
parent.spawn((
|
||||
ActiveEvents::COLLISION_EVENTS,
|
||||
Transform::default(),
|
||||
Collider::ball(0.5), // Interaction radius
|
||||
Sensor,
|
||||
));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user