60 lines
1.4 KiB
Rust
60 lines
1.4 KiB
Rust
use gdnative::prelude::*;
|
|
|
|
mod saves;
|
|
mod world;
|
|
|
|
#[derive(NativeClass)]
|
|
#[inherit(Node)]
|
|
#[register_with(Self::register)]
|
|
pub struct StateServer {
|
|
world: world::World,
|
|
}
|
|
|
|
#[methods]
|
|
impl StateServer {
|
|
fn new(_owner: &Node) -> Self {
|
|
StateServer {
|
|
world: world::World::new(0, 0, 0),
|
|
}
|
|
}
|
|
|
|
#[export]
|
|
fn load_from_file(&self, _owner: &Node, file: GodotString) {
|
|
saves::load(&file.to_string());
|
|
}
|
|
|
|
#[export]
|
|
fn save_from_file(&self, _owner: &Node, file: GodotString) {
|
|
saves::save(&file.to_string());
|
|
}
|
|
|
|
#[export]
|
|
fn _ready(&self, _owner: &Node) {
|
|
//godot_print!("hello world")
|
|
}
|
|
|
|
#[export]
|
|
fn generate_world(&mut self, _owner: &Node, xsize: usize, ysize: usize, zsize: usize) {
|
|
self.world = world::World::new(xsize, ysize, zsize);
|
|
self.world.generate();
|
|
_owner.emit_signal("request_full_reload", &[]);
|
|
}
|
|
|
|
#[export]
|
|
fn get_tile_at(&mut self, _owner: &Node, x: usize, y: usize, z: usize) -> &str {
|
|
self.world.get_tile_at(x, y, z).kind_to_string()
|
|
}
|
|
}
|
|
|
|
// signals
|
|
impl StateServer {
|
|
fn register(builder: &ClassBuilder<StateServer>) {
|
|
builder
|
|
.signal("changed_tile")
|
|
.with_param("position", VariantType::Vector3)
|
|
.with_param("new_type", VariantType::GodotString)
|
|
.done();
|
|
builder.signal("request_full_reload").done();
|
|
}
|
|
}
|