#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["DoremySpell2"]
#Text["By Lushie/Lusushime"]
#include ".\Functions.dnh"
#Background[".\Stage3bg.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
	bossanimation("doremy");

	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, 120);
	startattacking;
}


// if(n2 != 1){angle2 -= ap/2*(n2-1);}
task startattacking{
	pointdevicepositions;
	pdcount2 = pdcount;
	let pdc = pdcount;
	bmoveto(GetCenterX, 120, 60);
	setauracolor("purple");
	bottomwarning(150, 0, 255);
	spellcutin("Nightmare Sign \"Inherited Phobia\"", "doremy");
	SetShotAutoDeleteClip(64, 64, 64+384-(rez*384), 64+448-(rez*448));

	wait(40);
	if(pdc == pdcount){fireA(pdc); spelldamage(4, 15, 20);}
}

task fireA(pdc) {
	let s = 2.1;
	let acc = 0;
	let ms = s;
	let n = 3;
	let t = 6;
	let d = 190;
	let d2 = 30;
	let posneg = 1;
	let ap = 10.47;
	let angle = rand(0,360);
	let fw = 40;

	if(diff == 2){
		s = 2.3;
		n = 3;
		t = 5;
		ap = 8.66;
		fw = 60;
	}
	if(diff == 3){
		s = 2.4;
		n = 4;
		t = 5;
		ap = 7.89;
		fw = 20;
	}
	let maoff = ap*0.2;
	move = 3;
	while(lifecheck(pdc) == true){
			if(lifecheck(pdc) == false){return;}
			loop(n){
				let aoff = rand(-maoff,maoff);
				let x = BossX + d * cos(angle+aoff);
				let y = BossY + d2 * sin(angle+aoff);
				let bullet = CreateShotA2(x, y, s, 90, acc, ms, 333, 0);
				bcommand(bullet, 335);
				angle += ap/2 * posneg;
				aoff = rand(-maoff,maoff);
				x = BossX + d * cos(angle+aoff);
				y = BossY + d2 * sin(angle+aoff);
				let bullet2 = CreateShotA2(x, y, s, 270, acc, ms, 335, 0);
				bcommand(bullet2, 333);
				angle -= ap/2 * posneg;
				angle += 360/n;
				if(angle > 360){angle -= 360;}
				if(angle < 0){angle += 360;}
			}
			SE_Play2(shot1, shot1v+2);
			angle += ap * posneg;
			if(angle > 360){angle -= 360;}
			if(angle < 0){angle += 360;}
			wait(t);
		
	}

	task bcommand(bullet,graphic){
		

		if(angle > 180){
			//ObjShot_SetGraphic(bullet, graphic);
			
			ObjShot_SetIntersectionEnable(bullet, false); 
			Obj_SetRenderPriorityI(bullet, 40); 
			spawncustom(bullet, 1, 2, 1, 20, 40, 8, 1, 0);
		}
		else{
			spawncustom(bullet, 1, 2, 1, 70, 255, 8, 1, 0);
		}
		if(ObjMove_GetAngle(bullet) == 270){
			
			ObjShot_SetAutoDelete(bullet, false);
			while(ObjMove_GetY(bullet) > -20 && !Obj_IsDeleted(bullet)){
				yield;
			}
			ObjMove_SetY(bullet, ObjMove_GetY(bullet)+500);
			ObjMove_AddPatternA2(bullet, 0, 0, NO_CHANGE, 0, 0, 0);
			ObjMove_AddPatternA1(bullet, fw, s, NO_CHANGE);
			ObjShot_SetAutoDelete(bullet, true);
		}
		else{
			Obj_SetRenderPriorityI(bullet, Obj_GetRenderPriorityI(bullet)-1);
		}
	}
}

