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

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


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

reserve function PushButton_DebugDraw
reserve function PushButton_DebugSpawn


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

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, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_TOP
				object.stood = true
				player[currentPlayer].ypos += 400000
				PlaySfx(SfxName[Button Press], false)

				switch object.propertyValue
				default
				case 0 // Generic, up to the other [+1] object to change
					break
					
				case 1 // Open LZ3 Path
					// Uses hardcoded tile positions :(
					GetTileLayerEntry(temp0, 0, 12, 5)
					if temp0 == 302
						PlaySfx(SfxName[Large Wall], false)
						SetTileLayerEntry(30, 0, 12, 5)
						SetTileLayerEntry(31, 0, 13, 5)
					end if
					break
					
				case 2 // Unused
					break
					
				case 3 // Set Belt Dir
					LZSetup_beltdirection = FACING_LEFT
					break
					
				end switch
			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

	foreach (TypeName[Push Block], arrayPos0, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -16, -12, 16, 8, arrayPos0, -16, -16, 16, 16)
		if checkResult == true
			// I think this is a leftover from MZ/SYZ, this should probably set object.stood instead of object.propertyValue (which was object.stood in MZ/SYZ)
			object.propertyValue = 1
			if object.frame == 0
				PlaySfx(SfxName[Button Press], false)
			end if
		end if
	next
end event


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


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 1, 84)
	SpriteFrame(-16, -4, 32, 12, 85, 160)

	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
			checkResult &= 3
			break
			
		case 0 // type
			checkResult = object.propertyValue
			checkResult &= 3
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 3
			break
			
		case 0 // type
			object.propertyValue = editor.variableValue
			object.propertyValue &= 3
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


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

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Generic", 0)
	AddEnumVariable("Open LZ3 Path", 1)
	// AddEnumVariable("unused?", 2) // as far as I know this does nothing special so... (it's placed in LZ3 once but it acts the same as prop val 0)
	AddEnumVariable("Swap Belt Platform Dir", 3)
end event
