dora_ssr/dora/group.rs
1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10 fn group_type() -> i32;
11 fn entitygroup_get_count(slf: i64) -> i32;
12 fn entitygroup_get_first(slf: i64) -> i64;
13 fn entitygroup_find(slf: i64, func0: i32, stack0: i64) -> i64;
14 fn entitygroup_new(components: i64) -> i64;
15}
16use crate::dora::IObject;
17/// A struct representing a group of entities in the ECS game systems.
18pub struct Group { raw: i64 }
19crate::dora_object!(Group);
20impl Group {
21 pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
22 (unsafe { group_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
23 match raw {
24 0 => None,
25 _ => Some(Box::new(Group { raw: raw }))
26 }
27 })
28 }
29 /// Gets the number of entities in the group.
30 pub fn get_count(&self) -> i32 {
31 return unsafe { entitygroup_get_count(self.raw()) };
32 }
33 /// Gets the first entity in the group.
34 pub fn get_first(&self) -> Option<crate::dora::Entity> {
35 return unsafe { crate::dora::Entity::from(entitygroup_get_first(self.raw())) };
36 }
37 /// Finds the first entity in the group that satisfies a predicate function.
38 ///
39 /// # Arguments
40 ///
41 /// * `predicate` - The predicate function to test each entity with.
42 ///
43 /// # Returns
44 ///
45 /// * `Option<Entity>` - The first entity that satisfies the predicate, or None if no entity does.
46 pub fn find(&self, mut predicate: Box<dyn FnMut(&crate::dora::Entity) -> bool>) -> Option<crate::dora::Entity> {
47 let mut stack0 = crate::dora::CallStack::new();
48 let stack_raw0 = stack0.raw();
49 let func_id0 = crate::dora::push_function(Box::new(move || {
50 let result = predicate(&stack0.pop_cast::<crate::dora::Entity>().unwrap());
51 stack0.push_bool(result);
52 }));
53 unsafe { return crate::dora::Entity::from(entitygroup_find(self.raw(), func_id0, stack_raw0)); }
54 }
55 /// A method that creates a new group with the specified component names.
56 ///
57 /// # Arguments
58 ///
59 /// * `components` - A vector listing the names of the components to include in the group.
60 ///
61 /// # Returns
62 ///
63 /// * `Group` - The new group.
64 pub fn new(components: &Vec<&str>) -> Group {
65 unsafe { return Group { raw: entitygroup_new(crate::dora::Vector::from_str(components)) }; }
66 }
67}