#TouhouDanmakufu
#ScriptVersion[3]
#System["./../tricky_system/TrickySystem.dnh"]
#Title["Shokora Spell 2"]
#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{
	LoadTexture(GetCurrentScriptDirectory~"sprites/white.png");
	objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objEnemy);
	Invuln(objEnemy, 300, 10, 25);
	PlayEnemyHitSound(objEnemy, 1);
	
	let Patchouli = GetCurrentScriptDirectory~"sprites/Patchouli.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..2){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACK, 128*4, 128*x, 128*5, 128*x+128);}
	descent(x in 0..2){ObjAnim_AddFrameA1(objEnemy, ANIM_ATTACKEND, 128*4, 128*x, 128*5, 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, 1);
	ObjAnim_StartAnimationA2(objEnemy, 0, 0);
	ObjSprite2D_SetDestCenter(objEnemy); 
	
	SetAutoDeleteObject(true);
	delayedstart;
	End;
}

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

task delayedstart{
	
	ObjMove_SetDestAtFrame(objEnemy, GetCenterX, 120, 60);
	wait(60);
	
	ObjEnemyBossScene_StartSpell(objScene);
	ObjEnemyBossScene_SetSpellTimer(objScene, 60*59.75);
	PlaySoundEffect("cardcall", 90, GetCenterX);
	
	let lbcut = GetCurrentScriptDirectory~"sprites/shokorasprite.png";
	ObjCutin_SetSpellcardS4("Shot Enhancement &quot;Laminate&quot;", lbcut, "NAZRIN", "255", "255", "255");
	ObjCutin_LaunchS3("NAZRIN", "0", "Hard");
	NotifyEventAll(EV_SPELLBG, 1);
	
	wait(120);
	attack1;
	SetShotAutoDeleteClip(125, 125, 125, 50);
}

task attack1{	
	
	let radmult = 5.75;
	switch = -1;
	let color = 0;
	let set = 0;
	let radius = 49;
	let time = 0;
	let type = -1;
	let xcoord = 0;
	let ycoord = 0;
	angle+=360/7/2;
	angle+=360/8/2;
	angle2 = 90;
	wait(30);

	loop{
		PlaySoundEffect("charge2", 100, GetEnemyX);
		TCharge01(GetEnemyX, GetEnemyY, 3, 45, 255, 0, 255, BLEND_ADD_ARGB, 25);
		ObjAnim_SetAttackA1(objEnemy, true);
		wait(45);
		loop(7){
			PlaySoundEffect("shot3", 90, GetCenterX);
			BounceThing(GetEnemyX, GetEnemyY, 2.525, angle, 19, 12, switch, 6, 600, 90, color);
			angle+=360/8;
			wait(3);
			color++;
		}
		angle-=360/8/2;
		wait(120);
		ObjAnim_SetAttackA1(objEnemy, false);
		wait(750-120);
	}
}

task BounceThing(x,y,spd,angle,radius,amt,dir,rotspd,starttranstime,transtime,color){
	let angle2 = angle;
	let sillyangle = angle2;
	let angleacc = rotspd*dir;
	let initspd = spd;
	let bounces = 0;
	let timer = 0;
	let obj = CreateDNHShotA1(x, y, spd, angle, [CIRCLE_L[RED], CIRCLE_L[GREEN], CIRCLE_L[BLUE]][color%3], 15);
	ObjShot_SetSpellResist(obj, true);
	ObjRender_SetAlpha(obj, 0);
	DummyOrbitar;
	loop(amt){
		Orbitars(angle2);
		angle2+=360/amt;
	}

	while(!Obj_IsDeleted(obj)){
		sillyangle+=angleacc;
				if(trunc(ObjMove_GetX(obj))>GetClipMaxX){
						PlaySoundEffect("shot", 75, ObjMove_GetX(obj));
						loop(amt){
							CreateShotA2(ObjMove_GetX(obj)+radius*cos(sillyangle), ObjMove_GetY(obj)+radius*sin(sillyangle), 0, sillyangle, 0.0125, 1.55, [AMULET[RED], AMULET[GREEN], AMULET[BLUE]][color%3], 0);
							sillyangle+=360/amt;
						}
                        ObjMove_SetX(obj,GetClipMaxX-ObjMove_GetX(obj)+GetClipMaxX);
                        ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));
                        bounces++;
                }
                if(trunc(ObjMove_GetX(obj))<0){
					PlaySoundEffect("shot", 75, ObjMove_GetX(obj));
						loop(amt){
							CreateShotA2(ObjMove_GetX(obj)+radius*cos(sillyangle), ObjMove_GetY(obj)+radius*sin(sillyangle), 0, sillyangle, 0.0125, 1.55, [AMULET[RED], AMULET[GREEN], AMULET[BLUE]][color%3], 0);
							sillyangle+=360/amt;
						}
                        ObjMove_SetX(obj,0-ObjMove_GetX(obj)+0);
                        ObjMove_SetAngle(obj,180 - ObjMove_GetAngle(obj));  
                        bounces++;
                }
               
				if(trunc(ObjMove_GetY(obj))>GetClipMaxY){
					PlaySoundEffect("shot", 75, ObjMove_GetX(obj));
						loop(amt){
							CreateShotA2(ObjMove_GetX(obj)+radius*cos(sillyangle), ObjMove_GetY(obj)+radius*sin(sillyangle), 0, sillyangle, 0.0125, 1.55, [AMULET[RED], AMULET[GREEN], AMULET[BLUE]][color%3], 0);
							sillyangle+=360/amt;
						}
                        ObjMove_SetY(obj,GetClipMaxY-ObjMove_GetY(obj)+GetClipMaxY);
                        ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
						bounces++;
				}
				if(trunc(ObjMove_GetY(obj))<0){
					PlaySoundEffect("shot", 75, ObjMove_GetX(obj));
						loop(amt){
							CreateShotA2(ObjMove_GetX(obj)+radius*cos(sillyangle), ObjMove_GetY(obj)+radius*sin(sillyangle), 0, sillyangle, 0.0125, 1.55, [AMULET[RED], AMULET[GREEN], AMULET[BLUE]][color%3], 0);
							sillyangle+=360/amt;
						}
                        ObjMove_SetY(obj,0-ObjMove_GetY(obj)+0);
                        ObjMove_SetAngle(obj,360 - ObjMove_GetAngle(obj));
						bounces++;
				}
		
		if(timer>=starttranstime){
			angleacc-=(rotspd*dir)/transtime;
			initspd-=spd/transtime;
			ObjMove_SetSpeed(obj, initspd);
		}
		if(timer>=transtime+starttranstime){
			Obj_Delete(obj);
		}
		timer++;
		yield;
	}
	
	task Orbitars(angle){
		let obj2 = CreateDNHShotA1(ObjMove_GetX(obj)+radius*cos(angle), ObjMove_GetY(obj)+radius*sin(angle), 0, angle, [AMULET[RED], AMULET[GREEN], AMULET[BLUE]][color%3], 15);
		ObjShot_SetSpellResist(obj2, true);
		let rotangle = angle2;
		let shotangle = 0;
		while(!Obj_IsDeleted(obj)){
			rotangle+=angleacc;
			shotangle = AngleBetweenPoints(ObjMove_GetX(obj2), ObjMove_GetY(obj2), ObjMove_GetX(obj), ObjMove_GetY(obj))-180;
			ObjMove_SetAngle(obj2, shotangle);
			ObjMove_SetPosition(obj2, ObjMove_GetX(obj)+radius*cos(rotangle), ObjMove_GetY(obj)+radius*sin(rotangle));
			yield;
		}
		CreateShotA2(ObjMove_GetX(obj2), ObjMove_GetY(obj2), 0, shotangle, 0.0125, 1.55, [AMULET[RED], AMULET[GREEN], AMULET[BLUE]][color%3], 0);
		Obj_Delete(obj2);
	}
	task DummyOrbitar{
		let dummyangle = angle;
		let obj2 = CreateDNHShotA1(ObjMove_GetX(obj)+radius*cos(angle), ObjMove_GetY(obj)+radius*sin(angle), 0, angle, [CIRCLE_L[RED], CIRCLE_L[GREEN], CIRCLE_L[BLUE]][color%3], 15);
		ObjShot_SetSpellResist(obj2, true);
		while(!Obj_IsDeleted(obj)){
			ObjMove_SetPosition(obj2, ObjMove_GetX(obj), ObjMove_GetY(obj));
			yield;
		}
		loop(amt){
			CreateDNHShotA2(ObjMove_GetX(obj2), ObjMove_GetY(obj2), 0, dummyangle, 0.0125, 1.55, [CIRCLE_S[RED], CIRCLE_S[GREEN], CIRCLE_S[BLUE]][color%3], 30);
			dummyangle+=360/amt;
			}
			PlaySoundEffect("se_enep00", 100, ObjMove_GetX(obj2));
			Obj_Delete(obj2);
	}
	
}