Skip to content

Commit

Permalink
Merge branch 'main' into pawn_ai
Browse files Browse the repository at this point in the history
  • Loading branch information
lain-dono authored Jan 4, 2024
2 parents 75c5f4c + 04e674f commit 5f2d566
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/character.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::game_state::GameState;
use crate::{game_state::GameState, selectable::Selectable};

Check failure on line 1 in src/character.rs

View workflow job for this annotation

GitHub Actions / fmt

Diff in /home/runner/work/lifer/lifer/src/character.rs
use crate::mechanics::*;
use bevy::prelude::*;
use big_brain::prelude::*;
Expand Down Expand Up @@ -85,6 +85,7 @@ pub fn spawner_system(
transform,
..default()
},
Selectable {},
CharacterController {
speed: 5.0,
color: DEFAULT_COLOR,
Expand Down
29 changes: 28 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::character::SpawnCharacter;
use crate::raycast::PlaneRaycast;
use crate::selectable::{CurrentlySelected, Selectable};
use bevy::input::mouse::*;
use bevy::{
diagnostic::{FrameTimeDiagnosticsPlugin, LogDiagnosticsPlugin},
Expand All @@ -11,6 +13,7 @@ mod game_state;
mod main_menu;
mod mechanics;
mod raycast;
mod selectable;
mod splash_screen;

#[derive(Component)]
Expand All @@ -22,7 +25,7 @@ struct PlayerCamera {
pub struct PlayerInputPlugin;
impl Plugin for PlayerInputPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Update, update_player_camera);
app.add_systems(Update, (update_player_camera, object_select));
}
}

Expand Down Expand Up @@ -52,6 +55,7 @@ fn main() {
crate::raycast::RaycastPlugin,
));

app.init_resource::<CurrentlySelected>();
app.add_systems(Startup, setup);

app.run();
Expand Down Expand Up @@ -101,6 +105,29 @@ fn setup(
));
}

fn object_select(
mut query: Query<&PlaneRaycast, With<Camera>>,
selectables: Query<(&GlobalTransform, &Selectable, Entity)>,
mbtn: Res<Input<MouseButton>>,
mut sel: ResMut<CurrentlySelected>,
) {
if !mbtn.just_pressed(MouseButton::Left) {
return;
}

let raycast = query.single_mut();
let Some(pos) = raycast.result else { return };

let control_distance = 2.0;

for p in selectables.iter() {
if (p.0.translation() - pos).length() < control_distance {
sel.sel = p.2;
println!("Selected entity with id = {}", p.2.index());
}
}
}

fn update_player_camera(
mut query: Query<(&mut Transform, &PlayerCamera)>,
keyboard: Res<Input<KeyCode>>,
Expand Down
17 changes: 17 additions & 0 deletions src/selectable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use bevy::prelude::*;

#[derive(Component)]
pub struct Selectable {}

#[derive(Resource)]
pub struct CurrentlySelected {
pub sel: Entity,
}

impl Default for CurrentlySelected {
fn default() -> Self {
CurrentlySelected {
sel: Entity::from_raw(u32::MAX),
}
}
}

0 comments on commit 5f2d566

Please sign in to comment.