#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["YuukaSpell3"]
#Text["Yuuka plants seeds that quickly grow into bamboo."]
//#BGM["script/Kazami/resources/sounds/area4_boss.wav"]
#Image["./resources/images/yuu3.png"]

#include"script/Kazami/AllStars shot/AllStarShot_Const.dnh"
#include"script/default_system/Default_Effect.txt"

let objtext;
let objYuuka;
let bConcentrationMotion = false;
let frame=0;
let objPlayer;
let Target=0;
let wave=0;
let leaf=0;
let animation=0;
let Aim=0;
let stem=0;
let level=1;
let pos=rand(-70, 70);
let spellname=1;

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(6000)
	}
}

@Initialize
{	

	objYuuka = ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objYuuka);


	let imgYuuka = GetCurrentScriptDirectory ~ "resources/images/dot_yuka.png"; //file path to enemy image

	ObjPrim_SetTexture(objYuuka, imgYuuka); //Setting the above image file as a texture to the enemy object(objEnemy)
	ObjSprite2D_SetSourceRect(objYuuka, 39, 30, 95, 101); //Setting the rectangle coordinates in the enemy image to use(Left, Top, Right, Bottom).
	ObjSprite2D_SetDestCenter(objYuuka); //Positioning the center of the rectangle(w/texture) at (0, 0) on the stage(top left corner).

	let cx = GetStgFrameWidth() / 2;
	ObjMove_SetDestAtFrame(objYuuka, cx, 100, 20);
	
	MCircle(objYuuka);

}

@MainLoop{

	if(spellname==1){

	objtext = ObjText_Create();
	ObjText_SetText(objtext, "Earth Magic: Instant Bamboo");
     	Obj_SetRenderPriority(objtext, 0.74);//Render above cutin
     	ObjText_SetFontSize(objtext, 16);
     	ObjText_SetFontColorTop(objtext, 0, 136, 0);
     	ObjText_SetFontColorBottom(objtext, 0, 136, 0);
     	ObjText_SetFontBold(objtext, false);
     	ObjText_SetFontBorderType(objtext, BORDER_SHADOW);
    	ObjText_SetFontBorderColor(objtext, 232, 232, 232);
     	ObjText_SetFontBorderWidth(objtext, 1);
     	ObjRender_SetPosition(objtext, GetStgFrameLeft-10, 20, 0);
     	ObjMove_SetDestAtFrame(objtext, 267, 23, 6);

	}

	spellname++;

	let yx = ObjMove_GetX(objYuuka);
	let yy = ObjMove_GetY(objYuuka);
	
	let px = GetPlayerX();
	let py = GetPlayerY();

	let Player = atan2(py - yy, px - yx);
	
	if(animation==30){
		let angle=0;
		loop(20){
			angle+=360/20;
			
			LaserBulletA1(yx, yy, 4, angle, 0, KNIFE_RED, 10, 10, 10, ST_LASER_RED, 30);
			}
		}

	if(animation>=180){
	
		ObjEnemy_SetIntersectionCircleToShot(objYuuka, yx, yy, 32);

		ObjEnemy_SetIntersectionCircleToPlayer(objYuuka, yx, yy, 24);

		if(ObjEnemy_GetInfo(objYuuka, INFO_LIFE) <= 0){

			Obj_Delete(objYuuka);
			
			Obj_Delete(objtext);
	
			DeleteShotAll(TYPE_ALL, TYPE_ITEM);
			
			CloseScript(GetOwnScriptID());
	
			return;
		}
		
		if(frame==150){
			let type = truncate(rand(1, 3.9));		
			
			if(type==1){
				AimedBamboo;
				}
				
			if(type==2){
				MainBamboo;
				}
				
			if(type==3){
				SpreadBamboo;
				}
		
			frame=0;
			}

		if(stem==4){
		
			loop(level*2+1){
				let id= rand(51, (51+level*2)+1);
				
				if(level>=0){
					id= PELLET_MAGENTA;
					}
				let angle= rand(0,360);
				CreateShotA1(yx+15*cos(angle), yy+15*sin(angle), 2, angle, id, 15);
				
				}
			
			stem=0;
			}
		leaf++;
		if(leaf==120){
			
			ObjMove_SetDestAtSpeed(objYuuka, rand(110, GetStgFrameWidth-110), rand(90, 120), 1);
			
			leaf=0;
			}

		stem++;
			
		
		frame++;
		
		}

	animation++;
	yield;
	}

task LaserBulletA1(x, y, speed, angle, angVel, graphic, delay, LasW, Last, LasID, LasDelay)
	{
		let obj = CreateShotA1(x, y, speed, angle, graphic, delay);
		let angle1=0;
		let explode=0;
		
		ObjShot_SetAutoDelete(obj, false);
		
		let endX = 0;
		let endY = 0;
		
		while(!Obj_IsDeleted(obj))
		{
			endX = ObjMove_GetX(obj);
			endY = ObjMove_GetY(obj);
			if(ObjMove_GetX(obj) > GetStgFrameWidth)
			{
				Obj_Delete(obj);
				angle1=180;
				explode=1;
			}
			if(ObjMove_GetX(obj) < 0)
			{
				Obj_Delete(obj);
				angle1=0;
				explode=1;
			}
			if(ObjMove_GetY(obj) < 0)
			{
				Obj_Delete(obj);
				angle1=90;
				explode=1;
			}
			if(ObjMove_GetY(obj) > GetStgFrameHeight){
				Obj_Delete(obj);
				angle1=270;
				explode=1;
				}
			ObjMove_SetAngularVelocity(obj, angVel);
			yield;
		}
	if(explode==1){
		let obj2 = CreateStraightLaserA1(endX, endY, angle1, 800, LasW, Last, LasID, LasDelay);
			
		Obj_Delete(obj);
		}
	else{
		Obj_Delete(obj);
		}
	}
	
task LaserBulletA2(x, y, speed, angle, angVel, graphic, delay, LasW, Last, LasID, LasDelay)
	{
		let obj = CreateShotA1(x, y, speed, angle, graphic, delay);
		let angle1=0;
		let explode=0;
		
		ObjShot_SetAutoDelete(obj, false);
		
		let endX = 0;
		let endY = 0;
		
		while(!Obj_IsDeleted(obj))
		{
			endX = ObjMove_GetX(obj);
			endY = ObjMove_GetY(obj);
			if(ObjMove_GetX(obj) > GetStgFrameWidth)
			{
				Obj_Delete(obj);
				angle1=180;
				explode=1;
			}
			if(ObjMove_GetX(obj) < 0)
			{
				Obj_Delete(obj);
				angle1=0;
				explode=1;
			}
			if(ObjMove_GetY(obj) < 0)
			{
				Obj_Delete(obj);
				angle1=90;
				explode=1;
			}
			if(ObjMove_GetY(obj) > GetStgFrameHeight){
				Obj_Delete(obj);
				angle1=270;
				explode=1;
				}
			ObjMove_SetAcceleration(obj, angVel);
			yield;
		}
	if(explode==1){
		let obj2 = CreateStraightLaserA1(endX, endY, angle1, 600, LasW, Last, LasID, LasDelay);
			
		Obj_Delete(obj);
		}
	else{
		Obj_Delete(obj);
		}
	}
	
task MCircle(tobj){
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	
	let path = "script/Kazami/resources/images/e_ef_61.png";
	ObjPrim_SetTexture(obj,path);
	
	Obj_SetRenderPriorityI(obj,20);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(obj,200);
	let sx = (300-0)/2; let sy = (300-0)/2; //taken from BG code, don't know what it does.
	ObjSprite2D_SetSourceRect(obj,0,0,256, 256);
	
	ObjRender_SetPosition(obj,ObjMove_GetX(objYuuka),ObjMove_GetY(objYuuka),0);
	ObjSprite2D_SetDestRect(obj,-sx,-sy,sx,sy); //taken from BG code, don't know what it does.
	
	spinBG;
 
	task spinBG{
		let spin = 0;
		loop{
		let life = ObjEnemy_GetInfo(objYuuka, INFO_LIFE);
		ObjRender_SetAngleXYZ(obj,0,0,spin);
		ObjRender_SetPosition(obj,ObjMove_GetX(tobj),ObjMove_GetY(tobj),0);
		ObjRender_SetScaleXYZ(obj,life/4000,life/4000,0);
		spin+=-0.5;
		yield;
		}
	}
	
	}
	
task AimedBamboo{
	
	loop(5){
	
		let yx = ObjMove_GetX(objYuuka);
		let yy = ObjMove_GetY(objYuuka);
	
		let px = GetPlayerX();
		let py = GetPlayerY();

		let Player = atan2(py - yy, px - yx);
	
		LaserBulletA1(yx, yy, 4, Player, 0, KNIFE_GREEN, 10, 10, 30, ST_LASER_GREEN, 20);
		LaserBulletA1(yx, yy, 4, Player+5, 0, KNIFE_GREEN, 10, 10, 30, ST_LASER_GREEN, 20);
		LaserBulletA1(yx, yy, 4, Player-5, 0, KNIFE_GREEN, 10, 10, 30, ST_LASER_GREEN, 20);
		
		loop(20){
			yield;
			}
		}
	}
		
task MainBamboo{

	let yx = ObjMove_GetX(objYuuka);
	let yy = ObjMove_GetY(objYuuka);
		
	loop(5){
		Aim+=34;
			
		LaserBulletA1(yx+30*cos(Aim), yy+30*sin(Aim), 3, Aim, 0, KNIFE_GREEN, 10, 10, 30, ST_LASER_GREEN, 100);
		LaserBulletA1(yx+30*cos(Aim+180), yy+30*sin(Aim+180), 3, Aim+180, 0, KNIFE_GREEN, 10, 10, 30, ST_LASER_GREEN, 100);
		
		loop(10){
			yield;
			}
		}
	}
	
task SpreadBamboo{
	let angle=0;
	
	let yx = ObjMove_GetX(objYuuka);
	let yy = ObjMove_GetY(objYuuka);
	
	loop(15){
		angle+=360/15;
		
		LaserBulletA1(yx, yy, 3, angle, 0, KNIFE_GREEN, 10, 10, 30, ST_LASER_GREEN, 50);
		
		}
	}
				