1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::{
engine::Engine,
ecs::{ SceneHandle, EntityHandle, ComponentStorage, GlobalComponentStorage }, game::Resource,
};
use pill_core::{ PillTypeMap, PillTypeMapKey, PillSlotMapKey };
use std::{path::PathBuf, marker::PhantomData};
use anyhow::{ Context, Result, Error };
use dyn_clone::DynClone;
pub trait Component : PillTypeMapKey + Send {
fn initialize(&mut self, engine: &mut Engine) -> Result<()> { Ok(()) }
fn pass_handles(&mut self, self_scene_handle: SceneHandle, self_entity_handle: EntityHandle) {}
fn deferred_update(&mut self, engine: &mut Engine, request: usize) -> Result<()> { Ok(()) }
fn destroy(&mut self, engine: &mut Engine, self_scene_handle: SceneHandle, self_entity_handle: EntityHandle) -> Result<()> { Ok(()) }
}
pub trait GlobalComponent : PillTypeMapKey + Send {
fn initialize(&mut self, engine: &mut Engine) -> Result<()> { Ok(()) }
fn deferred_update(&mut self, engine: &mut Engine, request: usize) -> Result<()> { Ok(()) }
fn destroy(&mut self, engine: &mut Engine) -> Result<()> { Ok(()) }
}
pub trait ComponentDestroyer: DynClone {
fn destroy(&mut self, engine: &mut Engine, scene_handle: SceneHandle, entity_handle: EntityHandle) -> Result<()>;
}
dyn_clone::clone_trait_object!(ComponentDestroyer);
pub struct ConcreteComponentDestroyer<T> {
component_type: PhantomData<T>,
}
impl<T> ConcreteComponentDestroyer<T> {
pub fn new() -> Self {
Self {
component_type: PhantomData::<T>,
}
}
}
impl <T> Clone for ConcreteComponentDestroyer<T> {
fn clone(&self) -> Self {
Self { component_type: self.component_type.clone() }
}
}
impl<T> ComponentDestroyer for ConcreteComponentDestroyer<T>
where T: Component<Storage = ComponentStorage::<T>>
{
fn destroy(&mut self, engine: &mut Engine, scene_handle: SceneHandle, entity_handle: EntityHandle) -> Result<()> {
let component: Option<T>;
{
let target_scene = engine.scene_manager.get_scene_mut(scene_handle)?;
let component_storage = target_scene.components.get_mut::<T>().unwrap();
let component_slot = component_storage.data.get_mut(entity_handle.data().index as usize).expect("Critical: Vector not initialized");
component = Some(component_slot.take().unwrap());
}
component.unwrap().destroy(engine, scene_handle, entity_handle)?;
Ok(())
}
}