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



//-------------------------------------------------------//
// AIZ Waterfall Disappearing Rock                       //
//-------------------------------------------------------//

//# address-hook(0x02a252) end(0x02a366)
function void AIZWaterfallRock.Init()
{
	D0.u16 = objA0.subtype2c & 0x0f
	D1.u8 = objA0.subtype2c
	D2.u16 = D0.u16
	objA0.value32 = u16[0x02a232 + D0.u16 * 2]
	D2.u16 -= 3
	if (D2.s16 < 0)
		D2 = 0

	D1.u16 = (D1.u16 >> 4) & 0x0f
	D1.u16 <<= D2
	u16[A0 + 0x34] = D1.u16
	objA0.mapping_offset = 0x2294b4
	objA0.sprite_attributes = (sprite_attribute.PALETTE.LINE2 | 0x0001)
	objA0.box_size.x = 0x20
	objA0.box_size.y = 0x18
	objA0.render_flags = render_flag.WORLD
	objA0.sprite_priority = 0x280

	D0.u16 = (level.framecounter + u16[A0 + 0x34]) & objA0.value32
	if (D0.u16 != 0 && D0.u16 < 0xc8)
	{
		objA0.animation.timer = 0xc8 - D0.u16
		objA0.animation.frame = 0
		objA0.state.both = 0x0202
		objA0.animation.sprite = 5
	}

	objA0.update_address = addressof(AIZWaterfallRock.Update)
	AIZWaterfallRock.Update()
}


//# address-hook(0x02a2d6)
function void AIZWaterfallRock.Update()
{
	D0.u16 = (level.framecounter + u16[A0 + 0x34]) & objA0.value32
	if (D0.u16 == 0)
	{
		objA0.state.both = 0x0100
		u8[A0 + 0x36] = 0
		if (objA0.render_flags & render_flag.VISIBLE)
		{
			playSound(0x4b)
		}
	}

	Object.AlternativeAnimationUpdate(0x02a3b0)

	if (objA0.animation.sprite == 5 && u8[A0 + 0x36] == 0)
	{
		u8[A0 + 0x36] = 1
		if (allocDynamicObjectAfterA0())
		{
			// Spawn water splashes effect
			objA1.update_address = addressof(AIZWaterfallRock.SplashEffect.Update)
			objA1.position.x.u16 = objA0.position.x.u16
			objA1.position.y.u16 = objA0.position.y.u16
			objA1.mapping_offset = 0x2294c0
			objA1.sprite_attributes = (sprite_attribute.PALETTE.LINE3 | 0x02e9)
			objA1.box_size.x = 0x28
			objA1.box_size.y = 0x20
			objA1.render_flags = render_flag.WORLD
			objA1.sprite_priority = 0x200
			u16[A1 + 0x3c] = A0.u16
		}
	}

	DrawOrUnloadObject()
}


//# address-hook(0x02a36c) end(0x02a3aa)
function void AIZWaterfallRock.SplashEffect.Update()
{
	A1 = 0xffff0000 + u16[A0 + 0x3c]
	if (u8[A1 + 0x22] == 0x03)
	{
		objA0.position.x.u16 = 0x7ff0
	}

	--objA0.animation.timer
	if (s8(objA0.animation.timer) < 0)
	{
		objA0.animation.timer = 3
		++objA0.animation.sprite
		objA0.animation.sprite &= 3
	}

	D1.u16 = 0x2b
	D2.u16 = 0x18
	D3.u16 = 0x19
	D4.u16 = objA0.position.x.u16
	fn01dc56()

	DrawOrUnloadObject()
}
