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

//-------Aliases-------//
private alias 51 : TYPE_STAIRCASE

// Function declarations
reserve function Staircase_DebugDraw
reserve function Staircase_DebugSpawn

// Static Values
public value Staircase_value42 = 0;

// Tables

function Staircase_DebugDraw
	DrawSprite(0)
end function


function Staircase_DebugSpawn
	CreateTempObject(TypeName[Staircase], 0, object.xpos, object.ypos)
	GetBit(object[tempObjectPos].direction, object.direction, 0)
	object[tempObjectPos].propertyValue = object[0].value17
	object[tempObjectPos].propertyValue -= Staircase_value42
end function


event ObjectMain
	object.priority = PRIORITY_ACTIVE
	switch object.state
	case 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 112, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object.priority = PRIORITY_ACTIVE
				object.value0 = 32
				object.state++
			end if
		next
		break
	case 1
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 112, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		next
		object.value0--
		if object.value0 < 0
			object.value0 = 128
			object.state++
		end if
		break
	case 2
		temp0 = object.xpos
		temp1 = object.ypos
		if object.direction == FLIP_NONE
			object.xpos += 0x600000
			temp4 = -0x200000
		else
			temp4 = 0x200000
		end if
		temp5 = object.value1
		temp2 = 1
		while temp2 < 5
			object.ypos += temp5
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 16, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					temp6 = object.ypos
					temp6 &= -0x10000
					if object.propertyValue == 0
						temp7 = 0x4000
					else
						temp7 = -0x4000
					end if
					temp7 *= temp2
					temp7 += object.ypos
					temp7 &= -0x10000
					temp7 -= temp6
					object[currentPlayer].ypos += temp7
				end if
				if object[currentPlayer].gravity == GRAVITY_GROUND
					if checkResult == 4
						if object.propertyValue == 0
							CallFunction(PlayerObject_Kill)
						end if
					end if
				end if
			next
			object.xpos += temp4
			temp2++
		loop
		if object.propertyValue == 0
			object.value1 += 0x4000
		else
			object.value1 -= 0x4000
		end if
		object.xpos = temp0
		object.ypos = temp1
		object.value0--
		if object.value0 <= 0
			object.state++
		end if
		break
	case 3
		temp0 = object.xpos
		temp1 = object.ypos
		if object.direction == FLIP_NONE
			object.xpos += 0x600000
			temp4 = -0x200000
		else
			temp4 = 0x200000
		end if
		temp5 = object.value1
		temp2 = 1
		while temp2 < 5
			object.ypos += temp5
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_BOX, object.entityPos, -16, -16, 16, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if object[currentPlayer].gravity == GRAVITY_GROUND
					if checkResult == 4
						CallFunction(PlayerObject_Kill)
					end if
				end if
			next
			object.xpos += temp4
			temp2++
		loop
		object.xpos = temp0
		object.ypos = temp1
		break
	end switch
	temp1 = 0
	temp0 = screen.cameraX
	temp0 -= object.ixpos
	Abs(temp0)
	temp0 -= 192
	if temp0 >= screen.xcenter
		temp1 = 1
	end if
	temp0 = screen.cameraY
	temp0 -= object.iypos
	Abs(temp0)
	temp0 -= 192
	if temp0 >= screen.ycenter
		temp1 = 1
	end if
	if temp1 == 1
		object.priority = PRIORITY_ACTIVE_BOUNDS
		object.state = 0
		object.value1 = 0
		if object.propertyValue == 2
			object.state = 4
		end if
	end if
end event


event ObjectDraw
	switch object.state
	case 0
	case 1
		DrawSprite(0)
		break
	case 2
	case 3
		temp2 = object.xpos
		temp3 = object.ypos
		if object.direction == FLIP_NONE
			temp2 += 0x600000
			temp4 = -0x200000
		else
			temp4 = 0x200000
		end if
		temp5 = object.value1
		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
	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 = 4
		end if
	next
	temp0 = 0
	Staircase_value42 = 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

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
