Buildings Processing

Tile/Chunk updates events
This commit is contained in:
2024-08-03 18:48:47 -04:00
parent 59090fd3fb
commit bde25435ec
18 changed files with 318 additions and 52 deletions

View File

@@ -144,6 +144,7 @@ impl HexCoord {
};
}
/// Converts this coordinate to it's chunk local equivalent
pub fn to_chunk(&self) -> HexCoord {
let c_pos = self.to_chunk_pos();
let off = self.to_offset();
@@ -164,14 +165,20 @@ impl HexCoord {
return IVec2::new(self.hex.x + (self.hex.y / 2), self.hex.y);
}
/// Convert the current coordiante to an index
pub fn to_index(&self, width: usize) -> usize {
return ((self.hex.x + self.hex.y * width as i32) + (self.hex.y / 2)) as usize;
}
/// Gets the index of this coord in the chunk array.
///
/// [`width`] is in number of chunks
pub fn to_chunk_index(&self, width: usize) -> usize {
let pos = self.to_chunk_pos();
return (pos.x + pos.y * width as i32) as usize;
}
/// Gets the index of this tile in the chunk
pub fn to_chunk_local_index(&self) -> usize {
return self.to_chunk().to_index(Chunk::SIZE);
}
@@ -251,6 +258,37 @@ impl HexCoord {
return result;
}
pub fn hex_select_bounded(
&self,
radius: usize,
include_center: bool,
height: usize,
width: usize,
) -> Vec<HexCoord> {
assert!(radius != 0, "Radius cannot be zero");
let mut result = Vec::with_capacity(get_tile_count(radius));
if include_center {
if self.is_in_bounds(height, width) {
result.push(*self);
}
}
for k in 0..(radius + 1) {
let mut p = self.scale(4, k);
for i in 0..6 {
for _j in 0..k {
p = p.get_neighbor(i);
if p.is_in_bounds(height, width) {
result.push(p);
}
}
}
}
return result;
}
pub fn select_ring(&self, radius: usize) -> Vec<HexCoord> {
assert!(radius != 0, "Radius cannot be zero");
let mut result = Vec::with_capacity(radius * 6);

View File

@@ -1,4 +1,5 @@
use bevy::prelude::*;
use bevy_asset_loader::asset_collection::AssetCollection;
use bevy_inspector_egui::InspectorOptions;
use serde::{Deserialize, Serialize};

View File

@@ -87,22 +87,19 @@ impl Map {
self.chunks[pos.to_chunk_index(self.width)].heights[pos.to_chunk_local_index()] = height;
}
pub fn create_crater(&mut self, pos: &HexCoord, radius: usize, depth: f32) -> Vec<usize> {
pub fn create_crater(&mut self, pos: &HexCoord, radius: usize, depth: f32) -> Vec<(HexCoord, f32)> {
assert!(radius != 0, "Radius cannot be zero");
let width = self.width;
let mut chunks = self.hex_select_mut(pos, radius, true, |p, h, r| {
let tiles = self.hex_select_mut(pos, radius, true, |p, h, r| {
let d = (r as f32) / (radius as f32);
let cur = *h;
let h2 = cur - depth;
*h = h2.lerp(cur, d * d).max(0.);
return p.to_chunk_index(width);
return (*p, *h);
});
chunks.dedup();
return chunks;
return tiles;
}
pub fn hex_select<OP, Ret>(&self, center: &HexCoord, radius: usize, include_center: bool, op: OP) -> Vec<Ret>
@@ -111,20 +108,25 @@ impl Map {
{
assert!(radius != 0, "Radius cannot be zero");
let mut result = if include_center {
Vec::with_capacity(get_tile_count(radius) + 1)
} else {
Vec::with_capacity(get_tile_count(radius))
};
if include_center {
let h = self.sample_height(&center);
(op)(&center, h, 0);
result.push((op)(center, h, 0));
}
let mut result = Vec::with_capacity(get_tile_count(radius));
for k in 0..(radius + 1) {
let mut p = center.scale(4, k);
for i in 0..6 {
for _j in 0..k {
p = p.get_neighbor(i);
let h = self.sample_height(&p);
result.push((op)(&p, h, k));
if self.is_in_bounds(&p) {
let h = self.sample_height(&p);
result.push((op)(&p, h, k));
}
}
}
}
@@ -144,20 +146,25 @@ impl Map {
{
assert!(radius != 0, "Radius cannot be zero");
let mut result = if include_center {
Vec::with_capacity(get_tile_count(radius) + 1)
} else {
Vec::with_capacity(get_tile_count(radius))
};
if include_center {
let h = self.sample_height_mut(&center);
(op)(&center, h, 0);
result.push((op)(center, h, 0));
}
let mut result = Vec::with_capacity(get_tile_count(radius));
for k in 0..(radius + 1) {
let mut p = center.scale(4, k);
for i in 0..6 {
for _j in 0..k {
p = p.get_neighbor(i);
let h = self.sample_height_mut(&p);
result.push((op)(&p, h, k));
if self.is_in_bounds(&p) {
let h = self.sample_height_mut(&p);
result.push((op)(&p, h, k));
}
}
}
}

View File

@@ -7,5 +7,5 @@ pub enum GeneratorState {
SpawnMap,
Idle,
Regenerate,
Cleanup,
}