// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Buzzer 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.shootTimer

// States
private alias 0 : BUZZER_FLYING
private alias 1 : BUZZER_STOPPED
private alias 2 : BUZZER_SHOOTING
private alias 3 : BUZZER_INVISIBLE

// Animations
private alias 0 : BUZZER_ANI_FLYING
private alias 1 : BUZZER_ANI_STOPPED
private alias 2 : BUZZER_ANI_SHOOTING
private alias 3 : BUZZER_ANI_INVISBLE

// 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


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

reserve function Buzzer_DebugDraw
reserve function Buzzer_DebugSpawn


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

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


private function Buzzer_DebugSpawn
	CreateTempObject(TypeName[Buzzer], 0, object.xpos, object.ypos)
	GetBit(temp0, object.direction, 0)
	if temp0 == FACING_RIGHT
		object[tempObjectPos].direction = FLIP_NONE
		object[tempObjectPos].xvel = -0x10000
	else
		object[tempObjectPos].direction = FLIP_X
		object[tempObjectPos].xvel = 0x10000
	end if
end function


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

event ObjectUpdate
	switch object.state
	case BUZZER_FLYING
		object.priority = PRIORITY_ACTIVE
		if object.timer < 256
			object.timer++
			object.xpos += object.xvel
		else
			object.timer = 0
			object.state = BUZZER_STOPPED
			object.animation = BUZZER_ANI_STOPPED
			object.direction ^= FLIP_X
			FlipSign(object.xvel)
		end if
		break

	case BUZZER_STOPPED
		if object.shootTimer < 30
			object.shootTimer++
		else
			object.shootTimer = 0
			object.state = BUZZER_FLYING
			object.animation = BUZZER_ANI_FLYING
			object.hasShot = false
		end if
		break

	case BUZZER_SHOOTING
		if object.shootTimer < 50
			object.shootTimer++
			if object.shootTimer == 30
				CreateTempObject(TypeName[Buzzer Shot], 0, object.xpos, object.ypos)
				object[tempObjectPos].direction = object.direction
				if object.direction == FLIP_NONE
					object[tempObjectPos].xpos += 0xD0000
					object[tempObjectPos].xvel = -0x18000
				else
					object[tempObjectPos].xpos -= 0xD0000
					object[tempObjectPos].xvel = 0x18000
				end if
				object[tempObjectPos].ypos += 0x180000
				object[tempObjectPos].yvel = 0x18000
			end if
		else
			object.shootTimer = 0
			object.state = BUZZER_FLYING
			object.animation = BUZZER_ANI_FLYING
			object.hasShot = true
		end if
		break

	case BUZZER_INVISIBLE
		object.animation = BUZZER_ANI_INVISBLE
		break
		
	end switch

	if object.outOfBounds == true
		temp0 = object.xpos
		object.xpos = object.startPos.x
		if object.shootTimer == 1
			object.state = BUZZER_INVISIBLE
		end if

		if object.outOfBounds == true
			object.xpos = object.startPos.x
			GetBit(temp0, object.propertyValue, 0)
			if temp0 == 0
				object.direction = FLIP_NONE
				object.xvel = -0x10000
			else
				object.direction = FLIP_X
				object.xvel = 0x10000
			end if

#platform: USE_ORIGINS
			if game.playMode == BOOT_PLAYMODE_MISSION
				GetBit(temp0, object.propertyValue, 1)
				if temp0 != false
					object.xvel = 0
				end if
			end if
#endplatform

			object.timer = 0
			object.animation = BUZZER_ANI_FLYING
			object.priority = PRIORITY_BOUNDS
			object.state = BUZZER_FLYING
		else
			object.xpos = temp0
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.state == BUZZER_FLYING
			if object.hasShot == false
				if object.direction == FLIP_NONE
					BoxCollisionTest(C_TOUCH, object.entityPos, -28, -256, -30, 256, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, 28, -256, 30, 256, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				end if

				if checkResult == true
					object.state = BUZZER_SHOOTING
					object.animation = BUZZER_ANI_SHOOTING
				end if
			end if
		end if

		if object.state < BUZZER_INVISIBLE
			BoxCollisionTest(C_TOUCH, object.entityPos, -16, -12, 16, 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 /= 3
	object.frame += 3
	object.animationTimer++
	if object.animationTimer >= 6
		object.animationTimer = 0
	end if
end event


event ObjectDraw
	switch object.animation
	case BUZZER_ANI_FLYING
		DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
		DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
		break

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

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

	case BUZZER_ANI_INVISBLE
		break
		
	end switch
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("EHZ/Objects.gif")
		SpriteFrame(-24, -8, 48, 16, 1, 1)
		SpriteFrame(-24, -8, 42, 31, 1, 18)
		SpriteFrame(1, 6, 17, 17, 50, 1)
		SpriteFrame(5, -8, 6, 5, 19, 50)
		SpriteFrame(5, -8, 7, 5, 19, 56)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-24, -8, 48, 16, 1, 256)
		SpriteFrame(-24, -8, 42, 31, 1, 273)
		SpriteFrame(1, 6, 17, 17, 268, 501)
		SpriteFrame(5, -8, 6, 5, 137, 331)
		SpriteFrame(5, -8, 7, 5, 137, 337)
	end if

	foreach (TypeName[Buzzer], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos

		GetBit(temp0, object[arrayPos0].propertyValue, 0)
		if temp0 == 0
			object[arrayPos0].direction = FLIP_NONE
			object[arrayPos0].xvel = -0x10000
		else
			object[arrayPos0].direction = FLIP_X
			object[arrayPos0].xvel = 0x10000
		end if
		
#platform: USE_ORIGINS
		if game.playMode == BOOT_PLAYMODE_MISSION
			GetBit(temp0, object.propertyValue, 1)
			if temp0 != false
				object.xvel = 0
			end if
		end if
#endplatform
	next

	SetTableValue(TypeName[Buzzer], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Buzzer_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Buzzer_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 // static
			GetBit(checkResult, object.propertyValue, 1)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // direction
			CheckNotEqual(editor.variableValue, 0)
			SetBit(object.propertyValue, 0, checkResult)
			break
			
		case 1 // static
			CheckNotEqual(editor.variableValue, 0)
			SetBit(object.propertyValue, 1, checkResult)
			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
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("EHZ/Objects.gif")
		SpriteFrame(-24, -8, 48, 16, 1, 1)
		SpriteFrame(5, -8, 6, 5, 19, 50)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-24, -8, 48, 16, 1, 256)
		SpriteFrame(5, -8, 6, 5, 137, 331)
	end if

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
	
	// Only functional in Origins' Mission Mode
	// Not much of a good way to show that in the editor, though...
	AddEditorVariable("static")
	SetActiveVariable("static")
	AddEnumVariable("False", 0)
	AddEnumVariable("True",  1)
end event
