#TouhouDanmakufu[Single]
#ScriptVersion[3]
#Title["Rollout"]
#Text["Diddy doesn't kill Armydillos"]

let bossObj;
let BG;
let bossX = 0;
let bossY = 0;
let dir = 1;
let imgBoss = GetCurrentScriptDirectory ~ "BarrelBlast.png";
let bg = GetCurrentScriptDirectory ~ "bramble.png";


//
#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,GetCenterX,-100);//warp boss to location


	ObjMove_SetDestAtSpeed(bossObj,GetCenterX,75,5);// move boss to desired x y location at desired speed
	
	mainTask; //run main task
}

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

task renderBoss {
	let x =715;
	ObjPrim_SetTexture(bossObj,imgBoss);
	ObjSprite2D_SetSourceRect(bossObj,x,390,x+64,454);
	ObjSprite2D_SetDestCenter(bossObj);
	ObjRender_SetScaleXYZ(bossObj,1,1,0);
	let temp = 0;
	wait(35);
	loop(25){
		temp++;
		if(temp%5 == 0){
		x+=65;
		}
		ObjSprite2D_SetSourceRect(bossObj,x,390,x+64,454);
		yield;
	}
	temp = 0;
	while(!Obj_IsDeleted(bossObj)) {
		temp++;
		if(temp==2){
		x = modc(x+65,975);
		ObjSprite2D_SetSourceRect(bossObj,x,455,x+63,510);
		ObjRender_SetAngleY(bossObj,90-90*dir);
		temp=0;
		}
		yield;
	}

	

}


task movement {
	wait(90);
	while(!Obj_IsDeleted(bossObj)){
		ObjMove_SetDestAtSpeed(bossObj,(((1+dir)*GetStgFrameWidth)/2)-10*dir,75,5);
		if(ObjMove_GetX(bossObj)>GetStgFrameWidth-15){
			dir = -1;
		}else if(ObjMove_GetX(bossObj)<15){
			dir = 1;
		}
		yield;
	}
	
	
}

task roll{
	let count = 0;
	let iter = rand(50,90);
	
	while(!Obj_IsDeleted(bossObj)){
	count++;
		if(count >= iter){
			count = 0;
			let randSide = (-1)^(truncate(rand(0,4)));
			hommingArmy(randSide);
			randArmy(-randSide);
		}
		yield;
	}
}

task hommingArmy(side){
	let hA = CreateShotA1(GetCenterX+side*GetCenterX,75,6,90+90*side,ArmyRoll,0);
	let roll = true;
	let fall = false;
	while(roll){
		if(absolute(ObjMove_GetX(hA)-ObjMove_GetX(bossObj))<32){
			roll = false;
			fall = true;
			ObjMove_SetAngle(hA,90);
			ObjShot_SetGraphic(hA,ArmyFall);
		}
		yield;
	}
	while(fall){
		if(absolute(ObjMove_GetY(hA)-GetPlayerY)<16){
			roll = false;
			fall = true;
			ObjMove_SetSpeed(hA,0);
			ObjShot_SetGraphic(hA,Army);
			wait(20);
			ObjShot_SetGraphic(hA,ArmyStartRoll);
			wait(20);
			fall = false;
			ObjShot_SetGraphic(hA,ArmyRoll);
			let right = -1;
			if ((GetPlayerX - ObjMove_GetX(hA))>0){
				right = 1;
			}
			ObjMove_SetAngle(hA,90-90*right);
			ObjMove_SetSpeed(hA,4);
		}
		yield;
	}
	yield;
}

task randArmy(side){
	let rA = CreateShotA1(GetCenterX+side*GetCenterX,75,6,90+90*side,ArmyRoll,0);
	let roll = true;
	let fall = false;
	let fallTimer = rand(10,100);
	let temp = 0;
	while(roll){
		if(absolute(ObjMove_GetX(rA)-ObjMove_GetX(bossObj))<32){
			roll = false;
			fall = true;
			ObjMove_SetAngle(rA,90);
			ObjShot_SetGraphic(rA,ArmyFall);
		}
		yield;
	}
	while(fall){
		if(temp > fallTimer){
			roll = false;
			fall = true;
			ObjMove_SetSpeed(rA,0);
			ObjShot_SetGraphic(rA,Army);
			wait(20);
			ObjShot_SetGraphic(rA,ArmyStartRoll);
			wait(20);
			fall = false;
			ObjShot_SetGraphic(rA,ArmyRoll);
			let right = -1;
			if ((GetPlayerX - ObjMove_GetX(rA))>0){
				right = 1;
			}
			ObjMove_SetAngle(rA,90-90*right);
			ObjMove_SetSpeed(rA,4);
		}
		temp++;
		yield;
	}
	yield;
}

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

