#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Konn Spell 3"]
//#Background["./AmaneBG/bg.txt"]
#BGM["./LegendofKAGE.mp3"]
#Player["./Reimu_ultimadragon/Reimu.txt"]

#include"./Include.txt"


let objBoss;
let frame=0;
let Aim=0;
let Around=0;
let animation=0;
let angle=45;
let shot=0;
let move=0;
let s = 1;
let allow=0;
let allow1=0;

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(7000)
	}
	
	case(EV_REQUEST_TIMER){
		SetScriptResult(140);
		}
}

@Initialize
{	
	objBoss= ObjEnemy_Create(OBJ_ENEMY_BOSS);
	ObjEnemy_Regist(objBoss);

	let imgExRumia = GetCurrentScriptDirectory ~ "Kongaara.png"; //file path to enemy image
	//let imgMima = GetCurrentScriptDirectory ~ "Mima.png"; //file path to enemy image

	ObjPrim_SetTexture(objBoss, imgExRumia); //Setting the above image file as a texture to the enemy object(objBoss)
	//ObjPrim_SetTexture(objBoss, imgMima); //Setting the above image file as a texture to the enemy object(objBoss)

	ObjSprite2D_SetSourceRect(objBoss, 0, 0, 56, 62); //Setting the rectangle coordinates in the enemy image to use(Left, Top, Right, Bottom).
	//ObjSprite2D_SetSourceRect(objBoss, 0, 0, 56, 62); //Setting the rectangle coordinates in the enemy image to use(Left, Top, Right, Bottom).

	ObjSprite2D_SetDestCenter(objBoss); //Positioning the center of the rectangle(w/texture) at (0, 0) on the stage(top left corner).
	//ObjSprite2D_SetDestCenter(objBoss); //Positioning the center of the rectangle(w/texture) at (0, 0) on the stage(top left corner).

	ObjRender_SetScaleXYZ(objBoss, 1.2, 1.2, NULL);
	//ObjRender_SetScaleXYZ(objBoss, 1.2, 1.2, NULL);
	
	//ObjRender_SetAlpha(objBoss, 0);
	
	let cx = GetStgFrameWidth() / 2;
	ObjMove_SetDestAtFrame(objBoss, cx, GetStgFrameHeight/2, 30);
	
	
	//BACKG(path3, path1, objBoss);

	//BG;
	
	//SpellBG(objBoss, GetCurrentScriptDirectory~"Smoke.png");
	KonnBG(objBoss, GetCurrentScriptDirectory~"woodblock10d.jpg", GetCurrentScriptDirectory~"eff02.png", -5, 0, 250, 300, 256, 256, 1.7, 1.8);
}

@MainLoop{
	animation++;
	
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){
		death(objBoss);
		}
	
	if(animation==40){
		Concentration(objBoss, "STAR", 100, 40, 4, 250, 4, 250, 4, 250);
		
		}
		
	if(animation==120){
		//Slash(objBoss);
		//Concentration(objBoss, "STAR", 100, 400, 0, 0, 0, 250, 0, 250);
		//AlphaAdd(objBoss);
		ObjMove_SetDestAtFrame(objBoss, GetStgFrameWidth/2, 110, 50);
		Square;
		}
		
	if(animation==250){
		ObjCutin_SetSpellcardS4("Deep Space\"Lost In the Realm of Amatsu Mikabushi\"", GetCurrentScriptDirectory~"AmaneImage.png", YABUSAME, 104, 104, 104);
		ObjCutin_LaunchS3(YABUSAME, GetCurrentScriptDirectory~"KonnCutin.png", "Hard");
		MCircle(objBoss, GetCurrentScriptDirectory~"e_ef_61.png");
		}
	if(!Obj_IsDeleted(objBoss)){
	if(animation>=350){
		ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32);

		ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 24);
		
		frame++;
		shot++;
		move++;
		
		if(move==300){
			//ObjMove_SetDestAtFrame(objBoss, rand(30, GetStgFrameWidth-30), rand(30,GetStgFrameHeight-30), 250);
			move=0;
			}
		
		if(shot==300){
			Concentration(objBoss, "STAR", 100, 50, 4, 250, 4, 250, 4, 250);
			}
		
		if(shot==350){
			let NUM = 8;
			let starangle=rand(0, 360);
			Wave3SE;
			loop(NUM){
				starangle+=360/NUM;
				StarLaser(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 5, starangle+rand(-20, 20), rand(-0.9, 0.9), rand(161, 176), NUM);
				}
			s*=-1;
			
			allow1++;
			
			if(allow1<2){
				shot=-100;
				}
			}
			
		if(shot==500){
			ObjMove_SetDestAtFrame(objBoss, rand(40, GetStgFrameWidth-40), rand(70, 120), 140);
			Buster;
			shot=0;
			allow1=0;
			}
			
		if(frame==20){
			let NUM = 6;
			angle+=3.71;
			
			
			loop(NUM){
				angle+=360/NUM;
				
				BounceShot(ObjMove_GetX(objBoss)+70*cos(angle), ObjMove_GetY(objBoss)+70*sin(angle), 1, angle, AMULET_GRAY, rand(99, 111), 10);
				//BounceShot(ObjMove_GetX(objBoss)+70*cos(angle), ObjMove_GetY(objBoss)+70*sin(angle), 1.05, angle, AMULET_GRAY, rand(99, 111), 10);

				}
				
			//allow1++;
			
			frame=0;
			}
		}
		}
	yield;
	}
	
task Buster{
	let NUM = 14;
	let count = 4;
	
	loop(count){
		let angle= rand(0, 360);
		Shot5SE;
		
		loop(NUM){
			angle+=360/NUM;
			let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 2.1, angle, rand(324, 330)+500, 0);

			
			ObjMove_SetAngularVelocity(obj, 0.5*s);

			}
		s*=-1;
		loop(40){
			yield;
			}
		}
	}
	
task StarLaser(x, y, speed, angle, velocity, graphic, ALLOW){
	let obj = CreateShotA1(x, y, speed, angle, graphic, 0);
	ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
	let size = 2.5;
	let radius = GetShotDataInfoA1(graphic, TARGET_ENEMY, INFO_COLLISION)*2*size;
	
	
	
	//ObjMove_SetAcceleration(obj, acceleration);
	//ObjMove_SetMaxSpeed(obj, maxspeed);
	ObjMove_SetAngularVelocity(obj, velocity);
	
	ObjRender_SetScaleXYZ(obj, size, size, 0);
	
	let endX = ObjMove_GetX(obj);
	let endY = ObjMove_GetY(obj);
	
	let lasangle=0;
	let explode=0;
	
	
	while(!Obj_IsDeleted(obj)){
	
		ObjShot_SetIntersectionCircleA1(obj, radius);

		
		endX = ObjMove_GetX(obj);
		endY = ObjMove_GetY(obj);
		
		let angle1 = ObjMove_GetAngle(obj);
		
		ShrinkShot(endX, endY, angle1, graphic, size, 0.05);
		
		if(explode==0){
			if(endX >= GetStgFrameWidth){
				ObjMove_SetSpeed(obj, 0);
				lasangle=180;
				explode=1;
				allow++;
				}
			if(endX <= 0){
				ObjMove_SetSpeed(obj, 0);
				lasangle=0;
				explode=1;
				allow++;
				}
			
			if(endY <= 0){
				ObjMove_SetSpeed(obj, 0);
				lasangle=90;
				explode=1;
				allow++;
				}
			if(endY >= GetStgFrameHeight){
				ObjMove_SetSpeed(obj, 0);
				lasangle=270;
				explode=1;
				allow++;
				}
			}
		
		if(allow>=ALLOW){
			Obj_Delete(obj);
			LaserSE;
			}
		yield;
		}
		
	CreateStraightLaserA1(endX, endY, lasangle, 640, 30, 50, rand(1001, 1015), 40);
	CreateLooseLaserA1(endX, endY, 40, lasangle, 640, 50, PELLET_WHITE, 40);
	//StarBomb(endX+30*cos(lasangle), endY+30*sin(lasangle), 3, lasangle, rand(-0.6, 0.6), graphic, 0, rand(10, 60));
	allow=0;
	}
	
	
task ShrinkShot(x, y, angle, graphic, size, rate){
	let obj1 = CreateShotA1(x, y, 0, angle, graphic, 0);
	ObjRender_SetScaleXYZ(obj1, size, size, 0);
	ObjRender_SetBlendType(obj1, BLEND_ADD_RGB);
	
	let radius = GetShotDataInfoA1(ARROWHEAD_RED, TARGET_PLAYER, INFO_COLLISION)*size;
	
	while(!Obj_IsDeleted(obj1)){
		ObjRender_SetScaleXYZ(obj1, size, size, 0);
		radius = GetShotDataInfoA1(graphic, TARGET_ENEMY, INFO_COLLISION)*2*size;
		
		ObjShot_SetIntersectionCircleA1(obj1, radius);
		
		size-=rate;
			
		if(size<=0){
			Obj_Delete(obj1);
			}
		yield;
		}
	}
	
task Square{
	BotSquare;
	TopSquare;
	LeftSquare;
	RightSquare;
	}
	
task BotSquare{
	let X = 0;
	let Y = GetStgFrameHeight+16;
	let NUM = 100;
	loop(NUM){
		let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, rand(356, 362)+500, 50);
		ObjShot_SetIntersectionEnable(obj, false);
		
		ObjMove_SetDestAtFrame(obj, X, Y, 100);
		
		X+=GetStgFrameWidth/NUM;
		}
	}
	
task TopSquare{
	let X = 0;
	let Y = -16;
	let NUM = 100;
	loop(NUM){
		let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, rand(356, 362)+500, 50);
		ObjShot_SetIntersectionEnable(obj, false);
		
		ObjMove_SetDestAtFrame(obj, X, Y, 100);
		
		X+=GetStgFrameWidth/NUM;
		}
	}
	
task LeftSquare{
	let X = -16;
	let Y = 0;
	let NUM = 100;
	loop(NUM){
		let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, rand(356, 362)+500, 50);
		ObjShot_SetIntersectionEnable(obj, false);
		
		ObjMove_SetDestAtFrame(obj, X, Y, 100);
		
		Y+=GetStgFrameHeight/NUM;
		}
	}
	
task RightSquare{
	let X = GetStgFrameWidth+16;
	let Y = 0;
	let NUM = 100;
	loop(NUM){
		let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, rand(356, 362)+500, 50);
		ObjShot_SetIntersectionEnable(obj, false);
		
		ObjMove_SetDestAtFrame(obj, X, Y, 100);
		
		Y+=GetStgFrameHeight/NUM;
		}
	}
	
task BounceShot(x, y, speed, angle, graphic1, graphic2, delay){
	
	let obj= CreateShotA1(x, y, speed, angle, graphic1, delay);
	
	let endX= ObjMove_GetX(obj);
	let endY= ObjMove_GetY(obj);
	
	let explode=0;
	let mod=0;
	
	let angle1 = angle;
	
	while(!Obj_IsDeleted(obj)){
		endX= ObjMove_GetX(obj);
		endY= ObjMove_GetY(obj);
		
		if(endX>=GetStgFrameWidth){
			Obj_Delete(obj);
			explode=1;
			mod=180;
			}
			
		if(endX<=0){
			Obj_Delete(obj);
			explode=1;
			mod=180;
			}
			
		if(endY<=0){
			Obj_Delete(obj);
			explode=1;
			mod=360;
			}
		
		if(endY>=GetStgFrameHeight){
			Obj_Delete(obj);
			explode=1;
			mod=360;
			}
			
		yield;
		}
	if(explode==1){
		CreateShotA1(endX, endY, speed+0.1, mod-angle1, graphic2, 10);
		}
	}