// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: L Platform 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.centerPos.y
private alias object.value2  : object.stoodPlayers
private alias object.value31 : object.wrapOffset.y

// States
private alias 0 : LPLATFORM_IDLE
private alias 1 : LPLATFORM_SINKING
private alias 2 : LPLATFORM_SUNKEN

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


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

reserve function LPlatform_DebugDraw
reserve function LPlatform_DebugSpawn

private function LPlatform_DebugDraw
	DrawSprite(0)
end function


private function LPlatform_DebugSpawn
	CreateTempObject(TypeName[L Platform], 0, object.xpos, object.ypos)
	object[tempObjectPos].centerPos.y = object[tempObjectPos].ypos
end function


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

event ObjectUpdate
	temp0 = object.ypos
	temp0 &= 0xFFFF0000

	switch object.state
	case LPLATFORM_IDLE
		GetTableValue(temp6, 0, StageSetup_oscillationTable)
		temp6 <<= 7
		object.ypos = object.centerPos.y
		object.ypos -= temp6
		break

	case LPLATFORM_SINKING
		object.ypos += object.yvel
		object.yvel += 0x800

		if object.outOfBounds == true
			object.ypos = object.centerPos.y
			object.ypos += object.wrapOffset.y
			object.yvel 	= 0
			object.timer 	= 0
			object.state++
		end if
		break

	case LPLATFORM_SUNKEN
		if object.outOfBounds == true
			object.state 	= LPLATFORM_IDLE
			object.priority = PRIORITY_BOUNDS
		end if
		break
		
	end switch

	temp7 = object.ypos
	temp7 &= 0xFFFF0000
	temp7 -= temp0
	if object.state < 2
		temp2 = 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			GetBit(temp0, object.stoodPlayers, temp2)
			if temp0 == true
				player[currentPlayer].ypos += temp7
			end if

			SetBit(object.stoodPlayers, temp2, false)
			BoxCollisionTest(C_SOLID, object.entityPos, -32, -12, 32, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				SetBit(object.stoodPlayers, temp2, true)
				if object.timer == 0
					if object.state == LPLATFORM_IDLE
						object.priority = PRIORITY_ACTIVE
						object.state++
					end if
				end if
				break

			case COL_BOTTOM
				if player[currentPlayer].gravity == 0
					CallFunction(Player_Kill)
				end if
				break
			end switch

			temp2++
		next
	end if
end event


event ObjectDraw
	if object.state < 2
		DrawSprite(0)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-32, -12, 64, 24, 383, 207)

	foreach (TypeName[L Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].ypos += 0x100000
		object[arrayPos0].centerPos.y = object[arrayPos0].ypos
	next

	SetTableValue(TypeName[L Platform], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(LPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(LPlatform_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-32, -12, 64, 24, 383, 207)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
