#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Spell6"]
#Text["Woop"]


let objBoss;

let BossX;
let BossY;


#include "./Functions/Index.txt"
#include "./Functions/Dialogue.txt"


@Initialize {
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);
	renderkanako(objBoss);
	BossShotSound(objBoss, 100, 600);
	KanakoAura(objBoss, false);
	Sticks(objBoss, false);
	BottomEnemyMarker(objBoss, objBoss);
	DrawName("Kanako Yasaka", [20, 20, 200], 11);
	ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2, 10, 5);
	Maintask;
	end;
} 

@Event { 
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(5000);
     	}
	case(EV_REQUEST_TIMER){
		SetScriptResult(70);
	}
	case(EV_REQUEST_SPELL_SCORE){
		SetScriptResult(1000000);
	}
} 

@MainLoop {
	BossX = ObjMove_GetX(objBoss);
	BossY = ObjMove_GetY(objBoss);
	
	if(invisible==false){
	ObjEnemy_SetIntersectionCircleToShot(objBoss,BossX,BossY,21);
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss,BossX,BossY,16);
	}
	
	if(GetPlayerInvincibilityFrame<=0){invisible=false;}
	else{invisible=true;}
	
	yield;
} 

@Finalize {

} 


task Maintask{
wait(90);
SpellCard("Stick Sign &quot;big stick RINGS&quot;");
let point = CreateShotA1(BossX, BossY, 0, GetAngleToPlayer(objBoss), 0, 0);
Obj_SetVisible(point, false);
ObjShot_SetIntersectionEnable(point, 0);
ObjShot_SetAutoDelete(point, 0);

ascent(i in 0..8){
let obj = KillyStick(BossX+50*cos(i*360/8), BossY+50*sin(i*360/8), ObjMove_GetAngle(point));
Rotate(obj, point, i);
Shoot(obj, i);
wait(45);
}

wait(20);
Movement;
Tackle(point);
}

let StickCharge = false;
let ShotYes = false;

task Shoot(obj, i){
let count = 0;
loop{
let shotcount = 0;
	loop{
	if(StickCharge==true){break;}
	if(ShotYes==true){shotcount++;}
	if(shotcount==35){
		ascent(j in 0..12){
		CreateShotA2(ObjMove_GetX(obj)-85*cos(ObjMove_GetAngle(obj)), ObjMove_GetY(obj)-85*sin(ObjMove_GetAngle(obj)), 0, ObjMove_GetAngle(obj)+(j*360/12), 0.01, 5, PELLET_RED, 10);
		}
		shotcount=0;
	}
	yield;
	}
	if(PhaseStart==true){
		loop{
		if(StickCharge==false){break;}
		CreateShotA2(ObjMove_GetX(obj)-85*cos(ObjMove_GetAngle(obj)), ObjMove_GetY(obj)-85*sin(ObjMove_GetAngle(obj)), 0.2, ObjMove_GetAngle(obj)+(i*360/8)+count, 0.01, 5, PELLET_GRAY, 10);
		count+=12;
		wait(4);
		}
	}
	while(!Obj_IsDeleted(obj)){
	if(StickCharge==false){break;}
	CreateShotA2(ObjMove_GetX(obj)-85*cos(ObjMove_GetAngle(obj)), ObjMove_GetY(obj)-85*sin(ObjMove_GetAngle(obj)), 0.2, ObjMove_GetAngle(obj)-90+count, 0.01, 5, OVAL_GRAY, 10);
	count+=17;
	wait(6);
	}
	ShotYes=true;
}
}

task Rotate(obj, point, i){
let offset = i;
let counter = 0;
	while(!Obj_IsDeleted(obj)){
	loop{
	ObjMove_SetPosition(obj, ObjMove_GetX(point)+75*cos(offset), ObjMove_GetY(point)+50*sin(offset));
	Obj_SetRenderPriorityI(obj, 50+5*sin(offset));
	ObjMove_SetAngle(obj, ObjMove_GetAngle(point));
	counter++;
	offset++;
	yield;
	if(StickCharge==true){break;}
	}
	if(PhaseStart==false){
		loop(310){
		ObjMove_SetPosition(obj, ObjMove_GetX(point)+75*cos(offset), ObjMove_GetY(point)+50*sin(offset));
		Obj_SetRenderPriorityI(obj, 50+5*sin(offset));
		counter++;
		offset++;
		yield;
		}
	}
		else{
		let radius = 75;
		let boop = 0;
			loop{
			ObjMove_SetPosition(obj, ObjMove_GetX(point)+radius*cos(offset), ObjMove_GetY(point)+radius*sin(offset));
			Obj_SetRenderPriorityI(obj, 50+5*sin(offset));
			ObjMove_SetAngle(obj, ObjMove_GetAngle(point)+180);
			radius+=3*sin(boop);
			boop++;
			counter++;
			offset+=0.4;
			if(PhaseStart==false){break;}
			yield;
			}
			yield;
		}
	}
}

let phase = 0;
let PhaseStart = false;
task Movement{
ShotYes=true;
let count = 0;
let count2 = 180;
	loop{
		loop{
		ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2+100*cos(count2), GetStgFrameHeight/3+50*sin(count2), 5, 2);
		count++;
		count2+=1.4*sin(count);
		if(StickCharge==true){break;}
		yield;
		}
		phase++;
		wait(310);
		if(phase==3){
		ShotYes=false;
		ObjMove_SetDestAtWeight(objBoss, GetStgFrameWidth/2, GetStgFrameHeight/2, 5, 2);
		PhaseStart = true;
		wait(360*1.5);
		PhaseStart = false;
		}
	}
}

task Tackle(obj){
	while(!Obj_IsDeleted(obj)){
		loop(210){
		ObjMove_SetPosition(obj, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss));
		ObjMove_SetAngle(obj, GetAngleToPlayer(obj));
		yield;
		}
		StickCharge=true;
		if(PhaseStart==false){
			ObjMove_SetAngle(obj, GetAngleToPlayer(obj)+180);
			ObjMove_SetAcceleration(obj, 0.04);
			ObjMove_SetSpeed(obj, -5);
			ObjMove_SetMaxSpeed(obj, 2);
			wait(310);
		}
		else{
		ObjMove_SetAngle(obj, GetAngleToPlayer(obj)+180);
			loop{
			ObjMove_SetPosition(obj, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss));
			if(PhaseStart==false){break;}
			yield;
			}	
		}
		StickCharge=false;
	yield;
	}
}

function SpellCard(name){
	let Cutin = GetCurrentScriptDirectory ~ "Functions/img/Emotions/Kan1.png";
	ObjCutin_SetSpellcardS3(name, 25, 25, 255);
	ObjCutin_LaunchS3("KANAKO", Cutin, "Normal");
	TCallBg;
	bspell = true;
	let objScene = GetEnemyBossSceneObjectID();
	ObjEnemyBossScene_StartSpell(objScene);
	wait(210);
}


task end{
    while(ObjEnemy_GetInfo(objBoss, INFO_LIFE) > 0){yield;}
    Obj_Delete(objBoss);
	wait(1);
	SetCommonData("deathsound",1);
    DeleteShotAll(TYPE_ALL, TYPE_ITEM);
    SetAutoDeleteObject(true);
    CloseScript(GetOwnScriptID());
} 
