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

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

private alias object.value0  : object.fallVelocity
private alias object.value1  : object.centerPos.y
private alias object.value3  : object.stoodPlayers
private alias object.value31 : object.wrapOffset.y

// States
private alias 0 : EPLATFORM_ERUPTING
private alias 1 : EPLATFORM_FALLING

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


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

reserve function EPlatform_DebugDraw
reserve function EPlatform_DebugSpawn


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

private function EPlatform_DebugDraw
	DrawSprite(0)
end function


private function EPlatform_DebugSpawn
	CreateTempObject(TypeName[E 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 EPLATFORM_ERUPTING
		if object.fallVelocity != 0
			object.ypos += object.yvel
			if object.yvel == 0x2A800
				FlipSign(object.fallVelocity)
			end if

			object.yvel += object.fallVelocity
			if object.yvel == 0
				object.fallVelocity = 0
				object.state++
			end if
		end if
		break

	case EPLATFORM_FALLING
		if object.outOfBounds == true
			temp0 = object.ypos
			object.ypos = object.centerPos.y
			object.ypos += object.wrapOffset.y

			if object.outOfBounds == true
				object.state 		 = EPLATFORM_ERUPTING
				object.priority 	 = PRIORITY_BOUNDS
				object.yvel 		 = 0
				object.fallVelocity	 = 0
				object.stoodPlayers = 0
			else
				object.ypos = temp0
			end if
		end if
		break
		
	end switch

	temp7 = object.ypos
	temp7 &= 0xFFFF0000
	temp7 -= temp0

	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.state == EPLATFORM_ERUPTING
				if object.fallVelocity == 0
					object.priority = PRIORITY_ACTIVE
					object.fallVelocity = 0x800
				end if
			end if
			break

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

		temp2++
	next
end event


event ObjectDraw
	DrawSprite(0)
end event


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

	foreach (TypeName[E Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].centerPos.y = object[arrayPos0].ypos
	next

	SetTableValue(TypeName[E Platform], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(EPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(EPlatform_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
