aboutsummaryrefslogtreecommitdiffstats
path: root/rust
diff options
context:
space:
mode:
authorLibravatar David <david003@gmx.net>2022-07-15 17:33:27 +0200
committerLibravatar David <david003@gmx.net>2022-07-15 17:33:27 +0200
commita46a91e183a272e012354a6f0263299448205acb (patch)
treeb0700f933db7149c1b95afed2e04d53772f7e2dc /rust
download2022-a46a91e183a272e012354a6f0263299448205acb.tar.gz
2022-a46a91e183a272e012354a6f0263299448205acb.tar.bz2
2022-a46a91e183a272e012354a6f0263299448205acb.zip
initial commit: template
Diffstat (limited to 'rust')
-rw-r--r--rust/.vscode/launch.json13
-rw-r--r--rust/Cargo.toml12
-rw-r--r--rust/src/game.rs46
-rw-r--r--rust/src/lib.rs13
-rw-r--r--rust/src/spinning_cube.rs58
-rw-r--r--rust/tests/gd_test_main.rs2
6 files changed, 144 insertions, 0 deletions
diff --git a/rust/.vscode/launch.json b/rust/.vscode/launch.json
new file mode 100644
index 0000000..ed595f2
--- /dev/null
+++ b/rust/.vscode/launch.json
@@ -0,0 +1,13 @@
+{
+ "version": "0.2.0",
+ "configurations": [{
+ "name": "rust-gdb - attach to process - code-with-your-friends2022",
+ "type": "gdb",
+ "request": "attach",
+ "gdbpath": "rust-gdb",
+ "cwd": "${workspaceFolder}",
+ "target": "12345",
+ "printCalls": true,
+ "showDevDebugOutput": true
+ }]
+} \ No newline at end of file
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
new file mode 100644
index 0000000..c283d29
--- /dev/null
+++ b/rust/Cargo.toml
@@ -0,0 +1,12 @@
+[package]
+name = "code_with_your_friends2022"
+version = "0.1.0"
+authors = ["David <david003@gmx.net>"]
+edition = "2018"
+
+[lib]
+crate-type = ["cdylib", "staticlib"]
+
+[dependencies]
+gdnative = "0.10.0"
+# gdnative = { path = "../../godot-rust/gdnative" }
diff --git a/rust/src/game.rs b/rust/src/game.rs
new file mode 100644
index 0000000..ece5a54
--- /dev/null
+++ b/rust/src/game.rs
@@ -0,0 +1,46 @@
+use gdnative::api::*;
+use gdnative::prelude::*;
+
+/// The Game "class"
+#[derive(NativeClass)]
+#[inherit(Spatial)]
+#[register_with(Self::register_builder)]
+pub struct Game {
+ name: String,
+}
+
+// __One__ `impl` block can have the `#[methods]` attribute, which will generate
+// code to automatically bind any exported methods to Godot.
+#[methods]
+impl Game {
+ // Register the builder for methods, properties and/or signals.
+ fn register_builder(_builder: &ClassBuilder<Self>) {
+ godot_print!("Game builder is registered!");
+ }
+
+ /// The "constructor" of the class.
+ fn new(_owner: &Spatial) -> Self {
+ godot_print!("Game is created!");
+ Game {
+ name: "".to_string(),
+ }
+ }
+
+ // 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: &Spatial) {
+ // The `godot_print!` macro works like `println!` but prints to the Godot-editor
+ // output tab as well.
+ self.name = "Game".to_string();
+ godot_print!("{} is ready!", self.name);
+ }
+
+ // This function will be called in every frame
+ #[export]
+ unsafe fn _process(&self, _owner: &Spatial, delta: f64) {
+ godot_print!("Inside {} _process(), delta is {}", self.name, delta);
+ }
+}
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
new file mode 100644
index 0000000..1cc766a
--- /dev/null
+++ b/rust/src/lib.rs
@@ -0,0 +1,13 @@
+mod game;
+mod spinning_cube;
+
+use gdnative::prelude::{godot_init, InitHandle};
+
+// Function that registers all exposed classes to Godot
+fn init(handle: InitHandle) {
+ handle.add_class::<game::Game>();
+ handle.add_class::<spinning_cube::SpinningCube>();
+}
+
+// macros that create the entry-points of the dynamic library.
+godot_init!(init);
diff --git a/rust/src/spinning_cube.rs b/rust/src/spinning_cube.rs
new file mode 100644
index 0000000..41b429f
--- /dev/null
+++ b/rust/src/spinning_cube.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 SpinningCube {
+ 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 SpinningCube {
+ // 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 {
+ SpinningCube {
+ 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);
+
+ 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));
+ }
+ }
+}
diff --git a/rust/tests/gd_test_main.rs b/rust/tests/gd_test_main.rs
new file mode 100644
index 0000000..de4c7d1
--- /dev/null
+++ b/rust/tests/gd_test_main.rs
@@ -0,0 +1,2 @@
+#[test]
+fn test_get_message() {}