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

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

private alias object.state  : object.isDynamic

private alias object.value0  : object.rotateAngle
private alias object.value1  : object.drawPos.x
private alias object.value2  : object.drawPos.y
private alias object.value3  : object.collisionOffset.x
private alias object.value4  : object.collisionOffset.y
private alias object.value5  : object.length
private alias object.value6  : object.rotateSpeed
private alias object.value7  : object.useSpikeballs
private alias object.value8  : object.platformFrame
private alias object.value9  : object.pivotAnimTimer
private alias object.value10 : object.pivotFrame
private alias object.value11 : object.amplitude

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


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

reserve function RotatePlatform_DebugDraw
reserve function RotatePlatform_DebugSpawn


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

private table RotatePlatform_pivotFrames
	2, 2, 2, 2, 1, 0, 0, 1
end table


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

private function RotatePlatform_DebugDraw
	temp7 = 3
	DrawSprite(0)

	temp0 = 0
	temp1 = 16
	while temp0 < temp7
		Cos(temp2, 128)
		temp2 *= temp1
		temp2 <<= 7
		temp2 += object.xpos

		Sin(temp3, 128)
		temp3 *= temp1
		temp3 <<= 7
		temp3 += object.ypos

		DrawSpriteXY(1, temp2, temp3)

		temp0++
		temp1 += 16
	loop
	temp1 -= 8

	Cos(temp2, 128)
	temp2 *= temp1
	temp2 <<= 7
	temp2 += object.xpos

	Sin(temp3, 128)
	temp3 *= temp1
	temp3 <<= 7
	temp3 += object.ypos

	DrawSpriteXY(2, temp2, temp3)
end function


private function RotatePlatform_DebugSpawn
	CreateTempObject(TypeName[Rotate Platform], 0, object.xpos, object.ypos)
	object[tempObjectPos].length = 3
	object[tempObjectPos].rotateSpeed = 128
	object[tempObjectPos].rotateAngle = 0

	CreateTempObject(TypeName[Rotate Platform], 0, object.xpos, object.ypos)
	object[tempObjectPos].length = 3
	object[tempObjectPos].rotateSpeed = 128
	object[tempObjectPos].rotateAngle = 0x5500

	CreateTempObject(TypeName[Rotate Platform], 0, object.xpos, object.ypos)
	object[tempObjectPos].length = 3
	object[tempObjectPos].rotateSpeed = 128
	object[tempObjectPos].rotateAngle = 0xAB00
end function


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

event ObjectUpdate
	if object.isDynamic == true
		temp0 = oscillation
		temp0 <<= 1
		Sin(object.amplitude, temp0)
		object.amplitude >>= 3
		object.amplitude += 0xC0
	else
		object.amplitude = 0x100
	end if

	temp2 = object.xpos
	temp3 = object.ypos
	object.rotateAngle += object.rotateSpeed
	object.rotateAngle &= 0x1FFFF

	temp0 = object.rotateAngle
	temp0 >>= 8
	temp1 = object.length
	temp1++
	temp1 <<= 4
	object.xpos = object.drawPos.x
	object.ypos = object.drawPos.y

	Cos256(object.collisionOffset.x, temp0)
	object.collisionOffset.x *= temp1
	object.collisionOffset.x *= object.amplitude
	object.collisionOffset.x += temp2
	object.collisionOffset.x &= 0xFFFF0000
	object.drawPos.x = object.collisionOffset.x
	object.collisionOffset.x -= object.xpos

	Sin256(object.collisionOffset.y, temp0)
	object.collisionOffset.y *= temp1
	object.collisionOffset.y *= object.amplitude
	object.collisionOffset.y += temp3
	object.collisionOffset.y &= 0xFFFF0000
	object.drawPos.y = object.collisionOffset.y
	object.collisionOffset.y -= object.ypos

	if object.useSpikeballs == false
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -24, -8, 24, 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
	else
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_Hit)
			end if
		next
	end if

	object.xpos = temp2
	object.ypos = temp3

	temp0 = object.pivotAnimTimer
	temp0 >>= 2
	GetTableValue(object.pivotFrame, temp0, RotatePlatform_pivotFrames)
	object.pivotAnimTimer++
	object.pivotAnimTimer &= 31
end event


event ObjectDraw
	temp0 = 0
	temp1 = 16
	temp4 = object.rotateAngle
	temp4 >>= 8
	while temp0 < object.length
		Cos256(temp2, temp4)
		temp2 *= temp1
		temp2 *= object.amplitude
		temp2 += object.xpos

		Sin256(temp3, temp4)
		temp3 *= temp1
		temp3 *= object.amplitude
		temp3 += object.ypos

		DrawSpriteXY(3, temp2, temp3)

		temp0++
		temp1 += 16
	loop

	Cos256(object.drawPos.x, temp4)
	object.drawPos.x *= temp1
	object.drawPos.x *= object.amplitude
	object.drawPos.x += object.xpos
	object.drawPos.x &= 0xFFFF0000

	Sin256(object.drawPos.y, temp4)
	object.drawPos.y *= temp1
	object.drawPos.y *= object.amplitude
	object.drawPos.y += object.ypos
	object.drawPos.y &= 0xFFFF0000

	DrawSpriteXY(object.platformFrame, object.drawPos.x, object.drawPos.y)
	DrawSprite(object.pivotFrame)
end event


event ObjectStartup
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 181, 73)  	// orb (inactive) - #0
	SpriteFrame(-8, -8, 16, 16, 181, 90)  	// orb (activating) - #1
	SpriteFrame(-8, -8, 16, 16, 181, 107) 	// orb (active) - #2
	SpriteFrame(-8, -8, 16, 16, 181, 73)  	// orb (inactive) - #3
	SpriteFrame(-32, -8, 64, 16, 1, 183)  	// platform - #4
	SpriteFrame(-20, -20, 40, 40, 402, 66) 	// spike - #5

	SetTableValue(TypeName[Rotate Platform], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(RotatePlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(RotatePlatform_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++

	foreach (TypeName[Rotate Platform], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].isDynamic == true
			object[arrayPos0].length = 5
		else
			object[arrayPos0].length = 4
		end if

		object[arrayPos0].rotateSpeed = object[arrayPos0].propertyValue
		object[arrayPos0].rotateSpeed >>= 3
		object[arrayPos0].rotateSpeed <<= 7
		object[arrayPos0].rotateSpeed &= 0x7FB
		if object[arrayPos0].rotateSpeed >= 0x400
			object[arrayPos0].rotateSpeed -= 0x400
			FlipSign(object[arrayPos0].rotateSpeed)
		end if

		object[arrayPos0].rotateAngle = object[arrayPos0].propertyValue
		object[arrayPos0].rotateAngle &= 7
		object[arrayPos0].rotateAngle *= 0x2A00

		object[arrayPos0].useSpikeballs = object[arrayPos0].propertyValue
		object[arrayPos0].useSpikeballs >>= 7
		if object[arrayPos0].useSpikeballs == false
			object[arrayPos0].platformFrame = 4
		else
			object[arrayPos0].platformFrame = 5
		end if
	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 // angle
			checkResult = object.propertyValue
			checkResult &= 7
			break
			
		case 1 // rotateSpeed
			checkResult = object.propertyValue
			checkResult >>= 3
			checkResult &= 0xF
			break
			
		case 2 // useSpikeball
			GetBit(checkResult, object.propertyValue, 7)
			break
			
		case 3 // isDynamic
			checkResult = object.state
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // angle
			editor.variableValue &= 7

			temp0 = 7
			Not(temp0)
			object.propertyValue &= temp0

			object.propertyValue |= editor.variableValue
			break
			
		case 1 // rotateSpeed
			editor.variableValue &= 0xF
			editor.variableValue <<= 3

			temp0 = 0x78
			Not(temp0)
			object.propertyValue &= temp0

			object.propertyValue |= editor.variableValue
			break
			
		case 2 // useSpikeball
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 7, checkResult)
			break
			
		case 3 // isDynamic
			object.state = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	if object.isDynamic == true
		object.length = 5
	else
		object.length = 4
	end if

	object.rotateSpeed = object.propertyValue
	object.rotateSpeed >>= 3
	object.rotateSpeed <<= 7
	object.rotateSpeed &= 0x7FB
	if object.rotateSpeed >= 0x400
		object.rotateSpeed -= 0x400
		FlipSign(object.rotateSpeed)
	end if

	object.rotateAngle = object.propertyValue
	object.rotateAngle &= 7
	object.rotateAngle *= 0x2A00

	object.useSpikeballs = object.propertyValue
	object.useSpikeballs >>= 7
	if object.useSpikeballs == false
		object.platformFrame = 4
	else
		object.platformFrame = 5
	end if

	if object.isDynamic == true
		Sin(object.amplitude, 0)
		object.amplitude >>= 3
		object.amplitude += 0xC0
	else
		object.amplitude = 0x100
	end if

	temp2 = object.xpos
	temp3 = object.ypos
	object.rotateAngle &= 0x1FFFF

	temp0 = object.rotateAngle
	temp0 >>= 8
	temp1 = object.length
	temp1++
	temp1 <<= 4
	object.xpos = object.drawPos.x
	object.ypos = object.drawPos.y

	Cos256(object.collisionOffset.x, temp0)
	object.collisionOffset.x *= temp1
	object.collisionOffset.x *= object.amplitude
	object.collisionOffset.x += temp2
	object.collisionOffset.x &= 0xFFFF0000
	object.drawPos.x = object.collisionOffset.x
	object.collisionOffset.x -= object.xpos

	Sin256(object.collisionOffset.y, temp0)
	object.collisionOffset.y *= temp1
	object.collisionOffset.y *= object.amplitude
	object.collisionOffset.y += temp3
	object.collisionOffset.y &= 0xFFFF0000
	object.drawPos.y = object.collisionOffset.y
	object.collisionOffset.y -= object.ypos

	object.xpos = temp2
	object.ypos = temp3

	temp0 = 0
	temp1 = 16
	temp4 = object.rotateAngle
	temp4 >>= 8
	while temp0 < object.length
		Cos256(temp2, temp4)
		temp2 *= temp1
		temp2 *= object.amplitude
		temp2 += object.xpos

		Sin256(temp3, temp4)
		temp3 *= temp1
		temp3 *= object.amplitude
		temp3 += object.ypos

		DrawSpriteXY(3, temp2, temp3)

		temp0++
		temp1 += 16
	loop

	Cos256(object.drawPos.x, temp4)
	object.drawPos.x *= temp1
	object.drawPos.x *= object.amplitude
	object.drawPos.x += object.xpos
	object.drawPos.x &= 0xFFFF0000

	Sin256(object.drawPos.y, temp4)
	object.drawPos.y *= temp1
	object.drawPos.y *= object.amplitude
	object.drawPos.y += object.ypos
	object.drawPos.y &= 0xFFFF0000

	DrawSpriteXY(object.platformFrame, object.drawPos.x, object.drawPos.y)
	DrawSprite(2)
end event


event RSDKLoad
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 181, 73)  	// orb (inactive) - #0
	SpriteFrame(-8, -8, 16, 16, 181, 90)  	// orb (activating) - #1
	SpriteFrame(-8, -8, 16, 16, 181, 107) 	// orb (active) - #2
	SpriteFrame(-8, -8, 16, 16, 181, 73)  	// orb (inactive) - #3
	SpriteFrame(-32, -8, 64, 16, 1, 183)  	// platform - #4
	SpriteFrame(-20, -20, 40, 40, 402, 66) 	// spike - #5

	AddEditorVariable("angle")
	SetActiveVariable("angle")

	AddEditorVariable("rotateSpeed")
	SetActiveVariable("rotateSpeed")

	AddEditorVariable("useSpikeball")
	SetActiveVariable("useSpikeball")
	AddEnumVariable("false", false)
	AddEnumVariable("true", true)
	
	AddEditorVariable("isDynamic")
	SetActiveVariable("isDynamic")
	AddEnumVariable("false", false)
	AddEnumVariable("true", true)
end event
