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

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

private alias object.value0 : object.drawFunction
private alias object.value1 : object.flashingTimer
private alias object.value2 : object.timeOver

// Player Aliases
private alias object.type   : player.type
private alias object.xpos   : player.xpos
private alias object.ypos   : player.ypos
private alias object.value0 : player.rings

// Game Mode Aliases
private alias 2 : MODE_TIMEATTACK

// Global Variable ID Aliases
private alias 22 : GLOBAL_PLAYERSCORE
private alias 25 : GLOBAL_PLAYERLIVES


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

reserve function HUD_Draw_Standard
reserve function HUD_Draw_Mobile


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

private function HUD_Draw_Standard

#platform: USE_ORIGINS
	// temp4 will tell whether to draw lives or coins
	temp4 = game.coinMode
	
	if game.playMode == BOOT_PLAYMODE_MISSION
		if game.missionEnd == false
			game.hudEnable = HUDENABLE_ON
		end if
		temp4 = true
	else
		if game.playMode == BOOT_PLAYMODE_BOSSRUSH
			game.hudEnable = HUDENABLE_ON
			temp4 = false
		else
			if game.coinMode != false
				game.hudEnable = HUDENABLE_ON
			end if
#endplatform

			// Draw the base SCORE/TIME/RINGS text
			DrawSpriteScreenXY(10, 17, 13)
			
#platform: USE_ORIGINS
			temp2 = stage.minutes
			temp3 = stage.seconds
			
			CheckGreater(temp2, 9)
			temp5 = checkResult
			CheckNotEqual(object.timeOver, false)
			checkResult |= temp5
			
			if checkResult != false
				temp2 = 9
				temp3 = 59
			end if
			
			if temp2 == 9
#endplatform
#platform: USE_STANDALONE
			if stage.minutes == 9
#endplatform
				// Overlay the red TIME text
				if object.flashingTimer > 7
					DrawSpriteScreenXY(11, 17, 29)
				end if
			end if

			// Overlay the red RINGS text if needed as well
#platform: USE_STANDALONE
			if player[0].rings == 0
#endplatform
#platform: USE_ORIGINS
			if player[currentPlayer].rings == 0
#endplatform
				if object.flashingTimer > 7
					DrawSpriteScreenXY(12, 17, 45)
				end if
			end if

#platform: USE_STANDALONE
			DrawNumbers(0, 104, 13, player.score, 6, 8, false)
#endplatform
#platform: USE_ORIGINS
			arrayPos0 = GLOBAL_PLAYERSCORE // VarName[player.score]
			arrayPos0 += currentPlayer
			DrawNumbers(0, 104, 13, global[arrayPos0], 6, 8, false)
#endplatform

			if options.gameMode < MODE_TIMEATTACK
				// Normal game - only minutes and seconds are shown
				DrawSpriteScreenXY(14, 67, 29)
			else
				// Time attack - draw milliseconds alongside of the other values
				DrawSpriteScreenXY(13, 67, 29)
				DrawNumbers(0, 104, 29, stage.milliSeconds, 2, 8, true)
			end if

			// Rest of the time display
#platform: USE_STANDALONE
			DrawNumbers(0, 80, 29, stage.seconds, 2, 8, true)
			DrawNumbers(0, 56, 29, stage.minutes, 1, 8, true)
			DrawNumbers(0, 80, 45, player[0].rings, 3, 8, false)
#endplatform
#platform: USE_ORIGINS
			DrawNumbers(0, 80, 29, temp3, 2, 8, true)
			DrawNumbers(0, 56, 29, temp2, 1, 8, true)
			DrawNumbers(0, 80, 45, player[currentPlayer].rings, 3, 8, false)
#endplatform

			if stage.debugMode == true
				// Player position display

				// XPos display
#platform: USE_STANDALONE
				temp0 = player[0].xpos
#endplatform
#platform: USE_ORIGINS
				temp0 = object[currentPlayer].xpos
#endplatform
				temp0 >>= 16
				Abs(temp0)
				temp1 = screen.xsize
				temp1 -= 24
				DrawNumbers(0, temp1, 13, temp0, 5, 8, true)

				// YPos display
#platform: USE_STANDALONE
				temp0 = player[0].ypos
#endplatform
#platform: USE_ORIGINS
				temp0 = object[currentPlayer].ypos
#endplatform
				temp0 >>= 16
				Abs(temp0)
				DrawNumbers(0, temp1, 29, temp0, 5, 8, true)

				// Move 42 pixels to the left
				temp1 -= 42

				// Draw the "X:" and "Y:" icons

				// Use red variant if player is in a negative pos
#platform: USE_STANDALONE
				if player[0].xpos >= 0
#endplatform
#platform: USE_ORIGINS
				if object[currentPlayer].xpos >= 0
#endplatform
					DrawSpriteScreenXY(18, temp1, 15)
				else
					DrawSpriteScreenXY(36, temp1, 15)
				end if

#platform: USE_STANDALONE
				if player[0].ypos >= 0
#endplatform
#platform: USE_ORIGINS
				if object[currentPlayer].ypos >= 0
#endplatform
					DrawSpriteScreenXY(19, temp1, 31)
				else
					DrawSpriteScreenXY(37, temp1, 31)
				end if
			end if
			
#platform: USE_STANDALONE
			// Lives icon
			temp0 = stage.playerListPos
			temp0 += 15
			DrawSpriteScreenXY(temp0, 16, 212)
			
			if specialStage.emeralds == 0x7F
			DrawSpriteScreenXY(38, 68, 213)
			end if
			// Character text
			temp0 += 6
			DrawSpriteScreenXY(temp0, 33, 213)

			// X between icon and number
			DrawSpriteScreenXY(20, 38, 222)

			// The actual player lives number
			DrawNumbers(24, 56, 220, player.lives, 2, 8, false)
#endplatform

#platform: USE_ORIGINS
		end if
	end if
	
	if temp4 == false
		// Lives icon
		if currentPlayer == 0 // If this is Player 1
			temp0 = stage.playerListPos
		else
			temp0 = vs.player2Type
		end if
		temp0 += 15
		DrawSpriteScreenXY(temp0, 16, 212)
		
		// Character text
		temp0 += 6
		DrawSpriteScreenXY(temp0, 33, 213)

		// X between icon and number
		DrawSpriteScreenXY(20, 38, 222)
		
		// The actual player lives number
		arrayPos0 = GLOBAL_PLAYERLIVES // VarName[player.lives]
		arrayPos0 += currentPlayer
		DrawNumbers(24, 56, 220, global[arrayPos0], 2, 8, false)
	else
		if game.playMode != BOOT_PLAYMODE_MISSION
			// Draw the Coins counter
			DrawSpriteScreenXY(38, 16, 212)
			DrawSpriteScreenXY(39, 33, 213)
			DrawSpriteScreenXY(20, 36, 222)
			DrawNumbers(24, 59, 220, game.coinCount, 3, 8, false)
		end if
	end if
#endplatform
end function


private function HUD_Draw_Mobile
	// Although this is still there in Origins, none of it was updated for all the cool new additions
	DrawSpriteScreenXY(10, 17, 9)

	if stage.minutes == 9
		if object.flashingTimer > 7
			DrawSpriteScreenXY(11, 17, 25)
		end if
	end if

	if player[0].rings == 0
		if object.flashingTimer > 7
			DrawSpriteScreenXY(12, 17, 41)
		end if
	end if

	DrawNumbers(0, 104, 9, player.score, 6, 8, false)

	if options.gameMode < MODE_TIMEATTACK
		DrawSpriteScreenXY(14, 67, 25)
	else
		DrawSpriteScreenXY(13, 67, 25)
		DrawNumbers(0, 104, 25, stage.milliSeconds, 2, 8, true)
	end if

	DrawNumbers(0, 80, 25, stage.seconds, 2, 8, true)
	DrawNumbers(0, 56, 25, stage.minutes, 1, 8, true)
	DrawNumbers(0, 80, 41, player[0].rings, 3, 8, false)

	if player[0].type == TypeName[Debug Mode]
		// Player position display, only show it when the player is in Debug Mode (as opposed to Standard's, where it's drawn if Debug Mode is active at all)

		temp0 = player[0].xpos
		temp0 >>= 16
		Abs(temp0)
		temp1 = screen.xsize
		temp1 -= 16
		DrawNumbers(0, temp1, 25, temp0, 5, 8, true)

		temp0 = player[0].ypos
		temp0 >>= 16
		Abs(temp0)
		DrawNumbers(0, temp1, 41, temp0, 5, 8, true)

		temp1 -= 42
		if player[0].xpos >= 0
			DrawSpriteScreenXY(18, temp1, 27)
		else
			DrawSpriteScreenXY(36, temp1, 27)
		end if

		if player[0].ypos >= 0
			DrawSpriteScreenXY(19, temp1, 43)
		else
			DrawSpriteScreenXY(37, temp1, 43)
		end if

		DrawSpriteScreenXY(34, screen.xcenter, 16)
		DrawSpriteScreenXY(35, screen.xcenter, 16)
	end if

	temp1 = screen.xsize
	temp1 -= 64
	temp0 = stage.playerListPos
	temp0 += 15
	DrawSpriteScreenXY(temp0, temp1, 8)

	temp1 += 17
	temp0 += 6
	DrawSpriteScreenXY(temp0, temp1, 9)

	temp1 += 5
	DrawSpriteScreenXY(20, temp1, 18)

	temp1 += 18
	DrawNumbers(24, temp1, 16, player.lives, 2, 8, false)
end function


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

event ObjectDraw
	// Update flashing red texts
#platform: USE_STANDALONE
	if player[0].rings == 0
		object.flashingTimer++

		if object.flashingTimer == 16
			object.flashingTimer = 0
		end if
	else
		if stage.minutes == 9
			object.flashingTimer++

			if object.flashingTimer == 16
				object.flashingTimer = 0
			end if
		end if
	end if
#endplatform

#platform: USE_ORIGINS
	currentPlayer = screen.currentID
	
	if player[currentPlayer].rings == 0
		object.flashingTimer = oscillation
		object.flashingTimer &= 15
	else
		if stage.minutes == 9
			object.flashingTimer = oscillation
			object.flashingTimer &= 15
		end if
	end if
	
	if game.timeOver != false
		object.timeOver = true
	end if
#endplatform

	CallFunction(object.drawFunction)
end event


event ObjectStartup
	LoadSpriteSheet("Global/Display.gif")

	// HUD frames
	SpriteFrame(0, 0, 8, 11, 1, 50) 	// #0  - Number 0
	SpriteFrame(0, 0, 8, 11, 10, 50) 	// #1  - Number 1
	SpriteFrame(0, 0, 8, 11, 19, 50) 	// #2  - Number 2
	SpriteFrame(0, 0, 8, 11, 28, 50) 	// #3  - Number 3
	SpriteFrame(0, 0, 8, 11, 1, 62) 	// #4  - Number 4
	SpriteFrame(0, 0, 8, 11, 10, 62) 	// #5  - Number 5
	SpriteFrame(0, 0, 8, 11, 19, 62) 	// #6  - Number 6
	SpriteFrame(0, 0, 8, 11, 28, 62) 	// #7  - Number 7
	SpriteFrame(0, 0, 8, 11, 1, 74) 	// #8  - Number 8
	SpriteFrame(0, 0, 8, 11, 10, 74) 	// #9  - Number 9
	
	// A-F digits are unused
	
	SpriteFrame(0, 0, 40, 43, 1, 1) 	// #10 - Score, Time, Rings HUD text
	SpriteFrame(0, 0, 31, 11, 1, 119) 	// #11 - Red Time text
	SpriteFrame(0, 0, 39, 11, 1, 131) 	// #12 - Red Rings text
	SpriteFrame(0, 0, 29, 4, 1, 45) 	// #13 - Timer ticks
	SpriteFrame(0, 0, 3, 11, 37, 50) 	// #14 - Timer colon
	SpriteFrame(0, 0, 16, 16, 83, 18) 	// #15 - Sonic life icon
	SpriteFrame(0, 0, 16, 16, 100, 18) 	// #16 - Tails life icon
	SpriteFrame(0, 0, 16, 16, 117, 18) 	// #17 - Knuckles life icon
	SpriteFrame(0, 0, 8, 8, 1, 98) 		// #18 - Yellow X Debug Icon
	SpriteFrame(0, 0, 8, 8, 10, 98) 	// #19 - Yellow Y Debug Icon
	SpriteFrame(0, 0, 6, 6, 19, 98) 	// #20 - Small white x Debug Icon

	SpriteFrame(0, 0, 31, 7, 185, 18) 	// #21 - Sonic name tag

	// Tails or Miles
	if options.region == false
		SpriteFrame(0, 0, 31, 7, 217, 18) // #22 - Tails name tag
	else
		SpriteFrame(0, 0, 31, 7, 185, 26) // #22 - Miles name tag
	end if

	SpriteFrame(0, 0, 31, 7, 217, 26) 	// #23 - Knuckles name tag

	SpriteFrame(0, 0, 8, 8, 37, 64) 	// #24 - Small number 0
	SpriteFrame(0, 0, 8, 8, 46, 64) 	// #25 - Small number 1
	SpriteFrame(0, 0, 8, 8, 55, 64) 	// #26 - Small number 2
	SpriteFrame(0, 0, 8, 8, 64, 64) 	// #27 - Small number 3
	SpriteFrame(0, 0, 8, 8, 73, 64) 	// #28 - Small number 4
	SpriteFrame(0, 0, 8, 8, 82, 64) 	// #29 - Small number 5
	SpriteFrame(0, 0, 8, 8, 91, 64) 	// #30 - Small number 6
	SpriteFrame(0, 0, 8, 8, 100, 64) 	// #31 - Small number 7
	SpriteFrame(0, 0, 8, 8, 109, 64) 	// #32 - Small number 8
	SpriteFrame(0, 0, 8, 8, 118, 64) 	// #33 - Small number 9

	SpriteFrame(-16, 0, 8, 8, 127, 64) 	// #34 - Small debug Plus sign
	SpriteFrame(8, 0, 8, 8, 136, 64) 	// #35 - Small debug Minus sign
	SpriteFrame(0, 0, 8, 8, 25, 98) 	// #36 - Red X Debug Icon
	SpriteFrame(0, 0, 8, 8, 34, 98) 	// #37 - Red Y Debug Icon
	SpriteFrame(0, 0, 16, 14, 121, 46)	// #38 - Emerald Icon
#platform: USE_ORIGINS
	SpriteFrame(0, 0, 16, 16, 179, 180)	// #38 - Coins life icon
	SpriteFrame(0, 0, 39, 7, 179, 197)	// #39 - Coins name tag
#endplatform

	// Set the HUD's draw function based on the current platform
	foreach (TypeName[HUD], arrayPos0, ALL_ENTITIES)
		if engine.deviceType == STANDARD
			object[arrayPos0].drawFunction = HUD_Draw_Standard
		else
			object[arrayPos0].drawFunction = HUD_Draw_Mobile
		end if
	next
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(0, 0, 40, 43, 1, 1)
	
	// This gets set in Stage Setup, but it's just an old CD leftover for time periods and doesn't do anything in this game
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
