#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["RingoSpell2"]
#Text["By Lushie/Lusushime"]
#include ".\Functions.dnh"
#Background[".\Stage2bg.dnh"]
#System[".\default_system/Default_System.txt"]
#Player[".\player/LoLK Marisa (Lushie)/MarisaH.dnh"]

let objBoss; //global variable, can be used anywhere
let objScene = GetEnemyBossSceneObjectID(); //declares global variable enemy boss scene object


@Event{
	alternative(GetEventType())
    case(EV_REQUEST_LIFE){ //Sets boss's life
		SetScriptResult(spelllife);
    }
    case(EV_REQUEST_TIMER){ //Sets attack time
        SetScriptResult(spelltime);
    }
    case(EV_REQUEST_SPELL_SCORE){ //Spellcard bonus
        SetScriptResult(1000000);
    }
    case(EV_PLAYER_SHOOTDOWN){
    	pdreset(spelltime,spelllife);
    }
}

let spelltime = 40;
let spelllife = 500;

@Initialize{
	objBoss = ObjEnemy_Create(OBJ_ENEMY_BOSS); //gives the variable a value, object ID that makes it a boss
	ObjEnemy_Regist(objBoss); //registers it as an enemy

 //declares spellcard
 	bossanimation("ringo");
	TDrawLoop; //calls TDrawLoop task
	LSFinalize;
	MainTask; //calls MainTask task
}

@MainLoop{
	ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), bosshitbox); 
	ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 24);
	// CircleToShot means any player shoot hitting this will damage the boss, CircleToPlayer kills the player on colission
	yield; //necessary for task functions to work
}

task MainTask{
	startingposition(GetCenterX, 130);
	
	startattacking;
	
}

task startattacking{
	pointdevicepositions;
	pdplayerx = GetCenterX;
	pdplayery = 400;
	pdcount2 = pdcount;
	let pdc = pdcount;
	bmoveto(GetCenterX, 130, 60);
	setauracolor("orange");
	bottomwarning(0, 200, 200);
	spellcutin("Dango Sign \"Self Sustaining Paradise\"", "ringo");
	SetShotAutoDeleteClip(32, 32, 32+384-(rez*384), 32+448-(rez*448));

	wait(45);
	if(pdc == pdcount){
		spelldamage(5, 16, 20);
		fireA(pdc);
	}
}

task fireA(pdc) {
 	
 	let s = 0.5;
 	let acc = 0.1;
 	
	let d = 0;

	
	
	let ap = 23;
	let cm = 2;
	let m = 14;
	let d2m = 14;
	let acc2 = -0.015;
	let s2 = 0.4;
	
	let dt = 220;

	let i = 0;
	let g1 = [405,  407, 400];
	let g2 = [246, 247, 241];
	let g3 = [266, 267,255];
	let c = 0;
	let wt = 180;

	let n = 8;
	let ms2 = 0.13;
	let n2 = 2;
	let t = 56;
	let ms = 2.1;

	if(diff == 2){
		ms2 = 0.15;
		n = 10;
		n2 = 3;
		t = 45;
		ms = 2.3;
		dt = 250;
		wt = 170;
	}
	if(diff == 3){
		ms2 = 0.175;
		n = 12;
		n2 = 4;
		t = 43;
		ms = 2.5;
		dt = 260;
		wt = 170;
	}
	while(lifecheck(pdc) == true){
		let angle = GetAngleToPlayer(objBoss) + 360/n/2;
		if(c == 1){angle = GetAngleToPlayer(objBoss); c = -1;}
		move = 3;
		loop(n){
			let bullet = CreateShotA2(BossX + d * cos(angle), BossY + d * sin(angle), s, angle, acc, ms, g1[i], 0);
			angle += 360/n;
			bcommand(bullet,i);
			
		}
		SE_Play(shot2, shot2v-5);
		c+= 1;
		wait(80);
		if(lifecheck(pdc) == false){return;}
		bmovement(15, 30, 5, 10, 55);
		wait(wt);

		task bcommand(bullet,i){
			let apc = 0;
			let tc = round(t/2);
			let c = 0;
			while(!Obj_IsDeleted(bullet)){
				if(lifecheck(pdc) == false){return;}
				if(c != cm){
					let x = ObjMove_GetX(bullet);
					let y = ObjMove_GetY(bullet);
					if( x < -m || x > 384 + m || y < -m || y > 448 +m){
						if(lifecheck(pdc) == false){return;}
						let angle = ObjMove_GetAngle(bullet);
						while(angle > 360){angle -= 360;}
						while(angle < 0){angle += 360;}
						if(x < -m){angle = 90-(angle-90); ;}
						else if(x > 384 + m){angle = 90+(90-angle);}
						else if(y < -m){angle = 180-(angle-180);}
						else if(y > 448 +m){angle = 180+(180-angle);}
						SE_Play2(kira3, kira3v);
						c+=1;
						i += 1;
						ObjShot_SetGraphic(bullet, g1[i]);
						ObjMove_SetAngle(bullet, angle);
						wait(5);
						
						
					}
				}
				tc += 1;
				if(tc == t){
					let angle = rand(0,360);
					let array = [];
					loop(n2){
						let d2 = rand(0, d2m);
						let bullet2 = CreateShotA2(ObjMove_GetX(bullet), ObjMove_GetY(bullet), s2, angle, acc2, ms2, g2[i], 0);
						Obj_SetRenderPriorityI(bullet2, Obj_GetRenderPriorityI(bullet2)-1);
						array = array ~ [bullet2];
						
						angle += 360/n2;
					}
					bcommand2(array,i);
					
					apc += ap;
					tc = 0;
				}
				yield;
			}
		}
		task bcommand2(array,i2){
			let l = length(array);
			wait(dt);
			if(lifecheck(pdc) == false){return;}
			ascent(i in 0..l){
				if(!Obj_IsDeleted(array[i])){
					ObjShot_SetGraphic(array[i], g3[i2]);
				}
			}
			let graphic = g3[i2];
			wait(t);
			if(lifecheck(pdc) == false){return;}
			let array2 = [];
			ascent(i in 0..l){
				if(!Obj_IsDeleted(array[i])){
					let color = GetShotDataInfoA1(graphic, "./ZUNShot_Const.txt", INFO_DELAY_COLOR);
					let cancel = CreateShotA2(ObjMove_GetX(array[i]), ObjMove_GetY(array[i]), 0, 0, 0, 0, 1101, 0);
					Obj_Delete(array[i]);
					ObjShot_SetGraphic(cancel, 1101);
					ObjRender_SetBlendType(cancel, BLEND_ADD_ARGB);
					ObjMove_AddPatternA2(cancel, 0, 0, rand(0,360), 0, -4, 0);
					ObjRender_SetScaleXYZ(cancel, 0.4, 0.4, 1);
					ObjRender_SetColor(cancel, color[0], color[1], color[2]);
					ObjShot_SetSpellResist(cancel, true);
					ObjShot_SetIntersectionEnable(cancel, false);
					Obj_SetRenderPriorityI(cancel, 32);
					array2 = array2 ~ [cancel];
				}
				
			}
			loop(31){if(lifecheck(pdc) == true){yield;}}
			l = length(array2);
			ascent(i in 0..l){
				Obj_Delete(array2[i]);
			}
		}
	}
}