// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: H Platform 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.hitboxL
private alias object.value2 : object.hitboxR
private alias object.value3 : object.stoodPlayers

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

private alias object.value17 : debugMode.currentSelection


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

reserve function HPlatform_DebugDraw
reserve function HPlatform_DebugSpawn


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

private value HPlatform_debugStartID = 0


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

private function HPlatform_DebugDraw
	temp0 = debugMode[0].currentSelection
	temp0 -= HPlatform_debugStartID
	DrawSprite(temp0)
end function


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

	object[tempObjectPos].centerPos.x = object.xpos
	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.xpos
	GetTableValue(temp0, 6, StageSetup_oscillationTable)
	if object.direction == FLIP_X // (FLIP_XY won't flip the platform, so don't try that)
		FlipSign(temp0)
		temp0 += 0x6000
	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.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)
	SpriteFrame(-24, -16, 48, 26, 136, 183)

	foreach (TypeName[H Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].centerPos.x = object[arrayPos0].xpos
		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
	HPlatform_debugStartID = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[H Platform], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(HPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(HPlatform_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)

	if editor.showGizmos == true
		// Draw arrows to indicate that this is an H platform

		editor.drawingOverlay = true

		temp0 = object.xpos
		temp0 -= 0x320000
		DrawArrow(object.xpos, object.ypos, temp0, object.ypos, 0, 224, 0)
		temp0 = object.xpos
		temp0 += 0x320000
		DrawArrow(object.xpos, object.ypos, temp0, object.ypos, 0, 224, 0)

		editor.drawingOverlay = false
	end if
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
