ive decided its fine if the state server is public

This commit is contained in:
zoe 2022-05-08 17:56:37 +02:00
parent 74e9d94977
commit 5b58f246a0
7 changed files with 35 additions and 29 deletions

Binary file not shown.

View file

@ -14,6 +14,10 @@ config/name="routes"
run/main_scene="res://world/World.tscn"
config/icon="res://icon.png"
[autoload]
StateServer="*res://native/StateServer.tscn"
[importer_defaults]
texture={

View file

@ -1,28 +1,13 @@
extends Node
onready var server = $StateServer
export var xsize = 256
export var ysize = 256
export var zsize = 6
func _ready():
server.generate_world(xsize, ysize, zsize)
print(server.get_tile_at(0,0,0))
StateServer.generate_world(xsize, ysize, zsize)
print(StateServer.get_tile_at(0,0,0))
func respawn_tilemaps(amount: int):
$Tilemaps.respawn_tilemaps(amount)
func get_tile_at(pos: Vector3) -> String:
return server.get_tile_at(int(pos.x), int(pos.y), int(pos.z))
func update_tiles(tile_positions: PoolVector3Array):
for tile in tile_positions:
$Tilemaps.tilemaps[tile.z].set_tile_graphics(Vector2(tile.x, tile.y),
get_tile_at(tile))
func _on_StateServer_request_init():
respawn_tilemaps(zsize - 1)
func _on_StateServer_changed_tiletypes(tile_positions: PoolVector3Array):
call_deferred("update_tiles", tile_positions)

View file

@ -3,11 +3,29 @@ extends Node
var Tilemap = preload("res://world/Tilemap.tscn")
var tilemaps = []
func respawn_tilemaps(amount: int):
func _ready():
StateServer.connect("changed_tiletypes", self, "_on_StateServer_changed_tiletypes")
StateServer.connect("request_init", self, "_on_StateServer_request_init")
func respawn_tilemaps():
tilemaps = []
for i in range(0, amount + 1):
for i in range(0, StateServer.get_world_size().z):
var map = Tilemap.instance()
map.position.y -= i * map.cell_size.y
map.z_index = i
add_child(map)
tilemaps.push_back(map)
func get_tile_at(pos: Vector3) -> String:
return StateServer.get_tile_at(int(pos.x), int(pos.y), int(pos.z))
func update_tiles(tile_positions: PoolVector3Array):
for tile in tile_positions:
tilemaps[tile.z].set_tile_graphics(Vector2(tile.x, tile.y),
get_tile_at(tile))
func _on_StateServer_request_init():
call_deferred("respawn_tilemaps")
func _on_StateServer_changed_tiletypes(tile_positions: PoolVector3Array):
call_deferred("update_tiles", tile_positions)

View file

@ -1,8 +1,7 @@
[gd_scene load_steps=5 format=2]
[gd_scene load_steps=4 format=2]
[ext_resource path="res://world/StateApi.gd" type="Script" id=1]
[ext_resource path="res://world/Cam.tscn" type="PackedScene" id=2]
[ext_resource path="res://native/StateServer.tscn" type="PackedScene" id=3]
[ext_resource path="res://world/Tilemaps.gd" type="Script" id=4]
[node name="World" type="Node"]
@ -10,10 +9,5 @@ script = ExtResource( 1 )
[node name="Worldcam" parent="." instance=ExtResource( 2 )]
[node name="StateServer" parent="." instance=ExtResource( 3 )]
[node name="Tilemaps" type="Node" parent="."]
script = ExtResource( 4 )
[connection signal="changed_tiletypes" from="StateServer" to="." method="_on_StateServer_changed_tiletypes"]
[connection signal="request_init" from="StateServer" to="." method="_on_StateServer_request_init"]

View file

@ -39,6 +39,11 @@ impl StateServer {
_owner.emit_signal("changed_tiletypes", &[Variant::new(&w)]);
}
#[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)
}
#[export]
fn get_tile_at(&self, _owner: &Node, x: usize, y: usize, z: usize) -> u16 {
self.world.get_tile_at(x, y, z)

View file

@ -3,9 +3,9 @@ use gdnative::prelude::*;
mod tiles;
pub struct World {
xsize: usize,
ysize: usize,
zsize: usize,
pub xsize: usize,
pub ysize: usize,
pub zsize: usize,
tiles: Vec<Vec<Vec<tiles::Tiletypes>>>,
}