// ----------------------------------
// RSDK Project: Sonic 1
// 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

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


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

reserve function HPlatform_DebugDraw
reserve function HPlatform_DebugSpawn

private function HPlatform_DebugDraw
	DrawSprite(0)
end function


private function HPlatform_DebugSpawn
	CreateTempObject(TypeName[H Platform], 0, object.xpos, object.ypos)
	object[tempObjectPos].centerPos.x = object.xpos
	object[tempObjectPos].propertyValue = object.direction
end function


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

event ObjectUpdate
	temp6 = object.xpos
	GetTableValue(temp0, 6, StageSetup_oscillationTable)
	if object.propertyValue == 1
		FlipSign(temp0)
		temp0 += 0x6000
	end if
	temp0 <<= 8
	object.xpos = object.centerPos.x
	object.xpos -= temp0

	temp0 = object.xpos
	temp6 &= 0xFFFF0000
	temp4 = object.xpos
	temp4 &= 0xFFFF0000
	temp4 -= temp6
	object.xpos = temp6

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_PLATFORM, object.entityPos, -40, -16, 40, 0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			player[currentPlayer].xpos += temp4
		end if
	next
	object.xpos = temp0
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-48, -16, 96, 32, 159, 322)

	foreach (TypeName[H Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].centerPos.x = object[arrayPos0].xpos
	next

	SetTableValue(TypeName[H Platform], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(HPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(HPlatform_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
			checkResult &= 1
			break
			
		case 0 // startDir
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		case 0 // startDir
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-48, -16, 96, 32, 159, 322)

	AddEditorVariable("startDir")
	SetActiveVariable("startDir")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
end event
