Skip to content

Commit

Permalink
Use de_pathing crate for path finding
Browse files Browse the repository at this point in the history
Fixes #12.
  • Loading branch information
Indy2222 committed May 31, 2022
1 parent 68904b7 commit 8ee1226
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 24 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/game/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ repository = "https://github.com/Indy2222/Digital-Extinction"
de_core = { path = "../core", version = "0.1.0-dev" }
de_map = { path = "../map", version = "0.1.0-dev" }
de_index = { path = "../index", version = "0.1.0-dev" }
de_pathing = { path = "../pathing", version = "0.1.0-dev" }

# Other
bevy = "0.7"
Expand Down
14 changes: 9 additions & 5 deletions crates/game/src/game/command.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use bevy::{
input::mouse::MouseButtonInput,
prelude::{
App, Entity, EventReader, EventWriter, MouseButton, ParallelSystemDescriptorCoercion,
Plugin, Query, Res, SystemSet, With,
App, Entity, EventReader, EventWriter, GlobalTransform, MouseButton,
ParallelSystemDescriptorCoercion, Plugin, Query, Res, SystemSet, With,
},
};
use de_core::{objects::MovableSolid, projection::ToFlat};
Expand All @@ -26,7 +26,7 @@ impl Plugin for CommandPlugin {
fn mouse_click_handler(
mut click_events: EventReader<MouseButtonInput>,
mut send_entity_events: EventWriter<SendEntityEvent>,
selected: Query<Entity, (With<Selected>, With<MovableSolid>)>,
selected: Query<(Entity, &GlobalTransform), (With<Selected>, With<MovableSolid>)>,
pointer: Res<Pointer>,
) {
if !click_events.iter().any(|e| e.button == MouseButton::Right) {
Expand All @@ -38,7 +38,11 @@ fn mouse_click_handler(
None => return,
};

for entity in selected.iter() {
send_entity_events.send(SendEntityEvent::new(entity, target));
for (entity, transform) in selected.iter() {
send_entity_events.send(SendEntityEvent::new(
entity,
transform.translation.to_flat(),
target,
));
}
}
4 changes: 3 additions & 1 deletion crates/game/src/game/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use bevy::{
};
use de_core::{gconfig::GameConfig, player::Player, state::GameState};
use de_index::IndexPlugin;
use de_pathing::PathingPlugin;
use iyes_loopless::prelude::*;

use self::{
Expand Down Expand Up @@ -34,7 +35,8 @@ impl PluginGroup for GamePluginGroup {
.add(CommandPlugin)
.add(MovementPlugin)
.add(SpawnerPlugin)
.add(IndexPlugin);
.add(IndexPlugin)
.add(PathingPlugin);
}
}

Expand Down
73 changes: 55 additions & 18 deletions crates/game/src/game/movement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::f32::consts::{FRAC_2_PI, PI};

use bevy::prelude::*;
use de_core::projection::ToMsl;
use de_pathing::{Path, PathFinder};
use glam::Vec2;

use super::Labels;
Expand All @@ -24,44 +25,84 @@ impl Plugin for MovementPlugin {

pub struct SendEntityEvent {
entity: Entity,
source: Vec2,
target: Vec2,
}

impl SendEntityEvent {
pub fn new(entity: Entity, target: Vec2) -> Self {
Self { entity, target }
pub fn new(entity: Entity, source: Vec2, target: Vec2) -> Self {
Self {
entity,
source,
target,
}
}
}

#[derive(Component)]
struct Target {
position: Vec2,
remaining: Vec<Vec2>,
}

impl Target {
fn peek(&self) -> Vec2 {
*self.remaining.last().unwrap()
}

fn pop(&mut self) -> bool {
self.remaining.pop();
self.remaining.is_empty()
}
}

impl From<Vec2> for Target {
fn from(position: Vec2) -> Self {
Self { position }
impl From<Path> for Target {
fn from(path: Path) -> Self {
Self {
remaining: Vec::from(path.waypoints()),
}
}
}

fn process_events(mut commands: Commands, mut events: EventReader<SendEntityEvent>) {
fn process_events(
mut commands: Commands,
mut events: EventReader<SendEntityEvent>,
finder: Option<Res<PathFinder>>,
) {
let finder = match finder {
Some(finder) => finder,
None => return,
};

for event in events.iter() {
commands
.entity(event.entity)
.insert(Target::from(event.target));
if let Some(path) = finder.find_path(event.source, event.target) {
commands.entity(event.entity).insert(Target::from(path));
}
}
}

// TODO precess PathFinderUpdated events

fn move_objects(
mut commands: Commands,
mut objects: Query<(Entity, &Target, &mut Transform)>,
mut objects: Query<(Entity, &mut Target, &mut Transform)>,
time: Res<Time>,
) {
let time_delta = time.delta().as_secs_f32();

for (entity, target, mut transform) in objects.iter_mut() {
let target_3d = target.position.to_msl();
let object_to_target = target_3d - transform.translation;
for (entity, mut target, mut transform) in objects.iter_mut() {
let object_to_target = loop {
let target_3d = target.peek().to_msl();
let object_to_target = target_3d - transform.translation;

if object_to_target.length() < TARGET_ACCURACY {
if target.pop() {
commands.entity(entity).remove::<Target>();
break object_to_target;
}
} else {
break object_to_target;
}
};

let forward = transform.forward();
let angle = forward.angle_between(object_to_target);
Expand All @@ -83,9 +124,5 @@ fn move_objects(
let delta_scalar = MAX_SPEED * time_delta;
let delta_vec = (delta_scalar * forward).clamp_length_max(object_to_target.dot(forward));
transform.translation += delta_vec;

if (transform.translation - target_3d).length() < TARGET_ACCURACY {
commands.entity(entity).remove::<Target>();
}
}
}

0 comments on commit 8ee1226

Please sign in to comment.