#include "SquirrelTownStage.h"
#include "SquirrelClassEx.h"

extern SquirrelClassEx* TObject;
extern Cursor* Mouse_Object;

extern std::map<std::uint64_t, int>UI_HOVER_ZORDER;
extern std::map<std::uint64_t, UiFrameWork*>UI_HOVER_OBJECT;

void SquirrelTownStage::PushBaseUi()
{
	//创建鼠标UI图层
	LayerActorPtr MouseUiLayer = new LayerActor();
	MouseUiLayer->SetName("Scene_Mouse_UI_Layer");
	//设置鼠标图层层级最高
	MouseUiLayer->SetZOrder(100000);
	SquirrelClassEx::PushLayerActorPtrToMap(MouseUiLayer);
	this->AddChild(MouseUiLayer);

	//创建默认图层
	LayerActorPtr DefaultLayer = new LayerActor();
	DefaultLayer->SetName(this->GetName() + "_Default_Layer");
	SquirrelClassEx::PushLayerActorPtrToMap(DefaultLayer);
	this->AddChild(DefaultLayer);
}

void SquirrelTownStage::OnEnter()
{
	//将鼠标对象从父场景移除
	Mouse_Object->RemoveFromParent();

	ActorPtr MouseUiLayer = this->GetChild("Scene_Mouse_UI_Layer");

	//将鼠标对象设置为当前场景子对象
	MouseUiLayer->AddChild(Mouse_Object);

	//场景进入
	//TObject->RunSceneScript(this->GetName() + "_Enter", this);
	//全局进入
	TObject->RunSceneScript("Game_Enter", this);
}

void SquirrelTownStage::OnExit()
{
	//清空UI类Map
	UI_HOVER_ZORDER.clear();
	UI_HOVER_OBJECT.clear();

	//将鼠标对象从父场景移除
	Mouse_Object->RemoveFromParent();
	//全局退出
	TObject->RunSceneScript("Game_Exit", this);
}

void SquirrelTownStage::OnUpdate(Duration dt)
{
	if (Director::GetInstance().GetCurrentStage()->GetHashName() == this->GetHashName() && this->GetName().find("LoadingGame") == std::string::npos) {
		//全局Proc
		TObject->RunSceneScript("Game_Proc", this);
		//判断是否需要重载
		if (FILE* file = fopen("Yosin_Game_Reloading.Sign", "r")) {
			SquirrelClassEx::ReloadingScript();
			//OnExit();
			//OnEnter();
			fclose(file);
			remove("Yosin_Game_Reloading.Sign");
		}
	}
}
