Files
phos-neo/engine/world_generation/src/lib.rs
2024-04-27 23:13:43 -04:00

105 lines
2.8 KiB
Rust

pub mod biome_painter;
pub mod heightmap;
pub mod hex_utils;
pub mod mesh_generator;
pub mod tile_manager;
pub mod tile_mapper;
pub mod prelude {
use crate::hex_utils::HexCoord;
use bevy::math::{IVec2, UVec2};
use bevy::prelude::Resource;
use bevy::render::mesh::MeshVertexAttribute;
use bevy::render::render_resource::VertexFormat;
pub struct GenerationConfig {
pub noise_scale: f64,
pub sea_level: f64,
pub border_size: f32,
pub size: UVec2,
pub layers: Vec<GeneratorLayer>,
}
impl GenerationConfig {
pub fn get_total_width(&self) -> usize {
return self.size.x as usize * Chunk::SIZE;
}
pub fn get_total_height(&self) -> usize {
return self.size.y as usize * Chunk::SIZE;
}
}
pub struct GeneratorLayer {
pub strength: f64,
pub min_value: f64,
pub base_roughness: f64,
pub roughness: f64,
pub persistence: f64,
pub is_rigid: bool,
pub weight: f64,
pub weight_multi: f64,
pub layers: usize,
pub first_layer_mask: bool,
}
pub struct Chunk {
pub heights: [f32; Chunk::SIZE * Chunk::SIZE],
pub moisture: [f32; Chunk::SIZE * Chunk::SIZE],
pub temperature: [f32; Chunk::SIZE * Chunk::SIZE],
pub chunk_offset: IVec2,
}
impl Chunk {
pub const SIZE: usize = 64;
}
#[derive(Resource)]
pub struct Map {
pub chunks: Vec<Chunk>,
pub height: usize,
pub width: usize,
}
impl Map {
pub fn get_neighbors(&self, pos: &HexCoord) -> [Option<f32>; 6] {
let mut results: [Option<f32>; 6] = [None; 6];
let w = self.width * Chunk::SIZE;
let h = self.height * Chunk::SIZE;
let n_tiles = pos.get_neighbors();
for i in 0..6 {
let n_tile = n_tiles[i];
if !n_tile.is_in_bounds(h, w) {
continue;
}
let c_idx = n_tile.to_chunk_index(self.width);
let chunk = &self.chunks[c_idx];
let local = n_tile.to_chunk_local_index();
results[i] = Some(chunk.heights[local]);
}
return results;
}
pub fn get_height(&self, pos: &HexCoord) -> f32 {
let chunk = &self.chunks[pos.to_chunk_index(self.width)];
return chunk.heights[pos.to_chunk_local_index()];
}
pub fn get_moisture(&self, pos: &HexCoord) -> f32 {
let chunk = &self.chunks[pos.to_chunk_index(self.width)];
return chunk.moisture[pos.to_chunk_local_index()];
}
pub fn get_tempurature(&self, pos: &HexCoord) -> f32 {
let chunk = &self.chunks[pos.to_chunk_index(self.width)];
return chunk.temperature[pos.to_chunk_local_index()];
}
}
pub const ATTRIBUTE_PACKED_VERTEX_DATA: MeshVertexAttribute =
MeshVertexAttribute::new("PackedVertexData", 988540817, VertexFormat::Uint32);
pub const ATTRIBUTE_VERTEX_HEIGHT: MeshVertexAttribute =
MeshVertexAttribute::new("VertexHeight", 988540717, VertexFormat::Float32);
pub const ATTRIBUTE_TEXTURE_INDEX: MeshVertexAttribute =
MeshVertexAttribute::new("TextureIndex", 988540917, VertexFormat::Uint32);
}