Skip to content

Commit

Permalink
added path finding
Browse files Browse the repository at this point in the history
  • Loading branch information
Amatsugu committed Oct 5, 2024
1 parent 5e7ab32 commit 06ffb2b
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 70 deletions.
14 changes: 7 additions & 7 deletions engine/world_generation/src/hex_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pub fn tile_to_world_distance(dist: u32) -> f32 {
return dist as f32 * (2. * INNER_RADIUS);
}

pub fn get_tile_count(radius: usize) -> usize {
pub fn get_tile_count_in_range(radius: usize) -> usize {
return 1 + 3 * (radius + 1) * radius;
}

Expand Down Expand Up @@ -240,7 +240,7 @@ impl HexCoord {

pub fn hex_select(&self, radius: usize, include_center: bool) -> Vec<HexCoord> {
assert!(radius != 0, "Radius cannot be zero");
let mut result = Vec::with_capacity(get_tile_count(radius));
let mut result = Vec::with_capacity(get_tile_count_in_range(radius));

if include_center {
result.push(*self);
Expand All @@ -267,7 +267,7 @@ impl HexCoord {
width: usize,
) -> Vec<HexCoord> {
assert!(radius != 0, "Radius cannot be zero");
let mut result = Vec::with_capacity(get_tile_count(radius));
let mut result = Vec::with_capacity(get_tile_count_in_range(radius));

if include_center {
if self.is_in_bounds(height, width) {
Expand Down Expand Up @@ -296,10 +296,10 @@ impl HexCoord {

let mut p = self.scale(4, radius);

if radius == 1 {
result.push(*self);
return result;
}
// if radius == 1 {
// result.push(*self);
// return result;
// }

for i in 0..6 {
for _j in 0..radius {
Expand Down
8 changes: 4 additions & 4 deletions engine/world_generation/src/map/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ impl Map {
assert!(radius != 0, "Radius cannot be zero");

let mut result = if include_center {
Vec::with_capacity(get_tile_count(radius) + 1)
Vec::with_capacity(get_tile_count_in_range(radius) + 1)
} else {
Vec::with_capacity(get_tile_count(radius))
Vec::with_capacity(get_tile_count_in_range(radius))
};
if include_center {
let h = self.sample_height(&center);
Expand Down Expand Up @@ -218,9 +218,9 @@ impl Map {
assert!(radius != 0, "Radius cannot be zero");

let mut result = if include_center {
Vec::with_capacity(get_tile_count(radius) + 1)
Vec::with_capacity(get_tile_count_in_range(radius) + 1)
} else {
Vec::with_capacity(get_tile_count(radius))
Vec::with_capacity(get_tile_count_in_range(radius))
};
if include_center {
let h = self.sample_height_mut(&center);
Expand Down
3 changes: 1 addition & 2 deletions game/main/src/map_rendering/map_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ use crate::{
},
utlis::{
chunk_utils::{paint_map, prepare_chunk_mesh_with_collider},
render_distance_system::RenderDistanceVisibility,
},
};

use super::{chunk_rebuild::ChunkRebuildPlugin, terraforming_test::TerraFormingTestPlugin};
use super::{chunk_rebuild::ChunkRebuildPlugin, render_distance_system::RenderDistanceVisibility, terraforming_test::TerraFormingTestPlugin};

pub struct MapInitPlugin;

Expand Down
1 change: 1 addition & 0 deletions game/main/src/map_rendering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ pub mod chunk_rebuild;
pub mod map_init;
pub mod prelude;
pub mod terraforming_test;
pub mod render_distance_system;
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ impl Plugin for RenderDistancePlugin {
app.register_type::<RenderDistanceSettings>();
app.add_systems(PostUpdate, render_distance_system)
.insert_resource(RenderDistanceSettings::default());

#[cfg(debug_assertions)]
app.insert_resource(RenderDistanceSettings::new(f32::MAX));
}
}

Expand Down
2 changes: 1 addition & 1 deletion game/main/src/phos.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::camera_system::components::PhosCamera;
use crate::map_rendering::map_init::MapInitPlugin;
use crate::map_rendering::render_distance_system::RenderDistancePlugin;
use crate::utlis::editor_plugin::EditorPlugin;
use crate::utlis::render_distance_system::RenderDistancePlugin;
use crate::utlis::tile_selection_plugin::TileSelectionPlugin;
use crate::{camera_system::camera_plugin::PhosCameraPlugin, utlis::debug_plugin::DebugPlugin};
use bevy::{
Expand Down
27 changes: 2 additions & 25 deletions game/main/src/utlis/debug_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
use bevy::{prelude::*, window::PrimaryWindow};
use bevy_inspector_egui::bevy_egui::{systems::InputEvents, EguiContexts};
use bevy_inspector_egui::egui;
use bevy_rapier3d::prelude::*;
use bevy::prelude::*;
use shared::resources::TileUnderCursor;
use shared::states::GameplayState;
use shared::tags::MainCamera;
use world_generation::{
consts::HEX_CORNERS,
hex_utils::{HexCoord, INNER_RADIUS},
prelude::Map,
states::GeneratorState,
};
use world_generation::{consts::HEX_CORNERS, prelude::Map, states::GeneratorState};

pub struct DebugPlugin;

Expand Down Expand Up @@ -76,24 +67,10 @@ fn show_tile_heights(map: Res<Map>, mut gizmos: Gizmos, shape: Res<Shape>, tile_
Quat::IDENTITY,
Color::WHITE,
);
let nbors = map.get_neighbors(&contact.tile);
let contact_tile_pos = contact.tile.to_world(map.sample_height(&contact.tile));

// for i in 0..6 {
// if let Some(s) = nbors[i] {
// let coord = contact.tile.get_neighbor(i);
// let p = coord.to_world(s);
// gizmos.arrow(p, p + Vec3::Y * (i as f32 + 1.0), Color::WHITE);
// }

// let p = HEX_CORNERS[i] + contact_tile_pos;
// gizmos.arrow(p, p + Vec3::Y * (i as f32 + 1.0), LinearRgba::rgb(1.0, 0.0, 0.5));
// }

gizmos.line(contact.point, contact.point + Vec3::X, LinearRgba::RED);
gizmos.line(contact.point, contact.point + Vec3::Y, LinearRgba::GREEN);
gizmos.line(contact.point, contact.point + Vec3::Z, LinearRgba::BLUE);
//gizmos.sphere(contact_point, Quat::IDENTITY, 0.1, LinearRgba::rgb(1., 0., 0.5));
}
}

Expand Down
1 change: 0 additions & 1 deletion game/main/src/utlis/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod chunk_utils;
pub mod render_distance_system;
pub mod debug_plugin;
pub mod editor_plugin;
pub mod tile_selection_plugin;
2 changes: 1 addition & 1 deletion game/units/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ pub struct Target(pub HexCoord);
pub struct Path(pub Vec<Vec3>, pub usize);

#[derive(Component, Debug)]
pub struct PathTask(pub Task<CommandQueue>);
pub struct PathTask(pub Task<Option<CommandQueue>>);
39 changes: 32 additions & 7 deletions game/units/src/nav_data.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use bevy::prelude::Resource;
use ordered_float::OrderedFloat;
use world_generation::{
hex_utils::HexCoord,
prelude::{Chunk, Map},
};
use world_generation::{hex_utils::HexCoord, prelude::Map};

#[derive(Clone)]
#[derive(Clone, Resource)]
pub struct NavData {
pub tiles: Vec<NavTile>,
pub map_height: usize,
Expand All @@ -14,24 +12,32 @@ pub struct NavData {
impl NavData {
pub fn get_neighbors(&self, coord: &HexCoord) -> Vec<(HexCoord, OrderedFloat<f32>)> {
let mut neighbors = Vec::with_capacity(6);
let cur_height = self.get_height(coord);
for i in 0..6 {
let n = coord.get_neighbor(i);
if !self.is_in_bounds(&n) {
continue;
}
neighbors.push((n, OrderedFloat(1.0)));
let n_height = self.get_height(&n);
neighbors.push((n, OrderedFloat((cur_height - n_height).abs().powi(2))));
}
return neighbors;
}
pub fn get(&self, coord: &HexCoord) -> &NavTile {
return &self.tiles[coord.to_index(self.map_width)];
}

pub fn get_height(&self, coord: &HexCoord) -> f32 {
return self.tiles[coord.to_index(self.map_width)].height;
}

pub fn is_in_bounds(&self, pos: &HexCoord) -> bool {
return pos.is_in_bounds(self.map_height, self.map_width);
}

pub fn build(map: &Map) -> NavData {
#[cfg(feature = "tracing")]
let _path_span = info_span!("Build Nav Data").entered();
let mut tiles = Vec::with_capacity(map.get_tile_count());
let h = map.get_tile_height();
let w = map.get_tile_width();
Expand All @@ -54,6 +60,25 @@ impl NavData {
map_height: h,
};
}

pub fn update(&mut self, map: &Map) {
#[cfg(feature = "tracing")]
let _path_span = info_span!("Update Nav Data").entered();
let h = map.get_tile_height();
let w = map.get_tile_width();
for y in 0..h {
for x in 0..w {
let coord = HexCoord::from_grid_pos(x, y);
let height = map.sample_height(&coord);
let tile = NavTile {
coord,
height,
move_cost: 1.0,
};
self.tiles[y * w + x] = tile;
}
}
}
}

#[derive(Clone)]
Expand All @@ -65,6 +90,6 @@ pub struct NavTile {

impl NavTile {
pub fn calculate_heuristic(&self, to: &HexCoord) -> OrderedFloat<f32> {
todo!();
return (self.coord.distance(to) as f32).into();
}
}
24 changes: 20 additions & 4 deletions game/units/src/units_debug_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use std::f32::consts::E;

use bevy::prelude::*;
use shared::{resources::TileUnderCursor, sets::GameplaySet, states::AssetLoadState};
use world_generation::{heightmap, prelude::Map};

use crate::components::{LandUnit, Target, Unit};
use crate::components::{LandUnit, Path, Target, Unit};

pub struct UnitsDebugPlugin;

Expand All @@ -13,6 +10,7 @@ impl Plugin for UnitsDebugPlugin {
app.add_systems(Update, init.run_if(in_state(AssetLoadState::Loading)));

app.add_systems(Update, (spawn_test_unit, set_unit_target).in_set(GameplaySet));
app.add_systems(FixedUpdate, (visualize_paths).in_set(GameplaySet));
}
}

Expand Down Expand Up @@ -64,3 +62,21 @@ fn set_unit_target(
}
}
}

fn visualize_paths(units: Query<&Path, With<Unit>>, mut gizmos: Gizmos) {
for path in units.iter() {
if path.1 > path.0.len() {
continue;
}
for node in 1..path.0.len() {
let from = path.0[node];
let to = path.0[node - 1];
let color = if node > path.1 {
LinearRgba::rgb(1.0, 0.5, 0.0)
} else {
LinearRgba::rgb(1.0, 0.5, 1.5)
};
gizmos.line(from + Vec3::Y * 0.1, to + Vec3::Y * 0.1, color);
}
}
}
Loading

0 comments on commit 06ffb2b

Please sign in to comment.