/*
	This script file is part of the Sonic 3 A.I.R. script software distribution.
	Copyright (C) 2017-2024 by Eukaryot

	Published under the GNU GPLv3 open source software license, see license.txt
	or https://www.gnu.org/licenses/gpl-3.0.en.html
*/



// Fire ball emitter

//# address-hook(0x042bb8) end(0x042bf0)
function void fn042bb8()
{
	objA0.mapping_offset = 0x042cb2
	objA0.sprite_attributes = (sprite_attribute.PALETTE.LINE3 | 0x03a1)
	objA0.render_flags |= render_flag.WORLD
	objA0.box_size.x = 0x10
	objA0.box_size.y = 0x04
#if STANDALONE
	// The box is definitely too small, was this by intent or did someone confuse x and y?
	objA0.box_size.y = 0x10
#endif
	objA0.sprite_priority = 0x0280
	objA0.animation.sprite = 2
	u16[A0 + 0x30] = u16(objA0.subtype2c) * 4

	objA0.update_address = 0x042bf6
	fn042bf6()
}


//# address-hook(0x042bf6) end(0x042c7a)
function void fn042bf6()
{
	--objA0.countdown_value
	if (objA0.countdown_value < 0)
	{
		objA0.countdown_value = u16[A0 + 0x30]
		if (objA0.render_flags & render_flag.VISIBLE)
		{
			if (allocDynamicObjectAfterA0())
			{
				objA1.update_address = 0x042c80
				objA1.position.x.u16 = objA0.position.x.u16
				objA1.position.y.u16 = objA0.position.y.u16
				objA1.position.x.u16 += 0x08
				objA1.render_flags = objA0.render_flags
				objA1.mapping_offset = objA0.mapping_offset
				objA1.sprite_attributes = 0x03a1
				objA1.sprite_priority = 0x0300
				objA1.box_size.x = 0x0c
				objA1.box_size.y = 0x08
				objA1.collision_attributes = (collision.flag.THREAT | collision.size.8x4)
				u8[A1 + 0x2b] |= char.bonus.SHIELD_FIRE
				objA1.velocity.x = 0x0200
				if (objA0.flags2a & 0x01)
				{
					objA1.velocity.x = -objA1.velocity.x
					objA1.position.x.u16 -= 0x10
				}

				playSound(0x67)
			}
		}
	}

	DrawOrUnloadObject()
}


// Fire ball

//# address-hook(0x042c80) end(0x042cac)
function void fn042c80()
{
	if ((level.framecounter.low & 0x03) == 0)
	{
		++objA0.animation.sprite
		objA0.animation.sprite &= 0x01
	}

	if (objA0.render_flags & render_flag.VISIBLE)
	{
		UpdateMovementStraightSimple()
		AddAsDynamicObject()
		DrawObject()
	}
	else
	{
		UnloadObject()
	}
}
