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

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

private alias object.value0 : object.timer
private alias object.value1 : object.originPos.x
private alias object.value2 : object.targetPlayer
private alias object.value3 : object.bubbleUsed

private alias 0 : AIRBUBBLE_ACTIVE
private alias 1 : AIRBUBBLE_USED

// Player values
private alias object.state			: player.state
private alias object.animation		: player.animation
private alias object.animationSpeed	: player.animationSpeed
private alias object.xvel			: player.xvel
private alias object.yvel			: player.yvel
private alias object.speed			: player.speed
private alias object.value3			: player.drownTimer
private alias object.value4			: player.drownLevel
private alias object.value36		: player.flyCarryTimer
private alias object.value37		: player.shield

// Shield constants
private alias 2 : SHIELD_BUBBLE


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

event ObjectUpdate
	currentPlayer = object.targetPlayer

	if object.state == AIRBUBBLE_ACTIVE
		object.timer++
		if object.timer > 15
			object.timer = 0
			if object.frame == 8
				object.type = TypeName[Blank Object]
			end if

			if object.frame < object.propertyValue
				object.frame++
			end if
		end if

		object.ypos += object.yvel
		if currentPlayer != 0xFFFF
			if player[currentPlayer].state == Player_State_Clinging
				if object.propertyValue < 3
					object.originPos.x += 0x40000
				end if
			end if
		end if

		if object.frame < 7
			Sin(object.xpos, object.angle)
			object.xpos <<= 9
			object.xpos += object.originPos.x
			object.angle += 4
			object.angle &= 511
		end if

		if object.iypos < stage.waterLevel
			if object.propertyValue == 6
				object.frame = 7
				object.propertyValue = 8
				object.timer = 0
				object.yvel = 0
			else
				if object.propertyValue < 6
					object.type = TypeName[Blank Object]
				end if
			end if
		end if
	else
		CheckEqual(player[currentPlayer].animation, ANI_HURT)
		temp0 = checkResult
		CheckEqual(player[currentPlayer].animation, ANI_DYING)
		temp0 |= checkResult

		if temp0 != false
			object.bubbleUsed = false
		end if

		if object.timer < 20
			object.timer++
			if object.bubbleUsed != false
				player[currentPlayer].animation = ANI_BREATHING
			end if

			if object.timer > 9
				object.frame = 8
			end if
		else
			object.type = TypeName[Blank Object]

			if object.bubbleUsed != false
				player[currentPlayer].animation = ANI_WALKING
				player[currentPlayer].animationSpeed = 20
			end if
		end if
	end if

	if object.outOfBounds == true
		object.type = TypeName[Blank Object]
	end if

	if object.frame == 6
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if player[currentPlayer].animation != ANI_DYING
				if player[currentPlayer].shield != SHIELD_BUBBLE
					BoxCollisionTest(C_TOUCH, object.entityPos, -2, -2, 2, 2, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)

					if checkResult == true
						object.frame = 7
						object.state = AIRBUBBLE_USED
						object.timer = 0
						object.yvel = 0
						object.targetPlayer = currentPlayer
						player[currentPlayer].yvel = 0
						player[currentPlayer].xvel = 0
						player[currentPlayer].speed = 0
						CheckEqual(player[currentPlayer].animation, ANI_FLYING)
						temp0 = checkResult
						CheckEqual(player[currentPlayer].animation, ANI_FLYINGTIRED)
						temp0 |= checkResult
						CheckEqual(player[currentPlayer].animation, ANI_SWIM_LIFT)
						temp0 |= checkResult
						CheckEqual(player[currentPlayer].animation, ANI_GLIDING)
						temp0 |= checkResult
						CheckEqual(player[currentPlayer].animation, ANI_GLIDING_STOP)
						temp0 |= checkResult
						CheckEqual(player[currentPlayer].animation, ANI_CLIMBING)
						temp0 |= checkResult
						CheckEqual(player[currentPlayer].animation, ANI_LEDGEPULLUP)
						temp0 |= checkResult
						CheckEqual(player[currentPlayer].animation, ANI_CLINGING)
						temp0 |= checkResult
						CheckEqual(player[currentPlayer].animation, ANI_FANROTATE)
						temp0 |= checkResult

						if temp0 == false
							player[currentPlayer].animation = ANI_BREATHING
							object.bubbleUsed = true
#platform: USE_ORIGINS
							CallNativeFunction4(NotifyCallback, NOTIFY_STATS_PARAM_1, 1, 0, 0)
#endplatform
						end if

						player[currentPlayer].drownTimer = 0
						player[currentPlayer].drownLevel = 0

						if player[currentPlayer].state == Player_State_RollJump
							player[currentPlayer].state = Player_State_Air
						end if

						if player[currentPlayer].state == Player_State_Carried
							player[currentPlayer].state = Player_State_Air
							player[1].flyCarryTimer = 30
						end if

						PlaySfx(SfxName[Breathing], false)
					end if
				end if
			end if
		next
	end if
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")

	// Bubble frames
	SpriteFrame(-2, -2, 4, 4, 61, 1)
	SpriteFrame(-3, -3, 6, 6, 61, 6)
	SpriteFrame(-4, -4, 8, 8, 68, 4)
	SpriteFrame(-6, -6, 12, 12, 77, 0)
	SpriteFrame(-8, -8, 16, 16, 90, 1)
	SpriteFrame(-12, -12, 24, 24, 61, 13)
	SpriteFrame(-16, -16, 32, 32, 86, 18)
	SpriteFrame(-16, -16, 32, 32, 123, 1)
	SpriteFrame(-15, -15, 30, 30, 156, 3)
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 86, 18)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
