/*
	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 with Wood Platforms                     //
//-------------------------------------------------------//

//# address-hook(0x02b586) end(0x02b5ce)
function void AIZWaterfall.Init()
{
	if (objA0.position.x.u16 == 0x26b0 || objA0.position.x.u16 == 0x2700)
	{
		if (level.trigger00 != 0)
		{
			UnloadObject()
		}
	}

	D2.u16 = objA0.subtype2c & 0x0f
	objA0.value32 = u16[0x02b566 + D2.u16 * 2]
	D2.u16 -= 3
	if (D2.s16 < 0)
		D2 = 0

	u16[A0 + 0x34] = ((objA0.subtype2c >> 4) & 0x0f) << D2
	objA0.render_flags = render_flag.WORLD

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


//# address-hook(0x02b5d4) end(0x02b69a)
function void AIZWaterfall.Update()
{
	if (((level.framecounter + u16[A0 + 0x34]) & objA0.value32) == 0)
	{
		AIZWaterfall.spawnPlatform()
	}
	CheckForUnloadObject()
}


function void AIZWaterfall.spawnPlatform()
{
	// Spawn the next platform
	if (allocDynamicObjectAfterA0())
	{
		objA1.update_address = addressof(AIZWaterfall.Platform.Update)
		objA1.position.x.u16 = objA0.position.x.u16
		objA1.position.y.u16 = objA0.position.y.u16
		if (global.act == 0)
		{
			objA1.mapping_offset = 0x22ae30
			objA1.sprite_attributes = (sprite_attribute.PALETTE.LINE2 | 0x03cf)
		}
		else
		{
			objA1.mapping_offset = 0x22ae20
			objA1.sprite_attributes = (sprite_attribute.PALETTE.LINE2 | 0x02e9)
		}
		objA1.box_size.x = 0x18
		objA1.box_size.y = 8
		objA1.render_flags = render_flag.WORLD
		objA1.sprite_priority = 0x280
		A2 = A1

		// Spawn water splashes effect
		if (allocDynamicObjectAfterA0())
		{
			objA1.update_address = 0x02b72c
			objA1.position.x.u16 = objA0.position.x.u16
			objA1.position.y.u16 = objA0.position.y.u16
			if (global.act == 0)
			{
				objA1.mapping_offset = 0x22aeb0
				objA1.sprite_attributes = (sprite_attribute.PALETTE.LINE2 | 0x03cf)
			}
			else
			{
				objA1.mapping_offset = 0x22ae40
				objA1.sprite_attributes = (sprite_attribute.PALETTE.LINE3 | 0x02e9)
			}
			objA1.box_size.x = 0x20
			objA1.box_size.y = 0x10
			objA1.render_flags = render_flag.WORLD
			objA1.sprite_priority = 0x200
			u16[A1 + 0x3c] = A2.u16
			u16[A2 + 0x3c] = A1.u16
		}
	}
}


//# address-hook(0x02b6a0) end(0x02b6ba)
function void AIZWaterfall.Platform.Update()
{
	++objA0.position.y.u16
	if (objA0.position.y.u16 >= water.height.current)
	{
		objA0.update_address = 0x02b6bc
		objA0.animation.timer = 0x3b
	}
	fn02b6d8()
}


//# address-hook(0x02b6bc) end(0x02b726)
function void fn02b6bc()
{
	--objA0.animation.timer
	if (s8(objA0.animation.timer) < 0)
	{
		objA0.position.x.u16 = 0x7ff0
	}
	D0.u8 = objA0.animation.timer & 0x03
	if (D0.u8 == 0)
	{
		u8[A0 + 0x36] ^= 0x01
	}

	fn02b6d8()
}


//# address-hook(0x02b6d8) end(0x02b726)
function void fn02b6d8()
{
	D1 = objA0.box_size.x
	D3 = 8
	D4.u16 = objA0.position.x.u16
	PlatformCheckClimbOn()

	D0.u16 = (objA0.position.x.u16 & 0xff80) - level.despawn_position.x
	if (D0.u16 <= 0x280)
	{
		if (u8[A0 + 0x36] == 0)
		{
			DrawObject()
		}
	}
	else
	{
		D0.u16 = u16[A0 + 0x3c]
		if (D0.u16 != 0)
		{
			A1 = 0xffff0000 + D0.u16
			UnloadObjectA1()
		}

		if (objA0.respawn_entry)
		{
			A2 = 0xffff0000 + objA0.respawn_entry
			u8[A2] &= ~0x80
		}
		UnloadObject()
	}
}


//# address-hook(0x02b72c) end(0x02b752)
function void fn02b72c()
{
	A1 = 0xffff0000 + u16[A0 + 0x3c]
	objA0.position.x.u16 = objA1.position.x.u16
	objA0.position.y.u16 = objA1.position.y.u16
	--objA0.animation.timer
	if (s8(objA0.animation.timer) < 0)
	{
		objA0.animation.timer = 3
		++objA0.animation.sprite
		objA0.animation.sprite &= 0x03
	}
	DrawObject()
}
