diff options
author | cel <cel@blos.sm> | 2022-07-15 22:07:01 +0100 |
---|---|---|
committer | cel <cel@blos.sm> | 2022-07-15 22:07:01 +0100 |
commit | ae7b5a4d0627a1f944d59c3041b0bb0264d4e568 (patch) | |
tree | 08b99060154b99299ef23d16ae9d3bc9916a6a7f /rust | |
parent | a46a91e183a272e012354a6f0263299448205acb (diff) | |
download | 2022-ae7b5a4d0627a1f944d59c3041b0bb0264d4e568.tar.gz 2022-ae7b5a4d0627a1f944d59c3041b0bb0264d4e568.tar.bz2 2022-ae7b5a4d0627a1f944d59c3041b0bb0264d4e568.zip |
create base project structure
Diffstat (limited to 'rust')
-rw-r--r-- | rust/src/lib.rs | 2 | ||||
-rw-r--r-- | rust/src/test.rs | 58 |
2 files changed, 60 insertions, 0 deletions
diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 1cc766a..7958395 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -1,5 +1,6 @@ mod game; mod spinning_cube; +mod test; use gdnative::prelude::{godot_init, InitHandle}; @@ -7,6 +8,7 @@ use gdnative::prelude::{godot_init, InitHandle}; fn init(handle: InitHandle) { handle.add_class::<game::Game>(); handle.add_class::<spinning_cube::SpinningCube>(); + handle.add_class::<test::SpinningCubeReverse>(); } // macros that create the entry-points of the dynamic library. diff --git a/rust/src/test.rs b/rust/src/test.rs new file mode 100644 index 0000000..6c0774f --- /dev/null +++ b/rust/src/test.rs @@ -0,0 +1,58 @@ +use gdnative::api::*; +use gdnative::prelude::*; + +/// The SpinningCube "class" +#[derive(NativeClass)] +#[inherit(MeshInstance)] +#[register_with(Self::register_builder)] +pub struct SpinningCubeReverse { + start: Vector3, + time: f32, + #[property(path = "base/rotate_speed")] + rotate_speed: f64, +} + +// __One__ `impl` block can have the `#[methods]` attribute, which will generate +// code to automatically bind any exported methods to Godot. +#[methods] +impl SpinningCubeReverse { + // Register the builder for methods, properties and/or signals. + fn register_builder(_builder: &ClassBuilder<Self>) { + godot_print!("SpinningCube builder is registered!"); + } + + /// The "constructor" of the class. + fn new(_owner: &MeshInstance) -> Self { + SpinningCubeReverse { + start: Vector3::new(0.0, 0.0, 0.0), + time: 0.0, + rotate_speed: 0.05, + } + } + + // In order to make a method known to Godot, the #[export] attribute has to be used. + // In Godot script-classes do not actually inherit the parent class. + // Instead they are "attached" to the parent object, called the "owner". + // The owner is passed to every single exposed method. + #[export] + unsafe fn _ready(&mut self, owner: &MeshInstance) { + owner.set_physics_process(true); + } + + #[export] + unsafe 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 * -1.0); + + 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 = 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)); + } + } +} |