From 8ee1226525de5835bc13388cf53f7649c5d03ddc Mon Sep 17 00:00:00 2001 From: Martin Indra Date: Tue, 31 May 2022 15:40:45 +0200 Subject: [PATCH] Use de_pathing crate for path finding Fixes #12. --- Cargo.lock | 1 + crates/game/Cargo.toml | 1 + crates/game/src/game/command.rs | 14 +++--- crates/game/src/game/mod.rs | 4 +- crates/game/src/game/movement.rs | 73 ++++++++++++++++++++++++-------- 5 files changed, 69 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 50588c550..c5dc500ef 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1589,6 +1589,7 @@ dependencies = [ "de_core", "de_index", "de_map", + "de_pathing", "futures-lite", "glam", "iyes_loopless", diff --git a/crates/game/Cargo.toml b/crates/game/Cargo.toml index ef257ae2a..7a76ea07a 100644 --- a/crates/game/Cargo.toml +++ b/crates/game/Cargo.toml @@ -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" diff --git a/crates/game/src/game/command.rs b/crates/game/src/game/command.rs index d802f484a..4a6822e30 100644 --- a/crates/game/src/game/command.rs +++ b/crates/game/src/game/command.rs @@ -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}; @@ -26,7 +26,7 @@ impl Plugin for CommandPlugin { fn mouse_click_handler( mut click_events: EventReader, mut send_entity_events: EventWriter, - selected: Query, With)>, + selected: Query<(Entity, &GlobalTransform), (With, With)>, pointer: Res, ) { if !click_events.iter().any(|e| e.button == MouseButton::Right) { @@ -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, + )); } } diff --git a/crates/game/src/game/mod.rs b/crates/game/src/game/mod.rs index f8eab68b5..057c536ef 100644 --- a/crates/game/src/game/mod.rs +++ b/crates/game/src/game/mod.rs @@ -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::{ @@ -34,7 +35,8 @@ impl PluginGroup for GamePluginGroup { .add(CommandPlugin) .add(MovementPlugin) .add(SpawnerPlugin) - .add(IndexPlugin); + .add(IndexPlugin) + .add(PathingPlugin); } } diff --git a/crates/game/src/game/movement.rs b/crates/game/src/game/movement.rs index fc467c7f9..e163feb96 100644 --- a/crates/game/src/game/movement.rs +++ b/crates/game/src/game/movement.rs @@ -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; @@ -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, +} + +impl Target { + fn peek(&self) -> Vec2 { + *self.remaining.last().unwrap() + } + + fn pop(&mut self) -> bool { + self.remaining.pop(); + self.remaining.is_empty() + } } -impl From for Target { - fn from(position: Vec2) -> Self { - Self { position } +impl From for Target { + fn from(path: Path) -> Self { + Self { + remaining: Vec::from(path.waypoints()), + } } } -fn process_events(mut commands: Commands, mut events: EventReader) { +fn process_events( + mut commands: Commands, + mut events: EventReader, + finder: Option>, +) { + 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