forked from feather-rs/feather
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.rs
More file actions
39 lines (32 loc) · 1.07 KB
/
Copy pathsetup.rs
File metadata and controls
39 lines (32 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use std::marker::PhantomData;
use crate::Game;
/// Struct passed to your plugin's `enable()` function.
///
/// Allows you to register systems, etc.
pub struct Setup<Plugin> {
_marker: PhantomData<Plugin>,
}
impl<Plugin> Setup<Plugin> {
/// For Quill internal use only. Do not call.
#[doc(hidden)]
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
_marker: PhantomData,
}
}
/// Registers a function as system to be invoked
/// every tick.
///
/// The function should take as parameters your
/// plugin instance and an `&mut Game` and return nothing.
pub fn add_system<T: FnMut(&mut Plugin, &mut Game)>(&mut self, system: T) -> &mut Self {
let system: Box<dyn FnMut(&mut Plugin, &mut Game)> = Box::new(system);
let system_data = Box::leak(Box::new(system)) as *mut Box<_> as *mut u8;
let name = std::any::type_name::<T>();
unsafe {
quill_sys::register_system(system_data.into(), name.as_ptr().into(), name.len() as u32);
}
self
}
}