#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["KONG"]
#Text["collect the KONG Letters"]

let bossObj;
let BG;
let bossX = 0;
let bossY = 0;
let alphaTimer = 0;
let imgBoss = GetCurrentScriptDirectory ~ "BarrelBlast.png";
let bg = GetCurrentScriptDirectory ~ "bramble.png";
let familiar = GetCurrentScriptDirectory ~ "BarrelEnemy.txt";

//
#include "script/DonkeyKong/Donkey_ShotConst.txt"
#include "script/lib/usefulFunctions.txt"

@Initialize {

	bossObj = ObjEnemy_Create(OBJ_ENEMY_BOSS);//define boss and register it
	//BG = ObjPrim_Create(OBJ_SPRITE_2D);//not really a boss
	ObjEnemy_Regist(bossObj);
	ObjMove_SetPosition(bossObj,192,-100);//warp boss to location


	ObjMove_SetDestAtSpeed(bossObj,192,120,5);// move boss to desired x y location at desired speed
	ObjEnemy_SetDamageRate(bossObj, 0,0);
	mainTask; //run main task
}

@Event {
	alternative(GetEventType())//set boss timer and life
	case(EV_REQUEST_LIFE) {
		SetScriptResult(1);
	}
	case(EV_REQUEST_TIMER) {
		SetScriptResult(60);
	}
}

@MainLoop {
	bossX = ObjMove_GetX(bossObj);
	bossY = ObjMove_GetY(bossObj);
	
	ObjEnemy_SetIntersectionCircleToShot(bossObj,bossX,bossY,24);//collision for shots and player
	ObjEnemy_SetIntersectionCircleToPlayer(bossObj,bossX,bossY,21);
	
	yield;
}

task renderBG {
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	Obj_SetRenderPriorityI(obj,21); 
	ObjPrim_SetTexture(obj,bg); 
	ObjSprite2D_SetSourceRect(obj,0,0,256,256);
	ObjSprite2D_SetDestRect(obj,0,0,GetStgFrameWidth,GetStgFrameHeight);
} 

@Finalize {

}
/*
function wait(w) {
	loop(w){yield;}
}*/

task mainTask {
	renderBoss;
	renderBG;
	TFinalize;
	movement;
}

task renderBoss {
	let x =0;
	ObjPrim_SetTexture(bossObj,imgBoss);
	ObjSprite2D_SetSourceRect(bossObj,x,0,x+63,63);
	ObjSprite2D_SetDestCenter(bossObj);
	ObjRender_SetScaleXYZ(bossObj,1,1,0);
	let temp = 0;
	
	while(!Obj_IsDeleted(bossObj)) {
		temp++;
		if(temp==2){
		x = modc(x+65,1040);
		ObjSprite2D_SetSourceRect(bossObj,x,0,x+63,63);
		temp=0;
		}
		yield;
	}

	

}


task movement {
	wait(90);
	//KongLetter(GetStgFrameWidth-32,GetStgFrameHeight-32,"k");
	rngCoconut;
	rings;
	
	
	
}

function rings{
	let InnerRadius = 65;
	//let BananaRadius = 20;
	let OuterRadius = 220;
	let innerRing = [];
	let outerRing = [];
	let bananaRing = [];
	let bananaRingClock = []; 
	let kongList = [];
	let dropoutList = [ 111, 115, 114, 107];
	let temp=0;
	let bananaTemp=0;
	//let innerRingAngle = [];
	let AngleOff =0;
	while(length(kongList)<4){
		let kongListTemp = 0;
		let randValue = floor(rand(0,9.9));
		let kongListBool = true;
		loop(length(kongList)){
			if(kongList[kongListTemp] == randValue){
				kongListBool = false;
			}
			kongListTemp++;
		}
		if(kongListBool){kongList = kongList ~ [randValue];}
		
		yield;
	}
	
	loop(20){
		if(temp%2 == 0){
			let kongListTemp = 0;
			let kongListBool = false;
			loop(length(kongList)){
				if(temp == 2*kongList[kongListTemp]){
					kongListBool = true;
				}
				kongListTemp++;
			}
			if(!kongListBool){
				innerRing = innerRing ~ [CreateShotA1(bossX,bossY,2,temp*360/20,truncate(rand(101,126)),0)];
				if((ObjShot_GetImageID(innerRing[temp]) == 111)||(ObjShot_GetImageID(innerRing[temp]) == 115)||(ObjShot_GetImageID(innerRing[temp]) == 114)||(ObjShot_GetImageID(innerRing[temp]) == 107)){
					ObjShot_SetGraphic(innerRing[temp],truncate(rand(116,126)));
				}
			}else{
				let dropTemp = floor(rand(0,length(dropoutList)));
				innerRing = innerRing ~	[CreateShotA1(bossX,bossY,2,temp*360/20,dropoutList[dropTemp],0)];
				dropoutList = erase(dropoutList, dropTemp);
				
			}
			
			
		}else{
			innerRing = innerRing ~ [CreateShotA1(bossX,bossY,2,temp*360/20 + 360/20,100,0)];
		}
		if(temp%4 ==0){
			bananaTemp=0;
			loop(11){
				bananaRing = bananaRing ~ [CreateShotA1(bossX,bossY,2,temp*360/20,Banana,0)];
				ObjMove_SetDestAtFrame(bananaRing[length(bananaRing)-1],bossX+(InnerRadius+15+12*bananaTemp)*sin(temp*(360/20)+1.5*bananaTemp),bossY+(InnerRadius+15+12*bananaTemp)*cos(temp*(360/20)+1.5*bananaTemp),30+2*bananaTemp);
				ObjShot_SetAutoDelete(bananaRing[length(bananaRing)-1],false);
				bananaTemp++;
			}
		}
		if((temp+2)%4 == 0){
			bananaTemp=0;
			loop(11){
				bananaRingClock = bananaRingClock ~ [CreateShotA1(bossX,bossY,2,temp*360/20,Banana,0)];
				ObjMove_SetDestAtFrame(bananaRingClock[length(bananaRingClock)-1],bossX+(InnerRadius+15+12*bananaTemp)*sin(temp*(360/20)-1.5*bananaTemp),bossY+(InnerRadius+15+12*bananaTemp)*cos(temp*(360/20)-1.5*bananaTemp),30+2*bananaTemp);
				ObjShot_SetAutoDelete(bananaRingClock[length(bananaRingClock)-1],false);
				bananaTemp++;
			}
		}
		outerRing = outerRing ~ [CreateShotA1(bossX,bossY,2,temp*360/20,truncate(rand(101,126)),0)];
				if((ObjShot_GetImageID(outerRing[temp]) == 111)||(ObjShot_GetImageID(outerRing[temp]) == 115)||(ObjShot_GetImageID(outerRing[temp]) == 114)||(ObjShot_GetImageID(outerRing[temp]) == 107)){
					ObjShot_SetGraphic(outerRing[temp],truncate(rand(116,126)));
				}
		ObjMove_SetDestAtFrame(innerRing[temp],bossX+InnerRadius*sin(temp*360/20),bossY+InnerRadius*cos(temp*360/20),30);
		ObjMove_SetDestAtFrame(outerRing[temp],bossX+OuterRadius*sin(temp*360/20),bossY+OuterRadius*cos(temp*360/20),50);
		ObjShot_SetAutoDelete(outerRing[temp],false);
		
		//ObjMove_SetDestAtFrame(innerRing[temp+1],bossX + InnerRadius*cos(temp*360/20 + 360/20),bossY + InnerRadius*sin(temp*360/20 + 360/20),20);
		temp++;
	}
	wait(50);
	temp = 0;
	loop(length(innerRing)){
			if(ObjShot_GetImageID(innerRing[temp])==111){
				KongLetter(ObjMove_GetX(innerRing[temp]),ObjMove_GetY(innerRing[temp]),"k");
				ObjShot_SetIntersectionEnable(innerRing[temp], false);
				ObjShot_SetGraphic(innerRing[temp],99);//invisible
				break;
			}
			temp++;
	}
	while(!Obj_IsDeleted(bossObj)){//KONG ! get){
		temp=0;
		loop(length(innerRing)){
			ObjMove_SetDestAtFrame(innerRing[temp],bossX+InnerRadius*sin(temp*360/20+AngleOff),bossY+InnerRadius*cos(temp*360/20+AngleOff),1);
			ObjMove_SetDestAtFrame(outerRing[temp],bossX+OuterRadius*sin(temp*360/20-AngleOff),bossY+OuterRadius*cos(temp*360/20-AngleOff),1);
			temp++;
		}
		bananaTemp=0;
		loop(length(bananaRing)){
			temp =0;
			
			let bananaOff = bananaTemp;
			while(bananaOff>=11){
				bananaOff -= 11;
				temp++;
			}
			ObjMove_SetDestAtFrame(bananaRing[bananaTemp],bossX+(InnerRadius+15+12*bananaOff)*sin(temp*360/5+1.5*bananaOff-0.5*AngleOff),bossY+(InnerRadius+15+12*bananaOff)*cos(temp*360/5+1.5*bananaOff-0.5*AngleOff),1);
			bananaTemp++;
		}
		bananaTemp = 0;
		loop(length(bananaRingClock)){
			temp =0;
			
			let bananaOff = bananaTemp;
			while(bananaOff>=11){
				bananaOff -= 11;
				temp++;
			}
			ObjMove_SetDestAtFrame(bananaRingClock[bananaTemp],bossX+(InnerRadius+15+12*bananaOff)*sin(temp*360/5-1.5*bananaOff+0.5*AngleOff+36),bossY+(InnerRadius+15+12*bananaOff)*cos(temp*360/5-1.5*bananaOff+0.5*AngleOff+36),1);
			bananaTemp++;										//bossX+(InnerRadius+10+10*bananaTemp)*sin(temp*(360/20)-1.5*bananaTemp)
		}
		AngleOff--;
		yield;
		
	}
	//CreateShotA1(BossX,BossY,3,Angle,banana,0)
	
	
	function K{
		let k;
		let obj=ObjPrim_Create(OBJ_SPRITE_2D);
		//KongLetter(obj,bossX,bossY,"k");
	
		return obj;
	
	}

	function O{
		let o;
		while(!Obj_IsDeleted(o)){
		
		}
		N;
	}

	function N{
		let n;
		while(!Obj_IsDeleted(n)){
		
		}
		G;
	}

	function G{
		let g;
		Obj_Delete(bossObj);
        DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
        SetAutoDeleteObject(true);
        CloseScript(GetOwnScriptID());
	}


	task KongLetter(x,y,letter)
	{	

	
	let alphaTimer = 0;
	let dir = 1;
	let left = 227;
	let right = 242;
	let top = 942;
	let bottom = 957;
	let active = true;
	let kongAngleOff = 0;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"BarrelBlast.png");
	

	ObjRender_SetPosition(obj,x,y,0);
	let ang = 180 +GetPointAngle(bossX,bossY,x,y);
	//let ang =  180 + GetAtanAngle(bossObj,obj);
	let kongRadius = GetDistance(ObjMove_GetX(obj),ObjMove_GetY(obj),bossX,bossY);
	
	if ( letter == "k"){left=228;right=243;top=942;bottom=958;}
	if ( letter == "o"){left=228;right=243;top=958;bottom=974;}
	if ( letter == "n"){left=212;right=227;top=958;bottom=974;}
	if ( letter == "g"){left=228;right=243;top=926;bottom=942;}
	ObjSprite2D_SetSourceRect(obj, left, top, right, bottom);
	ObjSprite2D_SetDestCenter(obj);
	Get;

	while(active)
	{
		alphaTimer++;
		if(alphaTimer >= 10){ObjRender_SetAlpha(obj,0)}
		if(alphaTimer == 14){alphaTimer = 0;ObjRender_SetAlpha(obj,255)}
		ObjRender_SetPosition(obj,bossX+65*cos(ang+kongAngleOff),bossY+65*sin(ang+kongAngleOff),0);
		kongAngleOff++;
		yield;
	}
	while(!active){
		ObjRender_SetPosition(obj,bossX+65*cos(ang+kongAngleOff),bossY+65*sin(ang+kongAngleOff),0);
		kongAngleOff++;
		yield;
	}
	task Get{
		let Dis;
		while(!Obj_IsDeleted(obj)){
			Dis=(GetPlayerY-ObjRender_GetY(obj))^2+(GetPlayerX-ObjRender_GetX(obj))^2;

			if(Dis<=300){
				active = false;
				ObjRender_SetAlpha(obj,255);
				//ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
					ObjRender_SetColor(obj,150,150,25);
				//ObjRender_SetColorHSV(obj, 200, 255,255);
				if(letter == "k"){
					let disTemp = 0;
					loop(length(innerRing)){
						if(ObjShot_GetImageID(innerRing[disTemp])==115){
							KongLetter(ObjMove_GetX(innerRing[disTemp]),ObjMove_GetY(innerRing[disTemp]),"o");
							ObjShot_SetIntersectionEnable(innerRing[disTemp], false);
							ObjShot_SetGraphic(innerRing[disTemp],99);//invisible
							break;
						}
					disTemp++;
					}
				}else if(letter == "o"){
					let disTemp = 0;
					loop(length(innerRing)){
						if(ObjShot_GetImageID(innerRing[disTemp])==114){
							KongLetter(ObjMove_GetX(innerRing[disTemp]),ObjMove_GetY(innerRing[disTemp]),"n");
							ObjShot_SetIntersectionEnable(innerRing[disTemp], false);
							ObjShot_SetGraphic(innerRing[disTemp],99);//invisible
							break;
						}
					disTemp++;
					}
				}else if(letter == "n"){
					let disTemp = 0;
					loop(length(innerRing)){
						if(ObjShot_GetImageID(innerRing[disTemp])==107){
							KongLetter(ObjMove_GetX(innerRing[disTemp]),ObjMove_GetY(innerRing[disTemp]),"g");
							ObjShot_SetIntersectionEnable(innerRing[disTemp], false);
							ObjShot_SetGraphic(innerRing[disTemp],99);//invisible
							break;
						}
					disTemp++;
					}
				}else if(letter == "g"){
					ObjEnemy_SetDamageRate(bossObj, 100, 100);
				}

			}
			yield;
		}
	}
	}
	
}

task rngCoconut{
while(!Obj_IsDeleted(bossObj)){
	let angle = rand(0,360);
	CreateShotA1(bossX,bossY,1.5,angle,Coconut,0);
	wait(18);
	yield;
	}
}

task TFinalize{
        while(ObjEnemy_GetInfo(bossObj, INFO_LIFE) > 0){yield;}
        Obj_Delete(bossObj);
        DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
        SetAutoDeleteObject(true);
        CloseScript(GetOwnScriptID());
    }

