|
|
|
use std::collections::HashMap;
|
|
|
|
use std::fs;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::time::Instant;
|
|
|
|
|
|
|
|
use cgmath::{Euler, Quaternion};
|
|
|
|
use config::Config;
|
|
|
|
use config::File;
|
|
|
|
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};
|
|
|
|
|
|
|
|
pub struct EntityMeta {
|
|
|
|
pub name: String,
|
|
|
|
pub ent_type: String,
|
|
|
|
pub mesh: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RuntimeState {
|
|
|
|
config_db: Config,
|
|
|
|
mesh_cache: HashMap<String, RawMesh>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RuntimeState {
|
|
|
|
pub fn new() -> RuntimeState {
|
|
|
|
let mut settings = Config::default();
|
|
|
|
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.get_array("entities").unwrap() {
|
|
|
|
let table = entity.into_table().unwrap();
|
|
|
|
out.push(EntityMeta {
|
|
|
|
name: table.get("name").unwrap().kind.to_string(),
|
|
|
|
ent_type: table.get("type").unwrap().kind.to_string(),
|
|
|
|
mesh: table.get("mesh").unwrap().kind.to_string(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|