#include "game.h"
#include <godot_cpp/core/class_db.hpp>

using namespace godot;

void Game::_bind_methods()
{
	ClassDB::bind_method(D_METHOD("start","players"),&Game::start);
	ClassDB::bind_method(D_METHOD("kyaraspawn","data"),&Game::kyaraspawn);
}

Game::Game()
{
	spawn=memnew(MultiplayerSpawner);
	spawn->set_name("_spawn");
	spawn->set_spawn_function(callable_mp(this, &Game::kyaraspawn));
	spawn->set_spawn_path("..");
	add_child(spawn,false,INTERNAL_MODE_BACK);
}
Game::~Game(){}

void Game::start(const Dictionary &players, const int p_multiplayer_type)
{
	multiplayer_type=p_multiplayer_type;
	if (multiplayer_type&2) show(); // displaying
	if (!(multiplayer_type&1)) return; // hosting
	Array keys=players.keys();
	for (int i=0; i<keys.size(); i++)
	{
		static_cast<Dictionary>(players[keys[i]])["pid"]=keys[i];
		UtilityFunctions::print(get_multiplayer()->get_unique_id()," ",keys[i]," ",players[keys[i]]," ",static_cast<Dictionary>(players[keys[i]])["name"]);
		spawn->spawn(players[keys[i]]);
	}
}

Node* Game::kyaraspawn(const Variant &data)
{
	Dictionary dat=static_cast<Dictionary>(data);
	UtilityFunctions::print(get_multiplayer()->get_unique_id()," ",data);
	Character* kya=memnew(Character);
	kya->set_name(dat["name"]);
	kya->multiplayerowner=dat["pid"];
	kya->set_position(Vector2(50,50));
	kya->mummy=this;
	if (multiplayer_type&2) kya->set_texture(ResourceLoader::get_singleton()->load("res://design/char_top.svg")); // displaying
	return kya;
}