// ----------------------------------
// RSDK Project: Sonic 2
// 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

// Probably unused since this object was likely based off SLZ's RotatingStair
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.state : player.state
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.gravity : player.gravity
private alias object.animation : player.animation

private alias object.value1  : player.timer
private alias object.value17 : debugMode.currentSelection

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

reserve function RotatingStair_DebugDraw
reserve function RotatingStair_DebugSpawn
reserve function RotatingStair_ProcessBlock


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

private value RotatingStair_debugStartID = 0


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

private function RotatingStair_DebugDraw
	temp0 = object.xpos
	temp0 -= 0x300000

	temp1 = object.ypos
	temp1 -= 0x300000

	temp3 = debugMode[0].currentSelection
	temp3 -= RotatingStair_debugStartID

	if temp3 < 2
		temp2 = 0
		while temp2 < 4
			DrawSpriteXY(0, temp0, temp1)
			temp0 += 0x200000
			temp1 += 0x200000
			temp2++
		loop
	else
		DrawSpriteXY(0, temp0, temp1)
		temp0 += 0x600000
		temp1 += 0x600000
		DrawSpriteXY(0, temp0, temp1)
	end if
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
	object[tempObjectPos].propertyValue = debugMode[0].currentSelection
	object[tempObjectPos].propertyValue -= RotatingStair_debugStartID
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)
		switch checkResult
		case COL_TOP
			player[currentPlayer].xpos += temp0
			player[currentPlayer].ypos += temp1
			break

		case COL_LEFT
		case COL_RIGHT
			if player[currentPlayer].state == Player_State_Climb
				player[currentPlayer].timer = 0
				player[currentPlayer].animation = ANI_GLIDING_DROP
				player[currentPlayer].state = Player_State_GlideDrop
			end if
			break

		case COL_BOTTOM
			if player[currentPlayer].gravity == GRAVITY_GROUND
				if temp1 >= 0
					CallFunction(Player_Kill)
				end if
			end if
			break
			
		end switch
	next
end function


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

event ObjectUpdate
	temp4 = CPZSetup_rotatingStairTimer
	GetBit(temp0, object.propertyValue, 0)
	if temp0 == true
		temp4 ^= 1
	end if

	GetBit(temp0, object.propertyValue, 1)
	if temp0 == false
		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
	end if

	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.startPos.x
	object.ypos = object.startPos.y
end event


event ObjectDraw
	GetBit(temp0, object.propertyValue, 1)
	if temp0 == false
		DrawSpriteXY(0, object.blockPos1.x, object.blockPos1.y)
		DrawSpriteXY(0, object.blockpos2.x, object.blockpos2.y)
	end if

	DrawSpriteXY(0, object.blockpos3.x, object.blockpos3.y)
	DrawSpriteXY(0, object.blockpos4.x, object.blockpos4.y)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone02")
	if checkResult == true
		LoadSpriteSheet("CPZ/Objects3.gif")
		SpriteFrame(-16, -16, 32, 32, 1, 62)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 32, 130, 829)
	end if

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

	temp0 = 0
	RotatingStair_debugStartID = DebugMode_ObjCount
	while temp0 < 4
		SetTableValue(TypeName[Rotating Stair], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(RotatingStair_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(RotatingStair_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
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 // direction
			GetBit(checkResult, object.propertyValue, 0)
			break
			
		case 1 // type
			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 // direction
			CheckNotEqual(editor.variableValue, 0)
			SetBit(object.propertyValue, 0, checkResult)
			break
			
		case 1 // type
			CheckNotEqual(editor.variableValue, 0)
			SetBit(object.propertyValue, 1, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	temp0 = object.xpos
	temp0 -= 0x300000

	temp1 = object.ypos
	temp1 -= 0x300000

	if object.propertyValue < 2
		temp2 = 0
		while temp2 < 4
			DrawSpriteXY(0, temp0, temp1)
			temp0 += 0x200000
			temp1 += 0x200000
			temp2++
		loop
	else
		DrawSpriteXY(0, temp0, temp1)
		temp0 += 0x600000
		temp1 += 0x600000
		DrawSpriteXY(0, temp0, temp1)
	end if
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone02")
	if checkResult == true
		LoadSpriteSheet("CPZ/Objects3.gif")
		SpriteFrame(-16, -16, 32, 32, 1, 62)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -16, 32, 32, 130, 829)
	end if

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("No Flip", 0)
	AddEnumVariable("Flip X", 1)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("4 Blocks", 0)
	AddEnumVariable("2 Blocks", 1)
end event
