#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["ClownpieceNon3"]
#Text["By Lushie/Lusushime"]
#include ".\Functions.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 = 35;
let spelllife = 3000;

@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, 120);
	bmoveto(GetCenterX, 120, 60);
	wait(61);
	startattacking;
	
}

task startattacking{
	pointdevicepositions;
	pdplayerx = GetCenterX;
	pdplayery = 400;
	pdcount2 = pdcount;
	let pdc = pdcount;
	
	wait(50);
	setauracolor("green");
	chargeeffect2("red");
	wait(45);
	if(pdc == pdcount){
		spelldamage(30, 140, 13);
		fireA(pdc);
		fireB(pdc);
	}
}

task fireB(pdc){
	let t = 1;
	let ap = 90;
	let app = 8.23;
	let s = 0.5;
	let acc = 0.05;
	let ms = 1.5;
	let posneg = -1;
	let off = -20;
	if(diff == 1){
		app = 12.23;
		off = 20;
	}
	if(diff == 2){
		app = 10.23;
		off = -5;
	}
	if(diff == 3){
		app = 8.23;
		off = -20;
	}
	while(lifecheck(pdc) == true){
		let angle = GetAngleToPlayer(objBoss)-90;
		let bullet = CreateShotA2(BossX, BossY, s, angle + ap, acc, ms, 219, 0);
		spawncustom2(bullet, 1, 1.5, 0, 255, 12, 0);
		ap += app * posneg;
		if(ap < 0-off || ap > 180+off){posneg *= -1; ap += app/2*posneg;}
		SE_Play2(shot1, shot1v-4);
		wait(t);
	}
}

task fireA(pdc) {
	let on2 = 11;
	let t = 3;
	let s = 5;
	let at = 35;
	let dacc = -s/at;
	let dt = 60;
	let l = 600;
	let w = 18;
	let dur = 55;
	let ap = 15;
	let posneg = 1;
	let oxp = 210;
	let xp2 = -w;
	let oy = 100;
	let oyp = -10;
	let dt2 = 20;
	let n3 = 10;
	let posneg2 = 1;
	let n22 = 3;
	wait(30);
	if(diff == 1){
		oxp = 220;
		n22 = 3;
	}
	if(diff == 2){
		oxp = 215;
		n22 = 4;
	}
	if(diff == 3){
		oxp = 210;
		n22 = 5;
	}
	while(lifecheck(pdc) == true){
		loop(2){
			move = 3;
			let xp = oxp;
			let y = oy;
			let n2 = on2;
			let yp = oyp;
			if(posneg2 == -1){
				xp = -xp2/2;
				y = oy + (n2-1) * yp/2;
				n2 = n22;
			}
			loop(n2){
				if(lifecheck(pdc) == false){return;}
				loop(2){
					let bullet = CreateShotA2(BossX, BossY, 0, 0, 0, 0, 295, 0);
					ObjMove_SetDestAtFrame(bullet, BossX+xp*posneg, y, at);
					ObjRender_SetBlendType(bullet, BLEND_ADD_ARGB);
					bulletcommand(bullet, 0);
					spawncustom2(bullet, 1, 1.5, 70, 255, 12, 0);
					posneg *= -1;
					
				}
				
				xp += xp2 * posneg2;
				y += yp * posneg2;
				yp += 1;
				wait(t);
				
			}
			wait(105);
			posneg2 *= -1;
		}
		if(lifecheck(pdc) == false){return;}
		bmovement(15, 30, 10, 20, 40);
		wait(60);
	}

	task bulletcommand(bullet, c){
		ObjShot_SetSpellResist(bullet, true);
		ObjShot_SetAutoDelete(bullet, false);

		wait(at);
		SE_Play3(laser1, laser1v-5);
		ObjShot_SetGraphic(bullet, 704);
		ObjRender_SetScaleXYZ(bullet, 2, 2, 1);
		
		let laserarray = [];
		let dw = 30;
		if(c == 0){
			let laser = CreateStraightLaserA1(ObjMove_GetX(bullet), ObjMove_GetY(bullet), 90, 0, dw, dur, 7, dt);
			//let laser2 = CreateStraightLaserA1(ObjMove_GetX(bullet), ObjMove_GetY(bullet), 270, 0, dw, dur, 7, dt);
			laserarray = laserarray ~ [laser];
			//laserarray = laserarray ~ [laser2];
		}
		let la = length(laserarray);
		let cl = 0;
		loop(dt){
			cl += l/dt;
			ascent(i in 0..la){
				ObjLaser_SetLength(laserarray[i], cl);
			}
			wait(1);
		}
		ascent(i in 0..la){
			ObjLaser_SetInvalidLength(laserarray[i], 0, 0);
			ObjLaser_SetRenderWidth(laserarray[i], w);
			ObjLaser_SetIntersectionWidth(laserarray[i], w-3);

		}
		wait(dur);
		
		let cw = w;
		let alpha = 255;
		loop(dt2){
			cw -= w/dt2;
			alpha -= 255/dt2;
			ObjRender_SetAlpha(bullet, alpha);
			ascent(i in 0..la){
				ObjRender_SetAlpha(laserarray[i], alpha);
				ObjLaser_SetRenderWidth(laserarray[i], cw);
			}
			wait(1);
		}
		Obj_Delete(bullet);
		ascent(i in 0..la){
			Obj_Delete(laserarray[i]);
		}
	}
}

