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

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

private alias object.value0 : object.centerPos.x
private alias object.value1 : object.hitbox.left
private alias object.value2 : object.hitbox.right
private alias object.value3 : object.stoodPlayers

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

private alias object.value17 : debugMode.currentSelection


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

reserve function HPlatform2_DebugDraw
reserve function HPlatform2_DebugSpawn


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

private value HPlatform2_startDebugID = 0


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

private function HPlatform2_DebugDraw
	temp0 = debugMode[0].currentSelection
	temp0 -= HPlatform2_startDebugID
	DrawSprite(temp0)
end function


private function HPlatform2_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= HPlatform2_startDebugID
	CreateTempObject(TypeName[H Platform 2], temp0, object.xpos, object.ypos)

	object[tempObjectPos].centerPos.x = object.xpos
	object[tempObjectPos].direction = object.direction
	if object[tempObjectPos].propertyValue == 0
		object[tempObjectPos].hitbox.left 	= -32
		object[tempObjectPos].hitbox.right 	= 32
	else
		object[tempObjectPos].hitbox.left 	= -24
		object[tempObjectPos].hitbox.right 	= 24
	end if
end function


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

event ObjectUpdate
	temp6 = object.xpos
	GetTableValue(temp0, 4, StageSetup_oscillationTable)
	if object.direction == FLIP_X
		FlipSign(temp0)
		temp0 += 0x4000
	end if
	temp0 <<= 8

	object.xpos = object.centerPos.x
	object.xpos -= temp0
	temp6 &= 0xFFFF0000
	temp7 = object.xpos
	temp7 &= 0xFFFF0000
	temp7 -= temp6

	temp2 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		GetBit(temp0, object.stoodPlayers, temp2)
		if temp0 == true
			player[currentPlayer].xpos += temp7
		end if

		SetBit(object.stoodPlayers, temp2, false)
		BoxCollisionTest(C_PLATFORM, object.entityPos, object.hitbox.left, -16, object.hitbox.right, 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("SCZ/Objects.gif")
	SpriteFrame(-24, -16, 48, 24, 381, 178)
	SpriteFrame(-32, -16, 64, 24, 1, 146)

	foreach (TypeName[H Platform 2], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].centerPos.x = object[arrayPos0].xpos
		if object[arrayPos0].propertyValue == 0
			object[arrayPos0].hitbox.left = -32
			object[arrayPos0].hitbox.right = 32
		else
			object[arrayPos0].hitbox.left = -24
			object[arrayPos0].hitbox.right = 24
		end if
	next

	temp0 = 0
	HPlatform2_startDebugID = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[H Platform 2], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(HPlatform2_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(HPlatform2_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("SCZ/Objects.gif")
	SpriteFrame(-24, -16, 48, 24, 381, 178)
	SpriteFrame(-32, -16, 64, 24, 1, 146)

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