extends Node2D

class_name GameWorld

enum Map { RUNTIME, ODYSSEY, EMPTY }

const playerRes := preload("res://Actors/Player/Player.tscn")

var map: GameMap = null
var player = null

onready var scene_manager = $"/root/SceneManager"

func load_map(map_name):
	match map_name:
		Map.RUNTIME:
			map = scene_manager.queue.get_resource("res://Scenes/Maps/runtime.tscn").instance()
		Map.ODYSSEY:
			map = scene_manager.queue.get_resource("res://Scenes/Maps/odyssey.tscn").instance()
		Map.EMPTY:
			map = preload("res://Scenes/Maps/empty.tscn").instance()
	add_child(map)

func spawn_player(peer: int, is_current_user: bool):
	var playerNode := playerRes.instance()
	playerNode.set_network_master(peer, true)
	playerNode.is_controlled = is_current_user
	var spawnpoints := map.get_pois(POI.POIType.SpawnPoint, POI.POIClass.Player)
	if spawnpoints.size() > 0:
		playerNode.transform.origin = (spawnpoints[0] as Node2D).transform.origin
	else:
		print("Map does not have Player spawnpoint POI! Spawning at origin (very bad)")
	$players.add_child(playerNode, true)
	if is_current_user:
		player = playerNode
		for tilemap in map.tilemaps:
			if tilemap is MapTiles:
				tilemap.set_occluder_origin(playerNode)
	return playerNode
