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

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

private alias object.propertyValue : object.length

private alias object.value0 : object.swingAngle
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

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

private alias object.value17 : debugMode.currentSelection


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

reserve function SwingPlatform_DebugDraw
reserve function SwingPlatform_DebugSpawn


// ========================
// Static Values
// ========================

private value SwingPlatform_startDebugID = 0


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

private function SwingPlatform_DebugDraw
	temp7 = debugMode[0].currentSelection
	temp7 -= SwingPlatform_startDebugID
	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 SwingPlatform_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= SwingPlatform_startDebugID
	CreateTempObject(TypeName[Swing Platform], temp0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
end function


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

event ObjectUpdate
	temp2 = object.xpos
	temp3 = object.ypos
	GetTableValue(object.swingAngle, 12, StageSetup_oscillationTable)
	object.swingAngle >>= 8
	if object.direction == 1
		object.swingAngle ^= 255
		object.swingAngle += 129
	end if
	object.swingAngle <<= 1

	temp1 = object.length
	temp1++
	temp1 <<= 4
	temp1 -= 8
	object.xpos = object.drawPos.x
	object.ypos = object.drawPos.y

	Cos(object.collisionOffset.x, object.swingAngle)
	object.collisionOffset.x *= temp1
	object.collisionOffset.x <<= 7
	object.collisionOffset.x += temp2
	object.collisionOffset.x &= 0xFFFF0000
	object.drawPos.x = object.collisionOffset.x
	object.collisionOffset.x -= object.xpos

	Sin(object.collisionOffset.y, object.swingAngle)
	object.collisionOffset.y *= temp1
	object.collisionOffset.y <<= 7
	object.collisionOffset.y += temp3
	object.collisionOffset.y &= 0xFFFF0000
	object.drawPos.y = object.collisionOffset.y
	object.collisionOffset.y -= object.ypos

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_PLATFORM, object.entityPos, -32, -16, 32, 0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			player[currentPlayer].xpos += object.collisionOffset.x
			player[currentPlayer].ypos += object.collisionOffset.y
		else
			BoxCollisionTest(C_TOUCH, object.entityPos, -32, 0, 32, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_Hit)
			else
				BoxCollisionTest(C_TOUCH, object.entityPos, -16, 8, 16, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					CallFunction(Player_Hit)
				end if
			end if
		end if
	next
	object.xpos = temp2
	object.ypos = temp3
end event


event ObjectDraw
	DrawSprite(0)

	temp0 = 0
	temp1 = 16
	while temp0 < object.length
		Cos(temp2, object.swingAngle)
		temp2 *= temp1
		temp2 <<= 7
		temp2 += object.xpos
		Sin(temp3, object.swingAngle)
		temp3 *= temp1
		temp3 <<= 7
		temp3 += object.ypos
		DrawSpriteXY(1, temp2, temp3)
		temp0++
		temp1 += 16
	loop

	temp1 -= 8
	DrawSpriteXY(2, object.drawPos.x, object.drawPos.y)
end event


event ObjectStartup
	LoadSpriteSheet("SLZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 173, 141)
	SpriteFrame(-8, -8, 16, 16, 173, 158)
	SpriteFrame(-44, -16, 88, 46, 84, 141)

	temp0 = 0
	SwingPlatform_startDebugID = DebugMode_ObjCount
	SwingPlatform_startDebugID--
	while temp0 < 8
		SetTableValue(TypeName[Swing Platform], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(SwingPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(SwingPlatform_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


event RSDKDraw
	DrawSprite(0)
	temp0 = 0
	temp1 = 16

	while temp0 < object.length
		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 event


event RSDKLoad
	LoadSpriteSheet("SLZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 173, 141)
	SpriteFrame(-8, -8, 16, 16, 173, 158)
	SpriteFrame(-44, -16, 88, 46, 84, 141)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "length")
end event
