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

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

private alias object.value0 : object.movePos.x
private alias object.value1 : object.movePos.y
private alias object.value2 : object.collisionOffset.x
private alias object.value3 : object.collisionOffset.y
private alias object.value4 : object.tablePtr
private alias object.value5 : object.tablePos
private alias object.value6 : object.tableSize
private alias object.value7 : object.beltDir

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


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

reserve function BeltPlatform_UpdateMovement


// ========================
// Tables
// ========================

private table BeltPlatform_tableSizes
	10, 8, 6, 6, 8, 6
end table

private table BeltPlatform_tablePtrs[6]

private table BeltPlatform_movementTable0
	0x1078, 0x021A, 0x10BE, 0x0260, 0x10BE, 0x0393, 0x108C, 0x03C5, 0x1022, 0x0390, 0x1022, 0x0244
end table

private table BeltPlatform_movementTable1
	0x127E, 0x0280, 0x12CE, 0x02D0, 0x12CE, 0x046E, 0x1232, 0x0420, 0x1232, 0x02CC
end table

private table BeltPlatform_movementTable2
	0x0D22, 0x0482, 0x0D22, 0x05DE, 0x0DAE, 0x05DE, 0x0DAE, 0x0482
end table

private table BeltPlatform_movementTable3
	0x0D62, 0x03A2, 0x0DEE, 0x03A2, 0x0DEE, 0x04DE, 0x0D62, 0x04DE
end table

private table BeltPlatform_movementTable4
	0x0CAC, 0x0242, 0x0DDE, 0x0242, 0x0DDE, 0x03DE, 0x0C52, 0x03DE, 0x0C52, 0x029C
end table

private table BeltPlatform_movementTable5
	0x1252, 0x020A, 0x13DE, 0x020A, 0x13DE, 0x02BE, 0x1252, 0x02BE
end table


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

private function BeltPlatform_UpdateMovement
	temp0 = object[arrayPos0].tablePos
	GetTableValue(object[arrayPos0].movePos.x, temp0, object[arrayPos0].tablePtr)
	temp0++
	GetTableValue(object[arrayPos0].movePos.y, temp0, object[arrayPos0].tablePtr)

	temp0 = object[arrayPos0].ixpos
	temp0 -= object[arrayPos0].movePos.x
	Abs(temp0)
	temp1 = object[arrayPos0].iypos
	temp1 -= object[arrayPos0].movePos.y
	Abs(temp1)

	if temp1 < temp0
		object[arrayPos0].xvel = 0x10000
		temp1 <<= 16
		object[arrayPos0].yvel = temp1
		object[arrayPos0].yvel /= temp0
	else
		if temp1 != 0
			temp0 <<= 16
			object[arrayPos0].xvel = temp0
			object[arrayPos0].xvel /= temp1
		else
			object[arrayPos0].xvel = 0
		end if

		object[arrayPos0].yvel = 0x10000
	end if

	if object[arrayPos0].movePos.x < object[arrayPos0].ixpos
		FlipSign(object[arrayPos0].xvel)
	end if

	if object[arrayPos0].movePos.y < object[arrayPos0].iypos
		FlipSign(object[arrayPos0].yvel)
	end if
end function


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

event ObjectUpdate
	temp2 = false
	
	if object.beltDir != LZSetup_beltDirection
		object.direction ^= FLIP_X
		object.beltDir = LZSetup_beltDirection
		temp2 = true
	else
		temp0 = object.xpos
		temp1 = object.ypos
		temp0 >>= 16
		temp1 >>= 16
		if temp0 == object.movePos.x
			if temp1 == object.movePos.y
				temp2 = true
			end if
		end if
	end if

	if temp2 == true
		if object.direction == FLIP_NONE
			object.tablePos += 2
			if object.tablePos > object.tableSize
				object.tablePos = 0
			end if
		else
			object.tablePos -= 2
			if object.tablePos < 0
				object.tablePos = object.tableSize
			end if
		end if
		arrayPos0 = object.entityPos
		CallFunction(BeltPlatform_UpdateMovement)
	end if

	temp0 = object.xpos
	temp0 &= 0xFFFF0000
	temp1 = object.ypos
	temp1 &= 0xFFFF0000
	temp2 = object.xpos
	temp2 += object.xvel
	temp3 = object.ypos
	temp3 += object.yvel

	object.collisionOffset.x = temp2
	object.collisionOffset.x &= 0xFFFF0000
	object.collisionOffset.x -= temp0
	object.collisionOffset.y = temp3
	object.collisionOffset.y &= 0xFFFF0000
	object.collisionOffset.y -= temp1

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_PLATFORM, object.entityPos, -16, -8, 16, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			player[currentPlayer].xpos += object.collisionOffset.x
			player[currentPlayer].ypos += object.collisionOffset.y
		end if
	next

	object.xpos = temp2
	object.ypos = temp3
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 223, 154)

	SetTableValue(BeltPlatform_movementTable0, 0, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable1, 1, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable2, 2, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable3, 3, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable4, 4, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable5, 5, BeltPlatform_tablePtrs)

	foreach (TypeName[Belt Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_INACTIVE

		temp0 = object[arrayPos0].propertyValue
		temp0 >>= 4
		GetTableValue(object[arrayPos0].tablePtr, temp0, BeltPlatform_tablePtrs)
		GetTableValue(object[arrayPos0].tableSize, temp0, BeltPlatform_tableSizes)

		object[arrayPos0].tablePos = object[arrayPos0].propertyValue
		object[arrayPos0].tablePos &= 15
		object[arrayPos0].tablePos <<= 1
		CallFunction(BeltPlatform_UpdateMovement)
	next
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
			checkResult >>= 4
			checkResult %= 6
			break
			
		case 1 // startOffset
			checkResult = object.propertyValue
			checkResult &= 0x0F
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // type
			editor.variableValue %= 6
			editor.variableValue <<= 4
			
			object.propertyValue &= 0x0F
			object.propertyValue |= editor.variableValue
			break
			
		case 1 // startOffset
			editor.variableValue &= 0x0F

			object.propertyValue &= 0xF0
			object.propertyValue |= editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)

	if editor.showGizmos == true
		editor.drawingOverlay = true

		temp0 = object.propertyValue
		temp0 >>= 4
		GetTableValue(object.tablePtr, temp0, BeltPlatform_tablePtrs)
		GetTableValue(object.tableSize, temp0, BeltPlatform_tableSizes)

		object.tablePos = object.propertyValue
		object.tablePos &= 15
		object.tablePos <<= 1
		arrayPos0 = object.entityPos
		CallFunction(BeltPlatform_UpdateMovement)

		temp0 = object.xvel
		temp0 <<= 5
		temp0 += object.xpos

		temp1 = object.yvel
		temp1 <<= 5
		temp1 += object.ypos

		DrawArrow(object.xpos, object.ypos, temp0, temp1, 0x00, 0xFF, 0x00)
		
		editor.drawingOverlay = false
	end if
end event


event RSDKLoad
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 223, 154)

	SetTableValue(BeltPlatform_movementTable0, 0, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable1, 1, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable2, 2, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable3, 3, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable4, 4, BeltPlatform_tablePtrs)
	SetTableValue(BeltPlatform_movementTable5, 5, BeltPlatform_tablePtrs)

	AddEditorVariable("type")
	SetActiveVariable("type")

	AddEditorVariable("startOffset")
	SetActiveVariable("startOffset")
end event
