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

//-------Aliases-------//
private alias 52 : TYPE_GRABPANEL

// Function declarations
reserve function GrabPanel_DebugDraw
reserve function GrabPanel_DebugSpawn

// Static Values

// Tables

function GrabPanel_DebugDraw
	DrawSprite(0)
end function


function GrabPanel_DebugSpawn
	CreateTempObject(TypeName[Grab Panel], 0, object.xpos, object.ypos)
	object[tempObjectPos].value0 = 60
end function


event ObjectMain
	temp7 = 0
	temp4 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp2 = 0
		if object[currentPlayer].type == TypeName[Tails Object]
			temp3 = -0x100000
		else
			temp3 = -0x160000
		end if
		temp0 = object.xpos
		temp0 += temp3
		temp0 -= object[currentPlayer].xpos
		Abs(temp0)
		if temp0 <= 0x40000
			temp0 = object.ypos
			temp0 -= object[currentPlayer].ypos
			temp1 = temp0
			Abs(temp0)
			if temp0 <= 0x160000
				object[currentPlayer].animation = ANI_CLINGING
				object[currentPlayer].animationSpeed = 128
				object[currentPlayer].direction = FLIP_X
				object[currentPlayer].xpos = object.xpos
				object[currentPlayer].xpos += temp3
				if object.propertyValue == 127
					object[currentPlayer].state = PlayerObject_Blank
					object[currentPlayer].xvel = 0
					object[currentPlayer].yvel = 0
					object[currentPlayer].speed = 0
				end if
				temp2 = 1
			else
				if object[currentPlayer].animation == ANI_CLINGING
					object[currentPlayer].ypos = object.ypos
					if temp1 < 0
						object[currentPlayer].ypos += 0x160000
					else
						object[currentPlayer].ypos -= 0x160000
					end if
					temp2 = 1
				end if
			end if
			SetBit(temp4, currentPlayer, temp2)
			if temp2 != 0
				if currentPlayer == 0
					if object[currentPlayer].jumpPress != 0
						if object[currentPlayer].ypos < object.ypos
							temp7 = 1
						else
							temp7 = 2
						end if
					end if
					object.value0--
					if object.value0 == 0
						if object[currentPlayer].ypos < object.ypos
							temp7 = 1
						else
							temp7 = 2
						end if
					end if
				end if
			end if
		end if
	next
	if temp7 != 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			GetBit(temp2, temp4, currentPlayer)
			if temp2 == 1
				object[currentPlayer].animation = ANI_FANROTATE
			end if
		next
		
		PlaySfx(SfxName[Ledge Break], 0)
		temp0 = object.xpos
		temp0 -= 0x100000
		temp1 = object.ypos
		temp1 -= 0x100000
		if temp7 == 1
			CreateTempObject(TypeName[Grab Panel Part], 0, temp0, temp1)
			object[tempObjectPos].xvel = -0x40000
			object[tempObjectPos].state = 1
			temp1 += 0x200000
			CreateTempObject(TypeName[Grab Panel Part], 4, temp0, temp1)
			object[tempObjectPos].xvel = -0x40000
			temp0 -= 0x200000
			temp1 -= 0x200000
			CreateTempObject(TypeName[Grab Panel Part], 16, temp0, temp1)
			object[tempObjectPos].xvel = -0x40000
			temp1 += 0x200000
			CreateTempObject(TypeName[Grab Panel Part], 24, temp0, temp1)
			object[tempObjectPos].xvel = -0x40000
		else
			CreateTempObject(TypeName[Grab Panel Part], 4, temp0, temp1)
			object[tempObjectPos].xvel = -0x40000
			temp1 += 0x200000
			CreateTempObject(TypeName[Grab Panel Part], 0, temp0, temp1)
			object[tempObjectPos].xvel = -0x40000
			object[tempObjectPos].state = 1
			temp0 -= 0x200000
			temp1 -= 0x200000
			CreateTempObject(TypeName[Grab Panel Part], 24, temp0, temp1)
			object[tempObjectPos].xvel = -0x40000
			temp1 += 0x200000
			CreateTempObject(TypeName[Grab Panel Part], 16, temp0, temp1)
			object[tempObjectPos].xvel = -0x40000
		end if
		object.type = TypeName[Blank Object]
	end if
end event


event ObjectDraw
	if object.propertyValue != 127
		DrawSprite(0)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("SCZ/Objects.gif")
	SpriteFrame(-64, -32, 64, 64, 1, 69)
	foreach (TypeName[Grab Panel], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value0 = object[arrayPos0].propertyValue
		object[arrayPos0].value0 *= 60
	next
	SetTableValue(TypeName[Grab Panel], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(GrabPanel_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(GrabPanel_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

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