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

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

private alias object.value0 : object.timer
private alias object.value3 : object.selectedIcon
private alias object.value4 : object.zoneTimer

private alias 0 : STAGEMENU_SETUP
private alias 1 : STAGEMENU_UNUSED1
private alias 2 : STAGEMENU_UNUSED2
private alias 3 : STAGEMENU_SELECTING
private alias 4 : STAGEMENU_EXITZONES
private alias 5 : STAGEMENU_SHOWZONENAME
private alias 6 : STAGEMENU_FADETOSTAGE
private alias 7 : STAGEMENU_FADETOMENU

// ZoneButton Aliases
private alias object.value1 : zoneButton.zoneDetailsWidth
private alias object.value2 : zoneButton.disabled

// 2PVSSetup Aliases
private alias object.value0		: 2PVSSetup.timer
private alias object.state		: 2PVSSetup.state
private alias object.priority	: 2PVSSetup.priority

// 2PVSSetup States
private alias 1 : 2PVSSETUP_FADEIN


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

reserve function StageMenu_HandleIconMovement


// ========================
// Static Values
// ========================

private value StageMenu_livesHolder = 0


// ========================
// Tables
// ========================

private table StageMenu_zoneListPosTable
	0, 2, 4, 6, 8, 10, 20, 12, 14, 17, 18, 19
end table


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

private function StageMenu_HandleIconMovement
	object[10].xpos = -108
	object[10].xpos *= object.scale
	object[10].xpos >>= 8
	object[14].xpos = object[10].xpos
	object[18].xpos = object[10].xpos

	object[11].xpos = -36
	object[11].xpos *= object.scale
	object[11].xpos >>= 8
	object[15].xpos = object[11].xpos
	object[19].xpos = object[11].xpos

	object[12].xpos = 36
	object[12].xpos *= object.scale
	object[12].xpos >>= 8
	object[16].xpos = object[12].xpos
	object[20].xpos = object[12].xpos

	object[13].xpos = 108
	object[13].xpos *= object.scale
	object[13].xpos >>= 8
	object[17].xpos = object[13].xpos
	object[21].xpos = object[13].xpos

	object[10].ypos = -76
	object[10].ypos *= object.scale
	object[10].ypos >>= 8
	object[11].ypos = object[10].ypos
	object[12].ypos = object[10].ypos
	object[13].ypos = object[10].ypos

	object[14].ypos = -20
	object[14].ypos *= object.scale
	object[14].ypos >>= 8
	object[15].ypos = object[14].ypos
	object[16].ypos = object[14].ypos
	object[17].ypos = object[14].ypos

	object[18].ypos = 36
	object[18].ypos *= object.scale
	object[18].ypos >>= 8
	object[19].ypos = object[18].ypos
	object[20].ypos = object[18].ypos
	object[21].ypos = object[18].ypos
end function


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

event ObjectUpdate
	switch object.state
	case STAGEMENU_SETUP
		if engine.deviceType == STANDARD
			// Default with the cursor starting on EHZ
			options.physicalControls = true
			object.selectedIcon = 1
		end if
		
		object.scale = 0x100
		arrayPos0 = 10
		
		// EHZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 1, -108, -76)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// CPZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 2, -36, -76)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// ARZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 3, 36, -76)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// CNZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 4, 108, -76)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// HTZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 5, -108, -20)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// MCZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 6, -36, -20)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// HPZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 13, 36, -20)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// OOZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 7, 108, -20)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// MPZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 9, -108, 36)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// SCZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 10, -36, 36)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// WFZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 11, 36, 36)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		arrayPos0++

		// DEZ
		ResetObjectEntity(arrayPos0, TypeName[Zone Button], 12, 108, 36)
		object[arrayPos0].priority = PRIORITY_ACTIVE

		SetScreenFade(0x00, 0x00, 0x00, 0xFF)
		
		ResetObjectEntity(0, TypeName[2P VS Setup], 0, 0, 0)
		2PVSSetup[0].timer = 384
		2PVSSetup[0].state = 2PVSSETUP_FADEIN
		2PVSSetup[0].priority = PRIORITY_ACTIVE
		
		object.state = STAGEMENU_SELECTING
		break
		
	case STAGEMENU_UNUSED1
		// Was "STAGEMENU_FADEIN" in S1, unused here
		break

	case STAGEMENU_UNUSED2
		// This one is unused in both games, though
		break

	case STAGEMENU_SELECTING
		if options.physicalControls == false
			CheckTouchRect(0, 0, screen.xsize, screen.ysize)
			if checkResult == -1
				if object.selectedIcon > 0
					arrayPos0 = object.selectedIcon
					arrayPos0 += 9
					if zoneButton[arrayPos0].disabled == false
						object.state = STAGEMENU_EXITZONES
						PlaySfx(SfxName[Menu Select], false)
					else
						PlaySfx(SfxName[Fail], false)
					end if
				end if
			end if
			
			if object.state != STAGEMENU_EXITZONES
				object.selectedIcon = 0
				if keyPress[0].left == true
					options.physicalControls = true
					object.selectedIcon = 12
				end if

				if keyPress[0].right == true
					options.physicalControls = true
					object.selectedIcon = 1
				end if

				if keyPress[0].up == true
					options.physicalControls = true
					object.selectedIcon = 9
				end if

				if keyPress[0].down == true
					options.physicalControls = true
					object.selectedIcon = 1
				end if
			end if
		else
			if keyPress[0].left == true
				object.selectedIcon--
				if object.selectedIcon < 1
					object.selectedIcon = 12
				end if

				PlaySfx(SfxName[Menu Move], false)
			end if

			if keyPress[0].right == true
				object.selectedIcon++
				if object.selectedIcon > 12
					object.selectedIcon = 1
				end if

				PlaySfx(SfxName[Menu Move], false)
			end if

			if keyPress[0].up == true
				object.selectedIcon -= 4
				if object.selectedIcon < 1
					object.selectedIcon += 12
				end if

				PlaySfx(SfxName[Menu Move], false)
			end if

			if keyPress[0].down == true
				object.selectedIcon += 4
				if object.selectedIcon > 12
					object.selectedIcon -= 12
				end if

				PlaySfx(SfxName[Menu Move], false)
			end if

			if keyPress[0].start == true
				arrayPos0 = object.selectedIcon
				arrayPos0 += 9
				if zoneButton[arrayPos0].disabled == false
					object.state = STAGEMENU_EXITZONES
					PlaySfx(SfxName[Menu Select], false)
				else
					PlaySfx(SfxName[Fail], false)
				end if
			end if

			if keyPress[0].buttonA == true
				arrayPos0 = object.selectedIcon
				arrayPos0 += 9
				if zoneButton[arrayPos0].disabled == false
					object.state = STAGEMENU_EXITZONES
					PlaySfx(SfxName[Menu Select], false)
				else
					PlaySfx(SfxName[Fail], false)
				end if
			end if

			if object.state != STAGEMENU_EXITZONES
				CheckTouchRect(0, 0, screen.xsize, screen.ysize)
				if checkResult > -1
					options.physicalControls = false
					object.selectedIcon = 0
				end if
			end if
		end if
		break

	case STAGEMENU_EXITZONES
		if object.scale > 0
			object.scale -= 8
		end if
		object[10].alpha = object.scale
		object[11].alpha = object.scale
		object[12].alpha = object.scale
		object[13].alpha = object.scale
		object[14].alpha = object.scale
		object[15].alpha = object.scale
		object[16].alpha = object.scale
		object[17].alpha = object.scale
		object[18].alpha = object.scale
		object[19].alpha = object.scale
		object[20].alpha = object.scale
		object[21].alpha = object.scale
		object[10].drawOrder = 3
		object[11].drawOrder = 3
		object[12].drawOrder = 3
		object[13].drawOrder = 3
		object[14].drawOrder = 3
		object[15].drawOrder = 3
		object[16].drawOrder = 3
		object[17].drawOrder = 3
		object[18].drawOrder = 3
		object[19].drawOrder = 3
		object[20].drawOrder = 3
		object[21].drawOrder = 3

		arrayPos0 = object.selectedIcon
		arrayPos0 += 9
		object[arrayPos0].alpha = 0x100
		object[arrayPos0].drawOrder = 4

		arrayPos0 = object.selectedIcon
		arrayPos0 += 9
		CallFunction(StageMenu_HandleIconMovement)

		if object.zoneTimer < 32
			object.zoneTimer++
		else
			object.zoneTimer = 0
			object.state = STAGEMENU_SHOWZONENAME
		end if
		break

	case STAGEMENU_SHOWZONENAME
		arrayPos0 = object.selectedIcon
		arrayPos0 += 9
		if zoneButton[arrayPos0].zoneDetailsWidth < 224
			zoneButton[arrayPos0].zoneDetailsWidth += 8
		else
			object.timer = -640
			object.state = STAGEMENU_FADETOSTAGE
		end if
		break

	case STAGEMENU_FADETOSTAGE
		if object.timer < 256
			music.volume -= 10
			object.timer += 16
			if object.timer > 0
				SetScreenFade(0x00, 0x00, 0x00, object.timer)
			end if
		else
			StopMusic()
			player.score = 0
			player.scoreBonus = 50000
			stage.activeList = REGULAR_STAGE
			temp0 = object.selectedIcon
			temp0--
			GetTableValue(stage.listPos, temp0, StageMenu_zoneListPosTable)
			player.lives = StageMenu_livesHolder
			LoadStage()
			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
		end if
		break

	case STAGEMENU_FADETOMENU
		if object.timer < 256
			music.volume -= 10
			object.timer += 16
			SetScreenFade(0x00, 0x00, 0x00, object.timer)
		else
			StopMusic()
#platform: USE_STANDALONE
			engine.state = 8
#endplatform
#platform: USE_ORIGINS
			// This object isn't even used in Origins...
			
			stage.activeList = PRESENTATION_STAGE
			stage.listPos = 0
			LoadStage()
#endplatform
			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
		end if
		break
		
	end switch
end event


event ObjectDraw
	DrawSpriteScreenXY(0, screen.xcenter, 17)
	DrawSpriteScreenXY(1, screen.xcenter, 17)
end event


event ObjectStartup
	LoadSpriteSheet("LevelSelect/Icons.gif")
	SpriteFrame(-96, 0, 80, 14, 83, 335)	// STAGE  - #0
	SpriteFrame(0, 0, 96, 14, 1, 264)		// SELECT - #1

	foreach (TypeName[Stage Menu], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].type = TypeName[Blank Object]
		
		object[1].type = TypeName[Stage Menu]
		object[1].priority = PRIORITY_ACTIVE
		
		StageMenu_livesHolder = player.lives
		player.lives = 1
	next
end event


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

event RSDKDraw
	DrawSprite(0)
	DrawSprite(1)
end event


event RSDKLoad
	LoadSpriteSheet("LevelSelect/Icons.gif")
	SpriteFrame(-96, 0, 80, 14, 83, 335)	// STAGE  - #0
	SpriteFrame(0, 0, 96, 14, 1, 264)		// SELECT - #1
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
