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

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

private alias object.propertyValue : object.canMove

private alias object.value0 : object.stood
private alias object.value1 : object.stoodAngle
private alias object.value2 : object.collisionOffset.y
private alias object.value3 : object.timer
private alias object.value4 : object.centerPos.y

private alias 0 : STATICPLATFORM_IDLE
private alias 1 : STATICPLATFORM_WAITFORBUTTON
private alias 2 : STATICPLATFORM_MOVEDELAY
private alias 3 : STATICPLATFORM_MOVING

// Player Aliases
private alias object.ypos : player.ypos

// PushButton aliases
private alias object.propertyValue : pushButton.stood

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

reserve function StaticPlatform_DebugDraw
reserve function StaticPlatform_DebugSpawn


private function StaticPlatform_DebugDraw
	DrawSprite(0)
end function


private function StaticPlatform_DebugSpawn
	CreateTempObject(TypeName[Static Platform], 0, object.xpos, object.ypos)
	object[tempObjectPos].centerPos.y = object.ypos
	object[tempObjectPos].state = STATICPLATFORM_WAITFORBUTTON
end function


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

event ObjectUpdate
	if object.stood == true
		if object.stoodAngle < 16
			object.stoodAngle++

			temp0 = object.stoodAngle
			temp0 &= 3
			if temp0 == 3
				object.collisionOffset.y = 0x10000
			else
				object.collisionOffset.y = 0
			end if
		else
			object.collisionOffset.y = 0
		end if
	else
		if object.stoodAngle > 0
			object.stoodAngle--

			temp0 = object.stoodAngle
			temp0 &= 3
			if temp0 == 3
				object.collisionOffset.y = -0x10000
			else
				object.collisionOffset.y = 0
			end if
		else
			object.collisionOffset.y = 0
		end if
	end if

	object.stood = false
	switch object.state
	case STATICPLATFORM_IDLE
		break
		
	case STATICPLATFORM_WAITFORBUTTON
		if pushButton[-1].stood == true
			object.state++
		end if
		break

	case STATICPLATFORM_MOVEDELAY
		if object.timer < 120
			object.timer++
		else
			object.timer = 0
			object.state++
		end if
		break

	case STATICPLATFORM_MOVING
		if object.timer < 256
			object.timer++
			object.ypos -= 0x20000
			object.centerPos.y -= 0x20000
		else
			object.timer = 0
			object.state = STATICPLATFORM_IDLE
		end if
		break
	end switch

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.yvel < 0x80000
			BoxCollisionTest(C_PLATFORM, object.entityPos, -32, -8, 32, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				object.stood = true
				player[currentPlayer].ypos += object.collisionOffset.y
			end if
		end if
	next
	object.ypos += object.collisionOffset.y
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("SYZ/Objects.gif")
	SpriteFrame(-32, -10, 64, 32, 119, 1)

	arrayPos0 = 32
	while arrayPos0 < 0x420
		if object[arrayPos0].type == TypeName[Static Platform]
			object[arrayPos0].centerPos.y = object[arrayPos0].ypos
			if object[arrayPos0].canMove == true
				object[arrayPos0].state = STATICPLATFORM_WAITFORBUTTON
			end if
		end if
		arrayPos0++
	loop

	SetTableValue(TypeName[Static Platform], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(StaticPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(StaticPlatform_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 &= 1
			break
			
		case 0 // canMove
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		case 0 // canMove
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("SYZ/Objects.gif")
	SpriteFrame(-32, -10, 64, 32, 119, 1)

	AddEditorVariable("canMove")
	SetActiveVariable("canMove")
	AddEnumVariable("false", false)
	AddEnumVariable("true", true)
end event
