81 lines
2.1 KiB
Rust
Executable file
81 lines
2.1 KiB
Rust
Executable file
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))
|
|
}
|
|
}
|