use num1 to open gates

This commit is contained in:
2025-03-08 15:04:15 +01:00
parent 9cad8f63bb
commit 6cf7216ef4
5 changed files with 90 additions and 31 deletions

51
src/gates.rs Normal file
View File

@@ -0,0 +1,51 @@
use std::collections::HashMap;
use avian3d::math::PI;
use bevy::{prelude::*, state::commands};
use crate::tb_entities::{NamedEntity, Pivot};
#[derive(Resource)]
enum GatesState {
Init,
GateOpen1,
}
pub fn plugin(app: &mut App) {
app.insert_resource(GatesState::Init);
app.add_systems(Update, update);
}
fn update(
keyboard: Res<ButtonInput<KeyCode>>,
mut state: ResMut<GatesState>,
mut names_entities: Query<(&NamedEntity, &mut Transform), Without<Pivot>>,
pivot_entities: Query<(&Pivot, &Transform)>,
) {
if keyboard.just_pressed(KeyCode::Digit1) {
if matches!(*state, GatesState::Init) {
*state = GatesState::GateOpen1;
let fences: HashMap<_, _> = vec![
("fence_01", Quat::from_rotation_y(PI / -2.)),
("fence_02", Quat::from_rotation_y(PI / 2.)),
]
.into_iter()
.collect();
for (named_entity, mut transform) in names_entities.iter_mut() {
if let Some(fence) = fences.get(named_entity.name.as_str()) {
let Some((_, t)) = pivot_entities
.iter()
.find(|(p, _)| &p.name == named_entity.name.as_str())
else {
warn!("Pivot not found for {}", named_entity.name);
continue;
};
transform.rotate_around(t.translation, *fence);
}
}
}
}
}

View File

@@ -1,6 +1,7 @@
mod alien;
mod camera;
mod cash;
mod gates;
mod heads_ui;
mod player;
mod tb_entities;
@@ -58,6 +59,7 @@ fn main() {
app.add_plugins(cash::plugin);
app.add_plugins(player::plugin);
app.add_plugins(heads_ui::plugin);
app.add_plugins(gates::plugin);
app.insert_resource(AmbientLight {
color: Color::WHITE,

View File

@@ -42,6 +42,13 @@ pub struct NamedEntity {
pub name: String,
}
#[derive(PointClass, Component, Reflect, Default)]
#[reflect(Component)]
#[require(Transform)]
pub struct Pivot {
pub name: String,
}
#[derive(PointClass, Component, Reflect, Default)]
#[reflect(Component)]
#[require(Transform)]