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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
use std::any::{type_name, Any, TypeId};
use std::fmt::Debug;
use seq_macro::seq;
use crate::lock::WriteGuard;
use crate::system::{ComponentRefMut, Input as SystemInput};
use crate::type_name::TypeName;
use crate::{EntityComponent, World};
pub mod local;
pub(crate) mod storage;
pub trait Component: SystemInput + Any + TypeName
{
/// The component type in question. Will usually be `Self`
type Component: Component
where
Self: Sized;
type RefMut<'component>
where
Self: Sized;
/// Returns the ID of this component's type.
fn id(&self) -> Id;
#[doc(hidden)]
fn as_any_mut(&mut self) -> &mut dyn Any;
#[doc(hidden)]
fn as_any(&self) -> &dyn Any;
fn is_optional(&self) -> bool
{
false
}
}
impl dyn Component
{
pub fn downcast_mut<Real: 'static>(&mut self) -> Option<&mut Real>
{
self.as_any_mut().downcast_mut()
}
pub fn downcast_ref<Real: 'static>(&self) -> Option<&Real>
{
self.as_any().downcast_ref()
}
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()
}
}
impl TypeName for Box<dyn Component>
{
fn type_name(&self) -> &'static str
{
self.as_ref().type_name()
}
}
impl<ComponentT> Component for Option<ComponentT>
where
ComponentT: Component,
{
type Component = ComponentT;
type RefMut<'component> = Option<ComponentRefMut<'component, ComponentT>>;
fn id(&self) -> Id
{
Id::of::<Self>()
}
fn as_any_mut(&mut self) -> &mut dyn Any
{
self
}
fn as_any(&self) -> &dyn Any
{
self
}
fn is_optional(&self) -> bool
{
true
}
}
impl<ComponentT> TypeName for Option<ComponentT>
where
ComponentT: Component,
{
fn type_name(&self) -> &'static str
{
type_name::<Self>()
}
}
impl<ComponentT> SystemInput for Option<ComponentT> where ComponentT: Component {}
/// The ID of a [`Component`] type.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id
{
inner: TypeId,
}
impl Id
{
#[must_use]
pub fn of<ComponentT>() -> Self
where
ComponentT: Component,
{
Self { inner: TypeId::of::<ComponentT>() }
}
}
/// A sequence of components.
pub trait Sequence
{
type Refs<'component>
where
Self: 'component;
fn into_vec(self) -> Vec<Box<dyn Component>>;
fn metadata() -> Vec<Metadata>;
fn from_components<'component>(
components: impl Iterator<Item = &'component EntityComponent>,
world: &'component World,
) -> Self::Refs<'component>;
}
/// [`Component`] metadata.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Metadata
{
pub id: Id,
pub is_optional: IsOptional,
}
impl Metadata
{
pub fn of<ComponentT: Component + ?Sized>(component: &ComponentT) -> Self
{
Self {
id: component.id(),
is_optional: component.is_optional().into(),
}
}
}
/// Whether or not a `Component` is optional.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IsOptional
{
Yes,
No,
}
impl From<bool> for IsOptional
{
fn from(is_optional: bool) -> Self
{
if is_optional {
return IsOptional::Yes;
}
IsOptional::No
}
}
/// Returns whether the given component type is a optional component.
///
/// Will return `true` if the component is a [`Option`].
#[must_use]
pub fn is_optional<ComponentT: Component>() -> bool
{
if Id::of::<ComponentT>() == Id::of::<Option<ComponentT::Component>>() {
return true;
}
false
}
pub trait FromOptional<'comp>
{
fn from_optional_component(
optional_component: Option<WriteGuard<'comp, Box<dyn Component>>>,
world: &'comp World,
) -> Self;
}
macro_rules! inner {
($c: tt) => {
seq!(I in 0..=$c {
impl<#(Comp~I: Component,)*> Sequence for (#(Comp~I,)*)
where
#(for<'comp> Comp~I::RefMut<'comp>: FromOptional<'comp>,)*
{
type Refs<'component> = (#(Comp~I::RefMut<'component>,)*)
where Self: 'component;
fn into_vec(self) -> Vec<Box<dyn Component>>
{
Vec::from_iter([#(Box::new(self.I) as Box<dyn Component>,)*])
}
fn metadata() -> Vec<Metadata>
{
vec![
#(
Metadata {
id: Id::of::<Comp~I>(),
is_optional: is_optional::<Comp~I>().into()
},
)*
]
}
fn from_components<'component>(
components: impl Iterator<Item = &'component EntityComponent>,
world: &'component World,
) -> Self::Refs<'component>
{
#(
let mut comp_~I: Option<WriteGuard<Box<dyn Component>>> = None;
)*
for comp in components {
#(
if comp.id == Id::of::<Comp~I::Component>() {
comp_~I = Some(lock_component(comp));
continue;
}
)*
}
(#(
Comp~I::RefMut::from_optional_component(comp_~I, world),
)*)
}
}
});
};
}
seq!(C in 0..=64 {
inner!(C);
});
fn lock_component(
entity_component: &EntityComponent,
) -> WriteGuard<'_, Box<dyn Component>>
{
entity_component
.component
.write_nonblock()
.unwrap_or_else(|_| {
panic!(
"Failed to acquire read-write lock to component {}",
entity_component.name
);
})
}
|