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

//-------Aliases-------//
private alias 62 : TYPE_GRABBAR

reserve function GrabBar_AnimateShuffle

// Static Values

// Tables


function GrabBar_AnimateShuffle
	object.value1++
	if object.value1 == 8
		object.value1 = 0
		object[currentPlayer].frame++
		object[currentPlayer].frame %= 4
	end if
end function

event ObjectMain
	if object.value0 > 0
		object.value0--
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object[currentPlayer].state != PlayerObject_Blank
			if object.value0 == 0
				temp0 = object[currentPlayer].collisionTop
				BoxCollisionTest(C_TOUCH, object.entityPos, -24, 0, 24, 8, currentPlayer, -1, temp0, 1, temp0)
				if checkResult == 1
					if object[currentPlayer].gravity == GRAVITY_AIR
						object[currentPlayer].state = PlayerObject_Blank
						object[currentPlayer].animation = ANI_HANGMOVE
						object[currentPlayer].speed = 0
						object[currentPlayer].xvel = 0
						object[currentPlayer].yvel = 0
						object[currentPlayer].ypos = object[currentPlayer].collisionTop
						object[currentPlayer].ypos++
						FlipSign(object[currentPlayer].ypos)
						object[currentPlayer].ypos <<= 16
						object[currentPlayer].ypos += object.ypos
						PlaySfx(SfxName[Catch], 0)
					end if
				end if
			end if
		else
			temp1 = 0
			BoxCollisionTest(C_TOUCH, object.entityPos, -24, -24, 24, 24, currentPlayer, -1, -1, 1, 1)
			if checkResult == 1
				if object[currentPlayer].jumpPress == 1
					object[currentPlayer].animation = ANI_JUMPING
					PlaySfx(SfxName[Jump], 0)
					object[currentPlayer].value35 = 1
					object[currentPlayer].state = PlayerObject_HandleAir
					object[currentPlayer].yvel = -0x40000
					object[currentPlayer].gravity = GRAVITY_AIR
					object.value0 = 8
				else
					if object[currentPlayer].state == PlayerObject_Blank
						if object[currentPlayer].left == 1
							object[currentPlayer].direction = FLIP_X
							object[currentPlayer].xpos -= 0x10000
							temp0 = object.xpos
							temp0 -= 0x100000
							if object[currentPlayer].xpos < temp0
								object[currentPlayer].xpos = temp0
							else
								CallFunction(GrabBar_AnimateShuffle)
							end if
						else
							if object[currentPlayer].right == 1
								object[currentPlayer].direction = FLIP_NONE
								object[currentPlayer].xpos += 0x10000
								temp0 = object.xpos
								temp0 += 0x100000
								if object[currentPlayer].xpos > temp0
									object[currentPlayer].xpos = temp0
								else
								CallFunction(GrabBar_AnimateShuffle)
								end if
							else
								object[currentPlayer].frame = 0
							end if
						end if
						object[currentPlayer].ypos = object[currentPlayer].collisionTop
						object[currentPlayer].ypos++
						FlipSign(object[currentPlayer].ypos)
						object[currentPlayer].ypos <<= 16
						object[currentPlayer].ypos += object.ypos
					end if
				end if
			end if
		end if
	next
end event


event ObjectDraw
end event


event ObjectStartup
end event

event RSDKDraw
	DrawSprite(0)
end event

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