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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
use std::any::{Any, TypeId};
use std::fmt::Debug;
use std::ops::{Deref, DerefMut};
use seq_macro::seq;
use crate::system::{Input as SystemInput, Param as SystemParam, System};
use crate::ComponentStorage;
pub trait Component: SystemInput + Any
{
#[doc(hidden)]
fn as_any_mut(&mut self) -> &mut dyn Any;
#[doc(hidden)]
fn as_any(&self) -> &dyn Any;
}
impl dyn Component
{
pub fn downcast_mut<Real: 'static>(&mut self) -> Option<&mut Real>
{
self.as_any_mut().downcast_mut()
}
pub fn is<Other: 'static>(&self) -> bool
{
self.as_any().is::<Other>()
}
}
impl Debug for dyn Component
{
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
{
formatter.debug_struct("Component").finish_non_exhaustive()
}
}
/// A sequence of components.
pub trait Sequence
{
type MutRefs<'component>
where
Self: 'component;
fn into_vec(self) -> Vec<Box<dyn Component>>;
fn type_ids() -> Vec<TypeId>;
fn from_components(components: &mut [Box<dyn Component>]) -> Self::MutRefs<'_>;
}
macro_rules! inner {
($c: tt) => {
seq!(I in 0..=$c {
impl<#(Comp~I: Component,)*> Sequence for (#(Comp~I,)*) {
type MutRefs<'component> = (#(&'component mut Comp~I,)*)
where Self: 'component;
fn into_vec(self) -> Vec<Box<dyn Component>> {
Vec::from_iter([#(Box::new(self.I) as Box<dyn Component>,)*])
}
fn type_ids() -> Vec<TypeId> {
vec![
#(
TypeId::of::<Comp~I>(),
)*
]
}
fn from_components(
components: &mut [Box<dyn Component>],
) -> Self::MutRefs<'_>
{
#(
let mut comp_~I = None;
)*
for comp in components.iter_mut() {
#(
if comp.is::<Comp~I>() {
comp_~I = Some(comp);
continue;
}
)*
}
(#(
comp_~I.unwrap().downcast_mut::<Comp~I>().unwrap(),
)*)
}
}
});
};
}
seq!(C in 0..=64 {
inner!(C);
});
/// Holds a component which is local to a single system.
#[derive(Debug)]
pub struct Local<'world, LocalComponent: Component>
{
local_component: &'world mut LocalComponent,
}
impl<'world, LocalComponent> Local<'world, LocalComponent>
where
LocalComponent: Component,
{
fn new(local_component: &'world mut LocalComponent) -> Self
{
Self { local_component }
}
}
unsafe impl<'world, LocalComponent> SystemParam<'world> for Local<'world, LocalComponent>
where
LocalComponent: Component,
{
type Flags = ();
type Input = LocalComponent;
fn initialize<SystemImpl>(system: &mut impl System<SystemImpl>, input: Self::Input)
{
system.set_local_component(input);
}
fn new<SystemImpl>(
system: &'world mut impl System<SystemImpl>,
_component_storage: &'world mut ComponentStorage,
) -> Self
{
let local_component = system
.get_local_component_mut::<LocalComponent>()
.expect("Local component is uninitialized");
Self::new(local_component)
}
fn is_compatible<Other: SystemParam<'world>>() -> bool
{
let other_comparable = Other::get_comparable();
let Some(other_type_id) = other_comparable.downcast_ref::<TypeId>() else {
return true;
};
TypeId::of::<LocalComponent>() != *other_type_id
}
fn get_comparable() -> Box<dyn Any>
{
Box::new(TypeId::of::<LocalComponent>())
}
}
impl<'world, LocalComponent> Deref for Local<'world, LocalComponent>
where
LocalComponent: Component,
{
type Target = LocalComponent;
fn deref(&self) -> &Self::Target
{
self.local_component
}
}
impl<'world, LocalComponent> DerefMut for Local<'world, LocalComponent>
where
LocalComponent: Component,
{
fn deref_mut(&mut self) -> &mut Self::Target
{
self.local_component
}
}
|