dora_ssr/dora/platformer/
data.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_data_get_group_first_player() -> i32;
11	fn platformer_data_get_group_last_player() -> i32;
12	fn platformer_data_get_group_hide() -> i32;
13	fn platformer_data_get_group_detect_player() -> i32;
14	fn platformer_data_get_group_terrain() -> i32;
15	fn platformer_data_get_group_detection() -> i32;
16	fn platformer_data_get_store() -> i64;
17	fn platformer_data_set_should_contact(group_a: i32, group_b: i32, contact: i32);
18	fn platformer_data_get_should_contact(group_a: i32, group_b: i32) -> i32;
19	fn platformer_data_set_relation(group_a: i32, group_b: i32, relation: i32);
20	fn platformer_data_get_relation_by_group(group_a: i32, group_b: i32) -> i32;
21	fn platformer_data_get_relation(body_a: i64, body_b: i64) -> i32;
22	fn platformer_data_is_enemy_group(group_a: i32, group_b: i32) -> i32;
23	fn platformer_data_is_enemy(body_a: i64, body_b: i64) -> i32;
24	fn platformer_data_is_friend_group(group_a: i32, group_b: i32) -> i32;
25	fn platformer_data_is_friend(body_a: i64, body_b: i64) -> i32;
26	fn platformer_data_is_neutral_group(group_a: i32, group_b: i32) -> i32;
27	fn platformer_data_is_neutral(body_a: i64, body_b: i64) -> i32;
28	fn platformer_data_set_damage_factor(damage_type: i32, defence_type: i32, bounus: f32);
29	fn platformer_data_get_damage_factor(damage_type: i32, defence_type: i32) -> f32;
30	fn platformer_data_is_player(body: i64) -> i32;
31	fn platformer_data_is_terrain(body: i64) -> i32;
32	fn platformer_data_clear();
33}
34/// An interface that provides a centralized location for storing and accessing game-related data.
35pub struct Data { }
36impl Data {
37	/// Gets the group key representing the first index for a player group.
38	pub fn get_group_first_player() -> i32 {
39		return unsafe { platformer_data_get_group_first_player() };
40	}
41	/// Gets the group key representing the last index for a player group.
42	pub fn get_group_last_player() -> i32 {
43		return unsafe { platformer_data_get_group_last_player() };
44	}
45	/// Gets the group key that won't have any contact with other groups by default.
46	pub fn get_group_hide() -> i32 {
47		return unsafe { platformer_data_get_group_hide() };
48	}
49	/// Gets the group key that will have contacts with player groups by default.
50	pub fn get_group_detect_player() -> i32 {
51		return unsafe { platformer_data_get_group_detect_player() };
52	}
53	/// Gets the group key representing terrain that will have contacts with other groups by default.
54	pub fn get_group_terrain() -> i32 {
55		return unsafe { platformer_data_get_group_terrain() };
56	}
57	/// Gets the group key that will have contacts with other groups by default.
58	pub fn get_group_detection() -> i32 {
59		return unsafe { platformer_data_get_group_detection() };
60	}
61	/// Gets the dictionary that can be used to store arbitrary data associated with string keys and various values globally.
62	pub fn get_store() -> crate::dora::Dictionary {
63		return unsafe { crate::dora::Dictionary::from(platformer_data_get_store()).unwrap() };
64	}
65	/// Sets a boolean value indicating whether two groups should be in contact or not.
66	///
67	/// # Arguments
68	///
69	/// * `group_a` - An integer representing the first group.
70	/// * `group_b` - An integer representing the second group.
71	/// * `contact` - A boolean indicating whether the two groups should be in contact.
72	pub fn set_should_contact(group_a: i32, group_b: i32, contact: bool) {
73		unsafe { platformer_data_set_should_contact(group_a, group_b, if contact { 1 } else { 0 }); }
74	}
75	/// Gets a boolean value indicating whether two groups should be in contact or not.
76	///
77	/// # Arguments
78	///
79	/// * `group_a` - An integer representing the first group.
80	/// * `group_b` - An integer representing the second group.
81	///
82	/// # Returns
83	///
84	/// * A boolean indicating whether the two groups should be in contact.
85	pub fn get_should_contact(group_a: i32, group_b: i32) -> bool {
86		unsafe { return platformer_data_get_should_contact(group_a, group_b) != 0; }
87	}
88	/// Sets the relation between two groups.
89	///
90	/// # Arguments
91	///
92	/// * `group_a` - An integer representing the first group.
93	/// * `group_b` - An integer representing the second group.
94	/// * `relation` - The relation between the two groups.
95	pub fn set_relation(group_a: i32, group_b: i32, relation: crate::dora::platformer::Relation) {
96		unsafe { platformer_data_set_relation(group_a, group_b, relation as i32); }
97	}
98	/// Gets the relation between two groups.
99	///
100	/// # Arguments
101	///
102	/// * `group_a` - An integer representing the first group.
103	/// * `group_b` - An integer representing the second group.
104	///
105	/// # Returns
106	///
107	/// * The relation between the two groups.
108	pub fn get_relation_by_group(group_a: i32, group_b: i32) -> crate::dora::platformer::Relation {
109		unsafe { return core::mem::transmute(platformer_data_get_relation_by_group(group_a, group_b)); }
110	}
111	/// A function that can be used to get the relation between two bodies.
112	///
113	/// # Arguments
114	///
115	/// * `body_a` - The first body.
116	/// * `body_b` - The second body.
117	///
118	/// # Returns
119	///
120	/// * The relation between the two bodies.
121	pub fn get_relation(body_a: &dyn crate::dora::IBody, body_b: &dyn crate::dora::IBody) -> crate::dora::platformer::Relation {
122		unsafe { return core::mem::transmute(platformer_data_get_relation(body_a.raw(), body_b.raw())); }
123	}
124	/// A function that returns whether two groups have an "Enemy" relation.
125	///
126	/// # Arguments
127	///
128	/// * `group_a` - An integer representing the first group.
129	/// * `group_b` - An integer representing the second group.
130	///
131	/// # Returns
132	///
133	/// * A boolean indicating whether the two groups have an "Enemy" relation.
134	pub fn is_enemy_group(group_a: i32, group_b: i32) -> bool {
135		unsafe { return platformer_data_is_enemy_group(group_a, group_b) != 0; }
136	}
137	/// A function that returns whether two bodies have an "Enemy" relation.
138	///
139	/// # Arguments
140	///
141	/// * `body_a` - The first body.
142	/// * `body_b` - The second body.
143	///
144	/// # Returns
145	///
146	/// * A boolean indicating whether the two bodies have an "Enemy" relation.
147	pub fn is_enemy(body_a: &dyn crate::dora::IBody, body_b: &dyn crate::dora::IBody) -> bool {
148		unsafe { return platformer_data_is_enemy(body_a.raw(), body_b.raw()) != 0; }
149	}
150	/// A function that returns whether two groups have a "Friend" relation.
151	///
152	/// # Arguments
153	///
154	/// * `group_a` - An integer representing the first group.
155	/// * `group_b` - An integer representing the second group.
156	///
157	/// # Returns
158	///
159	/// * A boolean indicating whether the two groups have a "Friend" relation.
160	pub fn is_friend_group(group_a: i32, group_b: i32) -> bool {
161		unsafe { return platformer_data_is_friend_group(group_a, group_b) != 0; }
162	}
163	/// A function that returns whether two bodies have a "Friend" relation.
164	///
165	/// # Arguments
166	///
167	/// * `body_a` - The first body.
168	/// * `body_b` - The second body.
169	///
170	/// # Returns
171	///
172	/// * A boolean indicating whether the two bodies have a "Friend" relation.
173	pub fn is_friend(body_a: &dyn crate::dora::IBody, body_b: &dyn crate::dora::IBody) -> bool {
174		unsafe { return platformer_data_is_friend(body_a.raw(), body_b.raw()) != 0; }
175	}
176	/// A function that returns whether two groups have a "Neutral" relation.
177	///
178	/// # Arguments
179	///
180	/// * `group_a` - An integer representing the first group.
181	/// * `group_b` - An integer representing the second group.
182	///
183	/// # Returns
184	///
185	/// * A boolean indicating whether the two groups have a "Neutral" relation.
186	pub fn is_neutral_group(group_a: i32, group_b: i32) -> bool {
187		unsafe { return platformer_data_is_neutral_group(group_a, group_b) != 0; }
188	}
189	/// A function that returns whether two bodies have a "Neutral" relation.
190	///
191	/// # Arguments
192	///
193	/// * `body_a` - The first body.
194	/// * `body_b` - The second body.
195	///
196	/// # Returns
197	///
198	/// * A boolean indicating whether the two bodies have a "Neutral" relation.
199	pub fn is_neutral(body_a: &dyn crate::dora::IBody, body_b: &dyn crate::dora::IBody) -> bool {
200		unsafe { return platformer_data_is_neutral(body_a.raw(), body_b.raw()) != 0; }
201	}
202	/// Sets the bonus factor for a particular type of damage against a particular type of defence.
203	///
204	/// The builtin "MeleeAttack" and "RangeAttack" actions use a simple formula of `finalDamage = damage * bonus`.
205	///
206	/// # Arguments
207	///
208	/// * `damage_type` - An integer representing the type of damage.
209	/// * `defence_type` - An integer representing the type of defence.
210	/// * `bonus` - A number representing the bonus.
211	pub fn set_damage_factor(damage_type: i32, defence_type: i32, bounus: f32) {
212		unsafe { platformer_data_set_damage_factor(damage_type, defence_type, bounus); }
213	}
214	/// Gets the bonus factor for a particular type of damage against a particular type of defence.
215	///
216	/// # Arguments
217	///
218	/// * `damage_type` - An integer representing the type of damage.
219	/// * `defence_type` - An integer representing the type of defence.
220	///
221	/// # Returns
222	///
223	/// * A number representing the bonus factor.
224	pub fn get_damage_factor(damage_type: i32, defence_type: i32) -> f32 {
225		unsafe { return platformer_data_get_damage_factor(damage_type, defence_type); }
226	}
227	/// A function that returns whether a body is a player or not.
228	///
229	/// This works the same as `Data::get_group_first_player() <= body.group and body.group <= Data::get_group_last_player()`.
230	///
231	/// # Arguments
232	///
233	/// * `body` - The body to check.
234	///
235	/// # Returns
236	///
237	/// * A boolean indicating whether the body is a player.
238	pub fn is_player(body: &dyn crate::dora::IBody) -> bool {
239		unsafe { return platformer_data_is_player(body.raw()) != 0; }
240	}
241	/// A function that returns whether a body is terrain or not.
242	///
243	/// This works the same as `body.group == Data::get_group_terrain()`.
244	///
245	/// # Arguments
246	///
247	/// * `body` - The body to check.
248	///
249	/// # Returns
250	///
251	/// * A boolean indicating whether the body is terrain.
252	pub fn is_terrain(body: &dyn crate::dora::IBody) -> bool {
253		unsafe { return platformer_data_is_terrain(body.raw()) != 0; }
254	}
255	/// Clears all data stored in the "Data" object, including user data in Data.store field. And reset some data to default values.
256	pub fn clear() {
257		unsafe { platformer_data_clear(); }
258	}
259}