feo_oop_engine/registration/
relation.rs

1//! Systems for defining relationships between objects. IE Parent/Child
2//! 
3//! TODO
4//! 
5use {
6    crate::{
7        scene::{
8            game_object::GameObject, 
9            Scene,
10        },
11        registration::named::Named
12    },
13    std::sync::{Arc, RwLock}
14};
15
16#[derive(Clone, Debug)]
17pub enum ParentWrapper{ // make into a trait object and add as_parent
18    GameObject(Arc<RwLock<dyn GameObject>>),
19    Scene(Arc<RwLock<Scene>>)
20}
21
22pub trait Parent: Named {
23    fn get_children(&self) -> Vec<Arc<RwLock<dyn GameObject /* dyn Child */>>>; // use dyn Child here and make gameobject castable to child using a new as_child() do same for parent except with as_parent and dyn Parent and not enum
24    fn add_child(&mut self, child: Arc<RwLock<dyn GameObject>>);
25    
26    // unsafe because scripts running on child access (->) child -> parent or children -> new object or nothing 
27    // alternative use the swap feature
28    /// # Safety
29    /// Use `feo-oop-engine::scripting::swap::Swap` enum instead.
30    unsafe fn replace_child(&mut self, old: Arc<RwLock<dyn GameObject>>, new: Arc<RwLock<dyn GameObject>>) -> Result<(), ()>; // ensure the child extends child and set its parent as well
31    /// # Safety
32    /// Use `feo-oop-engine::scripting::swap::Swap` enum instead.
33    unsafe fn remove_child(&mut self, child: Arc<RwLock<dyn GameObject>>) -> Result<(), ()>;
34
35    fn get_child_by_name(&self, name: &str) -> Result<Arc<RwLock<dyn GameObject>>, &str> {
36        let mut result = Err("No child with that name was found.");
37        self.get_children().into_iter().for_each( |child|
38            if child.read().unwrap().get_name() == name {
39                match result {
40                    Ok(_) => panic!("Two children share the same name"),
41                    Err(_) => { result = Ok(child); }
42                }
43            }
44        );
45        result
46    }
47}
48
49pub trait Child: Named {
50    fn get_parent(&self) -> ParentWrapper; // await rmb
51
52    /// # Safety
53    /// Use `feo-oop-engine::scripting::swap::Swap` enum instead.
54    unsafe fn set_parent(&mut self, parent: ParentWrapper);
55}