This commit is contained in:
LorrensP-2158466
2025-04-06 22:51:58 +02:00
parent 910cb4c70e
commit b118a2580b
3 changed files with 64 additions and 2 deletions

View File

@@ -2,7 +2,6 @@ use bevy::prelude::*;
use bevy_rapier3d::prelude::ColliderDisabled; use bevy_rapier3d::prelude::ColliderDisabled;
use crate::{ use crate::{
level_instantiation::Door,
player::{Player, PlayerAction, toolbar::Item}, player::{Player, PlayerAction, toolbar::Item},
util::single_mut, util::single_mut,
}; };
@@ -17,13 +16,18 @@ pub struct KeyCardId(pub usize);
#[derive(Component, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)] #[derive(Component, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub struct OpenedByKey(pub KeyCardId); pub struct OpenedByKey(pub KeyCardId);
#[derive(Resource, Default, Clone, Copy)]
pub struct WrongKey(pub bool);
impl OpenedByKey { impl OpenedByKey {
pub fn can_be_opened_by(&self, card: KeyCardId) -> bool { pub fn can_be_opened_by(&self, card: KeyCardId) -> bool {
self.0 == card self.0 == card
} }
} }
pub fn plugin(_app: &mut App) {} pub fn plugin(app: &mut App) {
app.init_resource::<WrongKey>();
}
pub fn handle_pick_up( pub fn handle_pick_up(
mut commands: Commands, mut commands: Commands,
@@ -103,6 +107,7 @@ pub fn handle_door_interaction(
mut commands: Commands, mut commands: Commands,
mut player_query: Query<(&PlayerAction, &mut Item), With<Player>>, mut player_query: Query<(&PlayerAction, &mut Item), With<Player>>,
card_query: Query<&KeyCardId>, card_query: Query<&KeyCardId>,
mut wrong_key: ResMut<WrongKey>,
// current interactable // current interactable
interaction_opportunity: ResMut<InteractionOpportunity>, interaction_opportunity: ResMut<InteractionOpportunity>,
door_query: Query<(Entity, &OpenedByKey), With<Interact>>, 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 // unwrap is safe because of the item.inner().and_then
commands.entity(item.inner().unwrap()).despawn_recursive(); commands.entity(item.inner().unwrap()).despawn_recursive();
item.take(); item.take();
} else {
// wrong key :)
wrong_key.0 = true;
} }
} }

View File

@@ -7,6 +7,7 @@ use bevy_rapier3d::prelude::*;
use bevy_rapier3d::rapier::prelude::CollisionEventFlags; use bevy_rapier3d::rapier::prelude::CollisionEventFlags;
use std::iter; use std::iter;
use super::objects::WrongKey;
use super::{Interact, objects}; use super::{Interact, objects};
pub(super) fn plugin(app: &mut App) { pub(super) fn plugin(app: &mut App) {
@@ -35,6 +36,7 @@ fn update_interaction_opportunities(
mut collision_events: EventReader<CollisionEvent>, mut collision_events: EventReader<CollisionEvent>,
player_query: Query<Entity, With<Player>>, player_query: Query<Entity, With<Player>>,
parents: Query<&Parent>, parents: Query<&Parent>,
mut wrong_key: ResMut<WrongKey>,
target_query: Query<Entity, (Without<Player>, With<Interact>)>, target_query: Query<Entity, (Without<Player>, With<Interact>)>,
mut interaction_opportunity: ResMut<InteractionOpportunity>, mut interaction_opportunity: ResMut<InteractionOpportunity>,
) { ) {
@@ -63,6 +65,8 @@ fn update_interaction_opportunities(
if started { if started {
interaction_opportunity.0.replace(interactable); interaction_opportunity.0.replace(interactable);
} else { } else {
// reset when gone
wrong_key.0 = false;
interaction_opportunity.0.take_if(|t| *t == interactable); interaction_opportunity.0.take_if(|t| *t == interactable);
} }
} }
@@ -72,6 +76,7 @@ fn display_interaction_prompt(
interaction_opportunity: Res<InteractionOpportunity>, interaction_opportunity: Res<InteractionOpportunity>,
mut egui_contexts: EguiContexts, mut egui_contexts: EguiContexts,
primary_windows: Query<&Window, With<PrimaryWindow>>, primary_windows: Query<&Window, With<PrimaryWindow>>,
wrong_key: Res<WrongKey>,
names: Query<(&Name, Option<&Door>), With<Interact>>, // only the interactables ofcourse names: Query<(&Name, Option<&Door>), With<Interact>>, // only the interactables ofcourse
) { ) {
let Some(opportunity) = interaction_opportunity.0 else { 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.)) .fixed_pos(egui::Pos2::new(window.width() / 2., window.height() / 2.))
.show(egui_contexts.ctx_mut(), |ui| { .show(egui_contexts.ctx_mut(), |ui| {
ui.label(interaction_str); ui.label(interaction_str);
if wrong_key.0 {
ui.label("Wrong Key :)");
}
}); });
} }

View File

@@ -597,6 +597,7 @@ fn spawn_objects(
} else { } else {
Transform::from_xyz(2.0 * x as f32, 0.5, -2.0 * z as f32) Transform::from_xyz(2.0 * x as f32, 0.5, -2.0 * z as f32)
}; };
// Correct Key Card
commands commands
.spawn(( .spawn((
transform, transform,
@@ -627,6 +628,51 @@ fn spawn_objects(
Sensor, 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,
));
});
} }
} }