Finally got a grid loaded on the screen.
Signed-off-by: Louis Hollingworth <louis@hollingworth.nl>
This commit is contained in:
parent
7a4adf7e0d
commit
0b61e41639
|
@ -4,4 +4,7 @@
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
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
|
||||||
|
|
13
Rust.gdextension
Normal file
13
Rust.gdextension
Normal file
|
@ -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"
|
|
@ -11,6 +11,7 @@ config_version=5
|
||||||
[application]
|
[application]
|
||||||
|
|
||||||
config/name="Kolonio"
|
config/name="Kolonio"
|
||||||
|
run/main_scene="res://scenes/main.tscn"
|
||||||
config/features=PackedStringArray("4.1", "Forward Plus")
|
config/features=PackedStringArray("4.1", "Forward Plus")
|
||||||
config/icon="res://icon.svg"
|
config/icon="res://icon.svg"
|
||||||
|
|
||||||
|
|
11
rust/Cargo.toml
Normal file
11
rust/Cargo.toml
Normal file
|
@ -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" }
|
81
rust/src/lib.rs
Executable file
81
rust/src/lib.rs
Executable file
|
@ -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<Node2D>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[godot_api]
|
||||||
|
impl Node2DVirtual for Main {
|
||||||
|
fn init(base: Base<Node2D>) -> 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<Node2D>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[godot_api]
|
||||||
|
impl Node2DVirtual for WorldGrid {
|
||||||
|
fn init(base: Base<Node2D>) -> 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))
|
||||||
|
}
|
||||||
|
}
|
5
scenes/main.tscn
Normal file
5
scenes/main.tscn
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
[gd_scene format=3 uid="uid://dsjw61aafgqyb"]
|
||||||
|
|
||||||
|
[node name="Main" type="Main"]
|
||||||
|
|
||||||
|
[node name="WorldGrid" type="WorldGrid" parent="."]
|
Loading…
Reference in a new issue