diff --git a/godot/native/libroutes_native.so b/godot/native/libroutes_native.so index d080e0e..84e0fc9 100755 Binary files a/godot/native/libroutes_native.so and b/godot/native/libroutes_native.so differ diff --git a/godot/world/StateApi.gd b/godot/world/StateApi.gd index 46e53f8..c6a8462 100644 --- a/godot/world/StateApi.gd +++ b/godot/world/StateApi.gd @@ -1,5 +1,23 @@ extends Node + onready var server = $StateServer +export var xsize = 512 +export var ysize = 512 +export var zsize = 124 + +var Tilemap = preload("res://world/Tilemap.tscn") +var tilemaps = [] + func _ready(): - server.generate_world(1024,1024,512) + server.generate_world(xsize, ysize, zsize) + print(server.get_tile_at(xsize -1,ysize -1,zsize-1)) + +func _on_StateServer_request_full_reload(): + respawn_tilemaps(zsize - 1) + +func respawn_tilemaps(amount): + tilemaps = [] + for tile in range(0, amount + 1): + add_child(Tilemap.instance()) + tilemaps.push_back(tile) diff --git a/godot/world/Tilemap.tscn b/godot/world/Tilemap.tscn new file mode 100644 index 0000000..b14993a --- /dev/null +++ b/godot/world/Tilemap.tscn @@ -0,0 +1,10 @@ +[gd_scene load_steps=2 format=2] + +[ext_resource path="res://world/terrain/terrain.tres" type="TileSet" id=1] + +[node name="Tilemap" type="TileMap"] +position = Vector2( -2, 0 ) +mode = 1 +tile_set = ExtResource( 1 ) +cell_size = Vector2( 32, 16 ) +format = 1 diff --git a/godot/world/Tilemaps.gd b/godot/world/Tilemaps.gd new file mode 100644 index 0000000..487b284 --- /dev/null +++ b/godot/world/Tilemaps.gd @@ -0,0 +1,2 @@ +extends Node + diff --git a/godot/world/state_server.gd b/godot/world/state_server.gd deleted file mode 100644 index bf4a50c..0000000 --- a/godot/world/state_server.gd +++ /dev/null @@ -1,16 +0,0 @@ -extends Node - - -# Declare member variables here. Examples: -# var a = 2 -# var b = "text" - - -# Called when the node enters the scene tree for the first time. -func _ready(): - pass - - -# Called every frame. 'delta' is the elapsed time since the previous frame. -#func _process(delta): -# pass diff --git a/godot/world/terrain.gd b/godot/world/terrain.gd deleted file mode 100644 index 4a4af82..0000000 --- a/godot/world/terrain.gd +++ /dev/null @@ -1,13 +0,0 @@ -extends TileMap - -var width = 256 -var height = 256 - -# Called when the node enters the scene tree for the first time. -func _ready(): - fill() - -func fill(): - for x in width: - for y in height: - set_cell(x,y,0,false,false,false,Vector2(1,0)) diff --git a/godot/world/world.tscn b/godot/world/world.tscn index d09a08b..d2ddb26 100644 --- a/godot/world/world.tscn +++ b/godot/world/world.tscn @@ -1,25 +1,19 @@ -[gd_scene load_steps=5 format=2] +[gd_scene load_steps=4 format=2] -[ext_resource path="res://world/terrain/terrain.tres" type="TileSet" id=1] [ext_resource path="res://native/StateServer.tscn" type="PackedScene" id=2] [ext_resource path="res://world/worldcam.gd" type="Script" id=3] [ext_resource path="res://world/StateApi.gd" type="Script" id=5] -[node name="World" type="Node2D"] - -[node name="StateApi" type="Node" parent="."] -script = ExtResource( 5 ) - -[node name="StateServer" parent="StateApi" instance=ExtResource( 2 )] - -[node name="TileMap" type="TileMap" parent="."] -position = Vector2( -2, 0 ) -mode = 1 -tile_set = ExtResource( 1 ) -cell_size = Vector2( 32, 16 ) -format = 1 +[node name="Main" type="Node2D"] [node name="Worldcam" type="Camera2D" parent="."] current = true zoom = Vector2( 0.4, 0.4 ) script = ExtResource( 3 ) + +[node name="World" type="Node" parent="."] +script = ExtResource( 5 ) + +[node name="StateServer" parent="World" instance=ExtResource( 2 )] + +[connection signal="request_full_reload" from="World/StateServer" to="World" method="_on_StateServer_request_full_reload"] diff --git a/routes-native/Cargo.toml b/routes-native/Cargo.toml index fac4bf6..b5ae2a9 100644 --- a/routes-native/Cargo.toml +++ b/routes-native/Cargo.toml @@ -9,10 +9,11 @@ edition = "2021" crate-type = ["cdylib"] [dependencies] -gdnative = "0.10" -tokio = {version = "1.18.0", features = ["sync"]} -lazy_static = "1.4.0" -pathfinding = "3.0.12" +gdnative = {version = "0.10", features = ["async"]} +strum = { version = "0.24", features = ["derive"] } +# tokio = {version = "1.18.0", features = ["sync"]} +# lazy_static = "1.4.0" +# pathfinding = "3.0.12" toml = "0.5.9" [profile.dev.package."*"] diff --git a/routes-native/src/stateserver.rs b/routes-native/src/stateserver.rs index d8e0d19..321b7c9 100644 --- a/routes-native/src/stateserver.rs +++ b/routes-native/src/stateserver.rs @@ -1,26 +1,21 @@ use gdnative::prelude::*; -use lazy_static::lazy_static; -use tokio::sync::RwLock; mod saves; mod world; -lazy_static! { - static ref WORLD: RwLock>>> = RwLock::new(vec![vec![vec![]]]); -} - #[derive(NativeClass)] #[inherit(Node)] #[register_with(Self::register)] pub struct StateServer { - #[property] - a: u64, + world: world::World, } #[methods] impl StateServer { fn new(_owner: &Node) -> Self { - StateServer { a: 14 } + StateServer { + world: world::World::new(0, 0, 0), + } } #[export] @@ -39,9 +34,15 @@ impl StateServer { } #[export] - fn generate_world(&self, _owner: &Node, xsize: usize, ysize: usize, zsize: usize) { - let mut new_world = world::World::new(xsize, ysize, zsize); - new_world.generate(); + 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() } } @@ -53,5 +54,6 @@ impl StateServer { .with_param("position", VariantType::Vector3) .with_param("new_type", VariantType::GodotString) .done(); + builder.signal("request_full_reload").done(); } } diff --git a/routes-native/src/stateserver/world.rs b/routes-native/src/stateserver/world.rs index 31fe30c..67cf83a 100644 --- a/routes-native/src/stateserver/world.rs +++ b/routes-native/src/stateserver/world.rs @@ -1,5 +1,3 @@ -use gdnative::prelude::*; - mod tiles; pub struct World { @@ -10,6 +8,9 @@ pub struct World { } impl World { + pub fn get_tile_at(&self, x: usize, y: usize, z: usize) -> &tiles::Tile{ + &self.tiles[x][y][z] + } pub fn generate(&mut self) { self.tiles = get_vec3(self.xsize, self.ysize, self.zsize); for x in 0..self.xsize { diff --git a/routes-native/src/stateserver/world/tiles.rs b/routes-native/src/stateserver/world/tiles.rs index 913bfcd..76dc2fc 100644 --- a/routes-native/src/stateserver/world/tiles.rs +++ b/routes-native/src/stateserver/world/tiles.rs @@ -1,4 +1,6 @@ -#[derive(Debug, Clone)] +use strum::AsRefStr; + +#[derive(Debug, Clone, AsRefStr)] pub enum Tiletypes { Air, Water, @@ -16,4 +18,7 @@ impl Tile { pub fn new(kind: Tiletypes) -> Tile { Tile { kind } } + pub fn kind_to_string(&self) -> &str { + self.kind.as_ref() + } }