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

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

private alias object.value0 : object.timer
private alias object.value1 : object.blockMovePos

private alias 0 : STAIRCASE_0
private alias 1 : STAIRCASE_1
private alias 2 : STAIRCASE_2
private alias 3 : STAIRCASE_3
private alias 4 : STAIRCASE_STATIC

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

private alias object.value17 : debugMode.currentSelection


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

reserve function Staircase_DebugDraw
reserve function Staircase_DebugSpawn


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

private value Staircase_startDebugID = 0


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

private function Staircase_DebugDraw
	DrawSprite(0)
end function


private function Staircase_DebugSpawn
	CreateTempObject(TypeName[Staircase], 0, object.xpos, object.ypos)
	GetBit(object[tempObjectPos].direction, object.direction, 0)
	object[tempObjectPos].propertyValue = debugMode[0].currentSelection
	object[tempObjectPos].propertyValue -= Staircase_startDebugID
end function


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

event ObjectUpdate
	object.priority = PRIORITY_ACTIVE
	switch object.state
	case STAIRCASE_0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -16, -16, 112, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_TOP
				object.priority = PRIORITY_ACTIVE
				object.timer = 32
				object.state++
			end if
		next
		break

	case STAIRCASE_1
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -16, -16, 112, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		next

		object.timer--
		if object.timer < 0
			object.timer = 128
			object.state++
		end if
		break

	case STAIRCASE_2
		temp0 = object.xpos
		temp1 = object.ypos

		if object.direction == FLIP_NONE
			object.xpos += 0x600000
			temp4 = -0x200000
		else
			temp4 = 0x200000
		end if

		temp5 = object.blockMovePos
		temp2 = 1
		while temp2 < 5
			object.ypos += temp5
			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
					temp6 = object.ypos
					temp6 &= 0xFFFF0000
					if object.propertyValue == 0
						temp7 = 0x4000
					else
						temp7 = -0x4000
					end if
					temp7 *= temp2
					temp7 += object.ypos
					temp7 &= 0xFFFF0000
					temp7 -= temp6
					player[currentPlayer].ypos += temp7
				end if

				if player[currentPlayer].gravity == GRAVITY_GROUND
					if checkResult == COL_BOTTOM
						if object.propertyValue == 0
							CallFunction(Player_Kill)
						end if
					end if
				end if
			next
			object.xpos += temp4
			temp2++
		loop

		if object.propertyValue == 0
			object.blockMovePos += 0x4000
		else
			object.blockMovePos -= 0x4000
		end if

		object.xpos = temp0
		object.ypos = temp1
		object.timer--
		if object.timer <= 0
			object.state++
		end if
		break

	case STAIRCASE_3
		temp0 = object.xpos
		temp1 = object.ypos
		if object.direction == FLIP_NONE
			object.xpos += 0x600000
			temp4 = -0x200000
		else
			temp4 = 0x200000
		end if

		temp5 = object.blockMovePos
		temp2 = 1
		while temp2 < 5
			object.ypos += temp5
			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 player[currentPlayer].gravity == GRAVITY_GROUND
					if checkResult == COL_BOTTOM
						CallFunction(Player_Kill)
					end if
				end if
			next
			object.xpos += temp4
			temp2++
		loop

		object.xpos = temp0
		object.ypos = temp1
		break

	case STAIRCASE_STATIC
		break
		
	end switch

	temp1 = 0
	temp0 = camera[0].xpos
	temp0 -= object.ixpos
	Abs(temp0)
	temp0 -= 192
	if temp0 >= screen.xcenter
		temp1 = true
	end if

	temp0 = screen.xcenter
	temp0 += 192
	temp1 = screen.ycenter
	temp1 += 192
	CheckCameraProximity(object.ixpos, object.iypos, temp0, temp1)
	if checkResult == false
		object.priority = PRIORITY_BOUNDS
		object.state = STAIRCASE_0
		object.blockMovePos = 0
		if object.propertyValue == 2
			object.state = STAIRCASE_STATIC
		end if
	end if
end event


event ObjectDraw
	switch object.state
	case STAIRCASE_0
	case STAIRCASE_1
		DrawSprite(0)
		break

	case STAIRCASE_2
	case STAIRCASE_3
		temp2 = object.xpos
		temp3 = object.ypos
		if object.direction == FLIP_NONE
			temp2 += 0x600000
			temp4 = -0x200000
		else
			temp4 = 0x200000
		end if

		temp5 = object.blockMovePos
		temp3 += temp5
		DrawSpriteXY(1, temp2, temp3)

		temp2 += temp4
		temp3 += temp5
		DrawSpriteXY(1, temp2, temp3)

		temp2 += temp4
		temp3 += temp5
		DrawSpriteXY(1, temp2, temp3)

		temp2 += temp4
		temp3 += temp5
		DrawSpriteXY(1, temp2, temp3)
		break

	case STAIRCASE_STATIC
		break
		
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("CPZ/Objects3.gif")
	SpriteFrame(-16, -16, 128, 32, 1, 62)
	SpriteFrame(-16, -16, 32, 32, 1, 62)

	foreach (TypeName[Staircase], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].propertyValue == 2
			object[arrayPos0].state = STAIRCASE_STATIC
		end if
	next

	temp0 = 0
	Staircase_startDebugID = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[Staircase], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(Staircase_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(Staircase_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 // type
			checkResult = object.propertyValue
			checkResult &= 7
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // type
			object.propertyValue = editor.variableValue
			object.propertyValue &= 7
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("CPZ/Objects3.gif")
	SpriteFrame(-16, -16, 128, 32, 1, 62)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Extend Downwards", 0)
	AddEnumVariable("Extend Upwards", 4) // idk why its 4, 1 also works but whatever floats your boat ig
	AddEnumVariable("Static", 2)
end event
