refactoring hex coords

This commit is contained in:
2026-03-14 19:55:53 -04:00
parent c5da119109
commit 912ee376c6
36 changed files with 595 additions and 698 deletions

View File

@@ -1,15 +1,17 @@
use bevy::prelude::Resource;
use hex::prelude::*;
use shared::building::BuildingIdentifier;
use world_generation::hex_utils::HexCoord;
#[derive(Resource)]
pub struct BuildQueue {
pub struct BuildQueue
{
pub queue: Vec<QueueEntry>,
}
impl Default for BuildQueue {
fn default() -> Self {
impl Default for BuildQueue
{
fn default() -> Self
{
Self {
queue: Default::default(),
}
@@ -17,7 +19,8 @@ impl Default for BuildQueue {
}
#[derive(PartialEq, Eq)]
pub struct QueueEntry {
pub struct QueueEntry
{
pub building: BuildingIdentifier,
pub pos: HexCoord,
}

View File

@@ -1,21 +1,26 @@
use bevy::prelude::*;
use world_generation::{hex_utils::HexCoord, prelude::Chunk};
use hex::prelude::*;
#[derive(Resource)]
pub struct BuildingMap {
pub struct BuildingMap
{
pub chunks: Vec<BuildingChunk>,
pub size: UVec2,
}
impl BuildingMap {
pub fn new(size: UVec2) -> Self {
impl BuildingMap
{
pub fn new(size: UVec2) -> Self
{
let mut db = BuildingMap {
size,
chunks: Vec::with_capacity(size.length_squared() as usize),
};
for y in 0..size.y as i32 {
for x in 0..size.x as i32 {
for y in 0..size.y as i32
{
for x in 0..size.x as i32
{
let offset = IVec2::new(x, y);
let index = (x + y * size.x as i32) as usize;
db.chunks.push(BuildingChunk::new(offset, index));
@@ -25,7 +30,8 @@ impl BuildingMap {
return db;
}
pub fn get_buildings_in_range(&self, coord: &HexCoord, radius: usize) -> Vec<&BuildingEntry> {
pub fn get_buildings_in_range(&self, coord: &HexCoord, radius: usize) -> Vec<&BuildingEntry>
{
assert!(radius != 0, "Radius cannot be zero");
let w = self.size.x as usize * Chunk::SIZE;
@@ -34,10 +40,13 @@ impl BuildingMap {
return self.get_buildings_in_coords(coords);
}
pub fn get_buildings_in_coords(&self, coords: Vec<HexCoord>) -> Vec<&BuildingEntry> {
pub fn get_buildings_in_coords(&self, coords: Vec<HexCoord>) -> Vec<&BuildingEntry>
{
let mut result = Vec::new();
for coord in &coords {
if let Some(buidling) = self.get_building(coord) {
for coord in &coords
{
if let Some(buidling) = self.get_building(coord)
{
result.push(buidling);
}
}
@@ -45,25 +54,30 @@ impl BuildingMap {
return result;
}
pub fn get_building(&self, coord: &HexCoord) -> Option<&BuildingEntry> {
pub fn get_building(&self, coord: &HexCoord) -> Option<&BuildingEntry>
{
let chunk = &self.chunks[coord.to_chunk_index(self.size.x as usize)];
return chunk.get_building(coord);
}
pub fn add_building(&mut self, entry: BuildingEntry) {
pub fn add_building(&mut self, entry: BuildingEntry)
{
let chunk = &mut self.chunks[entry.coord.to_chunk_index(self.size.x as usize)];
chunk.add_building(entry);
}
}
pub struct BuildingChunk {
pub struct BuildingChunk
{
pub entries: Vec<BuildingEntry>,
pub index: usize,
pub offset: IVec2,
}
impl BuildingChunk {
pub fn new(offset: IVec2, index: usize) -> Self {
impl BuildingChunk
{
pub fn new(offset: IVec2, index: usize) -> Self
{
return BuildingChunk {
entries: Vec::new(),
index,
@@ -71,16 +85,19 @@ impl BuildingChunk {
};
}
pub fn get_building(&self, coord: &HexCoord) -> Option<&BuildingEntry> {
pub fn get_building(&self, coord: &HexCoord) -> Option<&BuildingEntry>
{
return self.entries.iter().find(|b| &b.coord == coord);
}
pub fn add_building(&mut self, entry: BuildingEntry) {
pub fn add_building(&mut self, entry: BuildingEntry)
{
self.entries.push(entry);
}
}
pub struct BuildingEntry {
pub struct BuildingEntry
{
pub coord: HexCoord,
pub entity: Entity,
pub is_main: bool,
@@ -89,8 +106,10 @@ pub struct BuildingEntry {
pub child_entities: Option<Vec<Entity>>,
}
impl BuildingEntry {
pub fn new(coord: HexCoord, entity: Entity) -> Self {
impl BuildingEntry
{
pub fn new(coord: HexCoord, entity: Entity) -> Self
{
return BuildingEntry {
coord,
entity,
@@ -101,7 +120,8 @@ impl BuildingEntry {
};
}
pub fn new_with_children(coord: HexCoord, entity: Entity, children: Vec<Entity>) -> BuildingEntry {
pub fn new_with_children(coord: HexCoord, entity: Entity, children: Vec<Entity>) -> BuildingEntry
{
return BuildingEntry {
coord,
entity,
@@ -112,7 +132,8 @@ impl BuildingEntry {
};
}
pub fn new_with_parent(coord: HexCoord, entity: Entity, main: Entity) -> BuildingEntry {
pub fn new_with_parent(coord: HexCoord, entity: Entity, main: Entity) -> BuildingEntry
{
return BuildingEntry {
coord,
entity,

View File

@@ -1,7 +1,7 @@
use bevy::math::{IVec2, Vec3Swizzles};
use hex::prelude::*;
use serde::{Deserialize, Serialize};
use shared::coords::CoordsCollection;
use world_generation::hex_utils::HexCoord;
#[derive(Serialize, Deserialize, Debug)]
pub struct BuildingFootprint
@@ -21,7 +21,7 @@ impl BuildingFootprint
let n_points: Vec<IVec2> = self
.footprint
.iter()
.flat_map(|p| HexCoord::from_hex(*p).get_neighbors())
.flat_map(|p| HexCoord::from_axial(*p).get_neighbors())
.map(|c| c.hex.xy())
.filter(|p| !self.footprint.contains(p))
.collect();