asset loader
added rapier
This commit is contained in:
11
engine/asset_loader/Cargo.toml
Normal file
11
engine/asset_loader/Cargo.toml
Normal file
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "asset_loader"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
serde = "1.0.197"
|
||||
serde_json = "1.0.115"
|
||||
bevy = "0.13.2"
|
||||
57
engine/asset_loader/src/lib.rs
Normal file
57
engine/asset_loader/src/lib.rs
Normal file
@@ -0,0 +1,57 @@
|
||||
pub mod macros {
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! create_asset_loader {
|
||||
(
|
||||
$plugin_name: ident,
|
||||
$loader_name: ident,
|
||||
$asset_type: ident,
|
||||
$extensions: expr
|
||||
) => {
|
||||
use bevy::prelude::*;
|
||||
pub struct $plugin_name;
|
||||
impl Plugin for $plugin_name {
|
||||
fn build(&self, app: &mut App) {
|
||||
app.init_asset::<$asset_type>()
|
||||
.init_asset_loader::<$loader_name>();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct $loader_name;
|
||||
|
||||
impl AssetLoader for $loader_name {
|
||||
type Asset = $asset_type;
|
||||
|
||||
type Settings = ();
|
||||
|
||||
type Error = String;
|
||||
|
||||
fn load<'a>(
|
||||
&'a self,
|
||||
reader: &'a mut bevy::asset::io::Reader,
|
||||
_settings: &'a Self::Settings,
|
||||
_load_context: &'a mut bevy::asset::LoadContext,
|
||||
) -> bevy::utils::BoxedFuture<'a, Result<Self::Asset, Self::Error>> {
|
||||
return Box::pin(async move {
|
||||
let mut data: String = String::new();
|
||||
let read_result = reader.read_to_string(&mut data).await;
|
||||
if read_result.is_err() {
|
||||
return Err(read_result.err().unwrap().to_string());
|
||||
}
|
||||
let serialized: Result<Self::Asset, serde_json::Error> =
|
||||
serde_json::from_str(&data);
|
||||
if serialized.is_err() {
|
||||
return Err(serialized.err().unwrap().to_string());
|
||||
}
|
||||
return Ok(serialized.unwrap());
|
||||
});
|
||||
}
|
||||
|
||||
fn extensions(&self) -> &[&str] {
|
||||
$extensions
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,8 @@ edition = "2021"
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
bevy = "0.13.1"
|
||||
bevy = "0.13.2"
|
||||
noise = "0.9.0"
|
||||
serde = {version="1.0.197", features=["derive"]}
|
||||
serde_json = "1.0.115"
|
||||
asset_loader = {path = "../asset_loader"}
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
pub mod heightmap;
|
||||
pub mod hex_utils;
|
||||
pub mod mesh_generator;
|
||||
pub mod tile_manager;
|
||||
|
||||
pub mod prelude {
|
||||
use crate::hex_utils::HexCoord;
|
||||
use bevy::math::{IVec2, UVec2};
|
||||
@@ -65,8 +70,3 @@ pub mod prelude {
|
||||
pub const ATTRIBUTE_TEXTURE_INDEX: MeshVertexAttribute =
|
||||
MeshVertexAttribute::new("TextureIndex", 988540917, VertexFormat::Uint32);
|
||||
}
|
||||
|
||||
pub mod heightmap;
|
||||
pub mod hex_utils;
|
||||
pub mod mesh_generator;
|
||||
pub mod tile_manager;
|
||||
|
||||
@@ -1,29 +1,36 @@
|
||||
use std::{collections::HashMap, fs};
|
||||
|
||||
use bevy::ecs::system::Resource;
|
||||
use asset_loader::create_asset_loader;
|
||||
use bevy::{
|
||||
asset::{Asset, AssetLoader, AsyncReadExt},
|
||||
ecs::system::Resource,
|
||||
reflect::TypePath,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Resource)]
|
||||
#[derive(Resource, Debug)]
|
||||
pub struct TileManager {
|
||||
pub tiles: HashMap<u32, TileDefination>,
|
||||
pub tiles: HashMap<u32, TileAsset>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct TileDefination {
|
||||
#[derive(Serialize, Deserialize, Debug, TypePath, Asset)]
|
||||
pub struct TileAsset {
|
||||
#[serde(skip_serializing, skip_deserializing)]
|
||||
pub id: u32,
|
||||
pub name: String,
|
||||
pub texture_id: u32,
|
||||
pub side_texture_id: u32,
|
||||
}
|
||||
|
||||
create_asset_loader!(TileAssetPlugin, TileAssetLoader, TileAsset, &["tile.json"]);
|
||||
|
||||
impl TileManager {
|
||||
pub fn new(path: String) -> Option<Self> {
|
||||
pub fn new(path: &str) -> Result<Self, String> {
|
||||
let mut cur_id: u32 = 0;
|
||||
|
||||
let paths = fs::read_dir(path);
|
||||
|
||||
if paths.is_err() {
|
||||
print!("{}", paths.err().unwrap());
|
||||
return None;
|
||||
return Err(paths.err().unwrap().to_string());
|
||||
}
|
||||
|
||||
let mut manager = TileManager {
|
||||
@@ -37,16 +44,14 @@ impl TileManager {
|
||||
}
|
||||
let data = fs::read_to_string(cur_path);
|
||||
if data.is_err() {
|
||||
print!("{}", data.err().unwrap());
|
||||
return None;
|
||||
return Err(data.err().unwrap().to_string());
|
||||
}
|
||||
|
||||
let result: Result<TileDefination, serde_json::Error> =
|
||||
let result: Result<TileAsset, serde_json::Error> =
|
||||
serde_json::from_str(data.unwrap().as_str());
|
||||
|
||||
if result.is_err() {
|
||||
print!("{}", result.err().unwrap());
|
||||
return None;
|
||||
return Err(result.err().unwrap().to_string());
|
||||
}
|
||||
|
||||
let mut tile = result.unwrap();
|
||||
@@ -56,6 +61,6 @@ impl TileManager {
|
||||
cur_id += 1;
|
||||
}
|
||||
|
||||
return Some(manager);
|
||||
return Ok(manager);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user