// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Bird 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.startPos.y
private alias object.value2 : object.velStore.y

// States
private alias 0 : BIRD_SETUP
private alias 1 : BIRD_FLYIN
private alias 2 : BIRD_ENTERBIRD
private alias 3 : BIRD_FLYWITHPLANE
private alias 4 : BIRD_FLYOUT

// Player Aliases
private alias object.ypos : player.ypos
private alias 0 : SUPERSTATE_NONE


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

event ObjectUpdate
	switch object.state
	case BIRD_SETUP
		Rand(object.xpos, 112)
		object.xpos += -160
		object.xpos <<= 16

		Rand(object.ypos, 224)
		object.ypos -= 112
		object.ypos += camera[0].ypos
		object.ypos <<= 16
		if stage.playerListPos == PLAYER_TAILS_ALONE
			object.frame = 4
		end if

		if Player_superState != SUPERSTATE_NONE
			object.frame = 2
		end if

		if object.ypos < player[0].ypos
			object.yvel = 0x2000
		else
			object.yvel = -0x2000
		end if
		object.state = BIRD_FLYIN
		break

	case BIRD_FLYIN
		object.timer--
		if object.timer < 0
			object.timer = 244

			if object.ypos < player[0].ypos
				object.ypos -= 0x68000
			else
				object.ypos += 0x68000
			end if

			object.state = BIRD_ENTERBIRD
		end if
		break

	case BIRD_ENTERBIRD
		object.xpos += 0x10000
		object.ypos += object.yvel

		object.timer--
		if object.timer < 0
			object.timer 		= 384
			object.startPos.y 	= object.ypos
			object.velStore.y 	= object.yvel
			object.yvel 		= 0x8000
			object.state 		= BIRD_FLYWITHPLANE
		end if
		break

	case BIRD_FLYWITHPLANE
		object.ypos += object.yvel
		if object.ypos > object.startPos.y
			object.yvel -= 0x400
		else
			object.yvel += 0x400
		end if

		object.timer--
		if object.timer < 0
			object.timer 	= 244
			object.yvel 	= object.velStore.y
			object.state 	= BIRD_FLYOUT
		end if
		break

	case BIRD_FLYOUT
		object.xpos -= 0x10000
		object.ypos += object.yvel

		object.timer--
		if object.timer < 0
			object.type = TypeName[Blank Object]
		end if
		break

	end switch

	// Animate the object
	object.animationTimer++
	if object.animationTimer == 6
		object.animationTimer = 0
		object.frame ^= 1
	end if
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("Global/Items2.gif")

	// 0-1 - Blue Flicky Frames
	SpriteFrame(-8, -7, 16, 15, 66, 239)
	SpriteFrame(-8, -8, 16, 15, 83, 239)

	// 2-3 - Eagle Frames
	SpriteFrame(-8, -8, 16, 15, 32, 239)
	SpriteFrame(-8, -7, 16, 15, 49, 239)

	// 4-5 - Cucky Frames
	SpriteFrame(-7, -8, 14, 16, 1, 239)
	SpriteFrame(-7, -8, 15, 16, 16, 239)
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Items2.gif")
	SpriteFrame(-8, -7, 16, 15, 66, 239)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
