Finally got a grid loaded on the screen.

Signed-off-by: Louis Hollingworth <louis@hollingworth.nl>
This commit is contained in:
Louis Hollingworth 2023-07-08 19:37:47 +01:00
parent 7a4adf7e0d
commit 0b61e41639
Signed by: lucxjo
GPG key ID: A11415CB3DC7809B
6 changed files with 115 additions and 1 deletions

11
rust/Cargo.toml Normal file
View 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
View 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))
}
}