You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
3.6 KiB

use std::collections::HashMap;
use std::fs;
use std::path::PathBuf;
use std::time::Instant;
use serde_derive::Deserialize;
use cgmath::{Euler, Quaternion, Deg};
use legion::world::SubWorld;
use legion::IntoQuery;
use legion::*;
use nalgebra::Quaternion as naQuaternion;
use rapier3d::dynamics::{IntegrationParameters, JointSet, RigidBodySet};
use rapier3d::geometry::{BroadPhase, ColliderSet, NarrowPhase};
use rapier3d::pipeline::PhysicsPipeline;
use crate::camera::{Camera, CameraController};
use crate::components::{Collider, LoopState, Mesh, Physics, Position};
use crate::geometry::{load_obj, RawMesh};
use std::io::Read;
pub struct EntityMeta {
pub name: String,
pub ent_type: String,
pub mesh: Option<String>,
pub position: Option<Position>
}
pub struct RuntimeState {
config_db: TomlEntityContainer,
mesh_cache: HashMap<String, RawMesh>,
}
#[derive(Deserialize)]
pub struct TomlPositionDescription {
pub x: f32,
pub y: f32,
pub z: f32,
}
#[derive(Deserialize)]
pub struct TomlEntityDescription {
pub name: String,
#[serde(rename = "type")]
pub type_name: String,
pub mesh: Option<String>,
pub position: Option<TomlPositionDescription>,
}
#[derive(Deserialize)]
pub struct TomlEntityContainer {
pub entities: Vec<TomlEntityDescription>,
}
impl RuntimeState {
pub fn new() -> RuntimeState {
let mut file = fs::File::open("./conf/entity_spawns.toml").unwrap();
let mut content = String::new();
file.read_to_string(&mut content).unwrap();
let mut settings : TomlEntityContainer = toml::from_str(content.as_str()).unwrap();
// settings
// // File::with_name(..) is shorthand for File::from(Path::new(..))
// .merge(File::with_name("conf/entity_spawns.toml"))
// .unwrap();
RuntimeState {
config_db: settings,
mesh_cache: Default::default(),
}
}
pub fn get_mesh(&mut self, mesh: &str) -> Option<&RawMesh> {
self.mesh_cache.get(mesh)
}
pub fn get_configured_entities(&mut self) -> Vec<EntityMeta> {
let mut out = Vec::new();
for entity in &self.config_db.entities {
let position = match &entity.position {
None => { None }
Some(pos) => {
Some(Position {
x: pos.x,
y: pos.y,
z: pos.z,
rot: Euler {
x: Deg(0.0),
y: Deg(0.0),
z: Deg(0.0)
}
})
}
};
// log::info!("{:?}", position);
// log::info!("{:?}", entity);
// log::info!("{:?}", entity.into_table());
// log::info!("{:?}", entity.into_array());
out.push(EntityMeta {
name: entity.name.clone(),
ent_type: entity.type_name.clone(),
mesh: entity.mesh.clone(),
position: position
});
}
out
}
pub fn preload_meshes(&mut self, resources_path: PathBuf) {
log::info!("Preloading meshes...");
let paths = fs::read_dir(resources_path).unwrap();
for file in paths {
let file = file.unwrap();
let filepath = file.path().clone();
let filename = String::from(file.file_name().to_str().unwrap());
if filename.ends_with(".obj") {
let mesh = load_obj(filepath.to_str().unwrap()).unwrap();
self.mesh_cache.insert(filename, mesh);
}
}
}
}