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

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

// States
private alias 0 : BUBBLER_FALLING
private alias 1 : BUBBLER_EXPLODE

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function Bubbler_DebugDraw
reserve function Bubbler_DebugSpawn


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

private function Bubbler_DebugDraw
	DrawSprite(0)
end function


private function Bubbler_DebugSpawn
	CreateTempObject(TypeName[Bubbler], 0, object.xpos, object.ypos)
end function


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

event ObjectUpdate
	object.priority = PRIORITY_ACTIVE

	switch object.state
	case BUBBLER_FALLING
		object.animationTimer++
		object.animationTimer &= 15
		object.frame = object.animationTimer
		object.frame >>= 2

		object.ypos += 0x10000

		ObjectTileCollision(CSIDE_FLOOR, 0, 0, PATH_A)
		if checkResult == true
			object.state = BUBBLER_EXPLODE
			object.animationTimer = 0
			object.frame = 4
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -2, -2, 2, 2, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_ProjectileHit)
			end if
		next
		break

	case BUBBLER_EXPLODE
		object.animationTimer++
		if object.animationTimer >= 4
			object.animationTimer = 0
			object.frame++
			if object.frame >= 11
				object.type = TypeName[Blank Object]
			end if
		end if

		if object.frame < 10
			temp0 = object.frame
			temp0 -= 4
			temp0 >>= 1
			temp0 <<= 2
			temp0 += 4
			temp1 = temp0
			FlipSign(temp0)

			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, temp0, -8, temp1, 0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					CallFunction(Player_Hit)
				end if
			next
		end if
		break
		
	end switch
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone02")
	if checkResult == true
		LoadSpriteSheet("CPZ/Objects.gif")
		SpriteFrame(-2, -2, 5, 5, 190, 141)
		SpriteFrame(-2, -2, 5, 5, 196, 141)
		SpriteFrame(-3, -4, 7, 7, 190, 147)
		SpriteFrame(-3, -4, 7, 7, 198, 147)
		SpriteFrame(-4, -6, 8, 6, 157, 2)
		SpriteFrame(-4, -7, 8, 7, 148, 1)
		SpriteFrame(-8, -8, 16, 8, 149, 9)
		SpriteFrame(-8, -14, 16, 14, 149, 18)
		SpriteFrame(-12, -15, 24, 15, 141, 33)
		SpriteFrame(-12, -16, 23, 16, 142, 49)
		SpriteFrame(-7, -13, 14, 14, 151, 66)
	else
		LoadSpriteSheet("MBZ/Objects.gif")

		// Huh?
		// Just one Sprite Frame?
		// And it's not even a Bubbler sprite, it's just the middle of the EHZ EggMobile...
		// But then, if you look at where this sprite would be on the CPZ sheet, it's a Ball sprite! What?
		SpriteFrame(-12, -12, 24, 24, 166, 1)
		
		// The strange thing is, Bubbler's sprites don't even exist on the MBZ sheet anyways, so I wonder how this came about...
	end if

	SetTableValue(TypeName[Bubbler], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(Bubbler_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(Bubbler_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone02")
	if checkResult == true
		LoadSpriteSheet("CPZ/Objects.gif")
		SpriteFrame(-2, -2, 5, 5, 190, 141)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-12, -12, 24, 24, 166, 1) // May as well stick with this frame anyways...
	end if

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
