// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Boss Fireball 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.fireballDir
private alias object.value3 : object.moveDistance

private alias 0 : BOSSFIREBALL_SPAWNING
private alias 1 : BOSSFIREBALL_DROPPED
private alias 2 : BOSSFIREBALL_IDLE
private alias 3 : BOSSFIREBALL_HITFLOOR
private alias 4 : BOSSFIREBALL_MOVE_AND_SPAWN
private alias 5 : BOSSFIREBALL_SPAWNER
private alias 6 : BOSSFIREBALL_MOVE_HORIZONTAL
private alias 7 : BOSSFIREBALL_DISSIPATE

// Path ID Aliases
private alias 0 : PATH_A


// ========================
// Function Declarations
// ========================

reserve function BossFireball_ProcessAnimation


// ========================
// Function Definitions
// ========================

// Shared by several states in order to animate the fireball
private function BossFireball_ProcessAnimation
	object.frame = object.animationTimer
	object.frame /= 12

	object.direction = object.animationTimer
	object.direction /= 6
	object.direction &= 1
	object.direction += object.fireballDir

	object.animationTimer++
	object.animationTimer %= 24
end function


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

event ObjectUpdate
	switch object.state
	case BOSSFIREBALL_SPAWNING
		object.fireballDir = 2
		object.timer++
		if object.timer == 30
			object.timer = 0
			object.fireballDir = 0
			object.state++
			PlaySfx(SfxName[Small Fireball], false)
		end if

		CallFunction(BossFireball_ProcessAnimation)
		break

	case BOSSFIREBALL_DROPPED
		object.yvel += 0x1800
		object.ypos += object.yvel

		if object.yvel > 0x30000
			object.drawOrder = 5
		end if

		ObjectTileCollision(CSIDE_FLOOR, 0, 4, PATH_A)
		if checkResult == true
			CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
			object[tempObjectPos].moveDistance = object.xpos
			object[tempObjectPos].moveDistance -= 0x100000
			object[tempObjectPos].xvel = -0xA000
			object[tempObjectPos].state = BOSSFIREBALL_HITFLOOR
			object[tempObjectPos].drawOrder = 5

			CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
			object[tempObjectPos].moveDistance = object.xpos
			object[tempObjectPos].moveDistance += 0x100000
			object[tempObjectPos].xvel = 0xA000
			object[tempObjectPos].state = BOSSFIREBALL_HITFLOOR
			object[tempObjectPos].drawOrder = 5
			object.state++
		end if

		CallFunction(BossFireball_ProcessAnimation)
		break

	case BOSSFIREBALL_IDLE
		object.timer++
		if object.timer == 120
			object.timer = 0
			object.state = BOSSFIREBALL_DISSIPATE
			object.frame = 2
			object.direction = FACING_RIGHT
		else
			CallFunction(BossFireball_ProcessAnimation)
		end if
		break

	case BOSSFIREBALL_HITFLOOR
		object.xpos += object.xvel

		if object.xvel > 0
			if object.xpos >= object.moveDistance
				object.xvel = 0
				object.xpos = object.moveDistance
				object.state = BOSSFIREBALL_IDLE

				CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
				object[tempObjectPos].moveDistance = object.xpos
				object[tempObjectPos].moveDistance += 0x100000
				object[tempObjectPos].xvel = 0xA000
				object[tempObjectPos].state = BOSSFIREBALL_MOVE_AND_SPAWN
				object[tempObjectPos].drawOrder = 5

			end if
		else
			if object.xpos <= object.moveDistance
				object.xvel = 0
				object.xpos = object.moveDistance
				object.state = BOSSFIREBALL_IDLE

				CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
				object[tempObjectPos].moveDistance = object.xpos
				object[tempObjectPos].moveDistance -= 0x100000
				object[tempObjectPos].xvel = -0xA000
				object[tempObjectPos].state = BOSSFIREBALL_MOVE_AND_SPAWN
				object[tempObjectPos].drawOrder = 5

			end if
		end if

		CallFunction(BossFireball_ProcessAnimation)
		break

	case BOSSFIREBALL_MOVE_AND_SPAWN
		object.xpos += object.xvel

		if object.xvel > 0
			if object.xpos >= object.moveDistance
				object.xpos = object.moveDistance
				object.state = BOSSFIREBALL_SPAWNER

				CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
				object[tempObjectPos].xvel = 0xA000
				object[tempObjectPos].state = BOSSFIREBALL_MOVE_HORIZONTAL
				object[tempObjectPos].drawOrder = 5

			end if
		else
			if object.xpos <= object.moveDistance
				object.xpos = object.moveDistance
				object.state = BOSSFIREBALL_SPAWNER

				CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)
				object[tempObjectPos].xvel = -0xA000
				object[tempObjectPos].state = BOSSFIREBALL_MOVE_HORIZONTAL
				object[tempObjectPos].drawOrder = 5

			end if
		end if

		CallFunction(BossFireball_ProcessAnimation)
		break

	case BOSSFIREBALL_SPAWNER
		object.timer++

		temp0 = object.timer
		temp0 %= 48
		if temp0 == 0
			CreateTempObject(TypeName[Boss Fireball], 0, object.xpos, object.ypos)

			object[tempObjectPos].xvel = object.xvel
			object[tempObjectPos].state = BOSSFIREBALL_MOVE_HORIZONTAL
			object[tempObjectPos].drawOrder = 5
		end if

		if object.timer == 120
			object.timer = 0
			object.state = BOSSFIREBALL_DISSIPATE
			object.frame = 2
			object.direction = FACING_RIGHT

		else
			CallFunction(BossFireball_ProcessAnimation)
		end if
		break

	case BOSSFIREBALL_MOVE_HORIZONTAL
		object.xpos += object.xvel

		if object.gravity == GRAVITY_GROUND
			ObjectTileGrip(CSIDE_FLOOR, 0, 4, PATH_A)
			object.gravity = checkResult
			object.gravity ^= 1
		else
			object.drawOrder = 4
			object.yvel += 0x1800
			object.ypos += object.yvel
		end if

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

		object.timer++
		if object.timer == 90
			object.type = TypeName[Blank Object]
		end if

		CallFunction(BossFireball_ProcessAnimation)
		break

	case BOSSFIREBALL_DISSIPATE
		object.timer++
		if object.timer == 8
			object.type = TypeName[Blank Object]
		end if
		break
		
	end switch

	// Check for player collision
	if object.state > BOSSFIREBALL_SPAWNING
		if object.frame < 2
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -6, -6, 6, 6, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					CallFunction(Player_FireHit)
				end if
			next
		end if
	end if
end event


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


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-7, -23, 15, 31, 52, 114)
	SpriteFrame(-8, -24, 16, 32, 68, 114)
	SpriteFrame(-8, -19, 16, 23, 85, 114)
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-7, -23, 15, 31, 52, 114)
	SpriteFrame(-8, -24, 16, 32, 68, 114)
	SpriteFrame(-8, -19, 16, 23, 85, 114)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
