dora_ssr/dora/observer.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 observer_type() -> i32;
11 fn entityobserver_new(event: i32, components: i64) -> i64;
12}
13use crate::dora::IObject;
14/// A struct representing an observer of entity changes in the game systems.
15pub struct Observer { raw: i64 }
16crate::dora_object!(Observer);
17impl Observer {
18 pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
19 (unsafe { observer_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
20 match raw {
21 0 => None,
22 _ => Some(Box::new(Observer { raw: raw }))
23 }
24 })
25 }
26 /// A method that creates a new observer with the specified component filter and action to watch for.
27 ///
28 /// # Arguments
29 ///
30 /// * `event` - The type of event to watch for.
31 /// * `components` - A vector listing the names of the components to filter entities by.
32 ///
33 /// # Returns
34 ///
35 /// * `Observer` - The new observer.
36 pub fn new(event: crate::dora::EntityEvent, components: &Vec<&str>) -> Observer {
37 unsafe { return Observer { raw: entityobserver_new(event as i32, crate::dora::Vector::from_str(components)) }; }
38 }
39}