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

//-------Aliases-------//
private alias 57 : TYPE_SEESAW

// Function declarations
reserve function SeeSaw_DebugDraw
reserve function SeeSaw_DebugSpawn
reserve function SeeSaw_Function131

// Static Values

// Tables
public table SeeSaw_table30
	36, 36, 38, 40, 42, 44, 42, 40, 38, 36, 35, 34, 33, 32, 31, 30
	29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14
	13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 2, 2, 2, 2
	
end table


function SeeSaw_DebugDraw
	DrawSprite(0)
end function


function SeeSaw_DebugSpawn
	CreateTempObject(TypeName[SeeSaw], 0, object.xpos, object.ypos)
	object[tempObjectPos].value8 = object.xpos
	object[tempObjectPos].value8 += 0x280000
	object[tempObjectPos].value9 = object.ypos
	object[tempObjectPos].value9 -= 8
	object[tempObjectPos].value10 = 2
	object[tempObjectPos].value4 = 2
	object[tempObjectPos].value6 = 2
	if object[tempObjectPos].propertyValue != 0
		object[tempObjectPos].state = 3
	end if
end function


function SeeSaw_Function131
	temp2 = 0
	if temp7 == 1
		temp0 = -21
		temp1 = -6
		temp2 = 48
	else
		temp1 = object[currentPlayer].xpos
		temp1 -= object.xpos
		temp1 >>= 16
		temp1 += 48
		temp3 = temp1
		if temp1 >= 0
			if temp1 < 96
				temp1 >>= 1
				if temp7 == 0
					FlipSign(temp1)
					temp1 += 47
				end if
				GetTableValue(temp0, temp1, SeeSaw_table30)
				FlipSign(temp0)
				temp1 = temp0
				temp1 += 18
				temp2 = 40
			end if
		end if
	end if
end function


event ObjectMain
	temp0 = object.value1
	temp0 |= object.value3
	if temp0 == 0
		if object.value2 != 0
			object.value6 = 1
		end if
	else
		if object.value1 == object.value3
			object.value6 = 1
		else
			if object.value1 > object.value3
				object.value6 = 0
			else
				object.value6 = 2
			end if
		end if
	end if
	if object.value4 != object.value6
		if object.value4 > object.value6
			object.value4--
		else
			object.value4++
		end if
		if object.state == 1
			if object.value6 == 1
				temp0 = -0x11400
				temp1 = -0x81800
			else
				if object.value0 == 0
					if object.value7 >= 0xA0000
						temp0 = -0xA000
						temp1 = -0xE0000
					else
						temp0 = -0xCC00
						temp1 = -0xAF000
					end if
				else
					if object.value7 >= 0x9C000
						temp0 = -0x8000
						temp1 = -0xA2000
					else
						temp0 = -0xF400
						temp1 = -0x96000
					end if
				end if
			end if
			if object.value8 < object.xpos
				FlipSign(temp0)
			end if
			object.xvel = temp0
			object.yvel = temp1
			object.state++
			object.priority = PRIORITY_ACTIVE
		end if
	end if
	if object.value8 < object.xpos
		switch object.value4
		case 0
			temp0 = -0x280000
			temp1 = -0x80000
			break
		case 1
			temp0 = -0x280000
			temp1 = -0x1C0000
			break
		case 2
			temp0 = -0x280000
			temp1 = -0x2F0000
			break
		end switch
	else
		switch object.value4
		case 0
			temp0 = 0x280000
			temp1 = -0x2F0000
			break
		case 1
			temp0 = 0x280000
			temp1 = -0x1C0000
			break
		case 2
			temp0 = 0x280000
			temp1 = -0x80000
			break
		end switch
	end if
	switch object.state
	case 0
		object.value8 = object.xpos
		object.value8 += temp0
		object.value9 = object.ypos
		object.value9 += temp1
		object.value7 = 0
		break
	case 1
		break
	case 2
		object.value8 += object.xvel
		object.value9 += object.yvel
		object.yvel += 0x3800
		if object.yvel > 0
			temp1 += object.ypos
			if object.value9 >= temp1
				object.value9 = temp1
				object.state = 0
				if object.value0 != 0
					if object.xvel != 0
						CreateTempObject(TypeName[Smoke Puff], 0, object.value8, object.value9)
						PlaySfx(SfxName[Explosion], 0)
						object.state = 3
						object.value0 = 0
					end if
				end if
				if object.value8 < object.xpos
					object.value6 = 0
					object.value11 = 1
				else
					object.value6 = 2
					object.value11 = 2
				end if
				object.priority = PRIORITY_ACTIVE_BOUNDS
			end if
		end if
		break
	end switch
	object.value1 = 0
	object.value2 = 0
	object.value3 = 0
	object.value12 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp7 = object.value5
		CallFunction(SeeSaw_Function131)
		if temp2 != 0
			temp3 = temp2
			FlipSign(temp3)
			if object[currentPlayer].yvel > object.value7
				temp4 = object[currentPlayer].yvel
				temp4 += 0x7000
			else
				temp4 = object.value7
			end if
			if object[currentPlayer].gravity == GRAVITY_AIR
				BoxCollisionTest(C_PLATFORM, object.entityPos, temp3, temp0, temp2, temp1, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			else
				checkResult = 0
			end if
			if checkResult == 1
				object.value12 = 1
				if object.value4 != object.value5
					temp6 = temp0
					temp7 = object.value4
					CallFunction(SeeSaw_Function131)
					temp0 -= temp6
					temp0 <<= 16
					object[currentPlayer].ypos += temp0
				end if
				object[currentPlayer].ypos += 0x20000
				if object.value11 == 0
					if object.state == 0
						object.state = 1
					else
						if object.state == 2
							temp4 = object.value7
						end if
					end if
					temp0 = object[currentPlayer].xpos
					temp0 -= object.xpos
					Abs(temp0)
					if temp0 < 0x80000
						object.value2++
					else
						if object[currentPlayer].xpos < object.xpos
							object.value1++
							if object.value6 != 0
								object.value7 = temp4
							end if
						else
							object.value3++
							if object.value6 != 2
								object.value7 = temp4
							end if
						end if
					end if
				else
					temp0 = object[currentPlayer].xpos
					temp0 -= object.xpos
					Abs(temp0)
					temp4 = object.yvel
					if temp0 >= 0x80000
						if object[currentPlayer].xpos < object.xpos
							if object.value11 == 1
								temp4 = 0
							end if
						else
							if object.value11 == 2
								temp4 = 0
							end if
						end if
					end if
					if temp4 != 0
						object[currentPlayer].state = PlayerObject_HandleAir
						object[currentPlayer].gravity = GRAVITY_AIR
						object[currentPlayer].speed = object[currentPlayer].xvel
						if object.state == 3
							object[currentPlayer].animation = ANI_JUMPING
						else
							if object.value0 != 0
								object[currentPlayer].animation = ANI_JUMPING
							else
								object[currentPlayer].animation = ANI_BOUNCING
							end if
						end if
						object[currentPlayer].value1 = 0
						PlaySfx(SfxName[Spring], 0)
						object[currentPlayer].yvel = temp4
						FlipSign(object[currentPlayer].yvel)
					end if
				end if
			end if
		end if
	next
	object.value11 = 0
	object.value5 = object.value4
	object.frame = object.value4
	object.frame &= 1
	object.direction = object.value4
	object.direction >>= FLIP_X
	object.direction ^= FLIP_X
	if object.value0 != 0
		object.animationTimer--
		if object.animationTimer <= 0
			object.value10 ^= 1
			if object.value0 > 120
				object.animationTimer = 10
			else
				if object.value0 > 60
					object.animationTimer = 5
				else
					object.animationTimer = 2
				end if
			end if
		end if
		CheckEqual(object.state, 0)
		temp0 = checkResult
		CheckEqual(object.state, 1)
		temp0 |= checkResult
		if temp0 == 1
			object.value0--
			if object.value0 == 0
				CreateTempObject(TypeName[Smoke Puff], 0, object.value8, object.value9)
				CreateTempObject(TypeName[Bomb Shrapnel], 0, object.value8, object.value9)
				object[tempObjectPos].xvel = -0x10000
				object[tempObjectPos].yvel = -0x34000
				CreateTempObject(TypeName[Bomb Shrapnel], 0, object.value8, object.value9)
				object[tempObjectPos].xvel = -0xA000
				object[tempObjectPos].yvel = -0x24000
				CreateTempObject(TypeName[Bomb Shrapnel], 0, object.value8, object.value9)
				object[tempObjectPos].xvel = 0x10000
				object[tempObjectPos].yvel = -0x34000
				CreateTempObject(TypeName[Bomb Shrapnel], 0, object.value8, object.value9)
				object[tempObjectPos].xvel = 0xA000
				object[tempObjectPos].yvel = -0x24000
				PlaySfx(SfxName[Explosion], 0)
				object.state = 3
			end if
		end if
	end if
	if object.state < 3
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.value8
		object.ypos = object.value9
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -7, -7, 7, 7, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		next
		
		object.xpos = temp0
		object.ypos = temp1
		
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if checkResult == 1
				Player.BounceDistance = 0x400
				Player.CheckHammerDrop = 0
				CallFunction(PlayerObject_CheckMightyUnspin)
				if checkResult == false
					CallFunction(PlayerObject_Hit)
				end if
			end if
		next
	end if
end event


event ObjectDraw
	if object.state < 3
		DrawSpriteXY(object.value10, object.value8, object.value9)
	end if
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("SLZ/Objects.gif")
	SpriteFrame(-42, -44, 91, 56, 84, 59)
	SpriteFrame(-48, -26, 96, 24, 84, 116)
	SpriteFrame(-12, -11, 24, 22, 156, 1)
	SpriteFrame(-12, -11, 24, 22, 131, 1)
	foreach (TypeName[SeeSaw], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value8 = object[arrayPos0].xpos
		object[arrayPos0].value8 += 0x280000
		object[arrayPos0].value9 = object[arrayPos0].ypos
		object[arrayPos0].value9 -= 8
		object[arrayPos0].value10 = 2
		object[arrayPos0].value4 = 2
		object[arrayPos0].value6 = 2
		if object[arrayPos0].propertyValue != 0
			object[arrayPos0].state = 3
		end if
	next
	SetTableValue(TypeName[SeeSaw], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(SeeSaw_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(SeeSaw_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
