// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Button Bridge 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.drawPos.x

// States
private alias 0 : BUTTONBRIDGE_HIDDEN
private alias 1 : BUTTONBRIDGE_SHOWING
private alias 2 : BUTTONBRIDGE_SHOWN
private alias 3 : BUTTONBRIDGE_HIDING

// Player Aliases
private alias object.state : player.state
private alias object.xpos : player.xpos
private alias object.animation : player.animation

private alias object.value1  : player.timer
private alias object.value17 : debugMode.currentSelection

// PushButton Aliases
private alias object.value0 : pushButton.pressed

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

reserve function ButtonBridge_Setup
reserve function ButtonBridge_DebugDraw
reserve function ButtonBridge_DebugSpawn


// ========================
// Static Values
// ========================

private value ButtonBridge_startDebugID = 0


// ========================
// Function Defintions
// ========================

private function ButtonBridge_Setup
	object[arrayPos0].priority 	= PRIORITY_ACTIVE
	object[arrayPos0].timer 	= 64
	object[arrayPos0].drawPos.x	= object[arrayPos0].xpos
	if object[arrayPos0].direction == FLIP_NONE
		object[arrayPos0].drawPos.x -= 0x4C0000
	else
		object[arrayPos0].drawPos.x += 0x4C0000
	end if

	if object[arrayPos0].propertyValue == 1
		if object[arrayPos0].direction == FLIP_NONE
			object[arrayPos0].xpos -= 0x800000
		else
			object[arrayPos0].xpos += 0x800000
		end if
	end if
end function


private function ButtonBridge_DebugDraw
	temp0 = debugMode[0].currentSelection
	temp0 -= ButtonBridge_startDebugID

	temp1 = object.xpos
	if object.direction == FLIP_NONE
		temp1 -= 0x4C0000
	else
		temp1 += 0x4C0000
	end if
	DrawSpriteXY(0, temp1, object.ypos)

	temp1 = object.xpos
	if temp0 == 1
		if object.direction == FLIP_NONE
			temp1 -= 0x800000
		else
			temp1 += 0x800000
		end if
	end if
	DrawSpriteXY(4, temp1, object.ypos)
end function


private function ButtonBridge_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= ButtonBridge_startDebugID
	CreateTempObject(TypeName[Button Bridge], temp0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
	arrayPos0 = object[tempObjectPos].entityPos
	CallFunction(ButtonBridge_Setup)
end function


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

event ObjectUpdate
	switch object.state
	case BUTTONBRIDGE_HIDDEN
		if pushButton[+1].pressed == true
			if object.propertyValue == 0
				object.state = BUTTONBRIDGE_SHOWING
			else
				object.state = BUTTONBRIDGE_HIDING
			end if
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -64, -12, 64, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		next
		break

	case BUTTONBRIDGE_SHOWING
		temp0 = object.xpos
		temp0 &= 0xFFFF0000
		if object.direction == FLIP_NONE
			object.xpos -= 0x20000
			object.frame++
		else
			object.xpos += 0x20000
			object.frame--
		end if

		object.frame &= 3

		object.timer--
		if object.timer <= 0
			if object.propertyValue == 0
				object.state = BUTTONBRIDGE_SHOWN
			else
				object.state = BUTTONBRIDGE_HIDDEN
				object.timer = 64
			end if
		end if

		temp1 = object.xpos
		temp1 &= 0xFFFF0000
		temp1 -= temp0
		temp2 = object.xpos

		object.xpos = temp0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -64, -12, 64, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				player[currentPlayer].xpos += temp1
				break

			case COL_LEFT
			case COL_RIGHT
				if player[currentPlayer].state == Player_State_Climb
					player[currentPlayer].timer 	= 0
					player[currentPlayer].animation = ANI_GLIDING_DROP
					player[currentPlayer].state 	= Player_State_GlideDrop
				end if
				break
			end switch
		next
		object.xpos = temp2
		break

	case BUTTONBRIDGE_SHOWN
		object.timer++
		if object.timer == 180
			object.timer = 0
			if object.propertyValue == 0
				object.state = BUTTONBRIDGE_HIDING
			else
				object.state = BUTTONBRIDGE_SHOWING
			end if
			object.timer = 64
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -64, -12, 64, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		next
		break

	case BUTTONBRIDGE_HIDING
		temp0 = object.xpos
		temp0 &= 0xFFFF0000
		if object.direction == FLIP_NONE
			object.xpos += 0x20000
			object.frame--
		else
			object.xpos -= 0x20000
			object.frame++
		end if
		object.frame &= 3

		object.timer--
		if object.timer <= 0
			if object.propertyValue == 0
				object.state = BUTTONBRIDGE_HIDDEN
				object.timer = 64
			else
				object.state = BUTTONBRIDGE_SHOWN
			end if
		end if

		temp1 = object.xpos
		temp1 &= 0xFFFF0000
		temp1 -= temp0
		temp2 = object.xpos

		object.xpos = temp0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -64, -12, 64, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				player[currentPlayer].xpos += temp1
				break

			case COL_LEFT
			case COL_RIGHT
				if player[currentPlayer].state == Player_State_Climb
					player[currentPlayer].timer 	= 0
					player[currentPlayer].animation = ANI_GLIDING_DROP
					player[currentPlayer].state 	= Player_State_GlideDrop
				end if
				break
			end switch
		next
		
		object.xpos = temp2
		break
		
	end switch
end event


event ObjectDraw
	DrawSpriteXY(object.frame, object.drawPos.x, object.ypos)
	DrawSprite(4)
end event


event ObjectStartup
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-12, 8, 24, 24, 133, 126)
	SpriteFrame(-12, 8, 24, 24, 158, 126)
	SpriteFrame(-12, 8, 24, 24, 158, 126)
	SpriteFrame(-12, 8, 24, 24, 183, 126)
	SpriteFrame(-64, -12, 128, 24, 350, 182)

	foreach (TypeName[Button Bridge], arrayPos0, ALL_ENTITIES)
		CallFunction(ButtonBridge_Setup)
	next

	temp0 = 0
	ButtonBridge_startDebugID = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[Button Bridge], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(ButtonBridge_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(ButtonBridge_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
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 // type
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // type
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	temp1 = object.xpos
	if object.direction == FLIP_NONE
		temp1 -= 0x4C0000
	else
		temp1 += 0x4C0000
	end if
	DrawSpriteXY(0, temp1, object.ypos)

	temp1 = object.xpos
	if object.propertyValue == 1
		if object.direction == FLIP_NONE
			temp1 -= 0x800000
		else
			temp1 += 0x800000
		end if
	end if
	DrawSpriteXY(1, temp1, object.ypos)
end event


event RSDKLoad
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-12, 8, 24, 24, 133, 126)
	SpriteFrame(-64, -12, 128, 24, 350, 182)
	
	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Show On Activate", 0)
	AddEnumVariable("Hide On Activate", 1)
	
	// Direction is to be set from editor
end event
