state.rs
1    use std::collections::HashMap;
2    use std::fs;
3    use std::path::PathBuf;
4    use std::time::Instant;
5    use serde_derive::Deserialize;
6    
7    use cgmath::{Euler, Quaternion, Deg};
8    use legion::world::SubWorld;
9    use legion::IntoQuery;
10   use legion::*;
11   use nalgebra::Quaternion as naQuaternion;
12   use rapier3d::dynamics::{IntegrationParameters, JointSet, RigidBodySet};
13   use rapier3d::geometry::{BroadPhase, ColliderSet, NarrowPhase};
14   use rapier3d::pipeline::PhysicsPipeline;
15   use crate::camera::{Camera, CameraController};
16   use crate::components::{Collider, LoopState, Mesh, Physics, Position};
17   use crate::geometry::{load_obj, RawMesh};
18   use std::io::Read;
19   
20   
21   
22   #[derive(Deserialize, Clone)]
23   pub struct TomlBallPhysicsBodyDescription {
24       pub radius: String
25   }
26   
27   #[derive(Deserialize, Clone)]
28   pub struct TomlCuboidPhysicsBodyDescription {
29       pub x: f32,
30       pub y: f32,
31       pub z: f32,
32   }
33   
34   #[derive(Deserialize, Clone)]
35   pub struct TomlPhysicsDescription {
36       pub body_status: String,
37       pub ball: Option<TomlBallPhysicsBodyDescription>,
38       pub cuboid: Option<TomlCuboidPhysicsBodyDescription>,
39   }
40   
41   #[derive(Deserialize, Clone)]
42   pub struct TomlRotationDescription {
43       pub x: f32,
44       pub y: f32,
45       pub z: f32,
46   }
47   
48   #[derive(Deserialize, Clone)]
49   pub struct TomlPositionDescription {
50       pub x: f32,
51       pub y: f32,
52       pub z: f32,
53       pub rot: Option<TomlRotationDescription>,
54   }
55   
56   #[derive(Deserialize, Clone)]
57   pub struct TomlEntityDescription {
58       pub name: String,
59       #[serde(rename = "type")]
60       pub type_name: String,
61       pub mesh: Option<String>,
62       pub position: Option<TomlPositionDescription>,
63       pub physics: Option<TomlPhysicsDescription>,
64   }
65   
66   #[derive(Deserialize, Clone)]
67   pub struct TomlEntityContainer {
68       pub entities: Vec<TomlEntityDescription>,
69   }
70   
71   pub struct RuntimeState {
72       config_db: TomlEntityContainer,
73       mesh_cache: HashMap<String, RawMesh>,
74   }
75   
76   impl RuntimeState {
77       pub fn new() -> RuntimeState {
78           // TODO: Hook this file to the gui
79           let mut file = fs::File::open("./conf/entity_spawns.toml").unwrap();
80           let mut content = String::new();
81           file.read_to_string(&mut content).unwrap();
82   
83           // TODO: gracefully fail
84           let mut settings : TomlEntityContainer = toml::from_str(content.as_str()).unwrap();
85   
86           RuntimeState {
87               config_db: settings,
88               mesh_cache: Default::default(),
89           }
90       }
91   
92       pub fn get_mesh(&mut self, mesh: &str) -> Option<&RawMesh> {
93           self.mesh_cache.get(mesh)
94       }
95   
96       pub fn get_entities(&mut self) -> Vec<TomlEntityDescription> {
97           self.config_db.entities.clone()
98       }
99   
100      pub fn preload_meshes(&mut self, resources_path: PathBuf) {
101          log::info!("Preloading meshes...");
102  
103          let paths = fs::read_dir(resources_path).unwrap();
104          for file in paths {
105              let file = file.unwrap();
106              let filepath = file.path().clone();
107              let filename = String::from(file.file_name().to_str().unwrap());
108  
109              if filename.ends_with(".obj") {
110                  let mesh = load_obj(filepath.to_str().unwrap()).unwrap();
111                  self.mesh_cache.insert(filename, mesh);
112              }
113          }
114      }
115  }
116