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

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

private alias object.value0 : object.centerPos.y
private alias object.value1 : object.hitboxL
private alias object.value2 : object.hitboxR
private alias object.value3 : object.stoodPlayers

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

private alias object.value17 : debugMode.currentSelection


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

reserve function VPlatform2_DebugDraw
reserve function VPlatform2_DebugSpawn


// ========================
// Static Values
// ========================

private value VPlatform2_debugStartID = 0


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

private function VPlatform2_DebugDraw
	temp0 = debugMode[0].currentSelection
	temp0 -= VPlatform2_debugStartID
	DrawSprite(temp0)
end function


private function VPlatform2_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= VPlatform2_debugStartID
	CreateTempObject(TypeName[V Platform 2], temp0, object.xpos, object.ypos)

	object[tempObjectPos].centerPos.y = object.ypos
	object[tempObjectPos].direction = object.direction
	if object[tempObjectPos].propertyValue == 0
		object[tempObjectPos].hitboxL = -32
		object[tempObjectPos].hitboxR = 32
	else
		object[tempObjectPos].hitboxL = -24
		object[tempObjectPos].hitboxR = 24
	end if
end function


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

event ObjectUpdate
	temp6 = object.ypos

	object.ypos += object.yvel
	temp0 = object.centerPos.y
	temp0 -= 0x600000
	if object.ypos <= temp0
		object.yvel += 0x800
	else
		object.yvel -= 0x800
	end if

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

	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_PLATFORM, object.entityPos, object.hitboxL, -16, object.hitboxR, 0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			SetBit(object.stoodPlayers, temp2, true)
		end if

		temp2++
	next
end event


event ObjectDraw
	DrawSprite(object.propertyValue)
end event


event ObjectStartup
	LoadSpriteSheet("CPZ/Objects.gif")
	SpriteFrame(-32, -16, 64, 27, 136, 155) // Large Platform
	SpriteFrame(-24, -16, 48, 26, 136, 183) // Small Platform

	foreach (TypeName[V Platform 2], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].centerPos.y = object[arrayPos0].ypos
		if object[arrayPos0].direction == FLIP_Y
			object[arrayPos0].direction -= 0xC00000 // prolly not supposed to be "object.direction"
		end if
		if object[arrayPos0].propertyValue == 0
			object[arrayPos0].hitboxL = -32
			object[arrayPos0].hitboxR = 32
		else
			object[arrayPos0].hitboxL = -24
			object[arrayPos0].hitboxR = 24
		end if
	next

	temp0 = 0
	VPlatform2_debugStartID = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[V Platform 2], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(VPlatform2_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(VPlatform2_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
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(object.propertyValue)
end event


event RSDKLoad
	LoadSpriteSheet("CPZ/Objects.gif")
	SpriteFrame(-32, -16, 64, 27, 136, 155)
	SpriteFrame(-24, -16, 48, 26, 136, 183)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Large", 0)
	AddEnumVariable("Small", 1)
end event
