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



//-------------------------------------------------------//
// Generic object                                        //
//-------------------------------------------------------//

//# address-hook(0x02bf20) end(0x02bf54)
function void GenericObject.Init()
{
	objA0.mapping_offset = 0x02bfda
	objA0.render_flags |= render_flag.WORLD
	objA0.state = objA0.subtype2c

	A1 = 0x02bf6c + objA0.subtype2c * 6
	objA0.sprite_attributes = u16[A1]
	objA0.sprite_priority = u16[A1+2]
	objA0.box_size.x = u8[A1+4]
	objA0.box_size.y = u8[A1+5]
	objA0.update_address = addressof(GenericObject.Update)

#if STANDALONE
	// Check if it's a flame in AIZ
	if (objA0.sprite_attributes == (sprite_attribute.PALETTE.LINE3 | 0x02e9) && objA0.mapping_offset == 0x02bfda)
	{
		// Make it a little bit higher to prevent early clipping
		objA0.box_size.y += 4
	}
#endif

	GenericObject.Update()
}


//# address-hook(0x02bf5a) end(0x02bf66)
function void GenericObject.Update()
{
#if STANDALONE
	// Check if it's an underwater flame in AIZ
	if (objA0.sprite_attributes == (sprite_attribute.PALETTE.LINE3 | 0x02e9) && objA0.mapping_offset == 0x02bfda)
	{
		if (objA0.position.y.u16 >= water.height.current - 10)
		{
			UnloadObject()
			return
		}
	}
#endif

	Object.animationUpdate(0x02bf9c)
	DrawOrUnloadObject()
}



//# address-hook(0x069946) end(0x069958)
function void fn069946()
{
	objA0.update_address = 0x069914
	u32[A0 + 0x30] = 0x069e29
	objA0.velocity.y = 0
}


//# address-hook(0x08dbd4) end(0x08dbea)
function void fn08dbd4()
{
	objA0.base_state = 0x08
	u32[A0 + 0x30] = 0x08dced
	objA0.countdown_callback = 0x08db76
}



//# address-hook(0x0465d6) end(0x046616)
function void fn0465d6()
{
	D2 = u32[A0 + 0x2e]
	if (u8[A0 + 0x36] != 0)
	{
		D1.s32 = -D1.s32
		u32[A0 + 0x32] += D2
		if (s32[A0 + 0x32] < 0)
		{
			u32[A0 + 0x2e] -= D1
		}
		else
		{
			u32[A0 + 0x2e] = D0
			u32[A0 + 0x32] = 0
			u8[A0 + 0x36] = 0
		}
	}
	else
	{
		u32[A0 + 0x32] += D2
		if (s32[A0 + 0x32] > 0)
		{
			u32[A0 + 0x2e] -= D1
		}
		else
		{
			D0.s32 = -D0.s32
			u32[A0 + 0x2e] = D0
			u32[A0 + 0x32] = 0
			u8[A0 + 0x36] = 0xff
		}
	}

	D0.u16 = objA0.value32
}
