// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Buzz Bomber Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.timer
private alias object.value1 : object.startPos.x
private alias object.value2 : object.hasShot
private alias object.value3 : object.hideOnOffScreen // Set from the editor

// Player Aliases
private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom

private alias 0 : BUZZBOMBER_BUZZAROUND
private alias 1 : BUZZBOMBER_IDLEDELAY
private alias 2 : BUZZBOMBER_FOUNDPLAYER
private alias 3 : BUZZBOMBER_SHOOTING
private alias 4 : BUZZBOMBER_HIDDEN

private alias 0 : BUZZBOMBER_ANI_FLY
private alias 1 : BUZZBOMBER_ANI_FLY_IDLE
private alias 2 : BUZZBOMBER_ANI_SHOOT
private alias 3 : BUZZBOMBER_ANI_NONE


// ========================
// Function Declarations
// ========================

reserve function BuzzBomber_DebugDraw
reserve function BuzzBomber_DebugSpawn


// ========================
// Function Definitions
// ========================

private function BuzzBomber_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(2, FX_FLIP, object.xpos, object.ypos)
end function


private function BuzzBomber_DebugSpawn
	CreateTempObject(TypeName[Buzz Bomber], 0, object.xpos, object.ypos)
	GetBit(temp0, object.direction, 0)
	if temp0 == FLIP_NONE
		// Facing left
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].xvel 		= -0x40000
	else
		// Facing right
		object[tempObjectPos].direction = FLIP_X
		object[tempObjectPos].xvel 		= 0x40000
	end if
	
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case BUZZBOMBER_BUZZAROUND
		object.priority = PRIORITY_ACTIVE
		if object.timer < 127
			object.timer++
			object.xpos += object.xvel
		else
			object.timer 	= 0
			object.state 		= BUZZBOMBER_IDLEDELAY
			object.animation 	= BUZZBOMBER_ANI_FLY_IDLE
			object.direction 	^= FLIP_X
			FlipSign(object.xvel)
		end if
		break

	case BUZZBOMBER_IDLEDELAY
		if object.timer < 59
			object.timer++
		else
			object.timer 	= 0
			object.state 		= BUZZBOMBER_BUZZAROUND
			object.animation 	= BUZZBOMBER_ANI_FLY
			object.hasShot	= false
		end if
		break

	case BUZZBOMBER_FOUNDPLAYER
		if object.timer < 29
			object.timer++
		else
			object.timer = 0
			object.state++
			object.animation = BUZZBOMBER_ANI_SHOOT
		end if
		break

	case BUZZBOMBER_SHOOTING
		if object.timer < 59
			object.timer++
			if object.timer == 15
				CreateTempObject(TypeName[Buzz Bomber Shot], 0, object.xpos, object.ypos)
				object[tempObjectPos].direction = object.direction
				if object.direction == FLIP_NONE
					object[tempObjectPos].xpos -= 0x180000
					object[tempObjectPos].xvel = -0x20000
				else
					object[tempObjectPos].xpos += 0x180000
					object[tempObjectPos].xvel = 0x20000
				end if
				object[tempObjectPos].ypos += 0x1C0000
				object[tempObjectPos].yvel = 0x20000
			end if
		else
			object.timer	= 0
			object.state 		= BUZZBOMBER_BUZZAROUND
			object.animation 	= BUZZBOMBER_ANI_FLY
			object.hasShot	= true
		end if
		break

	case BUZZBOMBER_HIDDEN
		object.animation = BUZZBOMBER_ANI_NONE
		break
		
	end switch

	if object.outOfBounds == true
		temp0 		= object.xpos
		object.xpos = object.startPos.x
		if object.hideOnOffScreen == true
			object.state = BUZZBOMBER_HIDDEN
		end if

		if object.outOfBounds == true
			object.xpos = object.startPos.x

			GetBit(temp0, object.propertyValue, 0)
			if temp0 == false
				object.direction = FLIP_NONE
				object.xvel 	 = -0x40000
			else
				object.direction = FLIP_X
				object.xvel 	 = 0x40000
			end if
			object.timer	= 0
			object.animation 	= BUZZBOMBER_ANI_FLY
			object.priority 	= PRIORITY_BOUNDS
			object.state 		= BUZZBOMBER_BUZZAROUND
		else
			object.xpos = temp0
		end if
	end if
	
	// Player Collisons
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.state == BUZZBOMBER_BUZZAROUND
			if object.hasShot == false
				if object.propertyValue < 2
					BoxCollisionTest(C_TOUCH, object.entityPos, -96, -256, 96, 256, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, -48, -256, 48, 256, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				end if

				if checkResult == true
					object.state 		= BUZZBOMBER_FOUNDPLAYER
					object.timer 		= 0
					object.animation 	= BUZZBOMBER_ANI_FLY_IDLE
				end if
			end if
		end if

		if object.state < BUZZBOMBER_HIDDEN
			BoxCollisionTest(C_TOUCH, object.entityPos, -24, -12, 24, 12, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
			if checkResult == true
				CallFunction(Player_BadnikBreak)
			end if
		end if
	next
	
	object.frame = object.animationTimer
	object.frame >>= 1
	object.frame += 2
	object.animationTimer++
	object.animationTimer &= 3
end event


event ObjectDraw
	if object.state < BUZZBOMBER_HIDDEN
		if object.direction == FLIP_NONE
			DrawSpriteFX(object.frame, FX_INK, object.xpos, object.ypos)
		else
			object.frame += 2
			DrawSpriteFX(object.frame, FX_INK, object.xpos, object.ypos)
			object.frame -= 2
		end if
	end if

	switch object.animation
	case BUZZBOMBER_ANI_FLY
		DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
		object.frame += 4
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
		object.frame -= 4
		break

	case BUZZBOMBER_ANI_FLY_IDLE
		DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
		break

	case BUZZBOMBER_ANI_SHOOT
		DrawSpriteFX(1, FX_FLIP, object.xpos, object.ypos)
		break

	default
		break
		
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("GHZ/Objects.gif")
		SpriteFrame(-23, -9, 45, 19, 98, 74)
		SpriteFrame(-23, -9, 36, 29, 149, 49)
		SpriteFrame(-17, -15, 35, 8, 144, 79)
		SpriteFrame(-18, -10, 37, 5, 144, 88)
		SpriteFrame(-18, -15, 35, 8, 145, 220)
		SpriteFrame(-19, -10, 37, 5, 143, 229)
		SpriteFrame(13, 4, 6, 5, 98, 68)
		SpriteFrame(13, 4, 10, 5, 104, 68)
	end if

	CheckCurrentStageFolder("Zone02")
	if checkResult == true
		LoadSpriteSheet("MZ/Objects.gif")
		SpriteFrame(-23, -9, 45, 19, 1, 127)
		SpriteFrame(-23, -9, 36, 29, 1, 147)
		SpriteFrame(-17, -15, 35, 8, 38, 147)
		SpriteFrame(-18, -10, 37, 5, 38, 156)
		SpriteFrame(-18, -15, 35, 8, 38, 162)
		SpriteFrame(-19, -10, 37, 5, 38, 171)
		SpriteFrame(13, 4, 6, 5, 76, 153)
		SpriteFrame(13, 4, 10, 5, 74, 147)
	end if

	CheckCurrentStageFolder("Zone03")
	if checkResult == true
		LoadSpriteSheet("SYZ/Objects.gif")
		SpriteFrame(-23, -9, 45, 19, 1, 81)
		SpriteFrame(-23, -9, 36, 29, 1, 101)
		SpriteFrame(-17, -15, 35, 8, 38, 101)
		SpriteFrame(-18, -10, 37, 5, 38, 110)
		SpriteFrame(-18, -15, 35, 8, 38, 116)
		SpriteFrame(-19, -10, 37, 5, 38, 125)
		SpriteFrame(13, 4, 6, 5, 76, 123)
		SpriteFrame(13, 4, 10, 5, 83, 123)
	end if

	CheckCurrentStageFolder("Zone07")
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-23, -9, 45, 19, 1, 1)
		SpriteFrame(-23, -9, 36, 29, 1, 21)
		SpriteFrame(-17, -15, 35, 8, 38, 21)
		SpriteFrame(-18, -10, 37, 5, 38, 30)
		SpriteFrame(-18, -15, 35, 8, 38, 36)
		SpriteFrame(-19, -10, 37, 5, 38, 45)
		SpriteFrame(13, 4, 6, 5, 47, 15)
		SpriteFrame(13, 4, 10, 5, 54, 15)
	end if

	// propertyValue info:
	// bit 0 = direction
	// bit 1 = range type (large/small)

	foreach (TypeName[Buzz Bomber], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].inkEffect  = 2
		object[arrayPos0].alpha 	 = 0xC0
		GetBit(temp0, object[arrayPos0].propertyValue, 0)
		if temp0 == 0
			object[arrayPos0].direction = FLIP_NONE
			object[arrayPos0].xvel 		= -0x40000
		else
			object[arrayPos0].direction = FLIP_X
			object[arrayPos0].xvel 		= 0x40000
		end if
	next

	SetTableValue(TypeName[Buzz Bomber], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(BuzzBomber_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(BuzzBomber_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // direction
			GetBit(checkResult, object.propertyValue, 0)
			break
			
		case 1 // rangeSize
			GetBit(checkResult, object.propertyValue, 1)
			break
			
		case 2 // hideOnOffScreen
			CheckEqual(object.hideOnOffScreen, true)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // direction
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 0, checkResult)
			break
			
		case 1 // rangeSize
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 1, checkResult)
			break
			
		case 2 // hideOnOffScreen
			object.hideOnOffScreen = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 0)
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	DrawSpriteFX(1, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	// Make sure we have a fall-back, in case the current folder isn't actually a valid one
	temp0 = false
	
	CheckCurrentStageFolder("Zone02")
	temp0 |= checkResult
	if checkResult == true
		LoadSpriteSheet("MZ/Objects.gif")
		SpriteFrame(-23, -9, 45, 19, 1, 127)
		SpriteFrame(-17, -15, 35, 8, 38, 147)
	end if

	CheckCurrentStageFolder("Zone03")
	temp0 |= checkResult
	if checkResult == true
		LoadSpriteSheet("SYZ/Objects.gif")
		SpriteFrame(-23, -9, 45, 19, 1, 81)
		SpriteFrame(-17, -15, 35, 8, 38, 101)
	end if

	CheckCurrentStageFolder("Zone07")
	temp0 |= checkResult
	if checkResult == true
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-23, -9, 45, 19, 1, 1)
		SpriteFrame(-17, -15, 35, 8, 38, 21)
	end if

	if temp0 == false
		// We use GHZ's sprites as the fallback, "Zone01" (GHZ's folder) will come here too
		LoadSpriteSheet("GHZ/Objects.gif")
		SpriteFrame(-23, -9, 45, 19, 98, 74)
		SpriteFrame(-17, -15, 35, 8, 144, 79)
	end if
	
	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)

	AddEditorVariable("rangeSize")
	SetActiveVariable("rangeSize")
	AddEnumVariable("Large", 0)
	AddEnumVariable("Small", 1)
	
	AddEditorVariable("hideOnOffScreen")
	SetActiveVariable("hideOnOffScreen")
	AddEnumVariable("False", false)
	AddEnumVariable("True",  true)
end event
