diff --git a/godot/native/libroutes_native.so b/godot/native/libroutes_native.so index 281b5e3..6ebb6aa 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 d271891..325f42c 100644 --- a/godot/world/StateApi.gd +++ b/godot/world/StateApi.gd @@ -2,12 +2,7 @@ extends Node export var xsize = 256 export var ysize = 256 -export var zsize = 6 +export var zsize = 16 func _ready(): StateServer.generate_world(xsize, ysize, zsize) - print(StateServer.get_tile_at(0,0,0)) - -func respawn_tilemaps(amount: int): - $Tilemaps.respawn_tilemaps(amount) - diff --git a/godot/world/Tilemaps.gd b/godot/world/Tilemaps.gd index d131844..3efcecd 100644 --- a/godot/world/Tilemaps.gd +++ b/godot/world/Tilemaps.gd @@ -24,6 +24,7 @@ func update_tiles(tile_positions: PoolVector3Array): if !StateServer.is_tile_hidden(int(tile.x), int(tile.y), int(tile.z)): tilemaps[tile.z].set_tile_graphics(Vector2(tile.x, tile.y), get_tile_at(tile)) + else: print("hidden tile hehe") func _on_StateServer_request_init(): call_deferred("respawn_tilemaps") diff --git a/routes-native/Cargo.toml b/routes-native/Cargo.toml index 631ac24..f483214 100644 --- a/routes-native/Cargo.toml +++ b/routes-native/Cargo.toml @@ -13,9 +13,7 @@ gdnative = {version = "0.10", features = ["async"]} strum = { version = "0.24", features = ["derive"] } strum_macros = "0.24" derive_builder = "0.11.2" -# tokio = {version = "1.18.0", features = ["sync"]} -# lazy_static = "1.4.0" -# pathfinding = "3.0.12" +rand = "0.8.5" toml = "0.5.9" [profile.dev.package."*"] diff --git a/routes-native/src/stateserver.rs b/routes-native/src/stateserver.rs index 5bc933c..b0e4de5 100644 --- a/routes-native/src/stateserver.rs +++ b/routes-native/src/stateserver.rs @@ -41,7 +41,11 @@ impl StateServer { #[export] fn get_world_size(&self, _ownser: &Node) -> Vector3 { - Vector3::new(self.world.xsize as f32, self.world.ysize as f32, self.world.zsize as f32) + Vector3::new( + self.world.xsize as f32, + self.world.ysize as f32, + self.world.zsize as f32, + ) } #[export] @@ -50,7 +54,7 @@ impl StateServer { } #[export] - fn is_tile_hidden(&self, _owner: &Node, x: usize, y: usize, z: usize) -> bool{ + fn is_tile_hidden(&self, _owner: &Node, x: usize, y: usize, z: usize) -> bool { self.world.is_tile_hidden(x, y, z) } } diff --git a/routes-native/src/stateserver/world.rs b/routes-native/src/stateserver/world.rs index 3f744e4..48477a3 100644 --- a/routes-native/src/stateserver/world.rs +++ b/routes-native/src/stateserver/world.rs @@ -1,4 +1,6 @@ use gdnative::prelude::*; +use rand::prelude::*; +use strum::{EnumCount, IntoEnumIterator}; mod tiles; @@ -10,25 +12,34 @@ pub struct World { } impl World { - pub fn get_tile_at(&self, x: usize, y: usize, z: usize) -> u16{ - //TODO: error handling, or maybe just do that in godot + pub fn get_tile_at(&self, x: usize, y: usize, z: usize) -> u16 { self.tiles[x][y][z] as u16 } - pub fn is_tile_hidden(&self, x: usize, y: usize, z: usize) -> bool{ - if x == self.xsize - 1 || y == self.ysize -1 || z == self.zsize - 1{ + pub fn is_tile_hidden(&self, x: usize, y: usize, z: usize) -> bool { + if x == self.xsize - 1 || y == self.ysize - 1 || z == self.zsize - 1 { + return false; + } + if !tiles::Tile::new(self.tiles[x + 1][y][z]).hide_top_left + || !tiles::Tile::new(self.tiles[x][y + 1][z]).hide_top_right + || !tiles::Tile::new(self.tiles[x][y][z + 1]).hide_below + { return false; } true } - pub fn generate(&mut self) -> Vector3Array{ + pub fn generate(&mut self) -> Vector3Array { + let mut rng = thread_rng(); self.tiles = get_vec3(self.xsize, self.ysize, self.zsize); let mut ret: Vector3Array = Vector3Array::new(); for x in 0..self.xsize { for y in 0..self.ysize { for z in 0..self.zsize { ret.push(Vector3::new(x as f32, y as f32, z as f32)); - self.tiles[x][y][z] = tiles::Tiletypes::Grass; + self.tiles[x][y][z] = tiles::Tiletypes::from_repr( + rng.gen_range(0..tiles::Tiletypes::COUNT) as u8, + ) + .unwrap(); } } } diff --git a/routes-native/src/stateserver/world/tiles.rs b/routes-native/src/stateserver/world/tiles.rs index 5a299d7..14bc517 100644 --- a/routes-native/src/stateserver/world/tiles.rs +++ b/routes-native/src/stateserver/world/tiles.rs @@ -1,7 +1,7 @@ use derive_builder::Builder; -use strum::{AsRefStr, EnumDiscriminants, EnumIter, FromRepr}; +use strum::{AsRefStr, EnumCount, EnumDiscriminants, EnumIter, FromRepr}; -#[derive(AsRefStr, EnumIter, FromRepr, EnumDiscriminants, Clone, Copy)] +#[derive(AsRefStr, EnumIter, FromRepr, EnumDiscriminants, Clone, Copy, PartialEq, EnumCount)] #[repr(u8)] pub enum Tiletypes { Air, @@ -24,16 +24,30 @@ pub struct Tile { #[builder(default = "vec![]")] can_be_next_to: Vec, #[builder(default = "true")] - hide_top_left: bool, + pub hide_top_left: bool, #[builder(default = "true")] - hide_top_right: bool, + pub hide_top_right: bool, #[builder(default = "true")] - hide_below: bool, + pub hide_below: bool, } impl Tile { pub fn new(kind: Tiletypes) -> Tile { - TileBuilder::default().kind(kind).is_support(false).build().unwrap() + let tile: Tile; + match kind { + Tiletypes::Air => { + tile = TileBuilder::default() + .kind(kind) + .is_support(false) + .hide_below(false) + .hide_top_right(false) + .hide_top_left(false) + .build() + .unwrap() + } + _ => tile = TileBuilder::default().kind(kind).build().unwrap(), + }; + tile } pub fn kind_to_string(&self) -> String { self.kind.as_ref().to_string()