#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Training: Spell Card 1-2"]
#Player["./resource/player/Marisa by plasmaflame22/Marisa_A.txt","./resource/player/Marisa by plasmaflame22/Marisa_B.txt","./resource/player/Marisa by plasmaflame22/Marisa_C.txt"]
#Background["./resource/bg/stg01.txt"]
#System["./system/system.txt"]

#include"./resource/cutin/cutin.txt"
#include"./system/effects.txt"
#include"./resource/[library] effects.txt"
#include"./resource/[library] boss.txt"
#include"./resource/[library] se.txt"
let bossObj = 0;
let bossX = 0;
let bossY = 0;
let attack = 0;
let damage = 0;
let csd = GetCurrentScriptDirectory;
let cutin = csd ~ "./resource/img/cutinstg02.png";
let face01 = csd ~ "./resource/img/face/enemy2/face01.png";
let dialog = csd ~ "./resource/img/stg01dialogue.png";
let dataFile = csd ~ "./resource/data/dialogue.dat";

@Initialize{
	SetCommonData("BOSS","Nemuno");
	InstallFont(csd ~ "./resource/blogger sans.ttf");

	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);

	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(bossObj);
	ObjMove_SetDestAtFrame(bossObj,500,-120,1);

	animateBossSakata;
	TEtama;
	LoadShot;
	MainTask;
	TEndScript;
	AddGraze(-GetGraze);
	
	SetPlayerLife(1);
	SetAutoDeleteObject(true);
	SetForbidPlayerSpell(true);
	CreateCommonDataArea("Dialogue");
	LoadCommonDataAreaA2("Dialogue",dataFile);
}

@Loading{
	LoadTexture(cutin);
	LoadTexture(face01);
	LoadTexture(dialog);}

@Event{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(500);
	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(120);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(11000000);
	}
}

@MainLoop{
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);
	if(damage==0){ObjEnemy_SetDamageRate(bossObj, 0, 0);}
	if(damage==1){ObjEnemy_SetDamageRate(bossObj, 10, 0);}
	ObjEnemy_SetIntersectionCircleToShot(bossObj, bossX, bossY, 32);
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj, bossX, bossY, 24);
	yield;
}

@Finalize{
}

task MainTask{
	TSnowfall;
	ObjMove_SetDestAtWeight(bossObj,192,120,6,4);
	
	loop(60){yield;}
	attack = 1;
	damage = 1;
	loop(30){yield;}
	let angle = 0;
	TSun;
	
	if(GetAreaCommonData("Dialogue","Nemuno",0) == 1){
		loop(50){
		if(Obj_IsDeleted(bossObj)){break;}
		SE(se_tan2);
		ascent(a in 0..1){
			ascent(i in -2..3){
				let s = CreateShotA1(bossX+90*cos(angle),bossY+90*sin(angle),3+a,0,71,15);
				ObjMove_SetAngle(s,GetAngleToPlayer(s)+i*30+rand(-2,2));
				ObjRender_SetScaleXYZ(s,1.2,1.2,0);
				let p = CreateShotA1(bossX+90*cos(-angle),bossY+90*sin(-angle),3+a,0,68,15);
				ObjMove_SetAngle(p,GetAngleToPlayer(p)+i*30+rand(-2,2));
				ObjRender_SetScaleXYZ(p,1.2,1.2,0);
			}
		}
		angle+=25;
		loop(8){yield;}
		}
	loop(360){yield;}
	}
	
	while(!Obj_IsDeleted(bossObj)){
		loop(20){
		if(Obj_IsDeleted(bossObj)){break;}
		SE(se_tan2);
		ascent(a in 0..1){
			ascent(i in -2..3){
				let s = CreateShotA1(bossX+90*cos(angle),bossY+90*sin(angle),3+a,0,71,15);
				ObjMove_SetAngle(s,GetAngleToPlayer(s)+i*30+rand(-2,2));
				ObjRender_SetScaleXYZ(s,1.2,1.2,0);
				let p = CreateShotA1(bossX+90*cos(-angle),bossY+90*sin(-angle),3+a,0,68,15);
				ObjMove_SetAngle(p,GetAngleToPlayer(p)+i*30+rand(-2,2));
				ObjRender_SetScaleXYZ(p,1.2,1.2,0);
			}
		}
		angle+=25;
		loop(8){yield;}
		}
	loop(60){yield;}}
	}

task TEffect{
	while(!Obj_IsDeleted(bossObj)){
		let angle = rand(0,360);
		loop(12){ TLaser(angle); angle+=30; }
		loop(10){yield;}
	}
	task TLaser(ang){
		let laser = CreateStraightLaserA1(bossX,bossY,ang,0,60,180,46,0);
		Obj_SetRenderPriorityI(laser,28);
		ObjShot_SetIntersectionEnable(laser,false);
		let ro = rand(-0.2,0.2);
		let length = rand(30,360);
		ascent(i in 0..30){
			ObjRender_SetAlpha(laser,150*sin(i*6));
			ObjStLaser_SetAngle(laser,ObjStLaser_GetAngle(laser)+ro);
			ObjLaser_SetLength(laser,length+30*sin(i*6));
			yield;
		}
	}
}	

task TSun{
	let angle = rand(0,360);
	loop(180){yield;}
	if(GetAreaCommonData("Dialogue","Nemuno",0) == 0){
	loop(760){yield;}}

	TEffect;
	while(!Obj_IsDeleted(bossObj)){
		SE(se_tan2);
		loop(20){
			let objShotA = CreateShotOA1(bossObj, 3, angle, 313, 20);
			ObjMove_AddPatternA1(objShotA, 30, 1.2, angle);
			task TTrail{
				while(!Obj_IsDeleted(objShotA)){
					let objShotB = CreateShotOA1(objShotA, 2, ObjMove_GetAngle(objShotA)+180, 813, 0);
					ObjRender_SetAlpha(objShotB, 20);
					ObjShot_FadeDelete(objShotB);
					yield;
				}
			}
			TTrail;
		angle+=360/20;
		}
		angle+=12.2;
		loop(60){yield;}
	}
}	
	
task TSnowfall{
	loop(200){yield;}

	if(GetAreaCommonData("Dialogue","Nemuno",0) == 0){
	loop(360){yield;}
		SetAreaCommonData("Dialogue","Nemuno",1);
		SaveCommonDataAreaA2("Dialogue",dataFile);
	damage = 0;
	let objF = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objF,face01);
	Obj_SetRenderPriorityI(objF,100);
	ObjSprite2D_SetSourceRect(objF,0,0,510,700);
	descent(move in 0..45){
		if(Obj_IsDeleted(bossObj)){break;}
		ObjRender_SetAlpha(objF, 255 - move*(255/45));
		ObjSprite2D_SetDestRect(objF,200+move, 80+move, 200+510/1.6+move, 80+700/1.6+move);
		yield;}
	
	let objD = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(objD,dialog);
	Obj_SetRenderPriorityI(objD,100);
	ObjSprite2D_SetSourceRect(objD,0,360,640,480);
	TManage();
	
	task TManage(){
		while(!Obj_IsDeleted(bossObj)){yield;}
		Obj_Delete(objF);Obj_Delete(objD);}
		
	ascent(move in 0..15){
		ObjSprite2D_SetDestRect(objD,30, 210+80/1.8*cos(move*6), 30+640/1.8, 210+120/1.8-40/1.8*cos(move*6));
		yield;}
	loop(120){yield;}
	descent(move in 0..15){
		ObjSprite2D_SetDestRect(objD,30, 210+80/1.8*cos(move*6), 30+640/1.8, 210+120/1.8-40/1.8*cos(move*6));
		yield;}
	ObjSprite2D_SetSourceRect(objD,0,480,640,600);
	ascent(move in 0..15){
		ObjSprite2D_SetDestRect(objD,30, 210+80/1.8*cos(move*6), 30+640/1.8, 210+120/1.8-40/1.8*cos(move*6));
		yield;}
	loop(120){yield;}
	descent(move in 0..15){
		ObjSprite2D_SetDestRect(objD,30, 210+80/1.8*cos(move*6), 30+640/1.8, 210+120/1.8-40/1.8*cos(move*6));
		yield;}
	Obj_Delete(objD);
	ascent(move in 0..45){
		ObjRender_SetAlpha(objF, 255 - move*(255/45));
		ObjSprite2D_SetDestRect(objF,200+move, 80+move, 200+510/1.6+move, 80+700/1.6+move);
		yield;}
	Obj_Delete(objF);
	damage = 1;
	}
	
	
	if(!Obj_IsDeleted(bossObj)){ SE(se_ch);
	MakeConcentrationA1(bossObj,"SNOW",255,255,255,80);}
	loop(80){yield;}
		if(!Obj_IsDeleted(bossObj)){SE(se_spell);}
		CutIn_Right(cutin,"Cursed Season \"Melting Snow of Sorrow\"");
	loop(120){yield;} 
		if(!Obj_IsDeleted(bossObj)){ SE(se_ch);
		MakeConcentrationA1(bossObj,"SNOW",255,255,255,45);}
	loop(60){yield;}
	
	let x = 20;
	let y = 20;
	let b = 0;
	
	while(!Obj_IsDeleted(bossObj)){
		let arg = round(rand(1,2));
		CreateSnowflake(x,y,arg);
		loop(10){yield;}
		SE(se_tan2);
		x+=15*sin(b)*3;
		b+=4.5*3;
	}
}

	task CreateSnowflake(x,y,arg){
		let objShot = CreateShotA1(x,y,3,90,384,0);
		if(arg == 1){
			ObjMove_AddPatternA3(objShot,30,1.5,NO_CHANGE,0,rand(-0.1,-0.01),0,NO_CHANGE);
		}
		else{
			ObjMove_AddPatternA3(objShot,30,rand(2,2.5),NO_CHANGE,0,rand(0.01,0.1),0,NO_CHANGE);
		}
		while(ObjMove_GetY(objShot) < GetStgFrameHeight-10){ yield; }
			ObjShot_SetIntersectionEnable(objShot,false);
			ObjMove_SetSpeed(objShot,0.3);
			SE(se_kira);
			ascent(i in 0..30){
				ObjRender_SetScaleXYZ(objShot,1-i/30,1-i/30,0);
				yield;
			}
		Obj_Delete(objShot);
	}