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



//-------------------------------------------------------//
// Time Over / Game Over display on screen               //
//-------------------------------------------------------//

//# address-hook(0x02d5c6) end(0x02d60c)
function void fn02d5c6()
{
	if (u32[0xfffff680] != 0)
		return

	if (objA0.animation.sprite == 0)
	{
		sram.block_interrupts.u8 = 0xff
		UpdateLivesContinuesInSaveGame()
	}

	bool isLeft = ((objA0.animation.sprite & 0x01) == 0)
	objA0.position.x.u16 = isLeft ? 0x50 : 0x1f0 + getScreenExtend() * 2
	objA0.position.y.u16 = 0xf0
	objA0.mapping_offset = 0x02edd0
	objA0.sprite_attributes = (sprite_attribute.PRIORITY | 0x079c)
	objA0.sprite_priority = 0

	objA0.update_address = 0x02d612
	fn02d612()
}


//# address-hook(0x02d612) end(0x02d636)
function void fn02d612()
{
	u16 targetPositionX = 0x120
	#if STANDALONE
		targetPositionX += getScreenExtend()
	#endif

	s16 diff = targetPositionX - objA0.position.x.u16
	if (diff != 0)
	{
		objA0.position.x.u16 += clamp(diff, -0x10, 0x10)
		DrawObject()
	}
	else
	{
		u16[A0 + 0x24] = 0x01e0
		objA0.update_address = 0x02d638
		#if STANDALONE
			// Prevent short flicker
			DrawObject()
		#endif
	}
}


//# address-hook(0x02d638) end(0x02d68a)
function void fn02d638()
{
	global.dynamic_objects.size = 0
	if ((objA0.animation.sprite & 0x01) == 0)
	{
		D0.u8 = (control.pad1.pressed | control.pad2.pressed) & (CONTROL_START | CONTROL_ABC)
		if (D0.u8 == 0 && u16[A0 + 0x24] != 0)
		{
			--u16[A0 + 0x24]
		}
		else
		{
			if (global.time_over)
			{
				checkpoint.time = 0
				level.restart = 1

			#if STANDALONE
				Standalone.onDeath()
			#endif
			}
			else
			{
				global.game_mode = (continues_counter == 0) ? 0 : 0x14		// Initialization/Continue Screen
			}
		}
	}
	DrawObject()
}
