// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: HUD Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value1 : object.totalRings
private alias object.value2 : object.drawPos.x

// Player Aliases
private alias object.value0  : player.rings
private alias object.value19 : player.vsCheckpointID


// ========================
// Function Declarations
// ========================

reserve function HUD_DrawRingCount


// ========================
// Function Definitions
// ========================

public function HUD_DrawRingCount
	DrawSpriteScreenXY(object.frame, object.drawPos.x, 20)
	object.drawPos.x += 44

	if player[currentPlayer].rings > 99
		// Draw 3 digits
		
		temp1 = player[currentPlayer].rings
		temp1 /= 100
		DrawSpriteScreenXY(temp1, object.drawPos.x, 29)

		object.drawPos.x += 8
		temp1 = player[currentPlayer].rings
		temp1 %= 100
		temp1 /= 10
		DrawSpriteScreenXY(temp1, object.drawPos.x, 29)

		object.drawPos.x += 8
		temp1 = player[currentPlayer].rings
		temp1 %= 10
		DrawSpriteScreenXY(temp1, object.drawPos.x, 29)
	else
		if player[currentPlayer].rings > 9
			// Draw 2 digits
			
			temp1 = player[currentPlayer].rings
			temp1 /= 10
			DrawSpriteScreenXY(temp1, object.drawPos.x, 29)

			object.drawPos.x += 8
			temp1 = player[currentPlayer].rings
			temp1 %= 10
			DrawSpriteScreenXY(temp1, object.drawPos.x, 29)
		else
			// Only draw a single digit
			
			DrawSpriteScreenXY(player[currentPlayer].rings, object.drawPos.x, 29)
		end if
	end if
end function


// ========================
// Events
// ========================

event ObjectDraw
	if options.vsMode == false
		if stage.player2Enabled == false
			currentPlayer = 2
			object.drawPos.x = screen.xcenter
			object.drawPos.x -= 24
			object.frame = stage.playerListPos
			object.frame += 10
			CallFunction(HUD_DrawRingCount)
		else
			currentPlayer = 2
			object.drawPos.x = screen.xcenter
			object.drawPos.x -= 104
			object.frame = stage.playerListPos
			object.frame += 10
			CallFunction(HUD_DrawRingCount)

			currentPlayer = 3
			object.drawPos.x = screen.xcenter
			object.drawPos.x += 48
			object.frame = 11
			CallFunction(HUD_DrawRingCount)

			object.totalRings = player[2].rings
			object.totalRings += player[3].rings
			DrawSpriteScreenXY(13, screen.xcenter, 21)

			object.drawPos.x = screen.xcenter
			if object.totalRings > 99
				object.drawPos.x -= 12
				temp1 = object.totalRings
				temp1 /= 100
				DrawSpriteScreenXY(temp1, object.drawPos.x, 29)

				object.drawPos.x += 8
				temp1 = object.totalRings
				temp1 %= 100
				temp1 /= 10
				DrawSpriteScreenXY(temp1, object.drawPos.x, 29)

				object.drawPos.x += 8
				temp1 = object.totalRings
				temp1 %= 10
				DrawSpriteScreenXY(temp1, object.drawPos.x, 29)
			else
				if object.totalRings > 9
					object.drawPos.x -= 8
					temp1 = object.totalRings
					temp1 /= 10
					DrawSpriteScreenXY(temp1, object.drawPos.x, 29)

					object.drawPos.x += 8
					temp1 = object.totalRings
					temp1 %= 10
					DrawSpriteScreenXY(temp1, object.drawPos.x, 29)
				else
					object.drawPos.x -= 4
					DrawSpriteScreenXY(object.totalRings, object.drawPos.x, 29)
				end if
			end if
		end if
	else
		currentPlayer = 2
		object.drawPos.x = screen.xcenter
		object.drawPos.x -= 104
		object.frame = stage.playerListPos
		object.frame += 10
		CallFunction(HUD_DrawRingCount)

		temp0 = 0
		object.drawPos.x = screen.xcenter
		object.drawPos.x -= 96
		while temp0 < player[2].vsCheckpointID
			DrawSpriteScreenXY(14, object.drawPos.x, 45)
			object.drawPos.x += 16
			temp0++
		loop

		currentPlayer = 3
		object.drawPos.x = screen.xcenter
		object.drawPos.x += 48
		object.frame = vs.player2Type
		object.frame += 10
		CallFunction(HUD_DrawRingCount)

		temp0 = 0
		object.drawPos.x = screen.xcenter
		object.drawPos.x += 56
		while temp0 < player[3].vsCheckpointID
			DrawSpriteScreenXY(14, object.drawPos.x, 45)
			object.drawPos.x += 16
			temp0++
		loop
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Special/Objects.gif")
	SpriteFrame(0, 0, 8, 15, 291, 42)		// Number 0 - #0
	SpriteFrame(0, 0, 8, 15, 300, 42)		// Number 1 - #1
	SpriteFrame(0, 0, 8, 15, 309, 42)		// Number 2 - #2
	SpriteFrame(0, 0, 8, 15, 318, 42)		// Number 3 - #3
	SpriteFrame(0, 0, 8, 15, 327, 42)		// Number 4 - #4
	SpriteFrame(0, 0, 8, 15, 336, 42)		// Number 5 - #5
	SpriteFrame(0, 0, 8, 15, 345, 42)		// Number 6 - #6
	SpriteFrame(0, 0, 8, 15, 354, 42)		// Number 7 - #7
	SpriteFrame(0, 0, 8, 15, 363, 42)		// Number 8 - #8
	SpriteFrame(0, 0, 8, 15, 372, 42)		// Number 9 - #9
	SpriteFrame(0, 0, 48, 24, 250, 1) 		// Sonic rings - #10
	if options.region == 0
		SpriteFrame(0, 0, 48, 24, 299, 1) 	// Tails rings - #11
	else
		SpriteFrame(0, 0, 48, 24, 348, 1) 	// Miles rings - #11
	end if
	SpriteFrame(0, 0, 48, 24, 397, 1) 		// KTE rings - #12
	SpriteFrame(-20, 0, 40, 28, 250, 26) 	// Total box - #13
	SpriteFrame(-8, 0, 16, 8, 442, 42) 		// Vs box - #14
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
