omp_gdk/scripting/pickups/
mod.rs

1use std::ffi::c_void;
2
3pub mod events;
4pub mod functions;
5
6pub use functions::load_functions;
7
8use crate::{players::Player, runtime::queue_api_call, types::vector::Vector3};
9
10pub struct Pickup {
11    handle: *const c_void,
12}
13
14#[deny(non_snake_case)]
15impl Pickup {
16    pub fn get_handle(&self) -> *const c_void {
17        self.handle
18    }
19
20    pub fn new(handle: *const c_void) -> Self {
21        Self { handle }
22    }
23
24    /// This function does exactly the same as AddStaticPickup, except it returns a pickup ID which can be used to destroy it afterwards and be tracked using OnPlayerPickUpPickup.
25    pub fn create(
26        model: i32,
27        pickup_type: i32,
28        position: Vector3,
29        virtual_world: i32,
30    ) -> Option<Pickup> {
31        let mut _id = -1;
32        functions::Pickup_Create(
33            model,
34            pickup_type,
35            position.x,
36            position.y,
37            position.z,
38            virtual_world,
39            &mut _id,
40        )
41    }
42
43    /// This function adds a 'static' pickup to the game.
44    pub fn create_static(
45        model: i32,
46        pickup_type: i32,
47        position: Vector3,
48        virtual_world: i32,
49    ) -> bool {
50        functions::Pickup_AddStatic(
51            model,
52            pickup_type,
53            position.x,
54            position.y,
55            position.z,
56            virtual_world,
57        )
58    }
59
60    /// Destroys a pickup created with CreatePickup.
61    pub fn destroy(&self) -> bool {
62        functions::Pickup_Destroy(self)
63    }
64
65    /// Checks if a pickup is streamed in for a specific player.
66    pub fn is_streamed_in(&self, player: &Player) -> bool {
67        functions::Pickup_IsStreamedIn(player, self)
68    }
69
70    /// Gets the coordinates of a pickup.
71    pub fn get_pos(&self) -> Vector3 {
72        let mut pos = Vector3::default();
73        functions::Pickup_GetPos(self, &mut pos.x, &mut pos.y, &mut pos.z);
74        pos
75    }
76
77    /// Gets the model ID of a pickup.
78    pub fn get_model(&self) -> i32 {
79        functions::Pickup_GetModel(self)
80    }
81
82    /// Gets the type of a pickup.
83    pub fn get_type(&self) -> i32 {
84        functions::Pickup_GetType(self)
85    }
86
87    /// Gets the virtual world ID of a pickup.
88    pub fn get_virtual_world(&self) -> i32 {
89        functions::Pickup_GetVirtualWorld(self)
90    }
91
92    /// Sets the position of a pickup.
93    pub fn set_pos(&self, pos: Vector3, update: bool) {
94        self.defer_api_call(Box::new(move |pickup| {
95            functions::Pickup_SetPos(&pickup, pos.x, pos.y, pos.z, update);
96        }));
97    }
98
99    /// Sets the model of a pickup.
100    pub fn set_model(&self, model: i32, update: bool) -> bool {
101        functions::Pickup_SetModel(self, model, update)
102    }
103
104    /// Sets the type of a pickup.
105    pub fn set_type(&self, pickup_type: i32, update: bool) -> bool {
106        functions::Pickup_SetType(self, pickup_type, update)
107    }
108
109    /// Sets the virtual world ID of a pickup.
110    pub fn set_virtual_world(&self, virtualworld: i32) -> bool {
111        functions::Pickup_SetVirtualWorld(self, virtualworld)
112    }
113
114    /// Shows a pickup for a specific player.
115    pub fn show_for_player(&self, player: &Player) {
116        let player_id = player.get_id();
117        self.defer_api_call(Box::new(move |pickup| {
118            let player = match Player::from_id(player_id) {
119                Some(player) => player,
120                None => {
121                    log::error!("player with id={player_id} not found");
122                    return;
123                }
124            };
125            functions::Pickup_ShowForPlayer(&player, &pickup);
126        }));
127    }
128
129    /// Hides a pickup for a specific player.
130    pub fn hide_for_player(&self, player: &Player) -> bool {
131        functions::Pickup_HideForPlayer(player, self)
132    }
133
134    /// Checks if a pickup is hidden for a specific player.
135    pub fn is_hidden_for_player(&self, player: &Player) -> bool {
136        functions::Pickup_IsHiddenForPlayer(player, self)
137    }
138
139    /// Get id of the pickup
140    pub fn get_id(&self) -> i32 {
141        functions::Pickup_GetID(self)
142    }
143
144    /// Get a pickup object from an id
145    pub fn get_from_id(pickupid: i32) -> Option<Pickup> {
146        functions::Pickup_FromID(pickupid)
147    }
148
149    fn defer_api_call(&self, callback: Box<dyn FnOnce(Self)>) {
150        let pickup_id = self.get_id();
151        queue_api_call(Box::new(move || {
152            let pickup = match Self::get_from_id(pickup_id) {
153                Some(pickup) => pickup,
154                None => {
155                    log::error!("pickup with id={pickup_id} not found");
156                    return;
157                }
158            };
159            callback(pickup);
160        }));
161    }
162}