#TouhouDanmakufu[Stage]
#ScriptVersion[3]
#ID["Main"]
#Title["LoCAA 12 - Sweet Dreams ~ Pristine Purity"]
#Text["pink blob girl"]
#System["./data/system/Awalys_System.dnh"]
#Player["./data/player/Sanae by Awalys/player.dnh"]
#Background["./data/background/Boss_BG.dnh"]

#include"./data/shot/CrispBright4/Shotconstants.dnh"
#include"./data/lib/lib_main.dnh"
#include"./data/lib/Boss_Anim.dnh"
#include"./data/lib/dialogue.dnh"

let dir = GetCurrentScriptDirectory();
let end = false;
let bgm = ObjSound_Create;
let bgm2 = ObjSound_Create;

@Event{
	alternative(GetEventType)
	case(EV_USER_STAGE) {
		let arg = GetEventArgument(0); // X and Y coordinates.
		CreateItemA1(ITEM_SPELL, arg[0], arg[1], 1500);
	}
	case(EV_USER_STAGE+1) {
		Play(ExpSE3, 40);
	}
	case(EV_USER_STAGE+2) {
		Play(SpellGetSE, 30);
	}
	case(EV_USER_STAGE+3) {
		Music(60);
	}
	case(EV_USER_STAGE+4) {
		let arg = GetEventArgument(0); // Spell score.
    	AddScore(arg);
	}
	/*case(EV_PAUSE_ENTER){
		ObjSound_SetVolumeRate(BGM, 10);
	}
    case(EV_PAUSE_LEAVE){
		ObjSound_SetVolumeRate(BGM, 100);
	}*/
}

@Initialize{
	TStage;
	
	SetCommonData("Main", GetOwnScriptID());
}

@MainLoop{
	if(GetPlayerState() == STATE_END){
		FinalizeStage;
	}
	yield;
}

task TStage{
	let pluralboss = dir ~ "./PLR1.dnh";
	let boss = LoadScriptInThread(pluralboss);

	SetForbidPlayerShot(true);
	SetForbidPlayerSpell(true);

	let objText = ObjText_Create(); 
	let objImg1 = ObjPrim_Create(OBJ_SPRITE_2D); // HAHA IT DOESN'T DESYNC REPLAYS ANYMOER
	let objImg2 = ObjPrim_Create(OBJ_SPRITE_2D);
	let objImg3 = ObjPrim_Create(OBJ_SPRITE_2D);
	let objBox = ObjPrim_Create(OBJ_SPRITE_2D);

	let objJunko = ObjEnemy_Create(OBJ_ENEMY);
	wait(60*1.5);
	ObjEnemy_Regist(objJunko);
	JunkoAnim(objJunko);
	ObjMove_SetDestAtWeight(objJunko, cx, 160, 15,6);
	wait(60*1.5);
	nTestCharge(ObjMove_GetX(objJunko), ObjMove_GetY(objJunko), 60*3, 255, 150, 50);
	Play(ChargeSE3, 30);
	DialogueBox(objBox);
	wait(60*2.5);
	DeploySheep;
	wait(60*4.5-65);
	dialogue(objText, 1, "Wha"); 
	Play(PlayerShotSE, 20);
	wait(65);

	Obj_SetVisible(objText, 0);
	dialogue(objText, 1, " "); 
	Obj_SetVisible(objBox, 0);

	Obj_Delete(objJunko);
	objBoss = ObjEnemy_Create(OBJ_ENEMY);
	ObjMove_SetPosition(objBoss, 0, 150);
	ObjEnemy_Regist(objBoss);
	BossRender;
	MagicCircle(objBoss);
	ObjMove_SetDestAtWeight(objBoss, cx, 160, 21, 3);
	wait(60*2);
	Obj_SetVisible(objText, 1);
	Obj_SetVisible(objBox, 1);
	DialogueBox(objBox);
	wait(60*2);

	dialogue(objText, 2, "Let's just get to the point.");
	loop(2){ character(objImg1, objImg2, 2, DoremySmug); }

	NotifyEvent(MyStageID, EV_USER_STAGE+3, NULL); // Music.
	MusicText;

	Play(PlayerShotSE, 20);
	wait(5);
	while(GetVirtualKeyState(VK_SHOT) != KEY_PUSH){yield;}
	Play(PlayerShotSE, 20);

	Obj_Delete(objText); // You HAVE to delete these manually.
	Obj_Delete(objImg1);
	Obj_Delete(objImg2);
	Obj_Delete(objBox);

	ObjMove_SetDestAtWeight(objBoss, cx, 120, 12, 6);

	wait(60);

	SetForbidPlayerShot(false);
	SetForbidPlayerSpell(false);
	StartScript(boss);
	Obj_Delete(objBoss);

	while(!IsCloseScript(boss)){yield;}

	STGClearScore(1000000000);

	FinalizeStage;
}

task FinalizeStage{
	wait(180);
	StopSound(BGM);
	if(GetScore > GetAreaCommonData("HighScore", "Score", 0)){ // Saves the high score.
		SetAreaCommonData("HighScore", "Score", GetScore);
		SaveCommonDataAreaA1("HighScore");
	}
	CloseStgScene();
}

task MusicText{
	let alpha = 0;

	let txt = ObjText_Create();
	ObjText_SetFontType(txt, "Andale Mono");
	ObjText_SetFontSize(txt, 12);
	ObjText_SetFontBold(txt, false);
	ObjText_SetFontColorTop(txt, 255, 255, 255);
	ObjText_SetFontColorBottom(txt, 28, 255, 28);
	ObjText_SetFontBorderType(txt, BORDER_FULL);
	ObjText_SetFontBorderColor(txt,0, 0, 0);
	ObjText_SetFontBorderWidth(txt, 1);
	Obj_SetRenderPriorityI(txt, 75);
	ObjRender_SetX(txt, 0);
	ObjRender_SetY(txt, GetStgFrameHeight-12);
	ObjText_SetHorizontalAlignment(txt, ALIGNMENT_LEFT);
	ObjText_SetMaxWidth(txt, 350);

	ObjText_SetText(txt, "Music: Checker Dance by Toby Fox");

	loop(30){
		ObjRender_SetAlpha(txt, alpha);
		alpha += 255/30;
		yield;
	}
	wait(60*2);
	loop(15){
		ObjRender_SetAlpha(txt, alpha);
		alpha -= 255/15;
		yield;
	}
	Obj_Delete(txt);
}


task JunkoAnim(obj){
	let idleFrame = 0;
	let moveFrame = 0;
	let attackFrame = 0;

	let Attack = false;

	let img = GetCurrentScriptDirectory ~ "./data/img/dot_junko.png";

	let dir;
	let speed; 

    ObjPrim_SetTexture(obj, img);
	ObjSprite2D_SetSourceRect(obj,0,0,128,128);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetScaleXYZ(obj, 1, 1, 0);
	ShaderTask_SetPointFilterMode(obj, img);
	
	while(!Obj_IsDeleted(obj)) {
	
		dir = ObjMove_GetAngle(obj);
		speed = ObjMove_GetSpeed(obj);
		
		if(speed == 0){ 
			ObjRender_SetAngleXYZ(obj,0,0,0);
			if(idleFrame < 15) { ObjSprite2D_SetSourceRect(obj,0,0,128,128); }
			if(idleFrame >= 15 && idleFrame < 30) { ObjSprite2D_SetSourceRect(obj,0,128,128,128*2); }
			if(idleFrame >= 30 && idleFrame < 45) { ObjSprite2D_SetSourceRect(obj,0,128*2,128,128*3); }
			if(idleFrame >= 45 && idleFrame < 60) { ObjSprite2D_SetSourceRect(obj,0,128,128,128*2); }
			if(idleFrame >= 60) { ObjSprite2D_SetSourceRect(obj,0,0,128,128); }
			moveFrame = 0;
		}
		else if(cos(dir) > 0){ 
			ObjRender_SetAngleXYZ(obj,0,0,0);
			if(moveFrame < 15) { ObjSprite2D_SetSourceRect(obj,128*2,128*2,128*3,128*3); }
			if(moveFrame >= 15 && moveFrame < 30) { ObjSprite2D_SetSourceRect(obj,128*2,128*2,128*3,128*3); }
			if(moveFrame >= 30 && moveFrame < 45) { ObjSprite2D_SetSourceRect(obj,128*2,128*3,128*3,128*4); }
			if(moveFrame >= 45) { ObjSprite2D_SetSourceRect(obj,128*2,128*3,128*3,128*4); }
		}
		else if(cos(dir) < 0){ 
			ObjRender_SetAngleXYZ(obj,0,0,0);
			if(moveFrame < 15) { ObjSprite2D_SetSourceRect(obj,128*2,0,128*3,128); }
			if(moveFrame >= 15 && moveFrame < 30) { ObjSprite2D_SetSourceRect(obj,128*2,0,128*3,128); }
			if(moveFrame >= 30 && moveFrame < 45) { ObjSprite2D_SetSourceRect(obj,128*2,128,128*3,128*2); }
			if(moveFrame >= 45) { ObjSprite2D_SetSourceRect(obj,128*2,128,128*3,128*2); }
		}
		if(Attack){ 
			ObjRender_SetAngleXYZ(obj,0,0,0);
			if(attackFrame < 15) { ObjSprite2D_SetSourceRect(obj,128*4,0,128*5,128); }
			if(attackFrame >= 15 && attackFrame < 30) { ObjSprite2D_SetSourceRect(obj,128*4,128,128*5,128*2); }
			if(attackFrame >= 30 && attackFrame < 45) { ObjSprite2D_SetSourceRect(obj,128*4,128*2,128*5,128*3); }
			if(attackFrame >= 45) { ObjSprite2D_SetSourceRect(obj,128*5,128*2,128*6,128*3); }
		}
		idleFrame=idleFrame+2;
		moveFrame=moveFrame+2;
		attackFrame=attackFrame+3;
		if(idleFrame > 60) { idleFrame = 0; }
		if(attackFrame > 90) { attackFrame = 45; }
		if(Attack == false) { attackFrame = 0; }
		if(moveFrame > 60) { moveFrame = 15; }
		
		yield;
	}
}

task DeploySheep{
	let BoomSE = GetCurrentScriptDirectory ~ "./data/sound/bom.wav";
	let BellSE = GetCurrentScriptDirectory ~ "./data/sound/bell04.wav";
	LoadSound(BoomSE); LoadSound(BellSE);

	let spd = 4;

	Play(BellSE, 40);
	let shspr = GetCurrentScriptDirectory ~ "./data/img/sheep.png";

	let sheep = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(sheep, shspr);
	ObjSprite2D_SetSourceRect(sheep, 0,0,64,64);
	ObjSprite2D_SetDestCenter(sheep);
	ObjRender_SetScaleXYZ(sheep, 0.75, 0.75, 0);
	ObjRender_SetY(sheep, 190);

	loop(90){
		ObjRender_SetX(sheep, ObjRender_GetX(sheep)+spd);
		spd -= 4/90;
		yield;
	}
	wait(60*2);
	Obj_Delete(sheep);
	PlaySE(BoomSE);
	let bbspr = GetCurrentScriptDirectory ~ "./data/img/bomb.png";

	let boom = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(boom, bbspr);
	ObjSprite2D_SetSourceRect(boom, 0,0,448,448);
	ObjSprite2D_SetDestCenter(boom);
	Obj_SetRenderPriorityI(boom, 75);
	ObjRender_SetScaleXYZ(boom, 1, 1, 0);
	ObjRender_SetX(boom, GetStgFrameWidth/2); ObjRender_SetY(boom, GetStgFrameHeight/2);
	wait(60);
	Obj_Delete(boom);
}