// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Rising Platform 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.collisionOffset.y
private alias object.value2 : object.timer
private alias object.value3 : object.startPos.y
private alias object.value4 : object.stoodAngle

private alias 0 : RISINGPLATFORM_IDLE
private alias 1 : RISINGPLATFORM_MOVING
private alias 2 : RISINGPLATFORM_RETURNTOSTART
private alias 3 : RISINGPLATFORM_OOB

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

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function RisingPlatform_DebugDraw
reserve function RisingPlatform_DebugSpawn


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

private function RisingPlatform_DebugDraw
	DrawSprite(0)
end function


private function RisingPlatform_DebugSpawn
	CreateTempObject(TypeName[Rising Platform], 0, object.xpos, object.ypos)
	object[tempObjectPos].startPos.y = object.ypos
	object[tempObjectPos].drawOrder = 4
end function


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

event ObjectUpdate
	if object.stood == true
		if object.timer == 0
			object.timer = 30
		end if

		if object.stoodAngle < 64
			object.stoodAngle += 4
		end if
	else
		if object.stoodAngle > 0
			object.stoodAngle -= 4
		end if
	end if

	temp0 = object.ypos
	temp0 &= 0xFFFF0000
	switch object.state
	case RISINGPLATFORM_IDLE
		temp1 = object.stoodAngle
		temp1 <<= 1
		Sin(temp2, temp1)
		temp2 <<= 9
		object.ypos = object.startPos.y
		object.ypos += temp2

		if object.timer != 0
			object.timer--
			if object.timer == 0
				object.state++
				object.priority = PRIORITY_ACTIVE
			end if
		end if
		break

	case RISINGPLATFORM_MOVING
		object.ypos += object.yvel
		object.yvel -= 0x800
		ObjectTileCollision(CSIDE_ROOF, 0, -12, PATH_A)
		if checkResult == true
			object.state++
		end if

		if object.outOfBounds == true
			object.ypos = object.startPos.y
			object.yvel = 0
			object.timer = 0
			object.state = RISINGPLATFORM_OOB
		end if
		break

	case RISINGPLATFORM_RETURNTOSTART
		if object.outOfBounds == true
			object.ypos = object.startPos.y
			object.yvel = 0
			object.timer = 0
			object.state++
		end if
		break

	case RISINGPLATFORM_OOB
		if object.outOfBounds == true
			object.state = RISINGPLATFORM_IDLE
			object.priority = PRIORITY_BOUNDS
		end if
		break
		
	end switch

	object.collisionOffset.y = object.ypos
	object.collisionOffset.y &= 0xFFFF0000
	object.collisionOffset.y -= temp0
	object.stood = false

	if object.state < RISINGPLATFORM_OOB
		temp1 = object.ypos
		object.ypos = temp0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -32, -12, 32, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_TOP
				object.stood = true
				player[currentPlayer].ypos += object.collisionOffset.y
			end if
		next
		object.ypos = temp1
	end if
end event


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


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-32, -12, 64, 24, 126, 137)

	foreach (TypeName[Rising Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		object[arrayPos0].drawOrder = 4
	next

	SetTableValue(TypeName[Rising Platform], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(RisingPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(RisingPlatform_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-32, -12, 64, 24, 126, 137)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
