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

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

private alias object.value0 : object.grabDelay

// Player Aliases
private alias object.state : player.state
private alias object.ypos : player.ypos
private alias object.xvel : player.xvel
private alias object.yvel : player.yvel
private alias object.speed : player.speed
private alias object.gravity : player.gravity
private alias object.animation : player.animation
private alias object.collisionTop : player.collisionTop
private alias object.jumpPress : player.jumpPress


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

event ObjectUpdate
	if object.grabDelay > 0
		object.grabDelay--
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if player[currentPlayer].state != Player_State_Static
			if object.grabDelay == 0
				temp0 = player[currentPlayer].collisionTop

				BoxCollisionTest(C_TOUCH, object.entityPos, -24, 0, 24, 8, currentPlayer, -1, temp0, 1, temp0)
				if checkResult == true
					if player[currentPlayer].gravity == 1
						player[currentPlayer].state 	= Player_State_Static
						player[currentPlayer].animation = ANI_HANGING
						player[currentPlayer].speed 	= 0
						player[currentPlayer].xvel 		= 0
						player[currentPlayer].yvel 		= 0
						player[currentPlayer].ypos 		= player[currentPlayer].collisionTop
						player[currentPlayer].ypos++
						FlipSign(player[currentPlayer].ypos)
						player[currentPlayer].ypos <<= 16
						player[currentPlayer].ypos += object.ypos
						PlaySfx(SfxName[Catch], false)
					end if
				end if
			end if
		else
			if player[currentPlayer].jumpPress == true
				player[currentPlayer].state 	= Player_State_Air
				player[currentPlayer].yvel 		= -0x40000
				player[currentPlayer].gravity 	= 1
				object.grabDelay = 8
			end if

			if player[currentPlayer].state == Player_State_Static
				player[currentPlayer].ypos = player[currentPlayer].collisionTop
				player[currentPlayer].ypos++
				FlipSign(player[currentPlayer].ypos)
				player[currentPlayer].ypos <<= 16
				player[currentPlayer].ypos += object.ypos
			end if
		end if
	next
end event


event ObjectDraw
	// Code?? maybe?? at one point??
end event


event ObjectStartup
	// Code?? maybe?? at one point??
end event


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

event RSDKDraw
	DrawSprite(0)
end event


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