/*
	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(0x034302) end(0x034388)
function void fn034302()
{
	objA0.mapping_offset = 0x233812
	objA0.sprite_attributes = (sprite_attribute.PRIORITY | sprite_attribute.PALETTE.LINE1 | 0x03ff)
	objA0.render_flags |= render_flag.WORLD
	objA0.sprite_priority = 0x0280
	objA0.box_size.x = 0x10
	objA0.box_size.y = 0x38
	objA0.animation.sprite = 2
	objA0.collision_attributes = collision.size.8x8_2
	u8[A0 + 0x29] = 3
	objA0.render_flags |= render_flag.COMPOUND
	objA0.compound.count = 1

	A2 = A0 + 0x18
	u16[(A2+=2)-2] = objA0.position.x.u16
	u16[(A2+=2)-2] = objA0.position.y.u16
	u16[(A2+=2)-2] = 0x06
	if ((objA0.flags2a & 0x01) == 0)
	{
		u16[A0 + 0x30] = 0xc0
	}

	A3 = 0xfffff7e0
	D0.u16 = objA0.subtype2c & 0x0f
	if (u8[A3 + D0.u16] != 0)
	{
		u8[A0 + 0x34] = 1
		objA0.animation.sprite = 0
		u8[A0 + 0x28] = 0
	}

	objA0.update_address = 0x03438e
	fn03438e()
}


//# address-hook(0x03438e) end(0x034512)
function void fn03438e()
{
	if (u8[A0 + 0x34] == 0)
	{
		D0.u16 = u16[0xffffb000 + 0x10] - objA0.position.x.u16 + u16[A0 + 0x30]
		if (D0.u16 < 0xc0)
		{
			D0.u16 = u16[0xffffb000 + 0x14] - objA0.position.y.u16 + 0x80
			if (D0.u16 < 0xc0)
			{
				if (objA0.state == 0)
				{
					objA0.state.both = 0x0100
				}
			}
		}

		if (objA0.collision_attributes == 0)
		{
			if (objA0.value32 != 0)
			{
				--objA0.value32
				if (objA0.value32 == 0)
				{
					objA0.collision_attributes = collision.size.8x8_2
				}
			}
			else
			{
				objA0.value32 = 0x3c
				if (allocDynamicObjectAfterA0())
				{
					objA1.update_address = 0x034536
					if (u8[A0 + 0x29] == 0)
					{
						objA1.update_address = addressof(SmallExplosion.BaseUpdate)
						objA1.sprite_attributes = sprite_attribute.PRIORITY
						u8[A1 + 0x05] = 2
						u8[A0 + 0x34] = 1
						objA0.animation.sprite = 0

						A3 = 0xfffff7e0
						D0.u16 = objA0.subtype2c & 0x0f
						u8[A3 + D0.u16] = 1
					}

					objA1.position.x.u16 = objA0.position.x.u16
					objA1.position.y.u16 = objA0.position.y.u16
					objA1.render_flags = objA0.render_flags
					objA1.render_flags &= ~render_flag.COMPOUND
					u8[A1 + 0x2a] = objA0.flags2a
					objA1.mapping_offset = objA0.mapping_offset
					objA1.sprite_attributes = objA0.sprite_attributes
					objA1.sprite_priority = 0x0200
					objA1.box_size.x = 0x08
					objA1.box_size.y = 0x08
					u8[A1 + 0x20] = 0x02
				}

				playSound(0x6e)
			}
		}

		Object.animationUpdate(0x034548)
		if (objA0.base_state != 0)
		{
			objA0.base_state = 0
			if (allocDynamicObjectAfterA0())
			{
				objA1.update_address = 0x034518
				objA1.position.x.u16 = objA0.position.x.u16 + 0x10
				objA1.position.y.u16 = objA0.position.y.u16 + 0x20
				objA1.render_flags = objA0.render_flags & ~render_flag.COMPOUND
				objA1.mapping_offset = objA0.mapping_offset
				objA1.sprite_attributes = objA0.sprite_attributes & ~sprite_attribute.PRIORITY
				objA1.sprite_priority = 0x0300
				objA1.box_size.x = 0x10
				objA1.box_size.y = 0x04
				objA1.collision_attributes = (collision.flag.THREAT | collision.size.8x4)
				objA1.velocity.x = -0x400
				if (objA0.flags2a & 0x01)
				{
					objA1.velocity.x = -objA1.velocity.x
					objA1.position.x.u16 -= 0x20
				}

				playSound(0x67)
			}
		}
	}

	fn01b662()
}


//# address-hook(0x034518) end(0x034530)
function void fn034518()
{
	if (objA0.render_flags & render_flag.VISIBLE)
	{
		UpdateMovementStraightSimple()
		AddAsDynamicObject()
		DrawObject()
	}
	else
	{
		UnloadObject()
	}
}


//# address-hook(0x034536) end(0x034542)
function void fn034536()
{
	Object.animationUpdate(0x034548)
	DrawOrUnloadObject()
}
