// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: 2P VS Menu 2 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 // Set, but never used
private alias object.value3 : object.selection
private alias object.value4 : object.selectionTimer
private alias object.value5 : object.charBox.x
private alias object.value6 : object.charBox.y
private alias object.value7 : object.sonicAlpha
private alias object.value8 : object.tailsAlpha
private alias object.value9 : object.knuxAlpha

// States
private alias 0 : 2PVSMENU2_SETUP
private alias 1 : 2PVSMENU2_SELECTING
private alias 2 : 2PVSMENU2_SELECTEDPLAYER
private alias 3 : 2PVSMENU2_SELECTEDBACK
private alias 4 : 2PVSMENU2_TOVSMENU3
private alias 5 : 2PVSMENU2_TOVSMENU1

// Selections
private alias 0 : 2PVSMENU2_SEL_NONE
private alias 1 : 2PVSMENU2_SEL_SONIC
private alias 2 : 2PVSMENU2_SEL_TAILS
private alias 3 : 2PVSMENU2_SEL_KNUX
private alias 4 : 2PVSMENU2_SEL_BACK

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

// 2PVSMenu1 States
private alias 1 : 2PVSMENU1_SELECTING

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

// 2PVSMenu3 States
private alias 1 : 2PVSMENU3_SELECTING


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

reserve function 2PVSMenu2_DrawSonicBox
reserve function 2PVSMenu2_DrawTailsBox
reserve function 2PVSMenu2_DrawKnuxBox


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

private function 2PVSMenu2_DrawSonicBox
	temp0 = screen.xcenter
	temp0 -= object.charBox.x
	if object.selection == 2PVSMENU2_SEL_SONIC
		if object.state == 2PVSMENU2_SELECTING
			temp1 = 3
		else
			temp1 = object.selectionTimer
			temp1 >>= 1
			temp1 &= 1
			temp1 += 2
		end if
	else
		temp1 = 2
	end if
	object.alpha = object.sonicAlpha
	DrawSpriteScreenFX(temp1, FX_INK, temp0, object.charBox.y)
	DrawSpriteScreenFX(4, FX_INK, temp0, object.charBox.y)
end function


private function 2PVSMenu2_DrawTailsBox
	if object.selection == 2PVSMENU2_SEL_TAILS
		if object.state == 2PVSMENU2_SELECTING
			temp1 = 3
		else
			temp1 = object.selectionTimer
			temp1 >>= 1
			temp1 &= 1
			temp1 += 2
		end if
	else
		temp1 = 2
	end if
	object.alpha = object.tailsAlpha
	DrawSpriteScreenFX(temp1, FX_INK, screen.xcenter, object.charBox.y)
	DrawSpriteScreenFX(5, FX_INK, screen.xcenter, object.charBox.y)
end function


private function 2PVSMenu2_DrawKnuxBox
	temp0 = screen.xcenter
	temp0 += object.charBox.x
	if object.selection == 2PVSMENU2_SEL_KNUX
		if object.state == 2PVSMENU2_SELECTING
			temp1 = 3
		else
			temp1 = object.selectionTimer
			temp1 >>= 1
			temp1 &= 1
			temp1 += 2
		end if
	else
		temp1 = 2
	end if
	object.alpha = object.knuxAlpha
	DrawSpriteScreenFX(temp1, FX_INK, temp0, object.charBox.y)
	DrawSpriteScreenFX(6, FX_INK, temp0, object.charBox.y)
end function


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

event ObjectUpdate
	switch object.state
	case 2PVSMENU2_SETUP
		LoadSpriteSheet("LevelSelect/Icons.gif")
		object.iconSheet = object.spriteSheet
		
		// Even if we're setting everything up for it, this 2PVS Menu in particular doesn't have any menus to draw so this goes unused
		LoadSpriteSheet("LevelSelect/Text.gif")
		object.textSheet = object.spriteSheet
		
		if engine.deviceType == STANDARD
			options.physicalControls = true
		end if
		
		if options.physicalControls == false
			object.selection = 2PVSMENU2_SEL_NONE
		else
			object.selection = 2PVSMENU2_SEL_SONIC
		end if
		
		object.charBox.x = 80
		object.charBox.y = 120
		object.sonicAlpha = 0x100
		object.tailsAlpha = 0x100
		object.knuxAlpha = 0x100
		object.inkEffect = INK_ALPHA
		object.state++
		break
		
	case 2PVSMENU2_SELECTING
		if options.physicalControls == false
			CheckTouchRect(0, 0, screen.xsize, screen.ysize)
			if checkResult > -1
				temp0 = screen.xcenter
				temp0 -= object.charBox.x
				temp0 -= 32
				
				temp1 = temp0
				temp1 += 64
				temp2 = object.charBox.y
				temp2 -= 32
				temp3 = temp2
				temp3 += 64
				object.selection = 2PVSMENU2_SEL_NONE
				CheckTouchRect(temp0, temp2, temp1, temp3)
				if checkResult > -1
					object.selection = 2PVSMENU2_SEL_SONIC
				end if
				
				temp0 = screen.xcenter
				temp0 -= 32
				temp1 = temp0
				temp1 += 64
				CheckTouchRect(temp0, temp2, temp1, temp3)
				if checkResult > -1
					object.selection = 2PVSMENU2_SEL_TAILS
				end if
				
				temp0 = screen.xcenter
				temp0 += object.charBox.x
				temp0 -= 32
				temp1 = temp0
				temp1 += 64
				CheckTouchRect(temp0, temp2, temp1, temp3)
				if checkResult > -1
					object.selection = 2PVSMENU2_SEL_KNUX
				end if
				
				temp0 = screen.xcenter
				temp0 -= 24
				temp1 = temp0
				temp1 += 48
				CheckTouchRect(temp0, 192, temp1, 224)
				if checkResult > -1
					object.selection = 2PVSMENU2_SEL_BACK
				end if
			else
				switch object.selection
				default
				case 2PVSMENU2_SEL_NONE
					break
					
				case 2PVSMENU2_SEL_SONIC
				case 2PVSMENU2_SEL_TAILS
				case 2PVSMENU2_SEL_KNUX
					object.state = 2PVSMENU2_SELECTEDPLAYER
					object.selectionTimer = 0
					PlaySfx(SfxName[Star Post], false)
					break
					
				case 2PVSMENU2_SEL_BACK
					object.state = 2PVSMENU2_SELECTEDBACK
					object.selectionTimer = 0
					PlaySfx(SfxName[Menu Back], false)
					break
					
				end switch
			end if
			
			if object.state != 2PVSMENU2_SELECTEDPLAYER
				if keyPress[0].left == true
					options.physicalControls = true
					object.selection = 2PVSMENU2_SEL_KNUX
				end if
				
				if keyPress[0].right == true
					options.physicalControls = true
					object.selection = 2PVSMENU2_SEL_SONIC
				end if
				
				if keyPress[0].up == true
					options.physicalControls = true
					object.selection = 2PVSMENU2_SEL_BACK
				end if
				
				if keyPress[0].down == true
					options.physicalControls = true
					object.selection = 2PVSMENU2_SEL_TAILS
				end if
			end if
		else
			if keyPress[0].left == true
				object.selection--
				if object.selection < 2PVSMENU2_SEL_SONIC
					object.selection = 2PVSMENU2_SEL_KNUX
				end if

				PlaySfx(SfxName[Menu Move], false)
			end if
			
			if keyPress[0].right == true
				object.selection++
				if object.selection > 2PVSMENU2_SEL_KNUX
					object.selection = 2PVSMENU2_SEL_SONIC
				end if

				PlaySfx(SfxName[Menu Move], false)
			end if
			
			if keyPress[0].up == true
				if object.selection < 2PVSMENU2_SEL_BACK
					object.selection = 2PVSMENU2_SEL_BACK
				else
					object.selection = 2PVSMENU2_SEL_TAILS
				end if

				PlaySfx(SfxName[Menu Move], false)
			end if
			
			if keyPress[0].down == true
				if object.selection < 2PVSMENU2_SEL_BACK
					object.selection = 2PVSMENU2_SEL_BACK
				else
					object.selection = 2PVSMENU2_SEL_TAILS
				end if

				PlaySfx(SfxName[Menu Move], false)
			end if
			
			if keyPress[0].start == true
				if object.selection < 2PVSMENU2_SEL_BACK
					object.state = 2PVSMENU2_SELECTEDPLAYER
					object.selectionTimer = 0
					PlaySfx(SfxName[Star Post], false)
				else
					object.state = 2PVSMENU2_SELECTEDBACK
					object.selectionTimer = 0
					PlaySfx(SfxName[Menu Back], false)
				end if
			end if
			
			if keyPress[0].buttonA == true
				object.state = 2PVSMENU2_SELECTEDPLAYER
				object.selectionTimer = 0
				PlaySfx(SfxName[Star Post], false)
			end if
			
			if keyPress[0].buttonB == true
				object.state = 2PVSMENU2_SELECTEDBACK
				object.selection = 2PVSMENU2_SEL_BACK
				object.selectionTimer = 0
				PlaySfx(SfxName[Menu Back], false)
			end if
			
			if object.state != 2PVSMENU2_SELECTEDPLAYER
				CheckTouchRect(0, 0, screen.xsize, screen.ysize)
				if checkResult > -1
					options.physicalControls = false
					object.selection = 2PVSMENU2_SEL_NONE
				end if
			end if
		end if
		break
		
	case 2PVSMENU2_SELECTEDPLAYER
		switch object.selection
		default
		case 2PVSMENU2_SEL_NONE
		case 2PVSMENU2_SEL_BACK
			break
			
		case 2PVSMENU2_SEL_SONIC
			object.tailsAlpha -= 8
			object.knuxAlpha -= 8
			break
			
		case 2PVSMENU2_SEL_TAILS
			object.sonicAlpha -= 8
			object.knuxAlpha -= 8
			break
			
		case 2PVSMENU2_SEL_KNUX
			object.sonicAlpha -= 8
			object.tailsAlpha -= 8
			break
			
		end switch
		
		if object.selectionTimer < 32
			object.selectionTimer++
			object.charBox.x = 32
			object.charBox.x -= object.selectionTimer
			object.charBox.x *= 80
			object.charBox.x >>= 5
		else
			object.selectionTimer = 0
			object.charBox.x = 0
			object.timer = -256
			object.state = 2PVSMENU2_TOVSMENU3
		end if
		break
		
	case 2PVSMENU2_SELECTEDBACK
		if object.selectionTimer < 32
			object.selectionTimer++
		else
			object.selectionTimer = 0
			object.state = 2PVSMENU2_TOVSMENU1
		end if
		break
		
	case 2PVSMENU2_TOVSMENU3
		if object.timer < 0x100
			object.timer += 0x10
			if object.timer > 0
				SetScreenFade(0x00, 0x00, 0x00, object.timer)
			end if
		else
			stage.playerListPos = object.selection
			stage.playerListPos--
			ResetObjectEntity(1, TypeName[2P VS Menu 3], 0, 0, 0)
			object[1].priority = PRIORITY_ACTIVE
			object[0].state = 2PVSMENU1_SELECTING
			2PVSMenu3[0].timer = 384
			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
			CallNativeFunction2(Connect2PVS, vs.gameLength, vs.itemMode)
		end if
		break
		
	case 2PVSMENU2_TOVSMENU1
		if object.timer < 0x100
			object.timer += 0x10
			SetScreenFade(0x00, 0x00, 0x00, object.timer)
		else
			ResetObjectEntity(1, TypeName[2P VS Menu 1], 0, 0, 0)
			object[1].priority = PRIORITY_ACTIVE
			object[0].state = 2PVSMENU3_SELECTING
			2PVSMenu1[0].timer = 384
			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
		end if
		break
		
	end switch
end event


event ObjectDraw
	object.spriteSheet = object.iconSheet
	
	// PLAYER SELECT
	DrawSpriteScreenXY(0, screen.xcenter, 17)
	DrawSpriteScreenXY(1, screen.xcenter, 17)
	
	if object.state == 2PVSMENU2_SELECTEDBACK
		temp0 = object.selectionTimer
		temp0 >>= 1
		temp0 &= 1
		temp0 += 7
	else
		if options.physicalControls == false
			if object.selection == 2PVSMENU2_SEL_BACK
				temp0 = 1
			else
				temp0 = 0
			end if
		else
			temp0 = 0
		end if
		
		temp0 += 7
	end if
	
	DrawSpriteScreenXY(temp0, screen.xcenter, 210)
	switch object.selection
	case 2PVSMENU2_SEL_NONE
	case 2PVSMENU2_SEL_SONIC
		CallFunction(2PVSMenu2_DrawTailsBox)
		CallFunction(2PVSMenu2_DrawKnuxBox)
		CallFunction(2PVSMenu2_DrawSonicBox)
		break
		
	case 2PVSMENU2_SEL_TAILS
		CallFunction(2PVSMenu2_DrawKnuxBox)
		CallFunction(2PVSMenu2_DrawSonicBox)
		CallFunction(2PVSMenu2_DrawTailsBox)
		break
		
	case 2PVSMENU2_SEL_KNUX
		CallFunction(2PVSMenu2_DrawTailsBox)
		CallFunction(2PVSMenu2_DrawSonicBox)
		CallFunction(2PVSMenu2_DrawKnuxBox)
		break
		
	case 2PVSMENU2_SEL_BACK
		CallFunction(2PVSMenu2_DrawSonicBox)
		CallFunction(2PVSMenu2_DrawTailsBox)
		CallFunction(2PVSMenu2_DrawKnuxBox)

		// Back Button Selection Box
		if object.state != 2PVSMENU2_SELECTEDBACK
			if options.physicalControls == true
				DrawSpriteScreenXY(9, screen.xcenter, 210)
			end if
		end if
		break
		
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("LevelSelect/Icons.gif")
	LoadSpriteSheet("LevelSelect/Text.gif") // Unused here
	SpriteFrame(-104, 0, 96, 14, 1, 249)		// PLAYER text					- #0
	SpriteFrame(8, 0, 96, 14, 1, 264)			// SELECT text					- #1
	SpriteFrame(-32, -32, 64, 64, 98, 249)		// Details Border				- #2
	SpriteFrame(-32, -32, 64, 64, 163, 249)		// Details Border (Selected)	- #3
	SpriteFrame(-12, -20, 24, 40, 1, 279)		// Sonic						- #4
	SpriteFrame(-16, -20, 32, 40, 54, 279)		// Tails						- #5
	SpriteFrame(-14, -20, 27, 40, 26, 279)		// Knuckles						- #6
	SpriteFrame(-20, -10, 40, 20, 1, 341)		// Back							- #7
	SpriteFrame(-20, -10, 40, 20, 42, 341)		// Back (Selected)				- #8
	SpriteFrame(-22, -12, 42, 22, 1, 362)		// Selection Box				- #9

	foreach (TypeName[2P VS Menu 2], 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(-104, 0, 96, 14, 1, 249)		// PLAYER text					- #0
	SpriteFrame(8, 0, 96, 14, 1, 264)			// SELECT text					- #1
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
