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

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

private alias object.value1 : object.drawPos.y
private alias object.value2 : object.collisionOffset.y

// STates
private alias 0 : VPLATFORM_MOVE_STAGE
private alias 1 : VPLATFORM_MOVE_STAGE_REVERSE
private alias 2 : VPLATFORM_MOVE_GLOBAL
private alias 3 : VPLATFORM_MOVE_GLOBAL_REVERSE

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

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

reserve function VPlatform_DebugDraw
reserve function VPlatform_DebugSpawn

private function VPlatform_DebugDraw
	DrawSprite(0)
end function


private function VPlatform_DebugSpawn
	CreateTempObject(TypeName[V Platform], object.direction, object.xpos, object.ypos)
end function


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

event ObjectUpdate
	switch object.propertyValue
	case VPLATFORM_MOVE_STAGE
		object.angle = SYZSetup_oscillation
		object.angle *= 0x200
		object.angle /= 356
		break

	case VPLATFORM_MOVE_STAGE_REVERSE
		object.angle = SYZSetup_oscillation
		object.angle *= 0x200
		object.angle /= 356
		object.angle += 0x100
		break

	case VPLATFORM_MOVE_GLOBAL
		object.angle = oscillation
		object.angle <<= 1
		break

	case VPLATFORM_MOVE_GLOBAL_REVERSE
		object.angle = oscillation
		object.angle <<= 1
		object.angle += 0x100
		break
		
	end switch

	Sin(object.collisionOffset.y, object.angle)
	object.collisionOffset.y *= 0x1800
	object.collisionOffset.y += object.ypos
	object.collisionOffset.y &= 0xFFFF0000
	object.collisionOffset.y -= object.drawPos.y

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp0 = object.ypos
		object.ypos = object.drawPos.y
		BoxCollisionTest(C_PLATFORM, object.entityPos, -32, -8, 32, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			player[currentPlayer].ypos += object.collisionOffset.y
		end if
		object.ypos = temp0
	next
end event


event ObjectDraw
	Sin(object.drawPos.y, object.angle)
	object.drawPos.y *= 0x1800
	object.drawPos.y += object.ypos
	object.drawPos.y &= 0xFFFF0000

	DrawSpriteXY(0, object.xpos, object.drawPos.y)
end event


event ObjectStartup
	LoadSpriteSheet("SYZ/Objects.gif")
	SpriteFrame(-32, -10, 64, 32, 119, 1)

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


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("SYZ/Objects.gif")
	SpriteFrame(-32, -10, 64, 32, 119, 1)

	AddEditorVariable("moveType")
	SetActiveVariable("moveType")
	AddEnumVariable("Use Stage Oscillation", VPLATFORM_MOVE_STAGE)
	AddEnumVariable("Use Stage Oscillation (Reverse)", VPLATFORM_MOVE_STAGE_REVERSE)
	AddEnumVariable("Use Global Oscillation", VPLATFORM_MOVE_GLOBAL)
	AddEnumVariable("Use Global Oscillation (Reverse)", VPLATFORM_MOVE_GLOBAL_REVERSE)
end event
