/*
	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
*/



//# address-hook(0x043698) end(0x0436a2)
function void fn043698()
{
	u16[A0 + 0x30] = objA0.subtype2c
	objA0.update_address = 0x0436a8
	fn0436a8()
}


//# address-hook(0x0436a8) end(0x043746)
function void fn0436a8()
{
	D0.u16 = level.framecounter & 0xff
	if (D0.u16 >= u16[A0 + 0x30])
	{
		--objA0.animation.timer
		if (s8(objA0.animation.timer) < 0)
		{
			objA0.animation.timer = 0x05
			if (allocDynamicObjectAfterA0())
			{
				// Spawn lava fall object
				objA1.update_address = 0x043764

				// Spawn object and play sound effect every third call
				++u8[A0 + 0x25]
				if (u8[A0 + 0x25] >= 2)
				{
					u8[A0 + 0x25] = 0
					objA1.update_address = 0x04374c
				}

				objA1.position.x.u16 = objA0.position.x.u16
				objA1.position.y.u16 = objA0.position.y.u16
				objA1.mapping_offset = 0x043782
				objA1.sprite_attributes = (sprite_attribute.PALETTE.LINE2 | 0x00d3)
				objA1.render_flags |= (render_flag.WORLD | render_flag.VISIBLE)
				objA1.sprite_priority = 0x0300
				objA1.box_size.x = 0x20
				objA1.box_size.y = 0x20
				objA1.collision_attributes = (collision.flag.THREAT | collision.size.32x8)
				u8[A1 + 0x2b] |= char.bonus.SHIELD_FIRE
				objA1.velocity.y = 0x0800
				u16[A1 + 0x2e] = (objA0.flags2a & 0x01) ? 0x24 : 0x1c
			}
		}
	}

	CheckForUnloadObject()
}


//# address-hook(0x04374c) end(0x04375e)
function void fn04374c()
{
	if (objA0.render_flags & render_flag.VISIBLE)
	{
		D0.u8 = level.framecounter.low & 0x0f
		if (D0.u8 == 0)
		{
			playSound(0xd5)
		}
	}

	fn043764()
}


//# address-hook(0x043764) end(0x04377c)
function void fn043764()
{
	--objA0.countdown_value
	if (objA0.countdown_value >= 0)
	{
		UpdateMovementStraightSimple()
		AddAsDynamicObject()
		DrawObject()
	}
	else
	{
		UnloadObject()
	}
}
