codename-routes/godot/world/Tilemaps.gd
2022-05-13 20:44:22 +02:00

33 lines
1,010 B
GDScript

extends Node
var Tilemap = preload("res://world/Tilemap.tscn")
var tilemaps = []
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, 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))
for map in tilemaps:
map.update_dirty_quadrants()
func _on_StateServer_request_init():
call_deferred("respawn_tilemaps")
func _on_StateServer_changed_tiletypes(tile_positions: PoolVector3Array):
call_deferred("update_tiles", tile_positions)