#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Kotohime Spell 1"]
#Background["./NamelessPath.dnh"]
#Player["./../lib/Okina_player_by_Python/Okina Matara/Okina.dnh"]

#include "../lib/UNL_Shot/lib_ShotConstants.dnh"
#include "./../lib/includes/BossIncludes.dnh"
#include "./../lib/includes/EffectFunctions.dnh"

@Event{
	alternative(GetEventType())
		case(EV_REQUEST_LIFE){ SetScriptResult(1000); }
		case(EV_REQUEST_TIMER){ SetScriptResult(999); }
		case(EV_REQUEST_SPELL_SCORE){ SetScriptResult(10000000); }
		case(EV_GAIN_SPELL){ AddScore(ObjEnemyBossScene_GetInfo(objScene, INFO_SPELL_SCORE)); }
}

@Initialize{

	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(objEnemy, 360, 12.5, 25);
	PlayEnemyHitSound(objEnemy, 1);
	
	let Patchouli = GetCurrentScriptDirectory~"sprites/Kaguya.png";
	ObjPrim_SetTexture(objEnemy, Patchouli); 
	ObjSprite2D_SetSourceRect(objEnemy, 0, 0, 128, 128);
	ascent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 0, 128*x, 128, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_IDLE, 128, 128*x, 256, 128*x+128);}
	ascent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_LEFT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_LEFT, 256, 0, 256+128, 128);
	ascent(x in 2..4){ObjAnim_AddFrameA1(objEnemy, ANIM_MOVE_RIGHT, 256, 128*x, 256+128, 128*x+128);}
	ObjAnim_AddFrameA1(objEnemy, ANIM_MOVEEND_RIGHT, 256, 128*2, 256+128, 128*3);
	ascent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACK, 128*5, 128*x, 128*6, 128*x+128);}
	descent(x in 0..3){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*5, 128*x, 128*6, 128*x+128);}
	ObjAnim_SetSpeedA1(objEnemy, ANIM_IDLE, 12);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_LEFT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_MOVE_RIGHT, 6);
	ObjAnim_SetSpeedA1(objEnemy, ANIM_ATTACK, 10);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_RIGHT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_MOVE_LEFT, 1);
	ObjAnim_SetLoopPointA1(objEnemy, ANIM_ATTACK, 2);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	LoadShotdataSpecial2;
	SetAutoDeleteObject(true);
	ObjMove_SetPosition(objEnemy, GetStgFrameWidth/2, 120);
	delayedstart;
	End;
}

task End {
	while(ObjEnemy_GetInfo(objEnemy, INFO_LIFE) > 0) { yield; }
	BossExplosionQuick(objEnemy, 2, GetOwnScriptID);
	//CreateItems02(14, ITEM_POINT_S, GetEnemyX, GetEnemyY, 0, -50, 35, 35, 10000);
	SetShotAutoDeleteClip(32, 40, 32, 32);
	//NotifyEventAll(EV_SPELLBGEND, 2);
	//DeleteShotWave(1,GetX,GetY,20);
	//DeleteShotAll(TYPE_ALL,TYPE_ITEM);
	
	//PlaySoundEffect("end",60,GetCenterX);
	//Obj_Delete(objEnemy);
	//CloseScript(GetOwnScriptID);
}

task delayedstart{

	ObjEnemyBossScene_StartSpell(objScene);
	ObjEnemyBossScene_SetSpellTimer(objScene, 60*60);
	PlaySoundEffect("cardcall", 90, GetCenterX);
	
	let shokocut = GetCurrentScriptDirectory~"sprites/kotohimesprite.png";
	ObjCutin_SetSpellcardS4("Bomb Sign &quot;Bomb Blockade&quot;", shokocut, "NAZRIN", "255", "15", "255");
	ObjCutin_LaunchS3("NAZRIN", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 2);
	
	wait(120);
	attack1;
	SetShotAutoDeleteClip(32, 40, 32, 32);
}

task attack1{

	let xcoord = 0;
	let ycoord = 0;
	let dist = 80;
	let offset = dist/2;
	let switch = -1;
	let radius = 120;
	let time = 0;
	let xoffset = 0;
	let switch2 = 1;
	let color = 1;
	wait(30);
	PlaySoundEffect("charge2", 100, GetEnemyX);
	TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 0, 255, BLEND_ADD_ARGB, 25);	
	wait(30);
	BombSpam(8, 15, 90, -26.72525*switch);
	wait(180);
	switch*=-1;
	loop{
		wait(30);
		PlaySoundEffect("charge2", 100, GetEnemyX);
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 0, 255, BLEND_ADD_ARGB, 25);	
		wait(30);
		BombSpam(16, 15, 90, -26.72525*switch);
		wait(15);
		ObjAnim_SetAttackA1(objEnemy, true);
		wait(15);
		loop(8){
			PlaySoundEffect("shot2", 100, GetEnemyX);
			ascent(i in -8..9){
				ascent(j in -4..5){
					CreateDNHShotA1(GetEnemyX+(i*78.5)+(j*6)+xoffset, GetEnemyY-10-absolute(j*5.5), 2.5, 90, ARROWHEAD[PURPLE], 15);
				}
			}
			xoffset = [0, 78.5/2][color%2];
			xoffset+=rand(-10, 10);
			color++;
			wait(28);
		}
		ObjAnim_SetAttackA1(objEnemy, false);
		switch*=-1;
		wait(30);
		PlaySoundEffect("charge2", 100, GetEnemyX);
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 0, 255, BLEND_ADD_ARGB, 25);	
		wait(30);
		BombSpam(16, 15, 90, -26.72525*switch);
		wait(15);
		ObjAnim_SetAttackA1(objEnemy, true);
		wait(15);
		loop(7){
			PlaySoundEffect("shot3", 100, GetEnemyX);
			ascent(i in -1..2){
				ascent(j in -1..2){
					//if(j!=0){
						loop(23){
							if(j == 0){ CreateDNHShotA1(GetEnemyX, GetEnemyY, 2.25+(i/5.5), angle+(j*2.65), POINTER[PURPLE], 10); }
							else{ CreateShotA1(GetEnemyX, GetEnemyY, 2.25+(i/5.5), angle+(j*2.65), POINTER[PURPLE], 10); }
							angle+=360/23;
						}
					//}
				}
			}
			angle+=360/24/2;
			angle+=rand(-3, 3);
			wait(32);
		}
		ObjAnim_SetAttackA1(objEnemy, false);
		switch*=-1;
		wait(30);
		PlaySoundEffect("charge2", 100, GetEnemyX);
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 0, 255, BLEND_ADD_ARGB, 25);	
		wait(30);
		BombSpam(16, 15, 90, -26.72525*switch);
		wait(15);
		ObjAnim_SetAttackA1(objEnemy, true);
		wait(15);
		loop(33){
			PlaySoundEffect("shot4", 100, GetEnemyX);
			loop(5){
				let obj = CreateDNHShotA2(GetEnemyX, GetEnemyY, 5, angle, -0.04, 3, BUBBLE[PURPLE], 10);
				ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
				angle+=360/5;
			}
		angle+=(360/5/2.4)*switch2;
		wait(8);
		}
		switch2*=-1;
		ObjAnim_SetAttackA1(objEnemy, false);
		switch*=-1;
		wait(30);
	}
}

task BombSpam(amount, waittime, startangle, increment){
	let x = GetPlayerX;
	let y = GetPlayerY;
	let angle = startangle;
	let rad = 125;
	loop(amount){
		loop(2){
			CreateBomb(x+rad*cos(angle), y+rad*sin(angle), 0, 90, CIRCLE_L[RED], 0);
			angle+=360/2;
		}
		angle+=increment;
		wait(waittime);
	}
}

task CreateBomb(x, y, v, dir, graphic, delay){
		let ang = dir;
		let alpha = 0;
		let bulcolor = 0;
		let time = 0;
		let obj = CreateShotA1(x, y, v, dir, graphic, delay);
		ObjRender_SetAlpha(obj, alpha);
		ObjShot_SetSpellResist(obj, true);
		ObjShot_SetAutoDelete(obj, false);
		loop(15){
			alpha+=255/15;
			ObjRender_SetAlpha(obj, alpha);
			yield;
		}
		loop(45){
		time++;
			if(time%5==0){
				ObjShot_SetGraphic(obj, [CIRCLE_L[RED], CIRCLE_L[ORANGE]][bulcolor%2]);
				bulcolor++;
			}
		yield;
		}
		PlaySoundEffect("se_enep02", 75, GetCenterX);
		loop(16){
			let childBullet = CreateShotA2(ObjMove_GetX(obj), ObjMove_GetY(obj), 2, ang, -0.04, 0, CIRCLE_L[RED], 10);
			ManageShot(childBullet);
			ang+=360/16;
		}
		ObjShot_FadeDelete(obj);
	}
	
task ManageShot(objid){
	ObjRender_SetBlendType(objid, BLEND_ADD_ARGB);
	ObjShot_SetAutoDelete(objid, false);
			while(!Obj_IsDeleted(objid)){
				if(ObjMove_GetSpeed(objid)<=0){
					ObjShot_FadeDelete(objid);
				}
				yield;
			}
		}