feo_oop_engine/scene/game_object/
mod.rs

1//! Engine/Game objects that can exist within a scene.
2//! 
3//! TODO: explain OOP here
4//! 
5use core::fmt;
6
7pub mod light;
8pub mod camera;
9pub mod group;
10pub mod obj;
11
12use {
13    self::{
14        camera::Camera,
15        light::Light,
16    },
17
18    crate::{
19        registration::{
20            relation::{Child, Parent},
21            id::ID,
22        },
23        scripting::Scriptable,
24        graphics::{
25            Drawable
26        },
27    },
28    feo_math::{
29        utils::space::Space,
30    },
31    std::{
32        any::Any,
33        sync::{
34            Arc, 
35            RwLock
36        }
37    },
38};
39
40// A construct that exists in the game.
41pub trait GameObject: 
42        GameObjectBoxClone +
43        Scriptable + 
44        Drawable + 
45        Parent +
46        Child + 
47        Any + 'static + 
48        Send + Sync {
49    fn as_any(&self) -> &dyn Any;
50    fn cast_camera_arc_rwlock(&self, this: Arc<RwLock<dyn GameObject>>) -> Result<Arc<RwLock<dyn Camera>>, ()>; 
51    fn cast_light_arc_rwlock(&self, this: Arc<RwLock<dyn GameObject>>) -> Result<Arc<RwLock<dyn Light>>, ()>; 
52
53    fn get_id(&self) -> ID;
54    fn get_subspace(&self) -> Space;
55    fn get_inversed_subspace(&self) -> Space;
56}
57
58impl PartialEq for dyn GameObject{
59    fn eq(&self, other: &Self) -> bool {
60        self.get_id() == other.get_id()
61    }
62}
63
64impl fmt::Debug for dyn GameObject {
65    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66        write!(f, "{} {}", self.get_id(), self.get_name())
67    }
68}
69
70/// Allows Box<dyn GameObject> to be clonable
71pub trait GameObjectBoxClone {
72    fn clone_game_object(&self) -> Box<dyn GameObject>;
73}
74
75impl<T> GameObjectBoxClone for T where T: 'static + GameObject + Clone {
76    fn clone_game_object(&self) -> Box<dyn GameObject> {
77        Box::new(self.clone())
78    }
79}
80
81impl Clone for Box<dyn GameObject> {
82    fn clone(&self) -> Self {
83        self.clone_game_object()
84    }
85}