dora_ssr/dora/platformer/
unit.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 platformer_unit_type() -> i32;
11	fn platformer_unit_set_playable(slf: i64, val: i64);
12	fn platformer_unit_get_playable(slf: i64) -> i64;
13	fn platformer_unit_set_detect_distance(slf: i64, val: f32);
14	fn platformer_unit_get_detect_distance(slf: i64) -> f32;
15	fn platformer_unit_set_attack_range(slf: i64, val: i64);
16	fn platformer_unit_get_attack_range(slf: i64) -> i64;
17	fn platformer_unit_set_face_right(slf: i64, val: i32);
18	fn platformer_unit_is_face_right(slf: i64) -> i32;
19	fn platformer_unit_set_receiving_decision_trace(slf: i64, val: i32);
20	fn platformer_unit_is_receiving_decision_trace(slf: i64) -> i32;
21	fn platformer_unit_set_decision_tree(slf: i64, val: i64);
22	fn platformer_unit_get_decision_tree(slf: i64) -> i64;
23	fn platformer_unit_is_on_surface(slf: i64) -> i32;
24	fn platformer_unit_get_ground_sensor(slf: i64) -> i64;
25	fn platformer_unit_get_detect_sensor(slf: i64) -> i64;
26	fn platformer_unit_get_attack_sensor(slf: i64) -> i64;
27	fn platformer_unit_get_unit_def(slf: i64) -> i64;
28	fn platformer_unit_get_current_action(slf: i64) -> i64;
29	fn platformer_unit_get_width(slf: i64) -> f32;
30	fn platformer_unit_get_height(slf: i64) -> f32;
31	fn platformer_unit_get_entity(slf: i64) -> i64;
32	fn platformer_unit_attach_action(slf: i64, name: i64) -> i64;
33	fn platformer_unit_remove_action(slf: i64, name: i64);
34	fn platformer_unit_remove_all_actions(slf: i64);
35	fn platformer_unit_get_action(slf: i64, name: i64) -> i64;
36	fn platformer_unit_each_action(slf: i64, func0: i32, stack0: i64);
37	fn platformer_unit_start(slf: i64, name: i64) -> i32;
38	fn platformer_unit_stop(slf: i64);
39	fn platformer_unit_is_doing(slf: i64, name: i64) -> i32;
40	fn platformer_unit_new(unit_def: i64, physics_world: i64, entity: i64, pos: i64, rot: f32) -> i64;
41	fn platformer_unit_with_store(unit_def_name: i64, physics_world_name: i64, entity: i64, pos: i64, rot: f32) -> i64;
42}
43use crate::dora::IObject;
44use crate::dora::IBody;
45impl IBody for Unit { }
46use crate::dora::INode;
47impl INode for Unit { }
48/// A struct represents a character or other interactive item in a game scene.
49pub struct Unit { raw: i64 }
50crate::dora_object!(Unit);
51impl Unit {
52	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
53		(unsafe { platformer_unit_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
54			match raw {
55				0 => None,
56				_ => Some(Box::new(Unit { raw: raw }))
57			}
58		})
59	}
60	/// Sets the property that references a "Playable" object for managing the animation state and playback of the "Unit".
61	pub fn set_playable(&mut self, val: &dyn crate::dora::IPlayable) {
62		unsafe { platformer_unit_set_playable(self.raw(), val.raw()) };
63	}
64	/// Gets the property that references a "Playable" object for managing the animation state and playback of the "Unit".
65	pub fn get_playable(&self) -> crate::dora::Playable {
66		return unsafe { crate::dora::Playable::from(platformer_unit_get_playable(self.raw())).unwrap() };
67	}
68	/// Sets the property that specifies the maximum distance at which the "Unit" can detect other "Unit" or objects.
69	pub fn set_detect_distance(&mut self, val: f32) {
70		unsafe { platformer_unit_set_detect_distance(self.raw(), val) };
71	}
72	/// Gets the property that specifies the maximum distance at which the "Unit" can detect other "Unit" or objects.
73	pub fn get_detect_distance(&self) -> f32 {
74		return unsafe { platformer_unit_get_detect_distance(self.raw()) };
75	}
76	/// Sets the property that specifies the size of the attack range for the "Unit".
77	pub fn set_attack_range(&mut self, val: &crate::dora::Size) {
78		unsafe { platformer_unit_set_attack_range(self.raw(), val.into_i64()) };
79	}
80	/// Gets the property that specifies the size of the attack range for the "Unit".
81	pub fn get_attack_range(&self) -> crate::dora::Size {
82		return unsafe { crate::dora::Size::from(platformer_unit_get_attack_range(self.raw())) };
83	}
84	/// Sets the boolean property that specifies whether the "Unit" is facing right or not.
85	pub fn set_face_right(&mut self, val: bool) {
86		unsafe { platformer_unit_set_face_right(self.raw(), if val { 1 } else { 0 }) };
87	}
88	/// Gets the boolean property that specifies whether the "Unit" is facing right or not.
89	pub fn is_face_right(&self) -> bool {
90		return unsafe { platformer_unit_is_face_right(self.raw()) != 0 };
91	}
92	/// Sets the boolean property that specifies whether the "Unit" is receiving a trace of the decision tree for debugging purposes.
93	pub fn set_receiving_decision_trace(&mut self, val: bool) {
94		unsafe { platformer_unit_set_receiving_decision_trace(self.raw(), if val { 1 } else { 0 }) };
95	}
96	/// Gets the boolean property that specifies whether the "Unit" is receiving a trace of the decision tree for debugging purposes.
97	pub fn is_receiving_decision_trace(&self) -> bool {
98		return unsafe { platformer_unit_is_receiving_decision_trace(self.raw()) != 0 };
99	}
100	/// Sets the string property that specifies the decision tree to use for the "Unit's" AI behavior.
101	/// the decision tree object will be searched in The singleton instance Data.store.
102	pub fn set_decision_tree(&mut self, val: &str) {
103		unsafe { platformer_unit_set_decision_tree(self.raw(), crate::dora::from_string(val)) };
104	}
105	/// Gets the string property that specifies the decision tree to use for the "Unit's" AI behavior.
106	/// the decision tree object will be searched in The singleton instance Data.store.
107	pub fn get_decision_tree(&self) -> String {
108		return unsafe { crate::dora::to_string(platformer_unit_get_decision_tree(self.raw())) };
109	}
110	/// Gets whether the "Unit" is currently on a surface or not.
111	pub fn is_on_surface(&self) -> bool {
112		return unsafe { platformer_unit_is_on_surface(self.raw()) != 0 };
113	}
114	/// Gets the "Sensor" object for detecting ground surfaces.
115	pub fn get_ground_sensor(&self) -> crate::dora::Sensor {
116		return unsafe { crate::dora::Sensor::from(platformer_unit_get_ground_sensor(self.raw())).unwrap() };
117	}
118	/// Gets the "Sensor" object for detecting other "Unit" objects or physics bodies in the game world.
119	pub fn get_detect_sensor(&self) -> crate::dora::Sensor {
120		return unsafe { crate::dora::Sensor::from(platformer_unit_get_detect_sensor(self.raw())).unwrap() };
121	}
122	/// Gets the "Sensor" object for detecting other "Unit" objects within the attack senser area.
123	pub fn get_attack_sensor(&self) -> crate::dora::Sensor {
124		return unsafe { crate::dora::Sensor::from(platformer_unit_get_attack_sensor(self.raw())).unwrap() };
125	}
126	/// Gets the "Dictionary" object for defining the properties and behavior of the "Unit".
127	pub fn get_unit_def(&self) -> crate::dora::Dictionary {
128		return unsafe { crate::dora::Dictionary::from(platformer_unit_get_unit_def(self.raw())).unwrap() };
129	}
130	/// Gets the property that specifies the current action being performed by the "Unit".
131	pub fn get_current_action(&self) -> crate::dora::platformer::UnitAction {
132		return unsafe { crate::dora::platformer::UnitAction::from(platformer_unit_get_current_action(self.raw())).unwrap() };
133	}
134	/// Gets the width of the "Unit".
135	pub fn get_width(&self) -> f32 {
136		return unsafe { platformer_unit_get_width(self.raw()) };
137	}
138	/// Gets the height of the "Unit".
139	pub fn get_height(&self) -> f32 {
140		return unsafe { platformer_unit_get_height(self.raw()) };
141	}
142	/// Gets the "Entity" object for representing the "Unit" in the ECS system.
143	pub fn get_entity(&self) -> crate::dora::Entity {
144		return unsafe { crate::dora::Entity::from(platformer_unit_get_entity(self.raw())).unwrap() };
145	}
146	/// Adds a new `UnitAction` to the `Unit` with the specified name, and returns the new `UnitAction`.
147	///
148	/// # Arguments
149	///
150	/// * `name` - The name of the new `UnitAction`.
151	///
152	/// # Returns
153	///
154	/// * The newly created `UnitAction`.
155	pub fn attach_action(&mut self, name: &str) -> crate::dora::platformer::UnitAction {
156		unsafe { return crate::dora::platformer::UnitAction::from(platformer_unit_attach_action(self.raw(), crate::dora::from_string(name))).unwrap(); }
157	}
158	/// Removes the `UnitAction` with the specified name from the `Unit`.
159	///
160	/// # Arguments
161	///
162	/// * `name` - The name of the `UnitAction` to remove.
163	pub fn remove_action(&mut self, name: &str) {
164		unsafe { platformer_unit_remove_action(self.raw(), crate::dora::from_string(name)); }
165	}
166	/// Removes all "UnitAction" objects from the "Unit".
167	pub fn remove_all_actions(&mut self) {
168		unsafe { platformer_unit_remove_all_actions(self.raw()); }
169	}
170	/// Returns the `UnitAction` with the specified name, or `None` if the `UnitAction` does not exist.
171	///
172	/// # Arguments
173	///
174	/// * `name` - The name of the `UnitAction` to retrieve.
175	///
176	/// # Returns
177	///
178	/// * The `UnitAction` with the specified name, or `None`.
179	pub fn get_action(&mut self, name: &str) -> Option<crate::dora::platformer::UnitAction> {
180		unsafe { return crate::dora::platformer::UnitAction::from(platformer_unit_get_action(self.raw(), crate::dora::from_string(name))); }
181	}
182	/// Calls the specified function for each `UnitAction` attached to the `Unit`.
183	///
184	/// # Arguments
185	///
186	/// * `visitorFunc` - A function to call for each `UnitAction`.
187	pub fn each_action(&mut self, mut visitor_func: Box<dyn FnMut(&crate::dora::platformer::UnitAction)>) {
188		let mut stack0 = crate::dora::CallStack::new();
189		let stack_raw0 = stack0.raw();
190		let func_id0 = crate::dora::push_function(Box::new(move || {
191			visitor_func(&crate::dora::platformer::UnitAction::from(stack0.pop_i64().unwrap()).unwrap())
192		}));
193		unsafe { platformer_unit_each_action(self.raw(), func_id0, stack_raw0); }
194	}
195	/// Starts the `UnitAction` with the specified name, and returns true if the `UnitAction` was started successfully.
196	///
197	/// # Arguments
198	///
199	/// * `name` - The name of the `UnitAction` to start.
200	///
201	/// # Returns
202	///
203	/// * `true` if the `UnitAction` was started successfully, `false` otherwise.
204	pub fn start(&mut self, name: &str) -> bool {
205		unsafe { return platformer_unit_start(self.raw(), crate::dora::from_string(name)) != 0; }
206	}
207	/// Stops the currently running "UnitAction".
208	pub fn stop(&mut self) {
209		unsafe { platformer_unit_stop(self.raw()); }
210	}
211	/// Returns true if the `Unit` is currently performing the specified `UnitAction`, false otherwise.
212	///
213	/// # Arguments
214	///
215	/// * `name` - The name of the `UnitAction` to check.
216	///
217	/// # Returns
218	///
219	/// * `true` if the `Unit` is currently performing the specified `UnitAction`, `false` otherwise.
220	pub fn is_doing(&mut self, name: &str) -> bool {
221		unsafe { return platformer_unit_is_doing(self.raw(), crate::dora::from_string(name)) != 0; }
222	}
223	/// A method that creates a new `Unit` object.
224	///
225	/// # Arguments
226	///
227	/// * `unit_def` - A `Dictionary` object that defines the properties and behavior of the `Unit`.
228	/// * `physics_world` - A `PhysicsWorld` object that represents the physics simulation world.
229	/// * `entity` - An `Entity` object that represents the `Unit` in ECS system.
230	/// * `pos` - A `Vec2` object that specifies the initial position of the `Unit`.
231	/// * `rot` - A number that specifies the initial rotation of the `Unit`.
232	///
233	/// # Returns
234	///
235	/// * The newly created `Unit` object.
236	pub fn new(unit_def: &crate::dora::Dictionary, physics_world: &dyn crate::dora::IPhysicsWorld, entity: &crate::dora::Entity, pos: &crate::dora::Vec2, rot: f32) -> Unit {
237		unsafe { return Unit { raw: platformer_unit_new(unit_def.raw(), physics_world.raw(), entity.raw(), pos.into_i64(), rot) }; }
238	}
239	/// A method that creates a new `Unit` object.
240	///
241	/// # Arguments
242	///
243	/// * `unit_def_name` - A string that specifies the name of the `Unit` definition to retrieve from `Data.store` table.
244	/// * `physics_world_name` - A string that specifies the name of the `PhysicsWorld` object to retrieve from `Data.store` table.
245	/// * `entity` - An `Entity` object that represents the `Unit` in ECS system.
246	/// * `pos` - A `Vec2` object that specifies the initial position of the `Unit`.
247	/// * `rot` - An optional number that specifies the initial rotation of the `Unit` (default is 0.0).
248	///
249	/// # Returns
250	///
251	/// * The newly created `Unit` object.
252	pub fn with_store(unit_def_name: &str, physics_world_name: &str, entity: &crate::dora::Entity, pos: &crate::dora::Vec2, rot: f32) -> Unit {
253		unsafe { return Unit { raw: platformer_unit_with_store(crate::dora::from_string(unit_def_name), crate::dora::from_string(physics_world_name), entity.raw(), pos.into_i64(), rot) }; }
254	}
255}