From b9b774c245f005bbeab76b74d8035efe948af8e4 Mon Sep 17 00:00:00 2001 From: tigerplush Date: Wed, 11 Mar 2026 17:29:10 +0100 Subject: [PATCH 1/5] rename Block coordinates to IWorldCoordinates --- crates/common/src/functions.rs | 8 +++---- crates/common/src/types.rs | 24 ++++++++++--------- crates/designations/src/designations.rs | 4 ++-- crates/map_generation/src/chunk.rs | 10 ++++---- .../map_generation/src/chunk_visualisation.rs | 4 ++-- crates/map_generation/src/map_generation.rs | 10 ++++---- crates/pathfinding/src/path.rs | 6 ++--- crates/pathfinding/src/pathfinder.rs | 18 +++++++------- crates/work/src/lib.rs | 6 ++--- crates/work/src/tasks.rs | 8 +++---- crates/work/src/tasks/dig.rs | 4 ++-- crates/work/src/tasks/walk_to.rs | 4 ++-- crates/work/src/tasks/walk_to_nearest.rs | 4 ++-- 13 files changed, 56 insertions(+), 54 deletions(-) diff --git a/crates/common/src/functions.rs b/crates/common/src/functions.rs index e5a1d31..0e8354c 100644 --- a/crates/common/src/functions.rs +++ b/crates/common/src/functions.rs @@ -1,19 +1,19 @@ use bevy::prelude::*; -use crate::{constants::TILE_SIZE, types::BlockCoordinates}; +use crate::{constants::TILE_SIZE, types::IWorldCoordinates}; -pub fn world_position_to_world_coordinates(world_position: Vec3) -> BlockCoordinates { +pub fn world_position_to_world_coordinates(world_position: Vec3) -> IWorldCoordinates { let x = world_position.x / TILE_SIZE.x; let y = world_position.y / TILE_SIZE.y; let z = world_position.z; - BlockCoordinates(IVec3::new( + IWorldCoordinates(IVec3::new( x.round() as i32, y.round() as i32, z.round() as i32, )) } -pub fn world_coordinates_to_world_position(world_coordinates: BlockCoordinates) -> Vec3 { +pub fn world_coordinates_to_world_position(world_coordinates: IWorldCoordinates) -> Vec3 { let x = world_coordinates.0.x as f32 * TILE_SIZE.x; let y = world_coordinates.0.y as f32 * TILE_SIZE.y; let z = world_coordinates.0.z as f32; diff --git a/crates/common/src/types.rs b/crates/common/src/types.rs index 8e2d84a..9b6f6e2 100644 --- a/crates/common/src/types.rs +++ b/crates/common/src/types.rs @@ -6,27 +6,28 @@ use bevy::{ use crate::traits::Neighbors; +/// These are essentially rounded world coordinates. #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Reflect)] -pub struct BlockCoordinates(pub IVec3); +pub struct IWorldCoordinates(pub IVec3); -impl BlockCoordinates { +impl IWorldCoordinates { /// Manually overrides the z value of the coordinates. - pub fn with_z_offset(mut self, z_offset: i32) -> BlockCoordinates { + pub fn with_z_offset(mut self, z_offset: i32) -> IWorldCoordinates { self.0.z = z_offset; self } } -impl Neighbors for BlockCoordinates { - fn same_layer_neighbors(&self) -> Vec<(BlockCoordinates, u32)> { +impl Neighbors for IWorldCoordinates { + fn same_layer_neighbors(&self) -> Vec<(IWorldCoordinates, u32)> { self.0 .same_layer_neighbors() .iter() - .map(|(vec, cost)| (BlockCoordinates(*vec), *cost)) + .map(|(vec, cost)| (IWorldCoordinates(*vec), *cost)) .collect() } - fn all_neighbors(&self) -> Vec<(BlockCoordinates, u32)> { + fn all_neighbors(&self) -> Vec<(IWorldCoordinates, u32)> { todo!("not implemented") } } @@ -43,17 +44,18 @@ impl From<(u32, u32, u32)> for ChunkBlockCoordinates { } } +/// World coordinates that can be directly taken from a translation. #[derive(Clone, Copy, Debug, PartialEq, Default, Reflect, Component)] pub struct WorldCoordinates(pub Vec3); impl WorldCoordinates { - pub fn block(&self) -> BlockCoordinates { - BlockCoordinates(self.0.round().as_ivec3()) + pub fn block(&self) -> IWorldCoordinates { + IWorldCoordinates(self.0.round().as_ivec3()) } } -impl From<&BlockCoordinates> for WorldCoordinates { - fn from(wc: &BlockCoordinates) -> WorldCoordinates { +impl From<&IWorldCoordinates> for WorldCoordinates { + fn from(wc: &IWorldCoordinates) -> WorldCoordinates { WorldCoordinates(wc.0.as_vec3()) } } diff --git a/crates/designations/src/designations.rs b/crates/designations/src/designations.rs index d7210ef..e226308 100644 --- a/crates/designations/src/designations.rs +++ b/crates/designations/src/designations.rs @@ -2,7 +2,7 @@ use bevy::{prelude::*, window::PrimaryWindow}; use bevy_inspector_egui::bevy_egui::{EguiContexts, EguiPrimaryContextPass}; use camera::CameraLayer; use common::{ - functions::world_position_to_world_coordinates, states::AppState, types::BlockCoordinates, + functions::world_position_to_world_coordinates, states::AppState, types::IWorldCoordinates, }; use leafwing_input_manager::{ Actionlike, @@ -23,7 +23,7 @@ pub(crate) enum MouseActions { #[derive(Message)] enum BrushInputEvent { - Designated(BlockCoordinates), + Designated(IWorldCoordinates), } #[derive(Default, Reflect, Resource)] diff --git a/crates/map_generation/src/chunk.rs b/crates/map_generation/src/chunk.rs index e0e61b5..84a2367 100644 --- a/crates/map_generation/src/chunk.rs +++ b/crates/map_generation/src/chunk.rs @@ -1,5 +1,5 @@ use bevy::prelude::*; -use common::types::{BlockCoordinates, ChunkBlockCoordinates, ChunkCoordinates}; +use common::types::{IWorldCoordinates, ChunkBlockCoordinates, ChunkCoordinates}; use noise::{NoiseFn, OpenSimplex}; use crate::block_type::{BlockType, SolidMaterial}; @@ -55,7 +55,7 @@ pub(crate) trait ToChunkAndBlock { fn to_chunk_and_block(&self) -> (ChunkCoordinates, ChunkBlockCoordinates); } -impl ToChunkAndBlock for BlockCoordinates { +impl ToChunkAndBlock for IWorldCoordinates { fn to_chunk_and_block(&self) -> (ChunkCoordinates, ChunkBlockCoordinates) { ( ChunkCoordinates(self.0.div_euclid(CHUNK_SIZE.as_ivec3())), @@ -75,12 +75,12 @@ pub(crate) fn to_index(coordinates: impl Into) -> usize { pub(crate) fn to_world_coordinates( chunk_coordinates: ChunkCoordinates, block_coordinates: impl Into, -) -> BlockCoordinates { +) -> IWorldCoordinates { let block_coordinates: ChunkBlockCoordinates = block_coordinates.into(); let x = chunk_coordinates.0.x * CHUNK_SIZE.x as i32 + block_coordinates.0.x as i32; let y = chunk_coordinates.0.y * CHUNK_SIZE.y as i32 + block_coordinates.0.y as i32; let z = chunk_coordinates.0.z * CHUNK_SIZE.z as i32 + block_coordinates.0.z as i32; - BlockCoordinates(IVec3::new(x, y, z)) + IWorldCoordinates(IVec3::new(x, y, z)) } #[test] @@ -101,6 +101,6 @@ fn test_to_world() { let chunk_coordinates = ChunkCoordinates(IVec3::ZERO); assert_eq!( to_world_coordinates(chunk_coordinates, (1, 2, 3)), - BlockCoordinates(IVec3::new(1, 2, 3)) + IWorldCoordinates(IVec3::new(1, 2, 3)) ) } diff --git a/crates/map_generation/src/chunk_visualisation.rs b/crates/map_generation/src/chunk_visualisation.rs index fa34a78..a6ed8a6 100644 --- a/crates/map_generation/src/chunk_visualisation.rs +++ b/crates/map_generation/src/chunk_visualisation.rs @@ -10,7 +10,7 @@ use camera::CameraLayer; use common::{ constants::TILE_SIZE, traits::Neighbors, - types::{BlockCoordinates, ChunkCoordinates}, + types::{IWorldCoordinates, ChunkCoordinates}, }; use std::ops::{Range, RangeInclusive}; @@ -281,7 +281,7 @@ pub(crate) fn on_chunk_visualisation_event( #[derive(Event)] pub enum ChunkVisualisationEvent { - SetDirty(BlockCoordinates), + SetDirty(IWorldCoordinates), } #[derive(Component, Reflect)] diff --git a/crates/map_generation/src/map_generation.rs b/crates/map_generation/src/map_generation.rs index 9390e05..9598a60 100644 --- a/crates/map_generation/src/map_generation.rs +++ b/crates/map_generation/src/map_generation.rs @@ -4,7 +4,7 @@ use common::{ constants::TILE_SIZE, states::AppState, traits::{AddNamedObserver, Neighbors}, - types::{BlockCoordinates, ChunkCoordinates}, + types::{IWorldCoordinates, ChunkCoordinates}, }; use noise::OpenSimplex; @@ -74,7 +74,7 @@ impl WorldMap { } /// Tries to fetch a block from world. Will return None, if the chunk doesn't exist or the block is of type BlockType::None - pub fn get_block(&self, coordinates: BlockCoordinates) -> Option { + pub fn get_block(&self, coordinates: IWorldCoordinates) -> Option { let (chunk_coordinates, block_coordinates) = coordinates.to_chunk_and_block(); let index = to_index(block_coordinates); self.chunks @@ -86,7 +86,7 @@ impl WorldMap { } /// Returns a result of type BlockType, if the corresponding chunk has been found. Returns an empty error, when the chunk is not loaded. - pub fn get_raw_block(&self, coordinates: BlockCoordinates) -> Option { + pub fn get_raw_block(&self, coordinates: IWorldCoordinates) -> Option { let (chunk_coordinate, block_coordinates) = coordinates.to_chunk_and_block(); let index = to_index(block_coordinates); self.chunks @@ -94,7 +94,7 @@ impl WorldMap { .map(|chunk| chunk.blocks[index]) } - pub fn solidness(&self, coordinates: BlockCoordinates) -> bool { + pub fn solidness(&self, coordinates: IWorldCoordinates) -> bool { let (chunk_coordinates, block_coordinates) = coordinates.to_chunk_and_block(); let index = to_index(block_coordinates); self.chunks @@ -103,7 +103,7 @@ impl WorldMap { } /// Adds damage to a block. Returns true, if the block is destroyed, false otherwise. - pub fn damage_block(&mut self, coordinates: BlockCoordinates, damage: f32) -> bool { + pub fn damage_block(&mut self, coordinates: IWorldCoordinates, damage: f32) -> bool { let remaining_health = { *self .block_states diff --git a/crates/pathfinding/src/path.rs b/crates/pathfinding/src/path.rs index 6a2df6b..16d3b5b 100644 --- a/crates/pathfinding/src/path.rs +++ b/crates/pathfinding/src/path.rs @@ -2,18 +2,18 @@ use bevy::prelude::*; use std::time::Duration; use crate::{PathEvent, PathState}; -use common::types::{BlockCoordinates, WorldCoordinates}; +use common::types::{IWorldCoordinates, WorldCoordinates}; #[derive(Clone, Component, Debug, PartialEq, Reflect)] #[reflect(Component)] pub struct Path { - set: Vec, + set: Vec, current_index: usize, current_t: f32, } impl Path { - pub(crate) fn new(set: Vec) -> Self { + pub(crate) fn new(set: Vec) -> Self { Path { set, current_index: 0, diff --git a/crates/pathfinding/src/pathfinder.rs b/crates/pathfinding/src/pathfinder.rs index 23e459c..21681fc 100644 --- a/crates/pathfinding/src/pathfinder.rs +++ b/crates/pathfinding/src/pathfinder.rs @@ -1,7 +1,7 @@ use std::cmp::Reverse; use bevy::{ecs::spawn::SpawnIter, platform::collections::HashMap, prelude::*}; -use common::{traits::Neighbors, types::BlockCoordinates}; +use common::{traits::Neighbors, types::IWorldCoordinates}; use map_generation::{block_type::BlockType, map_generation::WorldMap}; use priority_queue::PriorityQueue; @@ -45,7 +45,7 @@ pub(crate) struct PathfinderListener; impl Pathfinder { /// Creates a new pathfinder that will try to find a path via A* from start to target - fn new(start: BlockCoordinates, target: BlockCoordinates) -> Self { + fn new(start: IWorldCoordinates, target: IWorldCoordinates) -> Self { let frontier = PriorityQueue::from(vec![(start.0, Reverse(0))]); let mut came_from = HashMap::default(); came_from.insert(start.0, None); @@ -63,7 +63,7 @@ impl Pathfinder { /// Spawns a PathfinderListener with one separate Pathfinder child target the exact block /// /// Use this, if an entity has to land exactly on the given target - pub fn exact(start: BlockCoordinates, target: BlockCoordinates) -> impl Bundle { + pub fn exact(start: IWorldCoordinates, target: IWorldCoordinates) -> impl Bundle { ( PathfinderListener, children![( @@ -76,7 +76,7 @@ impl Pathfinder { /// Spawns a PathfinderListener with separate Pathfinder children targeting the blocks surrounding the given target /// /// Use this if an entity has to come close to a given target but not go onto it - pub fn nearest(start: BlockCoordinates, target: BlockCoordinates) -> impl Bundle { + pub fn nearest(start: IWorldCoordinates, target: IWorldCoordinates) -> impl Bundle { let finders: Vec<(Pathfinder, Name)> = target .same_layer_neighbors() .iter() @@ -138,7 +138,7 @@ impl Pathfinder { world_map: &WorldMap, neighbor: IVec3, ) -> Result { - let neighbor_block = world_map.get_raw_block(BlockCoordinates(neighbor)).ok_or({ + let neighbor_block = world_map.get_raw_block(IWorldCoordinates(neighbor)).ok_or({ if self.current_failures >= self.allowed_failures { PathfindingErrors::Unreachable } else { @@ -148,7 +148,7 @@ impl Pathfinder { trace!("checking {}, is {:?}", neighbor, neighbor_block); let block_below = world_map - .get_raw_block(BlockCoordinates(neighbor - IVec3::Z)) + .get_raw_block(IWorldCoordinates(neighbor - IVec3::Z)) .ok_or({ if self.current_failures >= self.allowed_failures { PathfindingErrors::Unreachable @@ -160,13 +160,13 @@ impl Pathfinder { Ok(neighbor_block == BlockType::None && matches!(block_below, BlockType::Solid(_))) } - fn to_path(&self) -> Vec { + fn to_path(&self) -> Vec { let mut points = vec![]; let mut next = self.target; - points.push(BlockCoordinates(next)); + points.push(IWorldCoordinates(next)); while let Some(point_option) = self.came_from.get(&next) { if let Some(point) = point_option { - points.push(BlockCoordinates(*point)); + points.push(IWorldCoordinates(*point)); next = *point; } else { break; diff --git a/crates/work/src/lib.rs b/crates/work/src/lib.rs index 080d985..334cc5f 100644 --- a/crates/work/src/lib.rs +++ b/crates/work/src/lib.rs @@ -1,7 +1,7 @@ use bevy::prelude::*; use common::{ traits::SpawnNamedObserver, - types::{BlockCoordinates, WorldCoordinates}, + types::{IWorldCoordinates, WorldCoordinates}, }; use tasks::{Task, TaskQueue, TaskState}; use work_order_queue::WorkOrderQueue; @@ -21,12 +21,12 @@ pub fn plugin(app: &mut App) { /// Represents work orders that can be created by the player #[derive(Clone, Component, Copy, Debug, PartialEq, Reflect)] pub enum WorkOrder { - Dig(BlockCoordinates), + Dig(IWorldCoordinates), } impl WorkOrder { /// Creates a digging work order for the given world position - pub fn dig(world_coordinates: BlockCoordinates) -> impl Bundle { + pub fn dig(world_coordinates: IWorldCoordinates) -> impl Bundle { ( Name::new(format!("WorkOrder - Dig {}", world_coordinates.0)), WorldCoordinates(world_coordinates.0.as_vec3()), diff --git a/crates/work/src/tasks.rs b/crates/work/src/tasks.rs index 043c70b..4019ed4 100644 --- a/crates/work/src/tasks.rs +++ b/crates/work/src/tasks.rs @@ -1,5 +1,5 @@ use bevy::prelude::*; -use common::types::BlockCoordinates; +use common::types::IWorldCoordinates; use dig::Dig; use map_generation::map_generation::WorldMap; use walk_to::WalkTo; @@ -49,16 +49,16 @@ pub(crate) enum Task { } impl Task { - pub(crate) fn dig(pos: BlockCoordinates) -> Task { + pub(crate) fn dig(pos: IWorldCoordinates) -> Task { Task::Dig(Dig(pos)) } #[allow(dead_code)] - pub(crate) fn walk_to(pos: BlockCoordinates) -> Task { + pub(crate) fn walk_to(pos: IWorldCoordinates) -> Task { Task::WalkTo(WalkTo(pos)) } - pub(crate) fn walk_to_nearest(pos: BlockCoordinates) -> Task { + pub(crate) fn walk_to_nearest(pos: IWorldCoordinates) -> Task { Task::WalkToNearest(WalkToNearest(pos)) } } diff --git a/crates/work/src/tasks/dig.rs b/crates/work/src/tasks/dig.rs index f159aac..5df44f5 100644 --- a/crates/work/src/tasks/dig.rs +++ b/crates/work/src/tasks/dig.rs @@ -1,12 +1,12 @@ use bevy::prelude::*; -use common::types::BlockCoordinates; +use common::types::IWorldCoordinates; use map_generation::{chunk_visualisation::ChunkVisualisationEvent, map_generation::WorldMap}; use super::Task; #[derive(Clone, Component, Copy, Debug, Reflect)] #[reflect(Component)] -pub(crate) struct Dig(pub(crate) BlockCoordinates); +pub(crate) struct Dig(pub(crate) IWorldCoordinates); pub(crate) fn handle( time: Res