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

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

private alias object.value0 : object.timer
private alias object.value1 : object.iconSheet
private alias object.value2 : object.textSheet
private alias object.value3 : object.selection
private alias object.value4 : object.buttonOption
private alias object.value5 : object.selectionTimer

private alias object.value6  : object.prevGameLenSelected
private alias object.value7  : object.prevItemModeSelected
private alias object.value8  : object.nextGameLenSelected
private alias object.value9  : object.nextItemModeSelected
private alias object.value10 : object.okSelected
private alias object.value11 : object.backSelected

// States
private alias 0 : 2PVSMENU1_SETUP
private alias 1 : 2PVSMENU1_SELECTING
private alias 2 : 2PVSMENU1_SELECTEDOPTION
private alias 3 : 2PVSMENU1_TOVSMENU2
private alias 4 : 2PVSMENU1_EXITTOMENU

// Selections
private alias 0 : 2PVSMENU1_SEL_NONE
private alias 1 : 2PVSMENU1_SEL_GAMELEN
private alias 2 : 2PVSMENU1_SEL_ITEMMODE
private alias 3 : 2PVSMENU1_SEL_OKBACK

// Button Selections
private alias 0 : 2PVSMENU1_BTN_OK
private alias 1 : 2PVSMENU1_BTN_BACK

// 2PVSMenu2 Aliases
private alias object.value0 : 2PVSMenu2.timer

// 2PVSMenu2 States
private alias 1 : 2PVSMENU2_SELECTING


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

reserve function 2PVSMenu1_SetupMatchLenText
reserve function 2PVSMenu1_SetupItemsText


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

private function 2PVSMenu1_SetupMatchLenText
	switch vs.gameLength
	case 0
		EditMenuEntry(MENU_1, "GAME: NO PREFERENCE", 0, true)
		break
		
	case 1
		EditMenuEntry(MENU_1, "GAME: 1 MATCH ", 0, true)
		break
		
	case 2
		EditMenuEntry(MENU_1, "GAME: 2 MATCHES", 0, true)
		break
		
	case 3
		EditMenuEntry(MENU_1, "GAME: 3 MATCHES", 0, true)
		break
		
	case 4
		EditMenuEntry(MENU_1, "GAME: 4 MATCHES", 0, true)
		break
		
	case 5
		EditMenuEntry(MENU_1, "GAME: 5 MATCHES", 0, true)
		break
		
	case 6
		EditMenuEntry(MENU_1, "GAME: 6 MATCHES", 0, true)
		break
		
	case 7
		EditMenuEntry(MENU_1, "GAME: 7 MATCHES", 0, true)
		break
		
	case 8
		EditMenuEntry(MENU_1, "GAME: 8 MATCHES", 0, true)
		break
		
	end switch
end function


private function 2PVSMenu1_SetupItemsText
	switch vs.itemMode
	case 0
		EditMenuEntry(MENU_1, "ITEMS: NO PREFERENCE", 8, true)
		break
		
	case 1
		EditMenuEntry(MENU_1, "ITEMS: RANDOM", 8, true)
		break
		
	case 2
		EditMenuEntry(MENU_1, "ITEMS: TELEPORT ONLY", 8, true)
		break
		
	case 3
		EditMenuEntry(MENU_1, "ITEMS: FIXED", 8, true)
		break
		
	end switch
end function


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

event ObjectUpdate
	switch object.state
	case 2PVSMENU1_SETUP
		SetupMenu(MENU_1, 0, 3, 2)
		LoadSpriteSheet("LevelSelect/Icons.gif")
		object.iconSheet = object.spriteSheet
		LoadSpriteSheet("LevelSelect/Text.gif")
		object.textSheet = object.spriteSheet

		if engine.deviceType == STANDARD
			options.physicalControls = true
		end if

		if options.physicalControls == false
			menu1.selection = -1
			object.selection = 2PVSMENU1_SEL_NONE
		else
			menu1.selection = 0
			object.selection = 2PVSMENU1_SEL_GAMELEN
		end if

#platform: USE_ORIGINS
		// It's local multiplayer, you choose or you lose.
		// (In other words, remove the No Preference options)
		if vs.gameLength == 0
			vs.gameLength = 1
		end if
		
		if vs.itemMode == 0
			vs.itemMode = 1
		end if
#endplatform

		AddMenuEntry(MENU_1, "GAME: NO PREFERENCE", true)
		CallFunction(2PVSMenu1_SetupMatchLenText)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "@", true)
		AddMenuEntry(MENU_1, "ITEMS: NO PREFERENCE", true)
		
		CallFunction(2PVSMenu1_SetupItemsText)
		
		object.state++
		break

	case 2PVSMENU1_SELECTING
		if options.physicalControls == false
			CheckTouchRect(0, 0, screen.xsize, screen.ysize)
			if checkResult > -1
				// Prev Game Length/Item Mode
				temp0 = screen.xcenter
				temp0 -= 152
				temp1 = temp0
				temp1 += 38
				CheckTouchRect(temp0, 72, temp1, 104)
				if checkResult > -1
					object.prevGameLenSelected = true
				else
					object.prevGameLenSelected = false
				end if
				CheckTouchRect(temp0, 136, temp1, 168)
				if checkResult > -1
					object.prevItemModeSelected = true
				else
					object.prevItemModeSelected = false
				end if

				// Next Game Length/Item Mode
				temp0 = screen.xcenter
				temp0 += 116
				temp1 = temp0
				temp1 += 38
				CheckTouchRect(temp0, 72, temp1, 104)
				if checkResult > -1
					object.nextGameLenSelected = true
				else
					object.nextGameLenSelected = false
				end if
				CheckTouchRect(temp0, 136, temp1, 168)
				if checkResult > -1
					object.nextItemModeSelected = true
				else
					object.nextItemModeSelected = false
				end if

				// OK button
				temp0 = screen.xcenter
				temp0 += 8
				temp1 = temp0
				temp1 += 64
				CheckTouchRect(temp0, 192, temp1, 224)
				if checkResult > -1
					object.okSelected = true
				else
					object.okSelected = false
				end if

				// Back button
				temp1 = screen.xcenter
				temp1 -= 8
				temp0 = temp1
				temp0 -= 64
				CheckTouchRect(temp0, 192, temp1, 224)
				if checkResult > -1
					object.backSelected = true
				else
					object.backSelected = false
				end if
			else
				if object.prevGameLenSelected == true
					object.prevGameLenSelected = false
					vs.gameLength--
					if vs.gameLength < 0
						vs.gameLength = 8
					end if

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

				if object.prevItemModeSelected == true
					object.prevItemModeSelected = false
					vs.itemMode--
					if vs.itemMode < 0
						vs.itemMode = 3
					end if

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

				if object.nextGameLenSelected == true
					object.nextGameLenSelected = false
					vs.gameLength++
					if vs.gameLength > 8
						vs.gameLength = 0
					end if

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

				if object.nextItemModeSelected == true
					object.nextItemModeSelected = false
					vs.itemMode++
					if vs.itemMode > 3
						vs.itemMode = 0
					end if

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

				if object.okSelected == true
					object.okSelected = false
					object.state = 2PVSMENU1_SELECTEDOPTION
					object.buttonOption = 2PVSMENU1_BTN_OK
					PlaySfx(SfxName[Menu Select], false)
				end if

				if object.backSelected == true
					object.backSelected = false
					object.state = 2PVSMENU1_SELECTEDOPTION
					object.buttonOption = 2PVSMENU1_BTN_BACK
					PlaySfx(SfxName[Menu Back], false)
				end if
			end if

			if object.state != 2PVSMENU1_SELECTEDOPTION
				if keyPress[0].up == true
					options.physicalControls = true
					object.selection = 2PVSMENU1_SEL_OKBACK
				end if

				if keyPress[0].down == true
					options.physicalControls = true
					object.selection = 2PVSMENU1_SEL_GAMELEN
				end if
			end if
		else
			if keyPress[0].up == true
				object.selection--
				if object.selection < 2PVSMENU1_SEL_GAMELEN
					object.selection = 2PVSMENU1_SEL_OKBACK
				end if

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

			if keyPress[0].down == true
				object.selection++
				if object.selection > 2PVSMENU1_SEL_OKBACK
					object.selection = 2PVSMENU1_SEL_GAMELEN
				end if

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

			switch object.selection
			default
			case 2PVSMENU1_SEL_NONE
				break

			case 2PVSMENU1_SEL_GAMELEN
				menu1.selection = 0
				if keyPress[0].left == true
					vs.gameLength--
#platform: USE_STANDALONE
					if vs.gameLength < 0
#endplatform
#platform: USE_ORIGINS
					if vs.gameLength < 1
#endplatform
						vs.gameLength = 8
					end if

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

				if keyPress[0].right == true
					vs.gameLength++
					if vs.gameLength > 8
#platform: USE_STANDALONE
						vs.gameLength = 0
#endplatform
#platform: USE_ORIGINS
						vs.gameLength = 1
#endplatform
					end if

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

				if keyPress[0].buttonB == true
					menu1.selection = -1
					object.buttonOption = 2PVSMENU1_BTN_BACK
					object.state = 2PVSMENU1_SELECTEDOPTION
					PlaySfx(SfxName[Menu Back], false)
				end if
				break

			case 2PVSMENU1_SEL_ITEMMODE
				menu1.selection = 8
				if keyPress[0].left == true
					vs.itemMode--
#platform: USE_STANDALONE
					if vs.itemMode < 0
#endplatform
#platform: USE_ORIGINS
					if vs.itemMode < 1
#endplatform
						vs.itemMode = 3
					end if

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

				if keyPress[0].right == true
					vs.itemMode++
					if vs.itemMode > 3
#platform: USE_STANDALONE
						vs.itemMode = 0
#endplatform
#platform: USE_ORIGINS
						vs.itemMode = 1
#endplatform
					end if

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

				if keyPress[0].buttonB == true
					menu1.selection = -1
					object.buttonOption = 2PVSMENU1_BTN_BACK
					object.state = 2PVSMENU1_SELECTEDOPTION
					PlaySfx(SfxName[Menu Back], false)
				end if
				break
				
			case 2PVSMENU1_SEL_OKBACK
				menu1.selection = -1
				if keyPress[0].left == true
					object.buttonOption ^= 1
					PlaySfx(SfxName[Menu Move], false)
				end if

				if keyPress[0].right == true
					object.buttonOption ^= 1
					PlaySfx(SfxName[Menu Move], false)
				end if

				if keyPress[0].start == true
					object.state = 2PVSMENU1_SELECTEDOPTION
					PlaySfx(SfxName[Menu Select], false)
				end if

				if keyPress[0].buttonA == true
					object.state = 2PVSMENU1_SELECTEDOPTION
					PlaySfx(SfxName[Menu Select], false)
				end if

				if keyPress[0].buttonB == true
					object.buttonOption = 2PVSMENU1_BTN_BACK
					object.state = 2PVSMENU1_SELECTEDOPTION
					PlaySfx(SfxName[Menu Back], false)
				end if
				break
				
			end switch

			if object.state != 2PVSMENU1_SELECTEDOPTION
				CheckTouchRect(0, 0, screen.xsize, screen.ysize)
				if checkResult > -1
					options.physicalControls = false
					object.selection = 2PVSMENU1_SEL_NONE
					menu1.selection = -1
				end if
			end if
		end if
		break

	case 2PVSMENU1_SELECTEDOPTION
		if object.selectionTimer < 32
			object.selectionTimer++
		else
			object.selectionTimer = 0
			if object.buttonOption == 2PVSMENU1_BTN_OK
				object.state = 2PVSMENU1_TOVSMENU2
			else
				object.state = 2PVSMENU1_EXITTOMENU
			end if
		end if
		break

	case 2PVSMENU1_TOVSMENU2 // Goes to VS Menu 4 in Origins, still calling it 2 here because may as well
		if object.timer < 256
			object.timer += 16
			SetScreenFade(0x00, 0x00, 0x00, object.timer)
		else
#platform: USE_STANDALONE
			// In networking mode we have only a single, online multiplayer menu
			ResetObjectEntity(1, TypeName[2P VS Menu 2], 0, 0, 0)
			object[1].priority = PRIORITY_ACTIVE
#endplatform
#platform: USE_ORIGINS
			// In local multiplayer mode we have two menus, one for each player present
			ResetObjectEntity(1, TypeName[2P VS Menu 4], 1, 0, 88)
			object[1].priority = PRIORITY_ACTIVE
			ResetObjectEntity(2, TypeName[2P VS Menu 4], 2, 0, 168)
			object[2].priority = PRIORITY_ACTIVE
#endplatform
			object[0].state = 2PVSMENU2_SELECTING
			2PVSMenu2[0].timer = 384
			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
		end if
		break

	case 2PVSMENU1_EXITTOMENU
		if object.timer < 256
			music.volume -= 10
			object.timer += 16
			SetScreenFade(0x00, 0x00, 0x00, object.timer)
		else
			StopMusic()
			options.vsMode = false
#platform: USE_STANDALONE
			engine.state = 8
#endplatform
#platform: USE_ORIGINS
			game.titleMode = true
			stage.activeList = PRESENTATION_STAGE
			stage.listPos = 0
			LoadStage()
#endplatform
			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
		end if
		break
		
	end switch
end event


event ObjectDraw
	object.spriteSheet = object.iconSheet
	DrawSpriteScreenXY(0, screen.xcenter, 17)
	DrawSpriteScreenXY(1, screen.xcenter, 17)

	if object.selection == 2PVSMENU1_SEL_GAMELEN
		DrawSpriteScreenXY(3, screen.xcenter, 64)
	else
		DrawSpriteScreenXY(2, screen.xcenter, 64)
	end if

	if object.selection == 2PVSMENU1_SEL_ITEMMODE
		DrawSpriteScreenXY(3, screen.xcenter, 128)
	else
		DrawSpriteScreenXY(2, screen.xcenter, 128)
	end if

	switch object.selection
	case 2PVSMENU1_SEL_NONE
		if object.prevGameLenSelected == true
			DrawSpriteScreenXY(5, screen.xcenter, 88)
		else
			DrawSpriteScreenXY(4, screen.xcenter, 88)
		end if

		if object.nextGameLenSelected == true
			DrawSpriteScreenXY(7, screen.xcenter, 88)
		else
			DrawSpriteScreenXY(6, screen.xcenter, 88)
		end if

		if object.prevItemModeSelected == true
			DrawSpriteScreenXY(5, screen.xcenter, 152)
		else
			DrawSpriteScreenXY(4, screen.xcenter, 152)
		end if

		if object.nextItemModeSelected == true
			DrawSpriteScreenXY(7, screen.xcenter, 152)
		else
			DrawSpriteScreenXY(6, screen.xcenter, 152)
		end if
		break

	case 2PVSMENU1_SEL_GAMELEN
		if keyDown[0].left == true
			DrawSpriteScreenXY(5, screen.xcenter, 88)
		else
			DrawSpriteScreenXY(4, screen.xcenter, 88)
		end if

		if keyDown[0].right == true
			DrawSpriteScreenXY(7, screen.xcenter, 88)
		else
			DrawSpriteScreenXY(6, screen.xcenter, 88)
		end if
		break

	case 2PVSMENU1_SEL_ITEMMODE
		if keyDown[0].left == true
			DrawSpriteScreenXY(5, screen.xcenter, 152)
		else
			DrawSpriteScreenXY(4, screen.xcenter, 152)
		end if

		if keyDown[0].right == true
			DrawSpriteScreenXY(7, screen.xcenter, 152)
		else
			DrawSpriteScreenXY(6, screen.xcenter, 152)
		end if
		break
		
	end switch

	temp0 = object.selectionTimer
	temp0 >>= 1
	temp0 &= 1
	if temp0 == 0
		if object.okSelected == true
			DrawSpriteScreenXY(9, screen.xcenter, 210)
		else
			DrawSpriteScreenXY(8, screen.xcenter, 210)
		end if

		if object.backSelected == true
			DrawSpriteScreenXY(11, screen.xcenter, 210)
		else
			DrawSpriteScreenXY(10, screen.xcenter, 210)
		end if
	else
		if object.buttonOption == 2PVSMENU1_BTN_OK
			DrawSpriteScreenXY(9, screen.xcenter, 210)
			DrawSpriteScreenXY(10, screen.xcenter, 210)
		else
			DrawSpriteScreenXY(8, screen.xcenter, 210)
			DrawSpriteScreenXY(11, screen.xcenter, 210)
		end if
	end if

	if object.state < 2PVSMENU1_SELECTEDOPTION
		if object.selection == 2PVSMENU1_SEL_OKBACK
			if object.buttonOption == 2PVSMENU1_BTN_OK
				DrawSpriteScreenXY(12, screen.xcenter, 210)
			else
				DrawSpriteScreenXY(13, screen.xcenter, 210)
			end if
		end if
	end if

	object.spriteSheet = object.textSheet
	temp0 = screen.xcenter
	temp0 -= 8
	DrawMenu(MENU_1, screen.xcenter, 84)
end event


event ObjectStartup
	LoadSpriteSheet("LevelSelect/Icons.gif")
	LoadSpriteSheet("LevelSelect/Text.gif")
	SpriteFrame(-56, 0, 32, 14, 185, 124)	// VS text						- #0
	SpriteFrame(-8, 0, 64, 14, 185, 109)	// MODE text					- #1
	SpriteFrame(-112, 0, 224, 48, 1, 151)	// Details Border 				- #2
	SpriteFrame(-112, 0, 224, 48, 1, 200)	// Details Border (Selected)	- #3
	SpriteFrame(-142, -10, 20, 20, 138, 22)	// Left							- #4
	SpriteFrame(-142, -10, 20, 20, 138, 64)	// Left (Selected)				- #5
	SpriteFrame(122, -10, 20, 20, 159, 22) 	// Right						- #6
	SpriteFrame(122, -10, 20, 20, 159, 64)	// Right (Selected)				- #7
	SpriteFrame(20, -10, 40, 20, 1, 320)	// OK							- #8
	SpriteFrame(20, -10, 40, 20, 42, 320)	// OK (Selected)				- #9
	SpriteFrame(-60, -10, 40, 20, 1, 341)	// Back							- #10
	SpriteFrame(-60, -10, 40, 20, 42, 341)	// Back (Selected)				- #11
	SpriteFrame(18, -12, 42, 22, 1, 362)	// Selection Box (OK)			- #12
	SpriteFrame(-62, -12, 42, 22, 1, 362)	// Selection Box (Back)			- #13

	foreach (TypeName[2P VS Menu 1], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
	next
end event


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

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


event RSDKLoad
	LoadSpriteSheet("LevelSelect/Icons.gif")
	SpriteFrame(-56, 0, 32, 14, 185, 124)	// VS text						- #0
	SpriteFrame(-8, 0, 64, 14, 185, 109)	// MODE text					- #1
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
