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
85
86
87
88
89
90
91
92
93
94
95
96
|
use std::any::type_name;
use std::ops::{Deref, DerefMut};
use ecs_macros::Component;
use crate::component::{
Component,
HandleMut as ComponentHandleMut,
IntoParts as _,
Parts as ComponentParts,
};
use crate::pair::Pair;
use crate::system::initializable::Param as InitializableParam;
use crate::system::{Metadata as SystemMetadata, Param as SystemParam};
use crate::World;
/// Holds a component which is local to a single system.
#[derive(Debug)]
pub struct Local<'world, LocalComponent: Component>
{
local_component: ComponentHandleMut<'world, LocalComponent>,
}
impl<'world, LocalComponent> SystemParam<'world> for Local<'world, LocalComponent>
where
LocalComponent: Component,
{
type Input = LocalComponent;
fn new(world: &'world World, system_metadata: &SystemMetadata) -> Self
{
let Some(system_ent) = world.get_entity(system_metadata.ent_id) else {
panic!(
"System entity with ID {} does not exist",
system_metadata.ent_id
);
};
let Some(local_component) = system_ent.get_with_id_mut::<LocalComponent>(
Pair::new::<IsLocalComponent>(LocalComponent::id()).id(),
) else {
panic!(
"Local component {} of system with ID {} is uninitialized",
type_name::<LocalComponent>(),
system_metadata.ent_id
);
};
Self { local_component }
}
}
impl<'world, LocalComponent, SystemT> InitializableParam<'world, SystemT>
for Local<'world, LocalComponent>
where
LocalComponent: Component,
SystemT: SystemWithLocalComponents,
Self: SystemParam<'world, Input = LocalComponent>,
{
fn initialize(system: &mut SystemT, input: Self::Input)
{
system.add_local_component(
Pair::new_with_comp_target::<IsLocalComponent>(input).into_parts(),
);
}
}
impl<LocalComponent> Deref for Local<'_, LocalComponent>
where
LocalComponent: Component,
{
type Target = LocalComponent;
fn deref(&self) -> &Self::Target
{
&self.local_component
}
}
impl<LocalComponent> DerefMut for Local<'_, LocalComponent>
where
LocalComponent: Component,
{
fn deref_mut(&mut self) -> &mut Self::Target
{
&mut self.local_component
}
}
pub trait SystemWithLocalComponents
{
fn add_local_component(&mut self, component_parts: ComponentParts);
}
#[derive(Component)]
struct IsLocalComponent;
|