#TouhouDanmakufu
#ScriptVersion[3]
#Title["Non 1"]

#include"./../ZUNShot_Const.txt"
#include"./../../drawstuff.txt"
#include "script/default_system/Default_Effect.txt"

let ex;
let ey;
let mx;
let my;
let l = 0;
let objEnemy;
let diff = GetCommonData("Difficulty", 0);
	@Initialize{
		objEnemy = ObjEnemy_Create(OBJ_ENEMY_BOSS);
		ObjEnemy_Regist(objEnemy);
		ObjEnemy_SetDamageRate(objEnemy, 0, 0);
		ObjMove_SetDestAtWeight(objEnemy,GetStgFrameWidth/2,100,10,10);
		DeleteShotAll(TYPE_ALL, TYPE_ITEM);
		EnglandMove(GetCurrentScriptDirectory ~ "img/enm1.png");
		AuraMakeBoss(objEnemy,120,150,200);
		mainTask;	
		}

	@MainLoop
	{
		ex = ObjMove_GetX(objEnemy);
		ey = ObjMove_GetY(objEnemy);
		l+=1;
		ObjEnemy_SetIntersectionCircleToShot(objEnemy, ex, ey, 32);//Sets the hitbox against player bullets
		ObjEnemy_SetIntersectionCircleToPlayer(objEnemy, ex, ey, 24);//Sets the hitbox against the player
		if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0||!GetEnemyBossObjectID()){
			Obj_Delete(objEnemy);
			DeleteShotAll(TYPE_ALL, TYPE_ITEM);
			Reset2DCamera();
			CloseScript(GetOwnScriptID());
			}
		yield;
	}
	

	@Event
	{
		alternative(GetEventType())
		case(EV_REQUEST_LIFE)
		{
			SetScriptResult(3000);
		}
		case(EV_REQUEST_TIMER)
		{
			SetScriptResult(120);//Sets the time limit to 60
		}
		case(EV_REQUEST_SPELL_SCORE)
		{
			SetScriptResult(0);//Sets spellcard bonus to 30000 points
		}
	}

task mainTask{
	yield;
	//EnemyMarker(objEnemy);
	/*SetAreaCommonData("SpellData", "Tries1", GetAreaCommonData("SpellData", "Tries1", 0)+1);
	backCall;
	backCall2;
	cutin(GetCurrentScriptDirectory~"img/cutin.png",0,0,0,0);
	SpellName("Echo Sign \"Echolocation\"",100,100,100,200,200,200,"name1");
	score(1200000,100,100,100,200,200,200,"Capture1","Tries1");*/
	loop(60){yield;}
	loop(60){yield;}
	shoot;
	loop(240){yield;}
	move;
	yield;
	}


task shoot{
	let x = 0;
	let y = 0;
	let bullet = 0;
	let veloc = 0;
	loop{
		DelaySE("SCharge1",0,0,0);
		charge(ex,ey,0);
		loop(60){yield;}
		ObjEnemy_SetDamageRate(objEnemy, 0, 0);
		ObjRender_SetAlpha(objEnemy, 128);
		x = rand(0,360);
		bullet+=10;
		loop(60){
		ascent(i in 3..5){
				DelaySE("SShot2",0,0,0);
				ascent(j in -2..3){
					CreateObjShotA1(ex,ey,i+1*diff,GetAngleToPlayer(objEnemy)+j*(30-diff*3),318,10);
				}
			}
			loop(5){yield;}
		}
		ObjEnemy_SetDamageRate(objEnemy, 100, 100);
		ObjRender_SetAlpha(objEnemy, 255);
		loop(120){yield;}
		DelaySE("SCharge1",0,0,0);
		charge(ex,ey,0);
		loop(60){yield;}
		ObjEnemy_SetDamageRate(objEnemy, 0, 0);
		ObjRender_SetAlpha(objEnemy, 128);
		loop(40+diff*13){
			DelaySE("SShot3",0,0,0);
			bullet-=10-diff;
			wallBounceSNB(CreateObjShotA1(ex,ey,4,bullet,311,10));
			wallBounceSNB(CreateObjShotA1(ex,ey,3.5,bullet,311,10));
			wallBounceSNB(CreateObjShotA1(ex,ey,3,bullet,311,10));
			loop(5-diff){yield;}
		}
		ObjEnemy_SetDamageRate(objEnemy, 100, 100);
		ObjRender_SetAlpha(objEnemy, 255);
		loop(120){yield;}
		DelaySE("SCharge1",0,0,0);
		charge(ex,ey,0);
		loop(60){yield;}
		ObjEnemy_SetDamageRate(objEnemy, 0, 0);
		ObjRender_SetAlpha(objEnemy, 128);
		ascent(j in -4..5){
			DelaySE("SShot2",0,0,0);
			ascent(i in 0..15+diff*5){
				curve2(CreateObjShotA1(ex,ey,3,i*360/(10+diff*5),320,10),j*10,3);
				curve2(CreateObjShotA1(ex,ey,3,i*360/(10+diff*5),320,10),j*10,2);
				curve2(CreateObjShotA1(ex,ey,3,i*360/(10+diff*5),320,10),j*10,1);
			}
		loop(30){yield;}
		}
		ObjEnemy_SetDamageRate(objEnemy, 100, 100);
		ObjRender_SetAlpha(objEnemy, 255);
		loop(120){yield;}
	}
task curve2(objA,a,s){
	let ang = ObjMove_GetAngle(objA);
	ObjMove_SetAcceleration(objA, -0.1);
	ObjMove_SetMaxSpeed(objA,0);
	loop(60){yield;}
	DelaySE("SShot1",0,0,0);
	ObjMove_SetAcceleration(objA, 0);
	ObjMove_SetSpeed(objA,s);
	if(!Obj_IsDeleted(objA)){ObjMove_SetAngle(objA,ang+a);}
}

task line(x,y,s,ang,grap,del,l,d,inc,sch,blend){
		loop(l){
			let a = CreateObjShotA1(x,y,s,ang,grap,del);
			ObjRender_SetBlendType(a,blend);
			wallBounceSNB(a);
			ang+=inc;
			s+=sch;
			loop(d){yield;}
			}
	}
}
task move{
	loop{
		if((GetPlayerX>ex||ex<120)&&ex<GetStgFrameWidth-120){
			ObjMove_SetDestAtWeight(objEnemy,ObjMove_GetX(objEnemy)+rand(30,50),rand(95,155),10,1);
		} else{
			ObjMove_SetDestAtWeight(objEnemy,ObjMove_GetX(objEnemy)-rand(30,50),rand(95,155),10,1);			
		}
		loop(300){yield;}
	}
}
task backCall{
	let objBgOne = ObjPrim_Create(OBJ_SPRITE_2D);
	background1(objBgOne,GetCurrentScriptDirectory~"img/bg2.png",0,0,674,674,10000000,255,-145,0,529,674,22,BLEND_ALPHA,0,0,0);
	}
task backCall2{
	let objBgTwo = ObjPrim_Create(OBJ_SPRITE_2D);
	background1(objBgTwo,GetCurrentScriptDirectory~"img/bg1.png",0,0,520,520,10000000,120,0,0,GetStgFrameWidth,GetStgFrameHeight(),22,BLEND_ALPHA,0,0.3,0);
	}

task background1(obj,imgbg1,r,t,l,b,time,alpha,rd,rt,rl,rb,prio,render,xmove,ymove,zangle){
let c = 0;
LoadTexture(imgbg1);
ObjPrim_SetTexture(obj,imgbg1);
Obj_SetRenderPriorityI(obj,prio);
ObjSprite2D_SetDestRect(obj,rd,rt,rl,rb);
ObjRender_SetBlendType(obj,render);
ObjRender_SetAlpha(obj,alpha);
ObjSprite2D_SetSourceRect(obj,r,t,l,b);
loop(time){
	if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0){
		Obj_Delete(obj);
		}
	ObjRender_SetAlpha(obj,c*12);
	ObjSprite2D_SetSourceRect(obj,r+(c*xmove),t+(c*ymove),l+(c*xmove),b+(c*ymove));
	ObjRender_SetAngleXYZ(obj,0,0,c*zangle);
	c++;
	yield;
}
Obj_Delete(obj);
}
task blackness(time){
let c = 0;
let obj = ObjPrim_Create(OBJ_SPRITE_2D);
ObjPrim_SetTexture(obj,GetCurrentScriptDirectory ~ "img/black.png");
Obj_SetRenderPriorityI(obj,70);
ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,GetStgFrameHeight);
ObjRender_SetAlpha(obj,255);
ObjSprite2D_SetSourceRect(obj,0,0,5,5);
loop(time){
	if(ObjEnemy_GetInfo(objEnemy,INFO_LIFE)<=0){
		Obj_Delete(obj);
		}
	ObjRender_SetAlpha(obj,255-c);
	c+=255/time;
	yield;
}
Obj_Delete(obj);
}

task Laser(posx,posy,angle,delay,grap,blend){
	let objAnc = ObjShot_Create(OBJ_STRAIGHT_LASER);
	let frame = 0;
	let n = 0.5;
	ObjMove_SetSpeed(objAnc,0);
	ObjMove_SetPosition(objAnc,posx,posy);
	ObjShot_SetGraphic(objAnc,1);
	ObjStLaser_SetAngle(objAnc,angle);
	ObjShot_SetSpellResist(objAnc,true);
	ObjShot_SetDelay(objAnc,delay);
	ObjLaser_SetLength(objAnc,600);
	ObjLaser_SetRenderWidth(objAnc,20);
	ObjLaser_SetIntersectionWidth(objAnc,10);
	ObjShot_Regist(objAnc);
	while(!Obj_IsDeleted(objAnc)){
		if(frame==60){
				let a = CreateLooseLaserA1(posx,posy,6,angle,200,20,grap,20);
				ObjRender_SetBlendType(a,blend);
				n++;
			loop(20){yield;}
			DelaySE("SLaser2",0,0,0);
			Obj_Delete(objAnc);
		}
		frame++;
		yield;
	}
}
