#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["ClownpieceSpell1"]
#Text["By Lushie/Lusushime"]
#include ".\Functions.dnh"
#include ".\EnemyTypes.dnh"
#Background[".\Stage5bg.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("clownpiece");
	TDrawLoop; //calls TDrawLoop task
	TFinalize; //calls TFinalize task
	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, 150);
	
	startattacking;
	
}

task startattacking{
	pointdevicepositions;
	pdplayerx = GetCenterX;
	pdplayery = 400;
	pdcount2 = pdcount;
	let pdc = pdcount;
	bmoveto(GetCenterX, 150, 60);
	setauracolor("green");
	spellcutin("Hellfire \"Madness Inducing Flames\"", "clownpiece");
	SetShotAutoDeleteClip(32, 32, 32+384-(rez*384), 32+448-(rez*448));
	wait(45);
	if(pdc == pdcount){
		spelldamage(5, 13, 20);
		fireA(pdc);
	}
}


task fireA(pdc) {
	let maxs = 2.9;
	let mins = -maxs/2;
	let at = 50;
	let at2 = at;
	

	let n2 = 20;
	let n = 4;
	
	let graphic = 1064;
	let wt = 40;
	move = 3;
	if(diff == 1){
		maxs = 2.6;
		mins = -maxs/2;
		at = 70;
		n = 6;
		n2 = 14;
		wt = 50;
	}
	if(diff == 2){
		maxs = 2.8;
		mins = -maxs/2;
		at = 60;
		n = 4;
		n2 = 20;
		wt = 45;
	}
	if(diff == 3){
		maxs = 3;
		mins = -maxs/2;
		at = 50;
		n = 3;
		n2 = 26;
		wt = 45;
	}
	let acc = (maxs-mins)/at;
	let ap = 360/n2/n/2;
	while(lifecheck(pdc) == true){
		let c = 1;
		let angle = rand(0,360);
		if(graphic == 1064){graphic = 1061;}
		else{graphic = 1064;}
		loop(2){
			loop(n2){
				loop(n){
					let bullet = CreateShotA2(BossX+5, BossY-45, (maxs-mins)/2, angle, 0, 0, graphic, (maxs-mins)/2);
					bcommand(bullet, c);
					angle += ap;
				}
				angle -= ap*n;
				angle += 360/n2;
			}
			angle += 360/n2/2;
			c -= 1;
		}
		SE_Play2(shot1, shot1v+3);
		wait(wt);
		SE_Play2(shot1, shot1v+3);
		wait(at*2-wt);
	}

	task bcommand(bullet, c){
		let t = 0;
		ObjRender_SetBlendType(bullet, BLEND_ADD_ARGB);
		wait(wt);
		if(c == 1){
			ObjMove_AddPatternA2(bullet, t, maxs, NO_CHANGE, -acc, 0, mins);
			t+= at;
		}
		loop(10){
			ObjMove_AddPatternA2(bullet, t, mins, NO_CHANGE, acc, 0, maxs);
			t+= at;
			ObjMove_AddPatternA2(bullet, t, maxs, NO_CHANGE, -acc, 0, mins);
			t+= at;
		}
	}
	
}