// ----------------------------------
// RSDK Project: Sonic 2
// 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

// States
private alias 0 : AIRBUBBLE_ACTIVE
private alias 1 : AIRBUBBLE_USED

// Player Aliases
private alias object.state 			: player.state
private alias object.xvel 			: player.xvel
private alias object.yvel 			: player.yvel
private alias object.speed 			: player.speed
private alias object.animationSpeed : player.animationSpeed
private alias object.animation 		: player.animation
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 &= 0x1FF
		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 != 0
			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 = 1
						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
						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("CPZ/Objects3.gif")

	// Bubble frames
	SpriteFrame(-2, -2, 4, 4, 157, 2)
	SpriteFrame(-3, -3, 6, 6, 156, 9)
	SpriteFrame(-4, -4, 8, 8, 155, 17)
	SpriteFrame(-6, -6, 12, 12, 209, 36)
	SpriteFrame(-8, -8, 16, 16, 207, 51)
	SpriteFrame(-12, -12, 24, 24, 164, 1)
	SpriteFrame(-16, -16, 32, 32, 189, 1)
	SpriteFrame(-16, -16, 32, 32, 222, 1)
	SpriteFrame(-15, -15, 30, 30, 224, 34)
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("CPZ/Objects3.gif")
	SpriteFrame(-16, -16, 32, 32, 222, 1)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
