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
use core::ffi::CStr;
use super::*;
/// `EntityView` mixin implementation
impl World {
/// Convert enum constant to entity
///
/// # Type Parameters
///
/// * `T` - The enum type.
///
/// # Arguments
///
/// * `enum_value` - The enum value to convert.
///
/// # Returns
///
/// `EntityView` wrapping the id of the enum constant.
#[doc(alias = "world::id")] //enum mixin implementation
pub fn entity_from_enum<T>(&self, enum_value: T) -> EntityView<'_>
where
T: ComponentId + ComponentType<Enum> + EnumComponentInfo,
{
EntityView::new_from(self, enum_value.id_variant(self))
}
/// Create an entity that's associated with a type and name
///
/// # Type Parameters
///
/// * `T` - The component type to associate with the new entity.
///
/// # Arguments
///
/// * `name` - The name to use for the new entity.
pub fn entity_from_named<'a, T: ComponentId>(&'a self, name: &str) -> EntityView<'a> {
EntityView::new_from(self, T::__register_or_get_id_named::<true>(self, name))
}
/// Create an entity that's associated with a type
///
/// # Type Parameters
///
/// * `T` - The component type to associate with the new entity.
pub fn entity_from<T: ComponentId>(&self) -> EntityView<'_> {
EntityView::new_from(self, T::entity_id(self))
}
/// Create an entity that's associated with a name.
/// The name does an extra allocation if it's bigger than 24 bytes. To avoid this, use `entity_named_cstr`.
/// length of 24 bytes: `"hi this is 24 bytes long"`
///
/// Named entities can be looked up with the lookup functions. Entity names
/// may be scoped, where each element in the name is separated by "::".
/// For example: "`Foo::Bar`". If parts of the hierarchy in the scoped name do
/// not yet exist, they will be automatically created.
///
/// # Example
///
/// ```
/// use flecs_ecs::prelude::*;
///
/// let world = World::new();
///
/// let entity = world.entity_named("Foo");
/// assert_eq!(entity.get_name(), Some("Foo".to_string()));
/// ```
///
/// # See also
///
/// * [`World::entity()`]
/// * [`World::entity_named_cstr()`]
pub fn entity_named(&self, name: &str) -> EntityView<'_> {
EntityView::new_named(self, name)
}
/// Create an entity that's associated with a name within a scope, using a custom separator and root separator.
/// The name does an extra allocation if it's bigger than 24 bytes. To avoid this, use `entity_named_cstr`.
/// length of 24 bytes: `"hi this is 24 bytes long"`
///
/// Named entities can be looked up with the lookup functions. Entity names
/// may be scoped, where each element in the name is separated by the sep you use.
/// For example: "`Foo-Bar`". If parts of the hierarchy in the scoped name do
/// not yet exist, they will be automatically created.
/// Note, this does still create the hierarchy as `Foo::Bar`.
///
/// # Example
///
/// ```
/// use flecs_ecs::prelude::*;
///
/// let world = World::new();
///
/// let entity = world.entity_named_scoped("Foo-Bar", "-", "::");
/// assert_eq!(entity.get_name(), Some("Bar".to_string()));
/// assert_eq!(entity.path(), Some("::Foo::Bar".to_string()));
/// ```
///
/// # See also
///
/// * [`World::entity()`]
/// * [`World::entity_named_cstr()`]
pub fn entity_named_scoped(&self, name: &str, sep: &str, root_sep: &str) -> EntityView<'_> {
EntityView::new_named_scoped(self, name, sep, root_sep)
}
/// Create an entity that's associated with a name.
/// The name must be a valid C str. No extra allocation is done.
///
/// Named entities can be looked up with the lookup functions. Entity names
/// may be scoped, where each element in the name is separated by "::".
/// For example: "`Foo::Bar`". If parts of the hierarchy in the scoped name do
/// not yet exist, they will be automatically created.
///
/// # Example
///
/// ```
/// use flecs_ecs::prelude::*;
///
/// let world = World::new();
///
/// let entity = world.entity_named("Foo");
/// assert_eq!(entity.get_name(), Some("Foo".to_string()));
/// ```
///
/// # See also
///
/// * [`World::entity()`]
/// * [`World::entity_named()`]
pub fn entity_named_cstr(&self, name: &CStr) -> EntityView<'_> {
EntityView::new_named_cstr(self, name)
}
/// Create a new entity.
///
/// # See also
///
/// * [`World::entity_named()`]
/// * [`World::entity_named_cstr()`]
#[inline(always)]
pub fn entity(&self) -> EntityView<'_> {
EntityView::new(self)
}
/// Create entity with id 0.
/// This function is useful when the API must provide an entity that
/// belongs to a world, but the entity id is 0.
///
/// # Example
///
/// ```
/// use flecs_ecs::prelude::*;
///
/// let world = World::new();
/// let entity = world.entity_null();
/// assert_eq!(entity.id(), 0);
/// ```
pub fn entity_null(&self) -> EntityView<'_> {
EntityView::new_null(self)
}
/// wraps an [`EntityView`] with the provided id.
///
/// # Arguments
///
/// * `id` - The id to use for the new entity.
pub fn entity_from_id(&self, id: impl Into<Entity>) -> EntityView<'_> {
EntityView::new_from(self, id.into())
}
/// Creates a prefab
///
/// # Returns
///
/// The prefab entity.
///
/// # See also
///
/// * [`World::prefab_named()`]
/// * [`World::prefab_type()`]
/// * [`World::prefab_type_named()`]
pub fn prefab(&self) -> EntityView<'_> {
let result = EntityView::new(self);
result.add(id::<flecs::Prefab>());
result
}
/// Creates a named prefab
///
/// # Arguments
///
/// * `name` - The name to use for the new prefab.
///
/// # Returns
///
/// The prefab entity.
///
/// # See also
///
/// * [`World::prefab()`]
/// * [`World::prefab_type()`]
/// * [`World::prefab_type_named()`]
pub fn prefab_named<'a>(&'a self, name: &str) -> EntityView<'a> {
let result = EntityView::new_named(self, name);
unsafe { result.add_id_unchecked(ECS_PREFAB) };
result
}
/// Creates a prefab that's associated with a type
///
/// # Type Parameters
///
/// * `T` - The component type to associate with the new prefab.
///
/// # Returns
///
/// The prefab entity.
///
/// # See also
///
/// * [`World::prefab()`]
/// * [`World::prefab_named()`]
/// * [`World::prefab_type_named()`]
pub fn prefab_type<T: ComponentId>(&self) -> EntityView<'_> {
let result = self.entity_from::<T>();
result.add(ECS_PREFAB);
result
}
/// Creates a named prefab that's associated with a type
///
/// # Type Parameters
///
/// * `T` - The component type to associate with the new prefab.
///
/// # Arguments
///
/// * `name` - The name to use for the new prefab.
///
/// # Returns
///
/// The prefab entity.
///
/// # See also
///
/// * [`World::prefab()`]
/// * [`World::prefab_named()`]
/// * [`World::prefab_type()`]
pub fn prefab_type_named<'a, T: ComponentId>(&'a self, name: &str) -> EntityView<'a> {
let result = self.entity_from_named::<T>(name);
result.add(ECS_PREFAB);
result
}
}