dora_ssr/dora/
entity.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 entity_type() -> i32;
11	fn entity_get_count() -> i32;
12	fn entity_get_index(slf: i64) -> i32;
13	fn entity_clear();
14	fn entity_remove(slf: i64, key: i64);
15	fn entity_destroy(slf: i64);
16	fn entity_new() -> i64;
17}
18use crate::dora::IObject;
19/// A struct representing an entity for an ECS game system.
20pub struct Entity { raw: i64 }
21crate::dora_object!(Entity);
22impl Entity {
23	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
24		(unsafe { entity_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
25			match raw {
26				0 => None,
27				_ => Some(Box::new(Entity { raw: raw }))
28			}
29		})
30	}
31	/// Gets the number of all running entities.
32	pub fn get_count() -> i32 {
33		return unsafe { entity_get_count() };
34	}
35	/// Gets the index of the entity.
36	pub fn get_index(&self) -> i32 {
37		return unsafe { entity_get_index(self.raw()) };
38	}
39	/// Clears all entities.
40	pub fn clear() {
41		unsafe { entity_clear(); }
42	}
43	/// Removes a property of the entity.
44	///
45	/// This function will trigger events for Observer objects.
46	///
47	/// # Arguments
48	///
49	/// * `key` - The name of the property to remove.
50	pub fn remove(&mut self, key: &str) {
51		unsafe { entity_remove(self.raw(), crate::dora::from_string(key)); }
52	}
53	/// Destroys the entity.
54	pub fn destroy(&mut self) {
55		unsafe { entity_destroy(self.raw()); }
56	}
57	/// Creates a new entity.
58	pub fn new() -> Entity {
59		unsafe { return Entity { raw: entity_new() }; }
60	}
61}