// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Fall 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

// Types
private alias 0 : FPLATFORM_FALL
private alias 1 : FPLATFORM_STATIC

// States
private alias 0 : FPLATFORM_IDLE
private alias 1 : FPLATFORM_FALLING_SOLID
private alias 2 : FPLATFORM_FALLING
private alias 3 : FPLATFORM_OFFSCREEN

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


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

reserve function FallPlatform_DebugDraw
reserve function FallPlatform_DebugSpawn


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

private function FallPlatform_DebugDraw
	DrawSprite(0)
end function


private function FallPlatform_DebugSpawn
	CreateTempObject(TypeName[Fall 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 FPLATFORM_IDLE
		if object.propertyValue == 0
			if object.timer != 0
				object.timer--
				if object.timer == 0
					object.state++
					object.timer = 32
				end if
			end if
		end if
		break

	case FPLATFORM_FALLING_SOLID
		object.timer--
		if object.timer == 0
			object.state++
			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
					player[currentPlayer].ypos 		+= object.yvel
					player[currentPlayer].yvel 		 = object.yvel
					player[currentPlayer].yvel 		-= 0x3800
					player[currentPlayer].gravity	 = GRAVITY_AIR
				end if
			next
		end if
		// [Fallthrough]
	case FPLATFORM_FALLING
		object.ypos += object.yvel
		object.yvel += 0x3800

		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 FPLATFORM_OFFSCREEN
		if object.outOfBounds == true
			object.state = FPLATFORM_IDLE
			object.priority = PRIORITY_BOUNDS
		end if
		break
		
	end switch

	temp7 = object.ypos
	temp7 &= 0xFFFF0000
	temp7 -= temp0
	if object.state < FPLATFORM_FALLING
		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 == FPLATFORM_IDLE
						object.priority = PRIORITY_ACTIVE
						object.timer 	= 30
					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 if
end event


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


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

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

	SetTableValue(TypeName[Fall Platform], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(FallPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(FallPlatform_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
			break
			
		case 0 // type
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // type
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


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

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Falling Platform", FPLATFORM_FALL)
	AddEnumVariable("Static Platform", FPLATFORM_STATIC)
end event
