#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Barrel Blast"]
#Text["Kongs Launch themselves at you"]

let bossDonkey;
let bossDiddy;
let bossDixie;
let bossKiddie;

let BG;
let bossX = 0;
let bossY = 0;
let planes = [];
let imgBoss = GetCurrentScriptDirectory ~ "BarrelBlast.png";
let bg = GetCurrentScriptDirectory ~ "bramble.png";
let imgEnemy = GetCurrentScriptDirectory ~ "BarrelEnemy.txt";
let barrelShotSE;
let barrelBreakSE;
let bananaSE;
let kkdkSE;
let barrelArray = [];
let brokenBarrelArray = [-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1];
let xOff = 100;
let yOff = 100;

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


@Initialize {

	bossDonkey = ObjEnemy_Create(OBJ_ENEMY_BOSS);//define boss and register it
	//BG = ObjPrim_Create(OBJ_SPRITE_2D);//not really a boss
	ObjEnemy_Regist(bossDonkey);//dk
	ObjMove_SetPosition(bossDonkey,48,GetStgFrameHeight+100);//warp boss to location
	
	bossDiddy = ObjEnemy_Create(OBJ_ENEMY);
	//define boss and register it
	//BG = ObjPrim_Create(OBJ_SPRITE_2D);//not really a boss
	ObjEnemy_Regist(bossDiddy);//diddy
	ObjEnemy_SetLife(bossDiddy, 250);//warp boss to location
	
	bossDixie = ObjEnemy_Create(OBJ_ENEMY);//define boss and register it
	//BG = ObjPrim_Create(OBJ_SPRITE_2D);//not really a boss
	ObjEnemy_Regist(bossDixie);//dixie
	ObjEnemy_SetLife(bossDixie, 250);
	ObjMove_SetPosition(bossDixie,248,GetStgFrameHeight+100);//warp boss to location
	
	bossKiddie = ObjEnemy_Create(OBJ_ENEMY);//define boss and register it
	//BG = ObjPrim_Create(OBJ_SPRITE_2D);//not really a boss
	ObjEnemy_Regist(bossKiddie);//kiddie
	ObjEnemy_SetLife(bossKiddie, 250);
	ObjMove_SetPosition(bossKiddie,348,GetStgFrameHeight+100);//warp boss to location


	
	
	barrelShotSE = CreateSFX(GetCurrentScriptDirectory ~ "./se/BarrelShoot.wav",65);
	barrelBreakSE = CreateSFX(GetCurrentScriptDirectory ~ "./se/BarrelBreak.wav",75);
	bananaSE = CreateSFX(GetCurrentScriptDirectory ~ "./se/Donkey kong 64 Oh Banana.mp3",100);
	kkdkSE = CreateSFX(GetCurrentScriptDirectory ~ "./se/Golden Banana Acquisition.mp3",90);
	
	mainTask; //run main task
}

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

@MainLoop {
	bossX = ObjMove_GetX(bossDonkey);
	bossY = ObjMove_GetY(bossDonkey);
	
	
	
	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 {
	
	renderBG;
	movement;
	renderDonkey;
	renderDiddy;
	renderDixie;
	renderKiddie;
}

task renderDonkey {
	let x =0;
	let alive = true;
	let cool = false;
	ObjPrim_SetTexture(bossDonkey,imgBoss);
	ObjSprite2D_SetSourceRect(bossDonkey,x,0,x+63,63);
	ObjSprite2D_SetDestCenter(bossDonkey);
	ObjRender_SetScaleXYZ(bossDonkey,1,1,0);
	ObjEnemy_SetDamageRate(bossDonkey, 0, 0);//this keeps you from killing the boss before it enters the screen
	let temp = 0;
	collision;
	topography;
	Death;
	while(alive) {
		temp++;
		if(temp==2){
			x = modc(x+65,1040);
			ObjSprite2D_SetSourceRect(bossDonkey,x,0,x+63,63);
			temp=0;
		
		}
		yield;
	}
	
	task collision{
		let collisionArray;
		let lastIntersected;
		while(alive){
			
			if(!cool){
				ObjEnemy_SetIntersectionCircleToShot(bossDonkey,bossX,bossY,24);//collision for shots and player
				ObjEnemy_SetIntersectionCircleToPlayer(bossDonkey,bossX,bossY,21);
				ascent(xBarrel in 0..4){
					ascent(yBarrel in 0..4){
						if(absolute(48+(100*xBarrel)-ObjMove_GetX(bossDonkey)) < 16 && absolute(48+(100*yBarrel)-ObjMove_GetY(bossDonkey)) < 16){
							if(barrelArray[12-4*xBarrel+yBarrel] != -1){
								enterBarrel(xBarrel,yBarrel);
								//break;
							}
							
						}
						//yield;
					}
					//yield;
				}			
				
			}
			yield;
		}
	}
	
	function enterBarrel(enterX,enterY){
		ObjRender_SetAlpha(bossDonkey,0);
		ObjEnemy_SetDamageRate(bossDonkey, 0, 0);
		ObjMove_SetSpeed(bossDonkey,0);
		ObjMove_SetPosition(bossDonkey,48+100*enterX,48+100*enterY);
		let waitPeriod = rand(30,90);
		cooldown(waitPeriod+20,enterX,enterY);
		wait(waitPeriod);
		if(barrelArray[12-4*enterX+enterY] != -1){
			ObjSound_Play(barrelShotSE);
			ObjRender_SetAlpha(bossDonkey,255);
			ObjEnemy_SetDamageRate(bossDonkey, 100, 100);
			ObjMove_SetSpeed(bossDonkey,4);
			ObjMove_SetAngle(bossDonkey,45*floor(barrelArray[12-4*enterX+enterY]/2)-45);
		}
		yield;
	}
	
	task cooldown(frames,coolX,coolY){
		cool = true;
		loop(frames-20){
			if(barrelArray[12-4*coolX+coolY] == -1 ){
				ObjSound_Play(barrelShotSE);
				ObjRender_SetAlpha(bossDonkey,255);
				ObjEnemy_SetDamageRate(bossDonkey, 100, 100);
				ObjMove_SetSpeed(bossDonkey,4);
				ObjMove_SetAngle(bossDonkey,45*floor(brokenBarrelArray[12-4*coolX+coolY]/2)-45);
				
				break;
			}
			yield;
		}
		loop(20){
			yield;
		}
		cool = false;
		yield;
	}
	
	task topography{
		while(alive){
			if(ObjMove_GetX(bossDonkey)<-8){
				ObjMove_SetPosition(bossDonkey,GetStgFrameWidth + 7,ObjMove_GetY(bossDonkey));
			}else if(ObjMove_GetX(bossDonkey)>GetStgFrameWidth + 8){
				ObjMove_SetPosition(bossDonkey, -7,ObjMove_GetY(bossDonkey));
			}
			if(ObjMove_GetY(bossDonkey)<-26){
				ObjMove_SetPosition(bossDonkey,ObjMove_GetX(bossDonkey),GetStgFrameHeight+25);
			}else if(ObjMove_GetY(bossDonkey)>GetStgFrameHeight+26){
				ObjMove_SetPosition(bossDonkey,ObjMove_GetX(bossDonkey),-25);
			}
			
				yield;
		}
	}
	
	task Death {
		while(ObjEnemy_GetInfo(bossDonkey, INFO_LIFE) > 0){
			yield;
		}
		alive = false;
		ObjRender_SetAlpha(bossDonkey,0);
		ObjMove_SetPosition(bossDonkey,GetStgFrameWidth+25,GetStgFrameHeight+7);
		while(ObjEnemy_GetInfo(bossDonkey, INFO_LIFE) <= 0){
			//ObjMove_SetPosition(bossDonkey,GetStgFrameWidth+30,GetStgFrameHeight+30);
			if(ObjEnemy_GetInfo(bossDiddy, INFO_LIFE) <= 0 && ObjEnemy_GetInfo(bossDixie, INFO_LIFE) <= 0 && ObjEnemy_GetInfo(bossKiddie, INFO_LIFE) <= 0 ){
				wait(30);
				OooBanana;
				return;
				
			}
			yield;
		}
		
		
			
			
		
	}

}


task renderDiddy {
	let x =0;
	let alive = true;
	let cool = false;
	
	
	ObjMove_SetPosition(bossDiddy,148,GetStgFrameHeight+100);
	
	
	ObjPrim_SetTexture(bossDiddy,imgBoss);
	ObjSprite2D_SetSourceRect(bossDiddy,x,65,x+63,128);
	ObjSprite2D_SetDestCenter(bossDiddy);
	ObjRender_SetScaleXYZ(bossDiddy,1,1,0);
	ObjEnemy_SetDamageRate(bossDiddy, 0, 0);//this keeps you from killing the boss before it enters the screen
	let temp = 0;
	//ObjMove_SetPosition(bossDiddy,50,425);
	wait(40);
	collision;
	topography;
	Death;
	while(alive) {
		temp++;
		if(temp==2){
			x = modc(x+65,1040);
			ObjSprite2D_SetSourceRect(bossDiddy,x,65,x+63,128);
			//ObjMove_SetPosition(bossDiddy,GetPlayerX,GetPlayerY);
			temp=0;
		
		}
		yield;
	}
	
	task collision{
		let collisionArray;
		let lastIntersected;
		while(alive){
			
			if(!cool){
				ObjEnemy_SetIntersectionCircleToShot(bossDiddy,ObjMove_GetX(bossDiddy),ObjMove_GetY(bossDiddy),24);//collision for shots and player
				ObjEnemy_SetIntersectionCircleToPlayer(bossDiddy,ObjMove_GetX(bossDiddy),ObjMove_GetY(bossDiddy),21);
				ascent(xBarrel in 0..4){
					ascent(yBarrel in 0..4){
						if(absolute(48+(100*xBarrel)-ObjMove_GetX(bossDiddy)) < 16 && absolute(48+(100*yBarrel)-ObjMove_GetY(bossDiddy)) < 16){
							if(barrelArray[12-4*xBarrel+yBarrel] != -1){
								enterBarrel(xBarrel,yBarrel);
								//break;
							}
							
						}
						//yield;
					}
					//yield;
				}			
				
			}
			yield;
		}
	}
	
	function enterBarrel(enterX,enterY){
		ObjRender_SetAlpha(bossDiddy,0);
		ObjEnemy_SetDamageRate(bossDiddy, 0, 0);
		ObjMove_SetSpeed(bossDiddy,0);
		ObjMove_SetPosition(bossDiddy,48+100*enterX,48+100*enterY);
		let waitPeriod = rand(30,90);
		cooldown(waitPeriod+20,enterX,enterY);
		wait(waitPeriod);
		if(barrelArray[12-4*enterX+enterY] != -1){
			ObjSound_Play(barrelShotSE);
			ObjRender_SetAlpha(bossDiddy,255);
			ObjEnemy_SetDamageRate(bossDiddy, 100, 100);
			ObjMove_SetSpeed(bossDiddy,4);
			ObjMove_SetAngle(bossDiddy,45*floor(barrelArray[12-4*enterX+enterY]/2)-45);
		}
		yield;
	}
	
	task cooldown(frames,coolX,coolY){
		cool = true;
		loop(frames-20){
			if(barrelArray[12-4*coolX+coolY] == -1 ){
				ObjSound_Play(barrelShotSE);
				ObjRender_SetAlpha(bossDiddy,255);
				ObjEnemy_SetDamageRate(bossDiddy, 100, 100);
				ObjMove_SetSpeed(bossDiddy,4);
				ObjMove_SetAngle(bossDiddy,45*floor(brokenBarrelArray[12-4*coolX+coolY]/2)-45);
				
				break;
			}
			yield;
		}
		loop(20){
			yield;
		}
		cool = false;
		yield;
	}
	
	task topography{
		while(alive){
			if(ObjMove_GetX(bossDiddy)<-8){
				ObjMove_SetPosition(bossDiddy,GetStgFrameWidth + 7,ObjMove_GetY(bossDiddy));
			}else if(ObjMove_GetX(bossDiddy)>GetStgFrameWidth + 8){
				ObjMove_SetPosition(bossDiddy, -7,ObjMove_GetY(bossDiddy));
			}
			if(ObjMove_GetY(bossDiddy)<-26){
				ObjMove_SetPosition(bossDiddy,ObjMove_GetX(bossDiddy),GetStgFrameHeight+25);
			}else if(ObjMove_GetY(bossDiddy)>GetStgFrameHeight+26){
				ObjMove_SetPosition(bossDiddy,ObjMove_GetX(bossDiddy),-25);
			}
			
				yield;
		}
	}
	
	task Death {
		while(ObjEnemy_GetInfo(bossDiddy, INFO_LIFE) > 0){
			yield;
		}
		alive = false;
		ObjRender_SetAlpha(bossDiddy,0);	
	}

}

task renderDixie {
	let x =0;
	let alive = true;
	let cool = false;
	
	
	ObjMove_SetPosition(bossDixie,248,GetStgFrameHeight+100);
	
	
	ObjPrim_SetTexture(bossDixie,imgBoss);
	ObjSprite2D_SetSourceRect(bossDixie,x,130,x+63,194);
	ObjSprite2D_SetDestCenter(bossDixie);
	ObjRender_SetScaleXYZ(bossDixie,1,1,0);
	ObjEnemy_SetDamageRate(bossDixie, 0, 0);//this keeps you from killing the boss before it enters the screen
	let temp = 0;
	//ObjMove_SetPosition(bossDixie,50,425);
	wait(40);
	collision;
	topography;
	Death;
	while(alive) {
		temp++;
		if(temp==2){
			x = modc(x+65,1040);
			ObjSprite2D_SetSourceRect(bossDixie,x,130,x+63,194);
			//ObjMove_SetPosition(bossDixie,GetPlayerX,GetPlayerY);
			temp=0;
		
		}
		yield;
	}
	
	task collision{
		let collisionArray;
		let lastIntersected;
		while(alive){
			
			if(!cool){
				ObjEnemy_SetIntersectionCircleToShot(bossDixie,ObjMove_GetX(bossDixie),ObjMove_GetY(bossDixie),24);//collision for shots and player
				ObjEnemy_SetIntersectionCircleToPlayer(bossDixie,ObjMove_GetX(bossDixie),ObjMove_GetY(bossDixie),21);
				ascent(xBarrel in 0..4){
					ascent(yBarrel in 0..4){
						if(absolute(48+(100*xBarrel)-ObjMove_GetX(bossDixie)) < 16 && absolute(48+(100*yBarrel)-ObjMove_GetY(bossDixie)) < 16){
							if(barrelArray[12-4*xBarrel+yBarrel] != -1){
								enterBarrel(xBarrel,yBarrel);
								//break;
							}
							
						}
						//yield;
					}
					//yield;
				}			
				
			}
			yield;
		}
	}
	
	function enterBarrel(enterX,enterY){
		ObjRender_SetAlpha(bossDixie,0);
		ObjEnemy_SetDamageRate(bossDixie, 0, 0);
		ObjMove_SetSpeed(bossDixie,0);
		ObjMove_SetPosition(bossDixie,48+100*enterX,48+100*enterY);
		let waitPeriod = rand(30,90);
		cooldown(waitPeriod+20,enterX,enterY);
		wait(waitPeriod);
		if(barrelArray[12-4*enterX+enterY] != -1){
			ObjSound_Play(barrelShotSE);
			ObjRender_SetAlpha(bossDixie,255);
			ObjEnemy_SetDamageRate(bossDixie, 100, 100);
			ObjMove_SetSpeed(bossDixie,4);
			ObjMove_SetAngle(bossDixie,45*floor(barrelArray[12-4*enterX+enterY]/2)-45);
		}
		yield;
	}
	
	task cooldown(frames,coolX,coolY){
		cool = true;
		loop(frames-20){
			if(barrelArray[12-4*coolX+coolY] == -1 ){
				ObjSound_Play(barrelShotSE);
				ObjRender_SetAlpha(bossDixie,255);
				ObjEnemy_SetDamageRate(bossDixie, 100, 100);
				ObjMove_SetSpeed(bossDixie,4);
				ObjMove_SetAngle(bossDixie,45*floor(brokenBarrelArray[12-4*coolX+coolY]/2)-45);
				
				break;
			}
			yield;
		}
		loop(20){
			yield;
		}
		cool = false;
		yield;
	}
	
	task topography{
		while(alive){
			if(ObjMove_GetX(bossDixie)<-8){
				ObjMove_SetPosition(bossDixie,GetStgFrameWidth + 7,ObjMove_GetY(bossDixie));
			}else if(ObjMove_GetX(bossDixie)>GetStgFrameWidth + 8){
				ObjMove_SetPosition(bossDixie, -7,ObjMove_GetY(bossDixie));
			}
			if(ObjMove_GetY(bossDixie)<-26){
				ObjMove_SetPosition(bossDixie,ObjMove_GetX(bossDixie),GetStgFrameHeight+25);
			}else if(ObjMove_GetY(bossDixie)>GetStgFrameHeight+26){
				ObjMove_SetPosition(bossDixie,ObjMove_GetX(bossDixie),-25);
			}
			
				yield;
		}
	}
	
	task Death {
		while(ObjEnemy_GetInfo(bossDixie, INFO_LIFE) > 0){
			yield;
		}
		alive = false;
		ObjRender_SetAlpha(bossDixie,0);	
	}

}


task renderKiddie {
	let x =0;
	let alive = true;
	let cool = false;
	
	
	ObjMove_SetPosition(bossKiddie,348,GetStgFrameHeight+100);
	
	
	ObjPrim_SetTexture(bossKiddie,imgBoss);
	ObjSprite2D_SetSourceRect(bossKiddie,x,195,x+63,259);
	ObjSprite2D_SetDestCenter(bossKiddie);
	ObjRender_SetScaleXYZ(bossKiddie,1,1,0);
	ObjEnemy_SetDamageRate(bossKiddie, 0, 0);//this keeps you from killing the boss before it enters the screen
	let temp = 0;
	//ObjMove_SetPosition(bossKiddie,50,425);
	wait(40);
	collision;
	topography;
	Death;
	while(alive) {
		temp++;
		if(temp==2){
			x = modc(x+65,715);
			ObjSprite2D_SetSourceRect(bossKiddie,x,195,x+63,259);
			//ObjMove_SetPosition(bossKiddie,GetPlayerX,GetPlayerY);
			temp=0;
		
		}
		yield;
	}
	
	task collision{
		let collisionArray;
		let lastIntersected;
		while(alive){
			
			if(!cool){
				ObjEnemy_SetIntersectionCircleToShot(bossKiddie,ObjMove_GetX(bossKiddie),ObjMove_GetY(bossKiddie),24);//collision for shots and player
				ObjEnemy_SetIntersectionCircleToPlayer(bossKiddie,ObjMove_GetX(bossKiddie),ObjMove_GetY(bossKiddie),21);
				ascent(xBarrel in 0..4){
					ascent(yBarrel in 0..4){
						if(absolute(48+(100*xBarrel)-ObjMove_GetX(bossKiddie)) < 16 && absolute(48+(100*yBarrel)-ObjMove_GetY(bossKiddie)) < 16){
							if(barrelArray[12-4*xBarrel+yBarrel] != -1){
								enterBarrel(xBarrel,yBarrel);
								//break;
							}
							
						}
						//yield;
					}
					//yield;
				}			
				
			}
			yield;
		}
	}
	
	function enterBarrel(enterX,enterY){
		ObjRender_SetAlpha(bossKiddie,0);
		ObjEnemy_SetDamageRate(bossKiddie, 0, 0);
		ObjMove_SetSpeed(bossKiddie,0);
		ObjMove_SetPosition(bossKiddie,48+100*enterX,48+100*enterY);
		let waitPeriod = rand(30,90);
		cooldown(waitPeriod+20,enterX,enterY);
		wait(waitPeriod);
		if(barrelArray[12-4*enterX+enterY] != -1){
			ObjSound_Play(barrelShotSE);
			ObjRender_SetAlpha(bossKiddie,255);
			ObjEnemy_SetDamageRate(bossKiddie, 100, 100);
			ObjMove_SetSpeed(bossKiddie,4);
			ObjMove_SetAngle(bossKiddie,45*floor(barrelArray[12-4*enterX+enterY]/2)-45);
		}
		yield;
	}
	
	task cooldown(frames,coolX,coolY){
		cool = true;
		loop(frames-20){
			if(barrelArray[12-4*coolX+coolY] == -1 ){
				ObjSound_Play(barrelShotSE);
				ObjRender_SetAlpha(bossKiddie,255);
				ObjEnemy_SetDamageRate(bossKiddie, 100, 100);
				ObjMove_SetSpeed(bossKiddie,4);
				ObjMove_SetAngle(bossKiddie,45*floor(brokenBarrelArray[12-4*coolX+coolY]/2)-45);
				
				break;
			}
			yield;
		}
		loop(20){
			yield;
		}
		cool = false;
		yield;
	}
	
	task topography{
		while(alive){
			if(ObjMove_GetX(bossKiddie)<-8){
				ObjMove_SetPosition(bossKiddie,GetStgFrameWidth + 7,ObjMove_GetY(bossKiddie));
			}else if(ObjMove_GetX(bossKiddie)>GetStgFrameWidth + 8){
				ObjMove_SetPosition(bossKiddie, -7,ObjMove_GetY(bossKiddie));
			}
			if(ObjMove_GetY(bossKiddie)<-26){
				ObjMove_SetPosition(bossKiddie,ObjMove_GetX(bossKiddie),GetStgFrameHeight+25);
			}else if(ObjMove_GetY(bossKiddie)>GetStgFrameHeight+26){
				ObjMove_SetPosition(bossKiddie,ObjMove_GetX(bossKiddie),-25);
			}
			
				yield;
		}
	}
	
	task Death {
		while(ObjEnemy_GetInfo(bossKiddie, INFO_LIFE) > 0){
			yield;
		}
		alive = false;
		ObjRender_SetAlpha(bossKiddie,0);	
	}

}


task movement {
	//wait(120);
	//ObjMove_SetDestAtFrame(bossDonkey,GetCenterX(),GetCenterY(),1);
	planeLeft;
	planeDrop;
	wait(100);
	ObjMove_SetDestAtSpeed(bossDonkey,48,120,4);// move boss to desired x y location at desired speed
	ObjMove_SetDestAtSpeed(bossDiddy,148,120,4);
	ObjMove_SetDestAtSpeed(bossDixie,248,120,4);
	ObjMove_SetDestAtSpeed(bossKiddie,348,120,4);
}	

function planeLeft{
	let Ytemp = 48;
	loop((GetStgFrameHeight-64)/96){
		
		planes = planes ~ [CreateShotA1(GetStgFrameWidth+64+2,Ytemp,2,180,19,0)];
		Ytemp += yOff;
		yield;
	}
	
	
}

function planeDrop{
	
	loop(4){
		let planeTemp = 0;
		wait(50);
		loop(length(planes)){
			BarrelEnemy(ObjMove_GetX(planes[planeTemp]),ObjMove_GetY(planes[planeTemp]));
			
			planeTemp++;
		}
		
		yield;
	}
}

task BarrelEnemy(x,y){	
	
	let obj = ObjEnemy_Create(OBJ_ENEMY);
	ObjEnemy_Regist(obj);
	
	ObjEnemy_SetLife(obj, 550);
	ObjEnemy_SetIntersectionCircleToShot(obj, x, y, 28);
	ObjEnemy_SetIntersectionCircleToPlayer(obj, x, y, 18);
	
	let state = truncate(rand(0,16));
	
	barrelArray = barrelArray ~ [state];

	let dir = (-1)^(truncate(rand(0,2)));
	let rotateSpeed = rand(6,12);
	let left = 65*state;
	let right = 65*state + 64;
	let top = 975;
	let bottom = 1039;
	let timer = 0;
	let alive = true;
	
	 //= ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, imgBoss);
	ObjMove_SetPosition(obj,x,y);
	
	
	ObjSprite2D_SetSourceRect(obj, left, top, right, bottom);
	ObjSprite2D_SetDestCenter(obj);
	Death;

	while(alive){
		timer++;
		if(timer >= rotateSpeed){
			left += dir*65;
			right += dir*65;
			timer = 0;
			state = (state + dir)%16;
			let  d = (4*truncate((GetStgFrameWidth-(38+x))/xOff)+truncate(y/yOff));
			barrelArray[d] = state;
			if(x <= 42 ){
				//CreateShotA1(x,y,2,270,19,0);
			}
			//if(left >= 1040){left = 0; right = 64;}
			ObjSprite2D_SetSourceRect(obj, left, top, right, bottom);
			
		}
		ObjEnemy_SetIntersectionCircleToShot(obj, x, y, 28);
		ObjEnemy_SetIntersectionCircleToPlayer(obj, x, y, 18);
		yield;
	}

	task Death {
		while(ObjEnemy_GetInfo(obj, INFO_LIFE) > 0){
			yield;
		}
		alive = false;
		brokenBarrelArray[(4*truncate((GetStgFrameWidth-(38+x))/xOff)+truncate(y/yOff))] = barrelArray[(4*truncate((GetStgFrameWidth-(38+x))/xOff)+truncate(y/yOff))];
		barrelArray[(4*truncate((GetStgFrameWidth-(38+x))/xOff)+truncate(y/yOff))] = -1;//same as d in whilw alivw loop
		Obj_Delete(obj);
		ObjSound_Play(barrelBreakSE);
		
		
			
			
		
	}
	
}
	
task OooBanana{	
	let timer = 33;
	let active = true;
	let obj = ObjPrim_Create(OBJ_SPRITE_2D);
	ObjPrim_SetTexture(obj, GetCurrentScriptDirectory~"BarrelBlast.png");
	ObjSound_Play(bananaSE);
	ObjRender_SetPosition(obj,GetCenterX,-10,0);
	
	
	ObjSprite2D_SetSourceRect(obj, 975, 845, 1040, 973);
	ObjSprite2D_SetDestCenter(obj);
	Get;

	while(active)
	{
		timer++;
		ObjRender_SetPosition(obj,GetCenterX,408-(448*32*(cos(timer/32))^2)/(0.5*timer),0);
		yield;
	}
	task Get{
		let Dis;
		while(!Obj_IsDeleted(obj)){
			Dis=(GetPlayerY-ObjRender_GetY(obj))^2+(GetPlayerX-ObjRender_GetX(obj))^2;

			if(Dis<=600){
				active = false;
				ObjRender_SetAlpha(obj,255);
				ObjSound_Play(kkdkSE);
				//ObjRender_SetBlendType(obj, BLEND_ADD_RGB);
				ObjRender_SetColor(obj,150,150,25);
				//ObjRender_SetColorHSV(obj, 200, 255,255);
				wait(80);
				Obj_Delete(bossDonkey);
				DeleteShotAll(TYPE_ALL, TYPE_IMMEDIATE);
				SetAutoDeleteObject(true);
				CloseScript(GetOwnScriptID());

			}
			yield;
		}
	}
}
	

