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

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


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

reserve function VPlatform2_DebugDraw
reserve function VPlatform2_DebugSpawn


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

private function VPlatform2_DebugDraw
	DrawSprite(0)
end function


private function VPlatform2_DebugSpawn
	CreateTempObject(TypeName[V Platform 2], 0, object.xpos, object.ypos)
	object[tempObjectPos].centerPos.y = object.ypos
	object[tempObjectPos].propertyValue = object.direction
end function


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

event ObjectUpdate
	temp7 = object.ypos
	
	GetTableValue(temp0, 12, StageSetup_oscillationTable)
	if object.propertyValue == 1
		FlipSign(temp0)
		temp0 += 0x4000
	else
		temp0 -= 0x4000
	end if
	temp0 <<= 7

	object.ypos = object.centerPos.y
	object.ypos -= temp0
	temp1 = object.ypos
	temp7 &= 0xFFFF0000
	object.collisionOffset.y = object.ypos
	object.collisionOffset.y &= 0xFFFF0000
	object.collisionOffset.y -= temp7
	object.ypos = temp7
	
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_PLATFORM, object.entityPos, -32, -2, 32, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			player[currentPlayer].ypos += object.collisionOffset.y
		end if
	next

	object.ypos = temp1
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("GHZ/Objects.gif")
	SpriteFrame(-32, -8, 64, 144, 191, 1)

	foreach (TypeName[V Platform 2], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].centerPos.y = object[arrayPos0].ypos
	next
	
	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++
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 // startDir
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // startDir
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
	
	if editor.showGizmos == true
		// Clarify that this object isn't just part of the level terrain, draw some lines
		
		// TODO: probably best to make sure these arrows somewhat accurately represent how far it'll go in-game
		// i have no idea how these currently look since i haven't been given any fancy re2 betas yet but the values probably look wrong
		// the "correct" values could probably be reused from Mania's version of the object preview, although i don't have access to that either...
		// edit: the miracle happen;
		// github down rn but snag values from there once it's back up
		// perhaps used cropped frame so that bottom of sprite so lower end doesn't stick out more than it should, only have grass ghost
		
		editor.drawingOverlay = true

		temp0 = 0x204000
		temp0 += object.ypos
		temp1 = -0x1FC000
		temp1 += object.ypos

		DrawLine(object.xpos, temp0, object.xpos, temp1, 255, 255, 255)

		editor.drawingOverlay = false
	end if
end event


event RSDKLoad
	LoadSpriteSheet("GHZ/Objects.gif")
	SpriteFrame(-32, -8, 64, 144, 191, 1)

	AddEditorVariable("startDir")
	SetActiveVariable("startDir")
	AddEnumVariable("Up", 0)
	AddEnumVariable("Down", 1)
end event
