1use bevy_ecs::component::Component;
2use bevy_ecs::entity::Entity;
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6#[derive(Component, Clone, Copy, Debug, PartialEq, Eq, Hash)]
9pub struct SceneId(pub Uuid);
10
11impl SceneId {
12 #[must_use]
13 pub fn new() -> Self {
14 Self(Uuid::new_v4())
15 }
16}
17
18impl Default for SceneId {
19 fn default() -> Self {
20 Self::new()
21 }
22}
23
24#[derive(Component, Clone, Copy, Debug, PartialEq)]
26pub struct GlobalTransform {
27 pub matrix: [[f32; 4]; 4],
28}
29
30impl Default for GlobalTransform {
31 fn default() -> Self {
32 Self {
33 matrix: nalgebra::Matrix4::<f32>::identity().into(),
34 }
35 }
36}
37
38#[derive(Component, Clone, Copy, Debug, PartialEq, Eq)]
39pub struct Parent(pub Entity);
40
41#[derive(Component, Clone, Debug, Default, PartialEq, Eq)]
42pub struct Children(pub Vec<Entity>);
43
44#[derive(Clone, Copy, Debug, PartialEq)]
45pub enum Projection {
46 Perspective {
47 vertical_fov_radians: f32,
48 near: f32,
49 far: f32,
50 },
51 Orthographic {
52 vertical_size: f32,
53 near: f32,
54 far: f32,
55 },
56}
57
58impl Default for Projection {
59 fn default() -> Self {
60 Self::Perspective {
61 vertical_fov_radians: std::f32::consts::FRAC_PI_3,
62 near: 0.1,
63 far: 1_000.0,
64 }
65 }
66}
67
68#[derive(Component, Clone, Copy, Debug, Default, PartialEq)]
69pub struct Camera {
70 pub projection: Projection,
71 pub active: bool,
72 pub priority: i32,
73}
74
75#[derive(Component, Clone, Debug, PartialEq, Eq)]
76pub struct Name(pub String);
77
78#[derive(
83 Component, Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize,
84)]
85pub struct ObjectClasses {
86 pub names: Vec<String>,
88}
89
90impl ObjectClasses {
91 #[must_use]
93 pub fn new(classes: impl IntoIterator<Item = impl Into<String>>) -> Self {
94 let mut result = Self::default();
95 for class in classes {
96 result.add(class);
97 }
98 result
99 }
100
101 pub fn add(&mut self, class: impl Into<String>) -> bool {
103 let class = class.into();
104 let class = class.trim();
105 if class.is_empty() || self.contains(class) {
106 return false;
107 }
108 self.names.push(class.to_owned());
109 true
110 }
111
112 pub fn remove(&mut self, class: &str) -> bool {
114 let old_len = self.names.len();
115 self.names.retain(|current| current != class);
116 self.names.len() != old_len
117 }
118
119 #[must_use]
121 pub fn contains(&self, class: &str) -> bool {
122 self.names.iter().any(|current| current == class)
123 }
124}
125
126#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
127pub struct DirectionalLight {
128 pub color: [f32; 3],
129 pub illuminance: f32,
130 pub shadows: bool,
131}
132
133impl Default for DirectionalLight {
134 fn default() -> Self {
135 Self {
136 color: [1.0; 3],
137 illuminance: 100_000.0,
138 shadows: true,
139 }
140 }
141}
142
143#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
144pub struct PointLight {
145 pub color: [f32; 3],
146 pub intensity: f32,
147 pub range: f32,
148}
149
150#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
151pub struct SpotLight {
152 pub color: [f32; 3],
153 pub intensity: f32,
154 pub range: f32,
155 pub inner_angle: f32,
157 pub outer_angle: f32,
159}
160
161impl Default for SpotLight {
162 fn default() -> Self {
163 Self {
164 color: [1.0; 3],
165 intensity: 1_000.0,
166 range: 10.0,
167 inner_angle: 20.0_f32.to_radians(),
168 outer_angle: 35.0_f32.to_radians(),
169 }
170 }
171}
172
173impl Default for PointLight {
174 fn default() -> Self {
175 Self {
176 color: [1.0; 3],
177 intensity: 1_000.0,
178 range: 10.0,
179 }
180 }
181}
182
183#[derive(Component, Clone, Copy, Debug, PartialEq, Serialize, Deserialize)]
184pub struct AmbientLight {
185 pub color: [f32; 3],
186 pub intensity: f32,
187}
188
189impl Default for AmbientLight {
190 fn default() -> Self {
191 Self {
192 color: [1.0; 3],
193 intensity: 0.1,
194 }
195 }
196}
197
198#[derive(Component, Clone, Copy, Debug, PartialEq, Eq)]
199pub struct Visibility {
200 pub visible: bool,
201}
202
203impl Default for Visibility {
204 fn default() -> Self {
205 Self { visible: true }
206 }
207}
208
209#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
210pub enum QualityProfile {
211 Auto,
212 Eco,
213 #[default]
214 Balanced,
215 High,
216}
217
218#[derive(bevy_ecs::prelude::Resource, Clone, Debug, PartialEq)]
219pub struct RenderSettings {
220 pub quality: QualityProfile,
221 pub vsync: bool,
222 pub limit_fps: bool,
223 pub max_fps: u32,
224 pub render_scale: f32,
225 pub background_color: [f32; 4],
227}
228
229impl Default for RenderSettings {
230 fn default() -> Self {
231 Self {
232 quality: QualityProfile::Auto,
233 vsync: false,
234 limit_fps: false,
235 max_fps: 120,
236 render_scale: 1.0,
237 background_color: [0.025, 0.04, 0.07, 1.0],
238 }
239 }
240}
241
242#[derive(bevy_ecs::prelude::Resource, Clone, Debug, PartialEq)]
243pub struct PhysicsSettings {
244 pub gravity: [f32; 3],
245 pub enabled: bool,
246}
247
248#[derive(
252 bevy_ecs::prelude::Resource, Clone, Copy, Debug, Default, PartialEq, Eq,
253)]
254pub struct PhysicsBackendStatus {
255 pub gameplay_available: bool,
256 pub gpu_dynamic_available: bool,
257}
258
259impl Default for PhysicsSettings {
260 fn default() -> Self {
261 Self {
262 gravity: [0.0, -9.81, 0.0],
263 enabled: true,
264 }
265 }
266}
267
268#[derive(Component, Clone, Copy, Debug, Default, PartialEq, Eq)]
270pub struct GpuEffectBody;
271
272#[derive(
274 Clone, Copy, Debug, Default, Deserialize, PartialEq, Eq, Serialize,
275)]
276pub enum SimulationClass {
277 None,
278 Static,
279 #[default]
280 Gameplay,
281 GpuDynamic,
282}
283
284#[derive(
286 Clone, Copy, Debug, Default, Deserialize, PartialEq, Eq, Serialize,
287)]
288pub enum PhysicsSolver {
289 #[default]
290 Full,
291 Simplified,
292 NoCollision,
293 Space,
294 Custom,
295}
296
297#[derive(Component, Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
303pub struct PhysicsBody {
304 pub simulation: SimulationClass,
305 pub solver: PhysicsSolver,
306 pub custom_shader: Option<String>,
307}
308
309impl Default for PhysicsBody {
310 fn default() -> Self {
311 Self {
312 simulation: SimulationClass::Gameplay,
313 solver: PhysicsSolver::Full,
314 custom_shader: None,
315 }
316 }
317}
318
319impl PhysicsBody {
320 #[must_use]
321 pub fn participates_in_dynamic_simulation(&self) -> bool {
322 matches!(
323 self.simulation,
324 SimulationClass::Gameplay | SimulationClass::GpuDynamic
325 )
326 }
327
328 #[must_use]
329 pub fn uses_gpu(&self) -> bool {
330 self.simulation == SimulationClass::GpuDynamic
331 }
332}
333
334#[derive(
335 Component,
336 Clone,
337 Copy,
338 Debug,
339 Default,
340 Deserialize,
341 PartialEq,
342 Eq,
343 Serialize,
344)]
345pub enum RigidBodyKind {
346 Fixed,
347 #[default]
348 Dynamic,
349 Kinematic,
350}
351
352#[derive(Component, Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
353pub struct RigidBody {
354 pub kind: RigidBodyKind,
355 pub mass: f32,
356 pub linear_velocity: [f32; 3],
357 pub angular_velocity: [f32; 3],
358 pub gravity_scale: f32,
359}
360
361impl Default for RigidBody {
362 fn default() -> Self {
363 Self {
364 kind: RigidBodyKind::Dynamic,
365 mass: 1.0,
366 linear_velocity: [0.0; 3],
367 angular_velocity: [0.0; 3],
368 gravity_scale: 1.0,
369 }
370 }
371}
372
373#[derive(Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
374pub enum ColliderShape {
375 Box { half_extents: [f32; 3] },
376 Sphere { radius: f32 },
377 Capsule { half_height: f32, radius: f32 },
378}
379
380#[derive(Component, Clone, Copy, Debug, Deserialize, PartialEq, Serialize)]
381pub struct Collider {
382 pub shape: ColliderShape,
383 pub friction: f32,
384 pub restitution: f32,
385 pub sensor: bool,
386}
387
388impl Default for Collider {
389 fn default() -> Self {
390 Self {
391 shape: ColliderShape::Box {
392 half_extents: [0.5; 3],
393 },
394 friction: 0.5,
395 restitution: 0.0,
396 sensor: false,
397 }
398 }
399}
400
401#[derive(
402 Component, Clone, Copy, Debug, Deserialize, PartialEq, Eq, Serialize,
403)]
404pub struct CollisionLayers {
405 pub memberships: u32,
406 pub filters: u32,
407}
408
409impl Default for CollisionLayers {
410 fn default() -> Self {
411 Self {
412 memberships: u32::MAX,
413 filters: u32::MAX,
414 }
415 }
416}
417
418#[cfg(test)]
419mod tests {
420 use super::*;
421 use bevy_ecs::{component::Component, resource::Resource};
422
423 fn assert_component<T: Component>() {}
424 fn assert_resource<T: Resource>() {}
425
426 #[test]
427 fn canonical_types_implement_bevy_ecs_traits() {
428 assert_component::<SceneId>();
429 assert_component::<GlobalTransform>();
430 assert_component::<Parent>();
431 assert_component::<Children>();
432 assert_component::<Camera>();
433 assert_component::<Name>();
434 assert_component::<ObjectClasses>();
435 assert_component::<DirectionalLight>();
436 assert_component::<PointLight>();
437 assert_component::<SpotLight>();
438 assert_component::<AmbientLight>();
439 assert_component::<Visibility>();
440 assert_component::<GpuEffectBody>();
441 assert_component::<PhysicsBody>();
442 assert_component::<RigidBodyKind>();
443 assert_component::<RigidBody>();
444 assert_component::<Collider>();
445 assert_component::<CollisionLayers>();
446
447 assert_resource::<RenderSettings>();
448 assert_resource::<PhysicsSettings>();
449 assert_resource::<PhysicsBackendStatus>();
450 }
451
452 #[test]
453 fn defaults_are_ready_for_a_basic_scene() {
454 let identity: [[f32; 4]; 4] =
455 nalgebra::Matrix4::<f32>::identity().into();
456 assert_eq!(GlobalTransform::default().matrix, identity);
457 assert!(Visibility::default().visible);
458 assert_eq!(RigidBody::default().mass, 1.0);
459 assert_eq!(PhysicsSettings::default().gravity, [0.0, -9.81, 0.0]);
460 assert_eq!(CollisionLayers::default().memberships, u32::MAX);
461 assert_eq!(RenderSettings::default().quality, QualityProfile::Auto);
462 }
463
464 #[test]
465 fn object_classes_clean_and_edit_names() {
466 let mut classes =
467 ObjectClasses::new([" gravity ", "", "gravity", "enemy"]);
468 assert_eq!(classes.names, ["gravity", "enemy"]);
469 assert!(classes.contains("gravity"));
470 assert!(!classes.add("enemy"));
471 assert!(classes.remove("enemy"));
472 assert!(!classes.contains("enemy"));
473 }
474
475 #[test]
476 fn physics_body_helpers_follow_simulation_class() {
477 let mut body = PhysicsBody::default();
478 assert!(body.participates_in_dynamic_simulation());
479 assert!(!body.uses_gpu());
480
481 body.simulation = SimulationClass::GpuDynamic;
482 assert!(body.participates_in_dynamic_simulation());
483 assert!(body.uses_gpu());
484
485 body.simulation = SimulationClass::Static;
486 assert!(!body.participates_in_dynamic_simulation());
487 assert!(!body.uses_gpu());
488 }
489}