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

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

private alias object.value0  : object.blockPos1.x
private alias object.value1  : object.blockPos1.y
private alias object.value2  : object.blockPos2.x
private alias object.value3  : object.blockPos2.y
private alias object.value4  : object.blockPos3.x
private alias object.value5  : object.blockPos3.y
private alias object.value6  : object.blockPos4.x
private alias object.value7  : object.blockPos4.y
private alias object.value8  : object.blockPos5.x
private alias object.value9  : object.blockPos5.y
private alias object.value10 : object.blockPos6.x
private alias object.value11 : object.blockPos6.y
private alias object.value12 : object.blockPos7.x
private alias object.value13 : object.blockPos7.y
private alias object.value14 : object.blockPos8.x
private alias object.value15 : object.blockPos8.y

private alias object.value16 : object.startPos.x
private alias object.value17 : object.startPos.y

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


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

reserve function RotatingStair_DebugDraw
reserve function RotatingStair_DebugSpawn
reserve function RotatingStair_ProcessBlock


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

private function RotatingStair_DebugDraw
	temp0 = object.xpos
	temp0 -= 0x700000
	temp1 = object.ypos
	temp1 -= 0x700000
	temp2 = 0
	while temp2 < 8
		DrawSpriteXY(0, temp0, temp1)
		temp0 += 0x200000
		temp1 += 0x200000
		temp2++
	loop
end function


private function RotatingStair_DebugSpawn
	CreateTempObject(TypeName[Rotating Stair], 0, object.xpos, object.ypos)
	object[tempObjectPos].startPos.x = object.xpos
	object[tempObjectPos].startPos.y = object.ypos
end function


private function RotatingStair_ProcessBlock
	temp1 = temp5
	GetTableValue(temp0, temp3, StageSetup_oscillationTable)
	temp0 <<= 8
	if temp3 == 20
		temp0 >>= 1
	end if

	switch temp4
	case 0
		temp0 -= temp1
		temp0 += object.startPos.x
		temp6 = temp0
		FlipSign(temp1)
		temp1 += object.startPos.y
		temp7 = temp1
		break
		
	case 1
		temp0 -= temp1
		FlipSign(temp0)
		temp0 += object.startPos.y
		temp7 = temp0
		temp1 += object.startPos.x
		temp6 = temp1
		break

	case 2
		temp0 -= temp1
		FlipSign(temp0)
		temp0 += object.startPos.x
		temp6 = temp0
		temp1 += object.startPos.y
		temp7 = temp1
		break

	case 3
		temp0 -= temp1
		temp0 += object.startPos.y
		temp7 = temp0
		FlipSign(temp1)
		temp1 += object.startPos.x
		temp6 = temp1
		break
		
	end switch

	temp6 &= 0xFFFF0000
	temp7 &= 0xFFFF0000
	temp0 = temp6
	temp0 -= object.xpos
	temp1 = temp7
	temp1 -= object.ypos

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_SOLID, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == COL_TOP
			player[currentPlayer].xpos += temp0
			player[currentPlayer].ypos += temp1
		end if

		if player[currentPlayer].gravity == 0
			if checkResult == COL_BOTTOM
				CallFunction(Player_Kill)
			end if
		end if
	next
end function


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

event ObjectUpdate
	temp4 = SLZSetup_rotateTimer
	temp5 = 0x100000
	temp3 = 20
	object.xpos = object.blockPos1.x
	object.ypos = object.blockPos1.y
	CallFunction(RotatingStair_ProcessBlock)
	object.blockPos1.x = temp6
	object.blockPos1.y = temp7

	temp4 ^= 2
	object.xpos = object.blockPos2.x
	object.ypos = object.blockPos2.y
	CallFunction(RotatingStair_ProcessBlock)
	object.blockPos2.x = temp6
	object.blockPos2.y = temp7

	temp4 ^= 2
	temp5 = 0x300000
	temp3 = 22
	object.xpos = object.blockPos3.x
	object.ypos = object.blockPos3.y
	CallFunction(RotatingStair_ProcessBlock)
	object.blockPos3.x = temp6
	object.blockPos3.y = temp7
	temp4 ^= 2

	object.xpos = object.blockPos4.x
	object.ypos = object.blockPos4.y
	CallFunction(RotatingStair_ProcessBlock)
	object.blockPos4.x = temp6
	object.blockPos4.y = temp7

	temp4 ^= 2
	temp5 = 0x500000
	temp3 = 24
	object.xpos = object.blockPos5.x
	object.ypos = object.blockPos5.y
	CallFunction(RotatingStair_ProcessBlock)
	object.blockPos5.x = temp6
	object.blockPos5.y = temp7

	temp4 ^= 2
	object.xpos = object.blockPos6.x
	object.ypos = object.blockPos6.y
	CallFunction(RotatingStair_ProcessBlock)
	object.blockPos6.x = temp6
	object.blockPos6.y = temp7

	temp4 ^= 2
	temp5 = 0x700000
	temp3 = 26
	object.xpos = object.blockPos7.x
	object.ypos = object.blockPos7.y
	CallFunction(RotatingStair_ProcessBlock)
	object.blockPos7.x = temp6
	object.blockPos7.y = temp7

	temp4 ^= 2
	object.xpos = object.blockPos8.x
	object.ypos = object.blockPos8.y
	CallFunction(RotatingStair_ProcessBlock)
	object.blockPos8.x = temp6
	object.blockPos8.y = temp7

	object.xpos = object.startPos.x
	object.ypos = object.startPos.y
end event


event ObjectDraw
	DrawSpriteXY(0, object.blockPos1.x, object.blockPos1.y)
	DrawSpriteXY(0, object.blockPos2.x, object.blockPos2.y)
	DrawSpriteXY(0, object.blockPos3.x, object.blockPos3.y)
	DrawSpriteXY(0, object.blockPos4.x, object.blockPos4.y)
	DrawSpriteXY(0, object.blockPos5.x, object.blockPos5.y)
	DrawSpriteXY(0, object.blockPos6.x, object.blockPos6.y)
	DrawSpriteXY(0, object.blockPos7.x, object.blockPos7.y)
	DrawSpriteXY(0, object.blockPos8.x, object.blockPos8.y)
end event


event ObjectStartup
	LoadSpriteSheet("SLZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 67, 26)

	foreach (TypeName[Rotating Stair], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
	next

	SetTableValue(TypeName[Rotating Stair], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(RotatingStair_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(RotatingStair_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	temp0 = object.xpos
	temp0 -= 0x700000
	temp1 = object.ypos
	temp1 -= 0x700000
	temp2 = 0
	while temp2 < 8
		DrawSpriteXY(0, temp0, temp1)
		temp0 += 0x200000
		temp1 += 0x200000
		temp2++
	loop
end event


event RSDKLoad
	LoadSpriteSheet("SLZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 67, 26)

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
