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

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

private alias object.value0 : object.stood
private alias object.value1 : object.exitPos.y
private alias object.value2 : object.fadeTimer

private alias 0 : PUSHBUTTON_ACTIVE
private alias 1 : PUSHBUTTON_PLAYERFELL

// Cutscene Bridge Aliases
private alias object.state  : cutBridge.state
private alias object.value0 : cutBridge.timer

private alias 0 : CUTBRIDGE_ACTIVE
private alias 1 : CUTBRIDGE_COLLAPSE

// Player Aliases
private alias object.type : player.type
private alias object.ypos : player.ypos
private alias object.yvel : player.yvel
private alias object.direction : player.direction
private alias object.gravity : player.gravity
private alias object.animation : player.animation
private alias object.collisionBottom : player.collisionBottom

// Game Mode Aliases
private alias 0 : MODE_NOSAVE
private alias 1 : MODE_SAVEGAME
private alias 2 : MODE_TIMEATTACK


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

reserve function PushButton_DebugDraw
reserve function PushButton_DebugSpawn


// ========================
// Tables
// ========================

private function PushButton_DebugDraw
	DrawSprite(0)
end function


private function PushButton_DebugSpawn
	CreateTempObject(TypeName[Push Button], 0, object.xpos, object.ypos)
end function


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

event ObjectUpdate
	object.stood = false

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.frame == 0
			BoxCollisionTest(C_SOLID, object.entityPos, -14, -4, 14, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_TOP
				object.stood = true
				player[currentPlayer].ypos += 0x60000
				PlaySfx(SfxName[Button Press], false)
			end if
		else
			if player[currentPlayer].yvel >= 0
				BoxCollisionTest(C_PLATFORM, object.entityPos, -14, -4, 14, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					object.stood = true
					player[currentPlayer].ypos += 0x20000
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, -20, -12, 20, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
					if checkResult == true
						player[currentPlayer].ypos = player[currentPlayer].collisionBottom
						FlipSign(player[currentPlayer].ypos)
						player[currentPlayer].ypos <<= 16
						player[currentPlayer].ypos += object.ypos
						player[currentPlayer].ypos -= 0x20000
						player[currentPlayer].gravity = GRAVITY_AIR
					end if
				end if
			end if
		end if
	next
	
	if object.propertyValue == 1
		// object[+1] should be an Eggman object
		arrayPos0 = object.entityPos
		arrayPos0++
		if object.frame == 0
			BoxCollisionTest(C_SOLID, object.entityPos, -14, -4, 14, 8, arrayPos0, -22, -24, 16, 32)
			if checkResult == COL_TOP
				object.stood = true
				object[arrayPos0].ypos += 0x60000
				object[arrayPos0].xvel = 0
				object[arrayPos0].yvel = 0
				object[arrayPos0].state++
				PlaySfx(SfxName[Button Press], false)
			end if
		else
			BoxCollisionTest(C_TOUCH, object.entityPos, -14, -4, 14, 8, arrayPos0, -22, -24, 16, 32)
			if checkResult == true
				object.stood = true
			end if
		end if

		if object.stood == true
			arrayPos0++
			if cutBridge[arrayPos0].state == CUTBRIDGE_ACTIVE
				object.exitPos.y = stage.deathBoundary
				object.exitPos.y += 0x100000
				stage.deathBoundary = 0x7FFF0000
				arrayPos1 = arrayPos0
				arrayPos1 += 7
				temp0 = 0
				while arrayPos1 >= arrayPos0
					cutBridge[arrayPos1].state = CUTBRIDGE_COLLAPSE
					cutBridge[arrayPos1].timer = temp0
					temp0 += 20
					arrayPos1--
				loop
			end if
		end if

		if object.state == PUSHBUTTON_PLAYERFELL
			options.touchControls = false
			object.fadeTimer += 4
			music.volume -= 2
			SetScreenFade(0, 0, 0, object.fadeTimer)
			if object.fadeTimer == 384
				fadeColor = 0
				object.fadeTimer = 0
				lampPostID = 0
				object.direction = FACING_RIGHT
				stage.listPos++
				if options.gameMode == MODE_SAVEGAME
					arrayPos1 = options.saveSlot
					arrayPos1 <<= 3
					if stage.player2Enabled == true
						saveRAM[arrayPos1] = 3
					else
						saveRAM[arrayPos1] = stage.playerListPos
					end if
					arrayPos1++
					saveRAM[arrayPos1] = player.lives
					arrayPos1++
					saveRAM[arrayPos1] = player.score
					arrayPos1++
					saveRAM[arrayPos1] = player.scoreBonus
					arrayPos1++
					saveRAM[arrayPos1] = stage.listPos
					saveRAM[arrayPos1]++
					arrayPos1++
					saveRAM[arrayPos1] = specialStage.emeralds
					arrayPos1++
					saveRAM[arrayPos1] = specialStage.listPos
					WriteSaveRAM()
				end if

				temp0 = engine.trialMode
				if stage.listPos >= stage.listSize
					temp0 = true
				end if

				if temp0 == false
					LoadStage()
				else
					stage.activeList = PRESENTATION_STAGE
					stage.listPos = 0
					LoadStage()
				end if
			end if
		else
			if player[0].ypos > object.exitPos.y
				object.state = PUSHBUTTON_PLAYERFELL
				recAnimation = 0
				temp0 = 0
				foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
					temp1 = player[currentPlayer].animation
					temp1 <<= 8
					temp1 |= player[currentPlayer].direction
					temp1 <<= temp0
					recAnimation |= temp1
					temp0 += 16
				next
				
				player[0].type = TypeName[Blank Object]
			end if
		end if
	end if
end event


event ObjectDraw
	object.frame = object.stood
	DrawSprite(object.frame)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 92, 1)
	SpriteFrame(-16, -2, 32, 10, 92, 18)
	foreach (TypeName[Push Button], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].exitPos.y = 0x7FFF0000
		end if
	next
	SetTableValue(TypeName[Push Button], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(PushButton_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(PushButton_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 // type
			checkResult = object.propertyValue
			checkResult &= 1
			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
			object.propertyValue &= 1
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 92, 1)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Normal", 0)
	AddEnumVariable("Cutscene", 1)
end event
