#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Konn Final"]
//#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=210;
let angle1=90;
let shot=0;
let move=0;
let s = -1;
let allow=1;
let DIRECTION=1;

@Event
{
	alternative(GetEventType())
	case(EV_REQUEST_LIFE){
		SetScriptResult(8000)
	}
	
	case(EV_REQUEST_TIMER){
		SetScriptResult(160);
		}
}

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

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

	ObjPrim_SetTexture(objBoss, imgExRumia); //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_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);
	
	let cx = GetStgFrameWidth() / 2;

	ObjMove_SetDestAtFrame(objBoss, cx, 110, 30);
	
	//BACKG(path3, path1, objBoss);
	
	ObjCutin_SetSpellcardS4("Awakened Rhakshasa \"The Ultimate Spiriting Away\"", GetCurrentScriptDirectory~"AmaneImage.png", YABUSAME, 104, 104, 104);
	ObjCutin_LaunchS3(YABUSAME, GetCurrentScriptDirectory~"KonnCutin.png", "Hard");
	//BG;
	
	//SpellBG(objBoss, GetCurrentScriptDirectory~"Smoke.png");
	KonnBG(objBoss, GetCurrentScriptDirectory~"woodblock10d.jpg", GetCurrentScriptDirectory~"eff02.png", -5, 0, 250, 300, 256, 256, 1.7, 1.8);
	MCircle(objBoss, GetCurrentScriptDirectory~"e_ef_61.png");

}

@MainLoop{
	animation++;
	
	if(ObjEnemy_GetInfo(objBoss, INFO_LIFE)<=0){
		death(objBoss);
		//BossDeath(objBoss);
		}
		
	if(animation==40){
		//Slash(objBoss);
		}
		
	if(animation==60){
		Concentration(objBoss, "STAR", 80, 120, 4, 250, 4, 250, 4, 250);
		}
	
	if(!Obj_IsDeleted(objBoss)){
	if(animation>=200){
	
		ObjEnemy_SetIntersectionCircleToShot(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 32);

		ObjEnemy_SetIntersectionCircleToPlayer(objBoss, ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 24);
		
		move++;
		
		if(move==300){
			if(allow<=110){
				//ObjMove_SetDestAtFrame(objBoss, rand(80, GetStgFrameWidth-80), rand(90,130), 150);
				}
			//move=0;
			}
			
		frame++;
		
		if(frame==20){
			
			let angle=rand(0, 360);
			let NUM = 14;
			loop(NUM){
				angle+=360/NUM;
				SpiritShot(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 3, angle, BUTTERFLY_WHITE, BL_SMALL_WHITE, 0);
				}
			
			frame=-20;
			s*=-1;
			}
			
			
		shot++;
		
		if(Around==1){
			if(GetPlayerX()>ObjMove_GetX(objBoss)){
				ObjMove_SetPosition(GetPlayerObjectID, GetPlayerX()-1.2, GetPlayerY());
				}
			if(GetPlayerX()<ObjMove_GetX(objBoss)){
				ObjMove_SetPosition(GetPlayerObjectID, GetPlayerX()+1.2, GetPlayerY());
				}
			}
		
		if(move==260){
			ObjMove_SetDestAtFrame(objBoss, GetPlayerX(),  rand(90,130), 30);
			}
		
		if(move==300){
			Concentration(objBoss, "STAR", 80, 50, 4, 250, 4, 250, 4, 250);
			Around=1;
			GreatSlash(objBoss);
			Warning;
			move=-200;
			}
			
		if(shot==600){
			WHOOOP;
			BulletConcentration(GetStgFrameWidth/2, 15, "STAR", 80, 40, 4, 250, 4, 250, 4, 250);
			StarSpawnFamiliar(GetStgFrameWidth/2, 15, BL_SMALL_BLUE);
			}
			
		if(shot==1800){
			//WHOOOP;
			//BulletConcentration(15, 15, GetCurrentScriptDirectory~"star.png", 80, 40, 128, 128, 4, 250, 4, 250, 4, 250);
			//BulletConcentration(GetStgFrameWidth-15, 15, GetCurrentScriptDirectory~"star.png", 80, 40, 128, 128, 4, 250, 4, 250, 4, 250);
			//StarSpawnFamiliar(15, 15, BL_SMALL_BLUE);
			//StarSpawnFamiliar(GetStgFrameWidth-15, 15, BL_SMALL_BLUE);
			}
			
		if(shot==1200){
			WHOOOP;
			BulletConcentration(15, 120, "STAR", 80, 40, 4, 250, 4, 250, 4, 250);
			BulletConcentration(GetStgFrameWidth-15, 120, "STAR", 80, 40, 4, 250, 4, 250, 4, 250);
			KnifeSpawnFamiliar(15, 120, BL_SMALL_RED);
			KnifeSpawnFamiliar(GetStgFrameWidth-15, 120, BL_SMALL_RED);
			}
		}
		}
		
	yield;
	}
	
task SpiritShot(x, y, speed, angle, graphic, graphic2, delay){
	let obj = CreateShotA1(x, y, speed, angle, graphic, delay);
	SetSpecialDelete(obj, graphic);
	
	ObjMove_SetAngularVelocity(obj, 0.6*s);
	
	let endX = ObjMove_GetX(obj);
	let endY = ObjMove_GetY(obj);
	let frame=0;
	
	while(!Obj_IsDeleted(obj)){
	
		endX = ObjMove_GetX(obj);
		endY = ObjMove_GetY(obj);
		
		frame++;
		
		if(frame==2){
			FaderShot(endX, endY, graphic2, 2.2);
			frame=0;
			}
			
		yield;
		}
		
	task FaderShot(x, y, graphic2, fademod){
		let obj1 = CreateShotA1(x, y, 0, 0, graphic2, 0);
		ObjShot_SetIntersectionEnable(obj1, false);
		ObjRender_SetBlendType(obj1,BLEND_ADD_ARGB);
		let BLEND = 50;
		ObjRender_SetAlpha(obj1,BLEND);
		
		while(!Obj_IsDeleted(obj1)){
			ObjRender_SetAlpha(obj1,BLEND);
			BLEND-=fademod;
			
			if(BLEND<=0){
				Obj_Delete(obj1);
				}
			
			yield;
			}
		}
	}
	
task KnifeSpawn(x, y, angle, graphic1, graphic2, graphic3, delay){

	while(!Obj_IsDeleted(objBoss)){
		LASSHOT(x, y, angle, graphic1, graphic2, graphic3, delay);

		angle+=rand(0, 360);
		
		loop(70){
			yield;
			}
		}
		
	task LASSHOT(x, y, angle, graphic1, graphic2, graphic3, delay){
		let obj = CreateShotA1(x, y, 3, angle, graphic1, delay);
		SetSpecialDelete(obj, graphic1);
		
		ObjRender_SetBlendType(obj, BLEND_ADD_ARGB);
		ObjRender_SetAlpha(obj, 0);
		
		let endX = ObjMove_GetX(obj);
		let endY = ObjMove_GetY(obj);
		
		let explode=0;
		
		while(!Obj_IsDeleted(obj)){
			endX = ObjMove_GetX(obj);
			endY = ObjMove_GetY(obj);
			
			if(endX >= GetStgFrameWidth){
				explode=1;
				Obj_Delete(obj);
				}
				
			if(endX <= 0){
				explode=1;
				Obj_Delete(obj);
				}
				
			if(endY >= GetStgFrameHeight){
				explode=1;
				Obj_Delete(obj);
				}
				
			if(endY <= 0){
				explode=1;
				Obj_Delete(obj);
				}
				
			yield;
			}
			
		if(explode==1){
			let lasX = endX;
			let lasY = endY;
			
			let LASANGLE = atan2(GetPlayerY()-lasY, GetPlayerX() - lasX)+rand(-2, 2);
			
			CreateStraightLaserA1(lasX, lasY, LASANGLE, 640, 20, 40, graphic2, 30);
			CreateLooseLaserA1(lasX, lasY, 40, LASANGLE, 640, 40, graphic3, 30);
			}
		}
	}
	
task StarSpawnFamiliar(X, Y, graphic){
	let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, graphic, 0);
	
	
	ObjShot_SetSpellResist(obj, true);
	
	ObjMove_SetDestAtFrame(obj, X, Y, 120);
	
	let frame=0;
	
	let endX = ObjMove_GetX(obj);
	let endY = ObjMove_GetY(obj);
	
	while(!Obj_IsDeleted(objBoss)){
		frame++;
		endX = ObjMove_GetX(obj);
		endY = ObjMove_GetY(obj);
		
		if(frame==140){
			let obj1 = CreateShotA1(endX, endY, rand(0.5, 2), atan2(GetPlayerY() - endY, GetPlayerX() - endX)+rand(-30, 30), S_STAR_GRAY, rand(0, 20)); 
			let obj2 = CreateShotA1(endX, endY, rand(0.5, 2), atan2(GetPlayerY() - endY, GetPlayerX() - endX)+rand(-30, 30), S_STAR_GRAY, rand(0, 20)); 
			let obj3 = CreateShotA1(endX, endY, rand(0.5, 2), atan2(GetPlayerY() - endY, GetPlayerX() - endX)+rand(-30, 30), S_STAR_GRAY, rand(0, 20)); 

			SetSpecialDelete(obj1, S_STAR_GRAY);
			SetSpecialDelete(obj2,  S_STAR_GRAY);
			SetSpecialDelete(obj3,  S_STAR_GRAY);
			frame=100;
			}
			
		yield;
		}
	}
	
task GreatSlash(tobj){
	let obj = CreateStraightLaserA1(ObjMove_GetX(objBoss)-80, -50, 90, 640, 250, 80, ST_LASER_GRAY, 100);
	let obj1 = CreateStraightLaserA1(ObjMove_GetX(objBoss)+80, -50, 90, 640, 250, 80, ST_LASER_GRAY, 100);
	
	ObjShot_SetDeleteFrame(obj1, 0);
	ObjShot_SetDeleteFrame(obj, 0);
	
	CreateLooseLaserA1(ObjMove_GetX(tobj), -50, 40, 90, 640, 340, PELLET_GRAY, 100);
	
	
	
	ObjStLaser_SetSource(obj, false);
	ObjStLaser_SetSource(obj1, false);
	
	DELAYSOUND;
	let POS = 110;
	//DelaySlash(POS);
	while(POS>0){
		let obj2 = CreateStraightLaserA1(ObjMove_GetX(objBoss)+rand(-POS, POS), 0, 90, 640, 5, 0, ST_LASER_GRAY, 0);
		let obj3 = CreateStraightLaserA1(ObjMove_GetX(objBoss)+rand(-POS, POS), 0, 90, 640, 5, 0, ST_LASER_GRAY, 0);
		
		ObjShot_SetDeleteFrame(obj2, 0);
		ObjShot_SetDeleteFrame(obj3, 0);
		
		ObjMove_SetX(obj, ObjMove_GetX(objBoss)-POS);
		ObjMove_SetX(obj1, ObjMove_GetX(objBoss)+POS);
		POS-=2.5;
		yield;
		}
		
	task DELAYSOUND {
		let COUNT = 0;
		
		while(COUNT<=100){
			COUNT++;
			if(COUNT==100){
				CHINK;
				Around=0;
				}
			yield;
			}
		}
	}
	
task KnifeSpawnFamiliar(X, Y, graphic){
	let obj = CreateShotA1(ObjMove_GetX(objBoss), ObjMove_GetY(objBoss), 0, 0, graphic, 0);
	
	ObjShot_SetSpellResist(obj, true);
	
	ObjMove_SetDestAtFrame(obj, X, Y, 120);
	
	let frame=0;
	
	let endX = ObjMove_GetX(obj);
	let endY = ObjMove_GetY(obj);
	
	while(!Obj_IsDeleted(objBoss)){
		frame++;
		endX = ObjMove_GetX(obj);
		endY = ObjMove_GetY(obj);
		
		if(frame==140){
			let obj1 = CreateShotA1(endX, endY, 1.2, atan2(GetPlayerY() - endY, GetPlayerX() - endX), KNIFE_GRAY, 5); 
			let obj2 = CreateShotA1(endX, endY, 1.3, atan2(GetPlayerY() - endY, GetPlayerX() - endX), KNIFE_GRAY, 5); 
			let obj3 = CreateShotA1(endX, endY, 1.4, atan2(GetPlayerY() - endY, GetPlayerX() - endX), KNIFE_GRAY, 5); 
			let obj4 = CreateShotA1(endX, endY, 1.5, atan2(GetPlayerY() - endY, GetPlayerX() - endX), KNIFE_GRAY, 5); 

			SetSpecialDelete(obj1, KNIFE_GRAY);
			SetSpecialDelete(obj2, KNIFE_GRAY);
			SetSpecialDelete(obj3, KNIFE_GRAY);
			SetSpecialDelete(obj4, KNIFE_GRAY);
			frame=0;
			}
			
		yield;
		}
	}
	
task BossDeath(tobj){
	//BossDeathGraphic(tobj, 90);
	
	loop(100){
		yield;
		}
		
	death(tobj);
	}
	
task BossDeathGraphic(tobj, COUNT){

	loop(COUNT){
	let X = ObjMove_GetX(tobj);
	let Y = ObjMove_GetY(tobj);
	let ANGLE = rand(0, 360);
	let DIST = 0;
	let graphic=GetCurrentScriptDirectory ~ "star.png";
	let ANGLE1 = rand(0, 360);
	let graphicx = 128;
	let graphicy = 128;
	let SCALE = 0;
	
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, graphic);
	Obj_SetRenderPriorityI(obj,22);
	ObjRender_SetBlendType(obj,BLEND_ADD_ARGB);
	ObjRender_SetAlpha(obj,230);
	ObjSprite2D_SetSourceRect(obj,0,0,graphicx, graphicy);
	ObjSprite2D_SetDestCenter(obj);
	ObjRender_SetPosition(obj, X+DIST+rand(-5, 5)*cos(ANGLE),Y+DIST+rand(-5, 5)*sin(angle),0);
	ObjRender_SetScaleXYZ(obj, SCALE, SCALE, 0);		
	ObjRender_SetAngleXYZ(obj,0, 0, ANGLE1);
	ObjRender_SetColor(obj, rand(4, 255), rand(4, 255), rand(4, 255));

	SHRINK(obj, SCALE);
	MOVE(obj, X, Y, DIST, ANGLE, 0);
		
		task SHRINK(TOBJ, MOD){
			while(!Obj_IsDeleted(TOBJ)){
			
			ObjRender_SetScaleXYZ(TOBJ, MOD, MOD, 0);
			
			if(MOD<0.6){
				MOD+=0.005;
				}
				
			if(MOD>=0.6){
				Obj_Delete(TOBJ);
				}
			
			yield;
			}
			}
			
		task MOVE(TOBJ1, XMOD, YMOD, MOD1, ANGLEMOD, MODCAP){
			while(!Obj_IsDeleted(TOBJ1)){
				ObjRender_SetPosition(TOBJ1, XMOD+MOD1*cos(ANGLEMOD), YMOD+MOD1*sin(ANGLEMOD), 0);
				
				if(MOD1<MODCAP){
					MOD1+=1;
					}
				yield;
				}
				
			}
			
		yield;
		}
	}