#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Non Card #1"]
#Text["non card With donkey"]

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

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


let b1 = ObjShot_Create(OBJ_SHOT);
let b2 = ObjShot_Create(OBJ_SHOT);
let dkSpeed = 6;
let width = ((GetStgFrameWidth-20)-20);
let TT = width/dkSpeed;//travel time
let move = true;
let left = true;

@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,16,-100);//warp boss to location
	barrelShotSE = CreateSFX(GetCurrentScriptDirectory ~ "./se/BarrelShoot.wav",65);

	ObjMove_SetDestAtSpeed(bossObj,16,GetStgFrameHeight,5);// move boss to desired x y location at desired speed
	//SetShotAutoDeleteClip(-10, -250, GetStgFrameWidth+10, GetStgFrameHeight+10);
	
	
	mainTask; //run main task
	
}

@Event {
	alternative(GetEventType())//set boss timer and life
	case(EV_REQUEST_LIFE) {
		SetScriptResult(600);
	}
	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 {

}


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

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 start{
	ObjSound_Play(barrelShotSE);
	movement;
	while(ObjMove_GetY(bossObj)<ObjMove_GetY(b1)){
		yield;
	}
	move = false;
	bees;
	
}

task movement {
	//wait(120);
	
	let s1 = barrelCircuit(b1,20,20,20,GetStgFrameHeight-20,4);
	let s2 = barrelCircuit(b2,GetStgFrameWidth-20,20,GetStgFrameWidth-20,GetStgFrameHeight-20,12);
	//ObjMove_SetDestAtFrame(bossObj,ObjMove_GetX(b1),ObjMove_GetY(b1),1);
	if(ObjMove_GetX(bossObj) < GetCenterX){
		left = true;
	}
	while(!Obj_IsDeleted(bossObj)){
		if(ObjMove_GetY(b1)<20 || ObjMove_GetY(b1)>GetStgFrameHeight-20){
			ObjMove_SetAngle(b1,ObjMove_GetAngle(b1)+180);
		}
		if(ObjMove_GetY(b2)<20 || ObjMove_GetY(b2)>GetStgFrameHeight-20){
			ObjMove_SetAngle(b2,ObjMove_GetAngle(b2)+180);
		}
		
		if(left){
			
			if(!move){
					ObjMove_SetPosition(bossObj,GetStgFrameWidth-20,ObjMove_GetY(b2));
			
			ObjMove_SetPosition(bossObj,GetStgFrameWidth-20,GetStgFrameHeight+30);

				let DTE = (GetStgFrameHeight-20-ObjMove_GetY(b2))*((1+sin(ObjMove_GetAngle(b2)))/2)+((ObjMove_GetY(b2)-20)*((1-sin(ObjMove_GetAngle(b2)))/2));
				if(0.75 > absolute(TT-( ( ((1+sin(ObjMove_GetAngle(b2))*cf(ObjMove_GetY(b2),ObjMove_GetY(b1)))/2)*2*DTE + (ObjMove_GetY(b2)-ObjMove_GetY(b1))*cf(ObjMove_GetY(b2),ObjMove_GetY(b1)))/ObjMove_GetSpeed(b2) ))){
					across(b1);
				}
			}
			
		}else{
			if(!move){
					ObjMove_SetPosition(bossObj,GetStgFrameWidth-20,ObjMove_GetY(b2));
			
			ObjMove_SetPosition(bossObj,GetStgFrameWidth-20,GetStgFrameHeight+30);

				let DTE = (GetStgFrameHeight-20-ObjMove_GetY(b1))*((1+sin(ObjMove_GetAngle(b1)))/2)+((ObjMove_GetY(b1)-20)*((1-sin(ObjMove_GetAngle(b1)))/2));
				if(0.75 > absolute(TT-( ( ((1+sin(ObjMove_GetAngle(b1))*cf(ObjMove_GetY(b1),ObjMove_GetY(b2)))/2)*2*DTE + (ObjMove_GetY(b1)-ObjMove_GetY(b2))*cf(ObjMove_GetY(b1),ObjMove_GetY(b2)))/ObjMove_GetSpeed(b1) ))){
					across(b2);
				}
			}
			
			/*
			if(!move){
					ObjMove_SetPosition(bossObj,GetStgFrameWidth-20,ObjMove_GetY(b2));
			
			ObjMove_SetPosition(bossObj,GetStgFrameWidth-20,GetStgFrameHeight+30);

				let DTE = (GetStgFrameHeight-20-ObjMove_GetY(b2))*((1+sin(ObjMove_GetAngle(b2)))/2)+((ObjMove_GetY(b2)-20)*((1-sin(ObjMove_GetAngle(b2)))/2));
				if(0.75 > absolute(TT-( ( ((1+sin(ObjMove_GetAngle(b2))*cf(ObjMove_GetY(b2),ObjMove_GetY(b1)))/2)*2*DTE + (ObjMove_GetY(b2)-ObjMove_GetY(b1))*cf(ObjMove_GetY(b2),ObjMove_GetY(b1)))/ObjMove_GetSpeed(b2) ))){
					across(b1);
				}
			}
			*/
			
		}
		yield;
	}
	
	task across(barrel){
		move = true;
		ObjMove_SetPosition(bossObj,ObjMove_GetX(barrel),ObjMove_GetY(barrel));
		if(barrel == b1){
			ObjMove_SetDestAtSpeed(bossObj,ObjMove_GetX(b2),ObjMove_GetY(barrel),dkSpeed);
		}else{
			ObjMove_SetDestAtSpeed(bossObj,ObjMove_GetX(b1),ObjMove_GetY(barrel),dkSpeed);
		}
		ObjSound_Play(barrelShotSE);
		loop(5){
			wait(TT/5);
			//CreateShotA1(bossX,bossY,3,angleToPlayer,banana,0);
			homingBanana;
		}
		move = false;
		left = !left;
	}
	//CreateEnemyFromFile(familiar, 25, 25, 0, 0, 0);
}

task homingBanana{
	if(!Obj_IsDeleted(bossObj)){
	let banana = ObjShot_Create(OBJ_SHOT);
	ObjMove_SetPosition(banana,ObjMove_GetX(bossObj),ObjMove_GetY(bossObj));
	ObjMove_SetSpeed(banana,0);
	ObjMove_SetAngle(banana,atan2(GetPlayerY-bossY,GetPlayerX-bossX));
	ObjShot_SetGraphic(banana,18);
	ObjRender_SetScaleXYZ(banana,1.70,1.70,0);

	ObjShot_Regist(banana);
	yield;
	wait(45);
	ObjMove_SetSpeed(banana,4);
	}
	
}

function barrelCircuit(id, x1,y1,x2,y2,graphic){
	let barrel = id;//ObjShot_Create(OBJ_SHOT);
	ObjMove_SetPosition(barrel,ceil(rand(x1,x2)),ceil(rand(y1,y2)));
	let speed = rand(2.9,5);
	ObjMove_SetSpeed(barrel,speed);
	ObjMove_SetAngle(barrel,90+180*ceil(rand(0,2)));
	ObjShot_SetGraphic(barrel,graphic);
	ObjShot_Regist(barrel);
	/*
	if(rand(-1,1)>0){
		ObjMove_SetDestAtSpeed(barrel,x1,y1,2);
	}else{
		ObjMove_SetDestAtSpeed(barrel,x2,y2,2);
	}
	let k = 2;
	while(k==2){
		if(ObjMove_GetX(barrel)==x1 && ObjMove_GetY(barrel)==y1){
			ObjMove_SetDestAtSpeed(barrel,x2,y2,4);
			
			k = 3;
			yield;
			//wait(5);
		}else if(ObjMove_GetX(barrel)==x2 && ObjMove_GetY(barrel)==y2){
			ObjMove_SetDestAtSpeed(barrel,x1,y1,4);
			k = 3;
			yield;
		}
		yield;
		/*if(ObjMove_GetY(barrel)<y1 || ObjMove_GetY(barrel)>y2){
			ObjMove_SetAngle(barrel,ObjMove_GetAngle(barrel)+180);
		}
		yield;*/
		
	//}
	
	/*
	while(!Obj_IsDeleted(barrel)){
		if(ObjMove_GetY(barrel) == y1){
			MoveDown;
			
		}
		if(ObjMove_GetY(barrel) == y2){
			MoveUp;
		}
		yield;
		
	}
	*/
	return speed;
	task MoveDown{
		ObjMove_SetDestAtSpeed(barrel,x2,y2,2);
	}
	
	task MoveUp{
		ObjMove_SetDestAtSpeed(barrel,x1,y1,2);
	}
}

function cf(int1,int2){
	if(int1>int2){
		return 1;
	}else if (int2>int1){
		return -1;
	}else{
		return 0;
	}
}


task bees{
	let beeRadius = 100;
	let xPoint = rand(beeRadius,GetStgFrameWidth-beeRadius);
	let yPoint = -(beeRadius+30);
	let theta1 = 270;
	let theta2 = 30;
	let theta3 = 90;
	
	let bee1 = CreateShotA1(xPoint+beeRadius*cos(theta1),yPoint+beeRadius*sin(theta1),3,90,Bee,0);
	let bee2 = CreateShotA1(xPoint+beeRadius*cos(theta1+180),yPoint+beeRadius*sin(theta1+180),3,90,Bee,0);
	let bee3 = CreateShotA1(xPoint+beeRadius*cos(theta2),yPoint+beeRadius*sin(theta2),3,90,Bee,0);
	let bee4 = CreateShotA1(xPoint+beeRadius*cos(theta2+180),yPoint+beeRadius*sin(theta2+180),3,90,Bee,0);
	let bee5 = CreateShotA1(xPoint+beeRadius*cos(theta3),yPoint+beeRadius*sin(theta3),3,90,Bee,0);
	let bee6 = CreateShotA1(xPoint+beeRadius*cos(theta3+180),yPoint+beeRadius*sin(theta3+180),3,90,Bee,0);
	
	ObjShot_SetAutoDelete(bee1, false);
	ObjShot_SetAutoDelete(bee2, false);
	ObjShot_SetAutoDelete(bee3, false);
	ObjShot_SetAutoDelete(bee4, false);
	ObjShot_SetAutoDelete(bee5, false);
	ObjShot_SetAutoDelete(bee6, false);
	
	while(!Obj_IsDeleted(bossObj)){
		ObjMove_SetDestAtFrame(bee1,xPoint+beeRadius*cos(theta1),yPoint+beeRadius*sin(theta1),1);
		ObjMove_SetDestAtFrame(bee2,xPoint+beeRadius*cos(theta1+180),yPoint+beeRadius*sin(theta1+180),1);
		ObjMove_SetDestAtFrame(bee3,xPoint+beeRadius*cos(theta2),yPoint+beeRadius*sin(theta2),1);
		ObjMove_SetDestAtFrame(bee4,xPoint+beeRadius*cos(theta2+180),yPoint+beeRadius*sin(theta2+180),1);
		ObjMove_SetDestAtFrame(bee5,xPoint+beeRadius*cos(theta3),yPoint+beeRadius*sin(theta3),1);
		ObjMove_SetDestAtFrame(bee6,xPoint+beeRadius*cos(theta3+180),yPoint+beeRadius*sin(theta3+180),1);
		theta1+=0.5;
		theta2 ++;
		theta3 += 2;
		yPoint ++;
		if(yPoint>(GetStgFrameHeight+2*beeRadius+16)){
			Obj_Delete(bee1);
			Obj_Delete(bee2);
			Obj_Delete(bee3);
			Obj_Delete(bee4);
			Obj_Delete(bee5);
			Obj_Delete(bee6);
			bees;
			break;
		}
		
		
		yield;
		
	}
	
}


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

 

 



 




