components.rs
1    use std::sync::Arc;
2    use std::time::{Duration, Instant};
3    
4    use cgmath::{Deg, Euler};
5    use rapier3d::dynamics::{RigidBody, RigidBodyHandle};
6    use rapier3d::geometry::Collider as r3dCollider;
7    use rapier3d::geometry::ColliderHandle;
8    use wgpu::{BindGroup, Buffer, TextureView};
9    
10   use crate::runtime::state::{TomlPositionDescription, TomlRotationDescription};
11   use imgui::Ui;
12   
13   // a component is any type that is 'static, sized, send and sync
14   
15   pub struct ImguiWindow<'a, T> {
16       pub window: fn() -> imgui::Window<'a>,
17       pub func: fn(&Ui, Vec<&T>),
18   }
19   
20   #[derive(Clone, Copy, Debug, PartialEq)]
21   pub struct LoopState {
22       pub delta_time: Duration,
23       pub start_time: Instant,
24       pub step_size: f32,
25   }
26   
27   #[derive(Clone, Copy, Debug, PartialEq)]
28   pub struct Position {
29       pub x: f32,
30       pub y: f32,
31       pub z: f32,
32       pub rot: cgmath::Euler<Deg<f32>>,
33   }
34   
35   impl Default for Position {
36       fn default() -> Self {
37           Position {
38               x: 0.0,
39               y: 0.0,
40               z: 0.0,
41               rot: Euler {
42                   x: Deg(0.0),
43                   y: Deg(0.0),
44                   z: Deg(0.0),
45               },
46           }
47       }
48   }
49   
50   impl From<TomlPositionDescription> for Position {
51       fn from(pos: TomlPositionDescription) -> Self {
52           let euler = match pos.rot {
53               None => Euler {
54                   x: Deg(0.0),
55                   y: Deg(0.0),
56                   z: Deg(0.0),
57               },
58               Some(v) => Euler {
59                   x: Deg(v.x),
60                   y: Deg(v.y),
61                   z: Deg(v.z),
62               },
63           };
64           Position {
65               x: pos.x,
66               y: pos.y,
67               z: pos.z,
68               rot: euler,
69           }
70       }
71   }
72   
73   impl From<Option<TomlPositionDescription>> for Position {
74       fn from(pos: Option<TomlPositionDescription>) -> Self {
75           match pos {
76               None => Position {
77                   x: 0.0,
78                   y: 0.0,
79                   z: 0.0,
80                   rot: Euler {
81                       x: Deg(0.0),
82                       y: Deg(0.0),
83                       z: Deg(0.0),
84                   },
85               },
86               Some(v) => Position::from(v),
87           }
88       }
89   }
90   
91   #[derive(Clone, Default, PartialEq, Eq, Hash, Copy, Debug)]
92   pub struct RangeCopy<Idx> {
93       pub start: Idx,
94       pub end: Idx,
95   }
96   
97   #[derive(Clone, Debug)]
98   pub struct Mesh {
99       pub index_buffer: Arc<Buffer>,
100      pub index_count: usize,
101      pub index_format: wgpu::IndexFormat,
102      pub vertex_buffer: Arc<Buffer>,
103      pub uniform_buffer: Arc<Buffer>,
104      pub bind_group: Arc<BindGroup>,
105      pub color: wgpu::Color,
106  }
107  
108  #[derive(Clone, Debug)]
109  pub struct Physics {
110      pub rigid_body: RigidBody,
111      pub rigid_body_handle: Option<RigidBodyHandle>,
112  }
113  
114  #[derive(Clone)]
115  pub struct Collider {
116      pub collider: r3dCollider,
117      pub collider_handle: Option<ColliderHandle>,
118  }
119