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

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

private alias object.value0  : object.platformMPos.x
private alias object.value1  : object.platformMPos.y
private alias object.value2  : object.platformLPos.x
private alias object.value3  : object.platformLPos.y
private alias object.value4  : object.platformRPos.x
private alias object.value5  : object.platformRPos.y
private alias object.value6  : object.jointLPos.x
private alias object.value7  : object.jointLPos.y
private alias object.value8  : object.jointRPos.x
private alias object.value9  : object.jointRPos.y
private alias object.value10 : object.indentPos.x
private alias object.value11 : object.indentPos.y
private alias object.value12 : object.stoodPlayers
private alias object.value13 : object.prevPlatformMPos.x
private alias object.value14 : object.prevPlatformMPos.y
private alias object.value15 : object.active

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


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

reserve function WheelPlatform_DebugDraw
reserve function WheelPlatform_DebugSpawn

private function WheelPlatform_DebugDraw
	DrawSprite(3)

	temp0 = object.xpos
	temp0 -= 0x880000
	temp1 = object.ypos
	temp1 += 0xC0000
	DrawSpriteXY(2, temp0, temp1)

	temp0 -= 0x380000
	DrawSpriteXY(0, temp0, temp1)

	temp0 = object.xpos
	temp0 += 0x880000
	DrawSpriteXY(2, temp0, temp1)

	temp0 += 0x380000
	DrawSpriteXY(1, temp0, temp1)
end function


private function WheelPlatform_DebugSpawn
	CreateTempObject(TypeName[Wheel Platform], 0, object.xpos, object.ypos)
end function


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

event ObjectUpdate
	object.active = true

	temp0 = object.xpos
	temp0 >>= 16
	temp0 -= camera[0].xpos
	Abs(temp0)
	temp0 -= 576
	temp1 = object.ypos
	temp1 >>= 16
	temp1 -= camera[0].ypos
	Abs(temp1)
	temp1 -= 128
	if temp0 >= screen.xcenter
		object.active = false
	end if

	if temp1 >= screen.ycenter
		object.active = false
	end if

	if object.active == true
		GetTableValue(object.platformMPos.x, 16, StageSetup_oscillationTable)
		object.platformMPos.x >>= 8
		object.indentPos.x = object.platformMPos.x
		object.indentPos.x >>= 1
		object.platformMPos.x -= 56
		object.indentPos.x -= 28

		GetTableValue(object.platformMPos.y, 18, StageSetup_oscillationTable)
		object.platformMPos.y >>= 8
		object.indentPos.y = object.platformMPos.y
		object.indentPos.y >>= 1
		object.platformMPos.y -= 56
		object.indentPos.y -= 28

		GetBit(temp0, object.propertyValue, 0)
		if temp0 == true
			FlipSign(object.platformMPos.x)
			FlipSign(object.platformMPos.y)
			object.rotation += 34
		else
			object.rotation -= 34
		end if

		GetBit(temp0, object.propertyValue, 1)
		if temp0 == true
			FlipSign(object.platformMPos.x)
			temp0 = object.platformMPos.x
			object.platformMPos.x = object.platformMPos.y
			object.platformMPos.y = temp0
		end if
		object.platformMPos.x <<= 16
		object.platformMPos.x += object.xpos
		object.platformMPos.y <<= 16
		object.platformMPos.y += object.ypos
		object.indentPos.x <<= 16
		object.indentPos.y <<= 16
		FlipSign(object.indentPos.x)
		FlipSign(object.indentPos.y)
		object.platformLPos.x = object.platformMPos.x
		object.platformLPos.x -= 0x880000
		object.platformLPos.y = object.platformMPos.y
		object.platformLPos.y += 0xC0000
		object.platformRPos.x = object.platformMPos.x
		object.platformRPos.x += 0x880000
		object.platformRPos.y = object.platformLPos.y
		object.jointLPos.x = object.platformMPos.x
		object.jointLPos.x -= 0xC00000
		object.jointLPos.y = object.platformLPos.y
		object.jointRPos.x = object.platformMPos.x
		object.jointRPos.x += 0xC00000
		object.jointRPos.y = object.platformLPos.y

		temp1 = object.platformMPos.x
		temp1 -= object.prevPlatformMPos.x
		temp2 = object.platformMPos.y
		temp2 -= object.prevPlatformMPos.y

		object.prevPlatformMPos.x = object.platformMPos.x
		object.prevPlatformMPos.y = object.platformMPos.y

		temp4 = object.xpos
		temp5 = object.ypos
		object.xpos = object.platformMPos.x
		object.ypos = object.platformMPos.y

		temp7 = 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			GetBit(temp6, object.stoodPlayers, temp7)
			if temp6 == true
				player[currentPlayer].xpos += temp1
				player[currentPlayer].ypos += temp2
			end if
			SetBit(object.stoodPlayers, temp7, false)
			temp0 = 0

			BoxCollisionTest(C_SOLID, object.entityPos, -64, -24, 64, 0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				SetBit(object.stoodPlayers, temp7, true)
				break

			case COL_BOTTOM
				if player[currentPlayer].gravity == 0
					CallFunction(Player_Kill)
				end if
				break
			end switch

			BoxCollisionTest(C_SOLID, object.entityPos, -96, -24, -64, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				SetBit(object.stoodPlayers, temp7, true)
				break

			case COL_BOTTOM
				if player[currentPlayer].gravity == 0
					CallFunction(Player_Kill)
				end if
				break
			end switch

			BoxCollisionTest(C_SOLID, object.entityPos, 64, -24, 96, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				SetBit(object.stoodPlayers, temp7, true)
				break

			case COL_BOTTOM
				if player[currentPlayer].gravity == 0
					CallFunction(Player_Kill)
				end if
				break
			end switch

			BoxCollisionTest(C_SOLID, object.entityPos, -176, 4, -96, 20, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				SetBit(object.stoodPlayers, temp7, true)
				break

			case COL_BOTTOM
				if player[currentPlayer].gravity == 0
					CallFunction(Player_Kill)
				end if
				break
			end switch

			BoxCollisionTest(C_SOLID, object.entityPos, 96, 4, 176, 20, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				SetBit(object.stoodPlayers, temp7, true)
				break

			case COL_BOTTOM
				if player[currentPlayer].gravity == 0
					CallFunction(Player_Kill)
				end if
				break
			end switch

			BoxCollisionTest(C_SOLID, object.entityPos, -208, 0, -176, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				SetBit(object.stoodPlayers, temp7, true)
				break

			case COL_BOTTOM
				if player[currentPlayer].gravity == 0
					CallFunction(Player_Kill)
				end if
				break
			end switch

			BoxCollisionTest(C_SOLID, object.entityPos, 176, 0, 208, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				SetBit(object.stoodPlayers, temp7, true)
				break

			case COL_BOTTOM
				if player[currentPlayer].gravity == 0
					CallFunction(Player_Kill)
				end if
				break
			end switch

			temp7++
		next
		object.xpos = temp4
		object.ypos = temp5
	end if
end event


event ObjectDraw
	if object.active == true
		temp6 = object.xpos
		temp6 -= 0xC00000
		temp7 = object.ypos
		temp7 += 0xC0000
		temp0 = 0
		temp2 = object.rotation

		object.rotation >>= 5
		object.rotation &= 0x1FF
		while temp0 < 8
			DrawSpriteFX(5, FX_ROTATE, temp6, temp7)
			object.rotation += 0x40
			object.rotation &= 0x1FF
			temp0++
		loop

		temp5 = object.xpos
		temp5 += 0xC00000
		temp0 = 0
		while temp0 < 8
			DrawSpriteFX(5, FX_ROTATE, temp5, temp7)
			object.rotation += 0x40
			object.rotation &= 0x1FF
			temp0++
		loop

		object.rotation = temp2
		temp6 += object.indentPos.x
		temp7 += object.indentPos.y
		DrawSpriteXY(4, temp6, temp7)

		temp5 += object.indentPos.x
		DrawSpriteXY(4, temp5, temp7)

		DrawSpriteXY(3, object.platformMPos.x, object.platformMPos.y)
		DrawSpriteXY(2, object.platformLPos.x, object.platformLPos.y)
		DrawSpriteXY(0, object.jointLPos.x, object.jointLPos.y)
		DrawSpriteXY(2, object.platformRPos.x, object.platformRPos.y)
		DrawSpriteXY(1, object.jointRPos.x, object.jointRPos.y)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-16, -12, 32, 24, 479, 182)		// Joint L 				- #0
	SpriteFrame(-16, -12, 32, 24, 479, 133)		// Joint R 				- #1
	SpriteFrame(-40, -8, 80, 16, 349, 165)		// Side Platforms 		- #2
	SpriteFrame(-96, -24, 192, 48, 319, 207)	// Center Platform 		- #3
	SpriteFrame(-12, -12, 24, 24, 487, 157)		// Indent 				- #4
	SpriteFrame(-2, -14, 4, 4, 40, 34)			// Blank Circle Thingy	- #5

	foreach (TypeName[Wheel Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
	next

	SetTableValue(TypeName[Wheel Platform], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(WheelPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(WheelPlatform_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 // flipMoveX
			GetBit(checkResult, object.propertyValue, 0)
			break
			
		case 1 // flipMoveY
			GetBit(checkResult, object.propertyValue, 1)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // flipMoveX
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 0, checkResult)
			break
			
		case 1 // flipMoveY
			CheckNotEqual(editor.variableValue, false)
			SetBit(object.propertyValue, 1, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(3)
	temp0 = object.xpos
	temp0 -= 0x880000
	temp1 = object.ypos
	temp1 += 0xC0000
	DrawSpriteXY(2, temp0, temp1)
	temp0 -= 0x380000
	DrawSpriteXY(0, temp0, temp1)
	temp0 = object.xpos
	temp0 += 0x880000
	DrawSpriteXY(2, temp0, temp1)
	temp0 += 0x380000
	DrawSpriteXY(1, temp0, temp1)
end event


event RSDKLoad
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-16, -12, 32, 24, 479, 182)
	SpriteFrame(-16, -12, 32, 24, 479, 133)
	SpriteFrame(-40, -8, 80, 16, 349, 165)
	SpriteFrame(-96, -24, 192, 48, 319, 207)
	SpriteFrame(-12, -12, 24, 24, 487, 157)
	SpriteFrame(-2, -14, 4, 4, 40, 34)

	AddEditorVariable("flipMoveX")
	SetActiveVariable("flipMoveX")
	AddEnumVariable("false", false)
	AddEnumVariable("true", true)

	AddEditorVariable("flipMoveY")
	SetActiveVariable("flipMoveY")
	AddEnumVariable("false", false)
	AddEnumVariable("true", true)
end event
