forked from godot-rust/gdnative
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
76 lines (64 loc) · 2 KB
/
lib.rs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use gdnative::api::MeshInstance;
use gdnative::prelude::*;
use gdnative::nativescript::export::property::{EnumHint, IntHint, StringHint};
#[derive(gdnative::derive::NativeClass)]
#[inherit(MeshInstance)]
#[register_with(register_properties)]
struct RustTest {
start: Vector3,
time: f32,
#[property(path = "base/rotate_speed")]
rotate_speed: f64,
}
fn register_properties(builder: &ClassBuilder<RustTest>) {
builder
.add_property::<String>("test/test_enum")
.with_hint(StringHint::Enum(EnumHint::new(vec![
"Hello".into(),
"World".into(),
"Testing".into(),
])))
.with_getter(|_: &RustTest, _| "Hello".to_string())
.done();
builder
.add_property("test/test_flags")
.with_hint(IntHint::Flags(EnumHint::new(vec![
"A".into(),
"B".into(),
"C".into(),
"D".into(),
])))
.with_getter(|_: &RustTest, _| 0)
.done();
}
#[methods]
impl RustTest {
fn new(_owner: &MeshInstance) -> Self {
RustTest {
start: Vector3::new(0.0, 0.0, 0.0),
time: 0.0,
rotate_speed: 0.05,
}
}
#[export]
fn _ready(&mut self, owner: &MeshInstance) {
owner.set_physics_process(true);
}
#[export]
fn _physics_process(&mut self, owner: &MeshInstance, delta: f64) {
use gdnative::api::SpatialMaterial;
self.time += delta as f32;
owner.rotate_y(self.rotate_speed * delta);
let offset = Vector3::new(0.0, 1.0, 0.0) * self.time.cos() * 0.5;
owner.set_translation(self.start + offset);
if let Some(mat) = owner.get_surface_material(0) {
let mat = unsafe { mat.assume_safe() };
let mat = mat.cast::<SpatialMaterial>().expect("Incorrect material");
mat.set_albedo(Color::from_rgba(self.time.cos().abs(), 0.0, 0.0, 1.0));
}
}
}
fn init(handle: InitHandle) {
handle.add_class::<RustTest>();
}
godot_init!(init);