dora_ssr/dora/sensor.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 sensor_type() -> i32;
11 fn sensor_set_enabled(slf: i64, val: i32);
12 fn sensor_is_enabled(slf: i64) -> i32;
13 fn sensor_get_tag(slf: i64) -> i32;
14 fn sensor_get_owner(slf: i64) -> i64;
15 fn sensor_is_sensed(slf: i64) -> i32;
16 fn sensor_get_sensed_bodies(slf: i64) -> i64;
17 fn sensor_contains(slf: i64, body: i64) -> i32;
18}
19use crate::dora::IObject;
20/// A struct to represent a physics sensor object in the game world.
21pub struct Sensor { raw: i64 }
22crate::dora_object!(Sensor);
23impl Sensor {
24 pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
25 (unsafe { sensor_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
26 match raw {
27 0 => None,
28 _ => Some(Box::new(Sensor { raw: raw }))
29 }
30 })
31 }
32 /// Sets whether the sensor is currently enabled or not.
33 pub fn set_enabled(&mut self, val: bool) {
34 unsafe { sensor_set_enabled(self.raw(), if val { 1 } else { 0 }) };
35 }
36 /// Gets whether the sensor is currently enabled or not.
37 pub fn is_enabled(&self) -> bool {
38 return unsafe { sensor_is_enabled(self.raw()) != 0 };
39 }
40 /// Gets the tag for the sensor.
41 pub fn get_tag(&self) -> i32 {
42 return unsafe { sensor_get_tag(self.raw()) };
43 }
44 /// Gets the "Body" object that owns the sensor.
45 pub fn get_owner(&self) -> crate::dora::Body {
46 return unsafe { crate::dora::Body::from(sensor_get_owner(self.raw())).unwrap() };
47 }
48 /// Gets whether the sensor is currently sensing any other "Body" objects in the game world.
49 pub fn is_sensed(&self) -> bool {
50 return unsafe { sensor_is_sensed(self.raw()) != 0 };
51 }
52 /// Gets the array of "Body" objects that are currently being sensed by the sensor.
53 pub fn get_sensed_bodies(&self) -> crate::dora::Array {
54 return unsafe { crate::dora::Array::from(sensor_get_sensed_bodies(self.raw())).unwrap() };
55 }
56 /// Determines whether the specified `Body` object is currently being sensed by the sensor.
57 ///
58 /// # Arguments
59 ///
60 /// * `body` - The `Body` object to check if it is being sensed.
61 ///
62 /// # Returns
63 ///
64 /// * `bool` - `true` if the `Body` object is being sensed by the sensor, `false` otherwise.
65 pub fn contains(&mut self, body: &dyn crate::dora::IBody) -> bool {
66 unsafe { return sensor_contains(self.raw(), body.raw()) != 0; }
67 }
68}