tile manager
This commit is contained in:
@@ -8,3 +8,5 @@ edition = "2021"
|
||||
[dependencies]
|
||||
bevy = "0.13.1"
|
||||
noise = "0.9.0"
|
||||
serde = {version="1.0.197", features=["derive"]}
|
||||
serde_json = "1.0.115"
|
||||
|
||||
@@ -69,3 +69,4 @@ pub mod prelude {
|
||||
pub mod heightmap;
|
||||
pub mod hex_utils;
|
||||
pub mod mesh_generator;
|
||||
pub mod tile_manager;
|
||||
|
||||
@@ -11,8 +11,6 @@ use bevy::{
|
||||
},
|
||||
};
|
||||
use std::vec::Vec;
|
||||
use bevy::render::mesh::MeshVertexAttribute;
|
||||
use bevy::render::render_resource::VertexFormat;
|
||||
|
||||
const HEX_CORNERS: [Vec3; 6] = [
|
||||
Vec3::new(0., 0., OUTER_RADIUS),
|
||||
@@ -23,7 +21,6 @@ const HEX_CORNERS: [Vec3; 6] = [
|
||||
Vec3::new(-INNER_RADIUS, 0., 0.5 * OUTER_RADIUS),
|
||||
];
|
||||
|
||||
|
||||
pub fn generate_chunk_mesh(chunk: &Chunk, map: &Map) -> Mesh {
|
||||
let vertex_count: usize = Chunk::SIZE * Chunk::SIZE * 6;
|
||||
let mut verts = Vec::with_capacity(vertex_count);
|
||||
@@ -55,11 +52,11 @@ pub fn generate_chunk_mesh(chunk: &Chunk, map: &Map) -> Mesh {
|
||||
PrimitiveTopology::TriangleList,
|
||||
RenderAssetUsages::MAIN_WORLD | RenderAssetUsages::RENDER_WORLD,
|
||||
)
|
||||
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, verts)
|
||||
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
|
||||
.with_inserted_indices(Indices::U32(indices))
|
||||
.with_duplicated_vertices()
|
||||
.with_computed_flat_normals();
|
||||
.with_inserted_attribute(Mesh::ATTRIBUTE_POSITION, verts)
|
||||
.with_inserted_attribute(Mesh::ATTRIBUTE_UV_0, uvs)
|
||||
.with_inserted_indices(Indices::U32(indices))
|
||||
.with_duplicated_vertices()
|
||||
.with_computed_flat_normals();
|
||||
return mesh;
|
||||
}
|
||||
|
||||
|
||||
61
engine/world_generation/src/tile_manager.rs
Normal file
61
engine/world_generation/src/tile_manager.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
use std::{collections::HashMap, fs};
|
||||
|
||||
use bevy::ecs::system::Resource;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct TileManager {
|
||||
pub tiles: HashMap<u32, TileDefination>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct TileDefination {
|
||||
pub id: u32,
|
||||
pub texture_id: u32,
|
||||
pub side_texture_id: u32,
|
||||
}
|
||||
|
||||
impl TileManager {
|
||||
pub fn new(path: String) -> Option<Self> {
|
||||
let mut cur_id: u32 = 0;
|
||||
|
||||
let paths = fs::read_dir(path);
|
||||
|
||||
if paths.is_err() {
|
||||
print!("{}", paths.err().unwrap());
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut manager = TileManager {
|
||||
tiles: HashMap::new(),
|
||||
};
|
||||
|
||||
for dir_entry in paths.unwrap() {
|
||||
let cur_path = dir_entry.unwrap().path();
|
||||
if !cur_path.is_file() {
|
||||
continue;
|
||||
}
|
||||
let data = fs::read_to_string(cur_path);
|
||||
if data.is_err() {
|
||||
print!("{}", data.err().unwrap());
|
||||
return None;
|
||||
}
|
||||
|
||||
let result: Result<TileDefination, serde_json::Error> =
|
||||
serde_json::from_str(data.unwrap().as_str());
|
||||
|
||||
if result.is_err() {
|
||||
print!("{}", result.err().unwrap());
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut tile = result.unwrap();
|
||||
tile.id = cur_id;
|
||||
|
||||
manager.tiles.insert(tile.id, tile);
|
||||
cur_id += 1;
|
||||
}
|
||||
|
||||
return Some(manager);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user