From 0b61e416395630e4e46c2a129dec83ca6ebe61ff Mon Sep 17 00:00:00 2001 From: Louis Hollingworth Date: Sat, 8 Jul 2023 19:37:47 +0100 Subject: [PATCH] Finally got a grid loaded on the screen. Signed-off-by: Louis Hollingworth --- README.md | 5 ++- Rust.gdextension | 13 ++++++++ project.godot | 1 + rust/Cargo.toml | 11 +++++++ rust/src/lib.rs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++ scenes/main.tscn | 5 +++ 6 files changed, 115 insertions(+), 1 deletion(-) create mode 100644 Rust.gdextension create mode 100644 rust/Cargo.toml create mode 100755 rust/src/lib.rs create mode 100644 scenes/main.tscn diff --git a/README.md b/README.md index 6e5f9e6..cbf1600 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,7 @@ -Just a project for me to learn Godot. +Just a project for me to learn Godot 4 with Rust. I'm following a [YouTube Tutorial](https://www.youtube.com/playlist?list=PLM-hFhoXjVl1hq0j9lxgRCzxkBpDZhDlT) +to make this game. +The video covers using GDScript, which is a bit too much like python for me, +so I'm converting the code to Rust as I follow along diff --git a/Rust.gdextension b/Rust.gdextension new file mode 100644 index 0000000..475e090 --- /dev/null +++ b/Rust.gdextension @@ -0,0 +1,13 @@ +[configuration] +entry_symbol = "gdext_rust_init" +compatibility_minimum = 4.1 + +[libraries] +linux.debug.x86_64 = "res://rust/target/debug/libkolonio.so" +linux.release.x86_64 = "res://rust/target/release/libkolonio.so" +windows.debug.x86_64 = "res://rust/target/debug/kolonio.dll" +windows.release.x86_64 = "res://rust/target/release/kolonio.dll" +macos.debug = "res://rust/target/debug/libkolonio.dylib" +macos.release = "res://rust/target/release/libkolonio.dylib" +macos.debug.arm64 = "res://rust/target/debug/libkolonio.dylib" +macos.release.arm64 = "res://rust/target/release/libkolonio.dylib" diff --git a/project.godot b/project.godot index 847b2b0..f8212a1 100644 --- a/project.godot +++ b/project.godot @@ -11,6 +11,7 @@ config_version=5 [application] config/name="Kolonio" +run/main_scene="res://scenes/main.tscn" config/features=PackedStringArray("4.1", "Forward Plus") config/icon="res://icon.svg" diff --git a/rust/Cargo.toml b/rust/Cargo.toml new file mode 100644 index 0000000..6bd4a8c --- /dev/null +++ b/rust/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "kolonio" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +godot = { git = "https://github.com/godot-rust/gdext", branch = "master" } diff --git a/rust/src/lib.rs b/rust/src/lib.rs new file mode 100755 index 0000000..42691a1 --- /dev/null +++ b/rust/src/lib.rs @@ -0,0 +1,81 @@ +use godot::prelude::*; + +struct MyExtension; + +#[gdextension] +unsafe impl ExtensionLibrary for MyExtension {} + +#[derive(GodotClass)] +#[class(base=Node2D)] +pub struct Main { + #[base] + base: Base, +} + +#[godot_api] +impl Node2DVirtual for Main { + fn init(base: Base) -> Self { + Main { + base + } + } +} + +#[derive(GodotClass)] +#[class(base=Node2D)] +pub struct WorldGrid { + pub width: usize, + pub height: usize, + pub cell_size: usize, + grid: Dictionary, + pub debug: bool, + + #[base] + base: Base, +} + +#[godot_api] +impl Node2DVirtual for WorldGrid { + fn init(base: Base) -> Self { + println!("Init start!"); + WorldGrid { + width: 16, + height: 16, + cell_size: 128, + grid: Dictionary::new(), + debug: true, + base, + } + } + + fn ready(&mut self) { + self.generate() + } +} + +#[godot_api] +impl WorldGrid { + pub fn generate(&mut self) { + for x in 0..self.width { + for y in 0..self.height{ + self.grid.insert(Vector2::new(x as f32, y as f32), 0); + if self.debug { + let mut rect = godot::engine::ReferenceRect::new_alloc(); + rect.set_position(self.grid_to_world(Vector2::new(x as f32, y as f32))); + rect.set_size(Vector2::new(self.cell_size as f32, self.cell_size as f32)); + rect.set_editor_only(false); + self.add_child(rect.share().upcast()); + let mut label = godot::engine::Label::new_alloc(); + label.set_position(self.grid_to_world(Vector2::new(x as f32, y as f32))); + label.set_text(format!("({}, {})", x, y).into()); + let label_scene = label.share().upcast(); + self.add_child(label_scene) + } + } + } + } + + fn grid_to_world(&self, pos: Vector2) -> Vector2 { + return Vector2::new(pos.x * (self.cell_size as f32), pos.y * (self.cell_size as f32)) + } +} diff --git a/scenes/main.tscn b/scenes/main.tscn new file mode 100644 index 0000000..bbe64bd --- /dev/null +++ b/scenes/main.tscn @@ -0,0 +1,5 @@ +[gd_scene format=3 uid="uid://dsjw61aafgqyb"] + +[node name="Main" type="Main"] + +[node name="WorldGrid" type="WorldGrid" parent="."]