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

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

private alias object.value0 : object.timer
private alias object.value4 : object.attractTimer
private alias object.value5 : object.menuSelection

// States
private alias  0 : START_AWAITACTION
private alias  1 : START_NEWGAME
private alias  1 : START_FLASHPRESSBUTTON
private alias  2 : START_LEVELSELECT
private alias  3 : START_CONTINUESCREEN
private alias  4 : START_ATTRACTMODE
private alias  5 : START_ORIGINSMENU
private alias  6 : START_FLASHOPTION
private alias  7 : START_1PSENDCALLBACK
private alias  8 : START_1PWAITFORCHARSELECT
private alias  9 : START_ORIGINSSTARTGAME
private alias 10 : START_2PSENDCALLBACK

// Player List Pos Aliases
private alias 2 : PLAYER_KNUX_ALONE

// Origins Menu Aliases
// (The in-game menu on the title screen, to clarify)
private alias 0 : MENU_1PLAYER
private alias 1 : MENU_2PLAYERVS


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

event ObjectUpdate
	// In Origins there's a whole new menu on the title screen and that's done in here but there's also the old Standalone script
	// here with a lot of changes between the two, so do pardon the dust...
#platform: USE_ORIGINS
	CheckTouchRect(0, 0, screen.xsize, screen.ysize) // Funny this is here since mouse input is mapped to buttonA in Origins
	if keyPress[0].buttonB == true
		checkResult = 1
	end if
	
	if input.pressButton == true
		checkResult = 2
	end if
#endplatform
	
	switch object.state
	case START_AWAITACTION
#platform: USE_STANDALONE
		CheckTouchRect(0, 0, screen.xsize, screen.ysize)
		if keyPress[0].start == true
			checkResult = true
		end if

		if keyPress[0].buttonA == true
			checkResult = true
		end if

		if keyPress[0].buttonB == true
			checkResult = true
		end if

		if keyPress[0].buttonC == true
			checkResult = true
		end if
#endplatform

#platform: USE_ORIGINS
		game.continueFlag = false
#endplatform

		if checkResult > -1
#platform: USE_STANDALONE
			if options.stageSelectFlag == true
				if checkResult > 0
					object.state = START_LEVELSELECT
				else
					object.state = START_NEWGAME
				end if
			else
				object.state = START_NEWGAME
			end if

			PlaySfx(SfxName[Menu Select], false)
			StopMusic()
#endplatform
#platform: USE_ORIGINS
			if options.stageSelectFlag == true
				object.state = START_LEVELSELECT
				StopMusic()
				PlaySfx(SfxName[Menu Select], false)
			else
				if keyPress[0].buttonB == true
					CallNativeFunction2(NotifyCallback, NOTIFY_BACK_TO_MAINMENU, 0)
				else
					object.state = START_FLASHPRESSBUTTON
				end if
			end if
#endplatform
		else
			object.attractTimer++
			if object.attractTimer == 420
				object.state = START_ATTRACTMODE
			end if
		end if
		break

#platform: USE_STANDALONE
	case START_NEWGAME
		object.timer += 8
		if object.timer == 320
			options.attractMode 	= false
			player.lives 			= 3
			player.continues 		= 0
			player.score 			= 0
			player.scoreBonus 		= 50000
			starPostID 				= 0
			specialStage.listPos 	= 0
			specialStage.emeralds 	= 0
			specialStage.nextZone 	= 0

			// New Game - to EHZ1 you go!
			stage.activeList 		= REGULAR_STAGE
			stage.listPos 			= 0
			LoadStage()
		end if
		SetScreenFade(0x00, 0x00, 0x00, object.timer)
		break
#endplatform

#platform: USE_ORIGINS
	case START_FLASHPRESSBUTTON
		object.animationTimer += 10
		object.timer++
		if object.timer >= 30
			object.animationTimer = 0
			object.menuSelection = MENU_1PLAYER
			object.state = START_ORIGINSMENU
		end if
		break
#endplatform
		
	case START_LEVELSELECT
		object.timer += 8
		if object.timer == 320
			options.attractMode 	= false
			player.lives 			= 3
			player.continues 		= 0
			player.score 			= 0
			player.scoreBonus 		= 50000
			starPostID 				= 0
			specialStage.listPos 	= 0
			specialStage.emeralds 	= 0
			specialStage.nextZone 	= 0

			// Level Select
			stage.activeList 	= PRESENTATION_STAGE
			stage.listPos 		= 5
			LoadStage()
		end if
		SetScreenFade(0x00, 0x00, 0x00, object.timer)
		break

	case START_CONTINUESCREEN
		object.timer += 8
		if object.timer == 320
			// Continue Screen...?
			options.attractMode = false
			stage.listPos 		= 4
			LoadStage()
		end if
		SetScreenFade(0x00, 0x00, 0x00, object.timer)
		break

	case START_ATTRACTMODE
		object.timer += 8
		if object.timer >= 320
			player.lives 			= 3
			player.continues 		= 0
			player.score 			= 0
			player.scoreBonus 		= 50000
			starPostID 				= 0
			specialStage.listPos 	= 0
			specialStage.emeralds 	= 0
			specialStage.nextZone 	= 0

			// Attract Mode timee
			stage.activeList = REGULAR_STAGE
			stage.listPos = attract.stage
			stage.listPos <<= 1

			attract.stage++
			attract.stage &= 3
			options.attractMode = true
			credits.screen = 0

			LoadStage()
		end if
		SetScreenFade(0x00, 0x00, 0x00, object.timer)
		break

#platform: USE_ORIGINS
	case START_ORIGINSMENU
		temp0 = false
		
		if keyPress[0].up == true
			temp0 = true
		end if
		if keyPress[0].down == true
			temp0 = true
		end if
		
		if checkResult > 0
			object.animationTimer = 0
			object.timer = 0
			
			if checkResult > 1
				object.state = START_FLASHOPTION
			else
				object.state = START_AWAITACTION
			end if
			
			temp0 = false
		end if
		
		if temp0 != false
			if object.menuSelection == MENU_1PLAYER
				object.menuSelection = MENU_2PLAYERVS
			else
				object.menuSelection = MENU_1PLAYER
			end if
		end if
		break
		
	case START_FLASHOPTION
		object.animationTimer += 11
		if object.animationTimer > 59
			object.animationTimer = 0
		end if
		object.timer++
		if object.timer >= 30
			object.timer = 0
			object.animationTimer = 0
			
			if object.menuSelection == MENU_1PLAYER
				object.state = START_1PSENDCALLBACK
			else
				game.callbackResult = -1
				CallNativeFunction2(NotifyCallback, NOTIFY_CONTROLLER_SUPPORT, 0) // Opens the controller setup screen on Switch
				object.state = START_2PSENDCALLBACK
			end if
		end if
		break
		
	case START_1PSENDCALLBACK
		game.continueFlag = false
		game.callbackResult = -1
		CallNativeFunction2(NotifyCallback, NOTIFY_CHARACTER_SELECT, 0)
		object.state = START_1PWAITFORCHARSELECT
		break
		
	case START_1PWAITFORCHARSELECT
		if game.callbackResult > 0
			object.state = START_ORIGINSSTARTGAME
			if stage.playerListPos > PLAYER_KNUX_ALONE
				CallNativeFunction2(NotifyCallback, NOTIFY_1P_VS_SELECT, 0)
			end if
			StopMusic()
		else
			if game.callbackResult == 0
				object.state = START_AWAITACTION
			end if
		end if
		
	case START_ORIGINSSTARTGAME
		object.timer += 8
		if object.timer >= 320
			if game.continueFlag != false
				game.continueFlag = false
				engine.state = RESET_GAME
			else
				options.attractMode = false
				player.lives = 3
				player.continues = 0
				player.score = 0
				player.scoreBonus = 50000
				starPostID = 0
				specialStage.listPos = 0
				specialStage.emeralds = 0
				specialStage.nextZone = 0
				
				if object.menuSelection == MENU_1PLAYER
					stage.activeList = REGULAR_STAGE
					stage.listPos = 0
				else
					stage.activeList = PRESENTATION_STAGE
					stage.listPos = 3
				end if
				LoadStage()
			end if
		end if
		SetScreenFade(0, 0, 0, object.timer)
		break
		
	case START_2PSENDCALLBACK
		if game.callbackResult > 0
			object.state = START_ORIGINSSTARTGAME
		else
			if game.callbackResult == 0
				object.state = START_ORIGINSMENU
			end if
		end if
		break
#endplatform
	end switch
end event


event ObjectDraw
#platform: USE_STANDALONE
	object.animationTimer++
	if object.animationTimer > 59
		object.animationTimer = 0
	end if

	if object.animationTimer > 29
		DrawSpriteScreenXY(0, screen.xcenter, object.ypos)
	end if
#endplatform

#platform: USE_ORIGINS
	switch object.state
	case START_AWAITACTION
	case START_FLASHPRESSBUTTON
	case START_LEVELSELECT
	case START_CONTINUESCREEN
	case START_ATTRACTMODE
		object.animationTimer++
		if object.animationTimer > 59
			object.animationTimer = 0
		end if
		if object.animationTimer > 29
			DrawSpriteScreenXY(0, screen.xcenter, object.ypos)
		end if
		break
		
	case START_ORIGINSMENU
	case START_FLASHOPTION
	case START_1PSENDCALLBACK
		temp0 = object.ypos
		temp1 = object.ypos
		temp1 += 12
		if object.menuSelection == MENU_1PLAYER
			if object.animationTimer < 29
				DrawSpriteScreenXY(2, screen.xcenter, temp0)
			end if
			DrawSpriteScreenXY(3, screen.xcenter, temp1)
			DrawSpriteScreenXY(5, screen.xcenter, temp0)
		else
			DrawSpriteScreenXY(1, screen.xcenter, temp0)
			if object.animationTimer < 29
				DrawSpriteScreenXY(4, screen.xcenter, temp1)
			end if
			DrawSpriteScreenXY(5, screen.xcenter, temp1)
		end if
		break
		
	case START_1PWAITFORCHARSELECT
	case START_ORIGINSSTARTGAME
	case START_2PSENDCALLBACK
		break
		
	end switch
#endplatform
end event


event ObjectStartup
#platform: USE_ORIGINS
	CallNativeFunction2(NotifyCallback, NOTIFY_LEVEL_SELECT_MENU, 0) // On the title screen??
#endplatform
	LoadSpriteSheet("Title/Title.gif")
	switch engine.deviceType
	case STANDARD
		SpriteFrame(-49, -1, 98, 9, 1, 380)		// Press Button 	- #0
			break
			
		case MOBILE
		SpriteFrame(-57, -1, 114, 9, 1, 390)	// Touch To Start 	- #0
		break
		
	end switch
	
#platform: USE_ORIGINS
	SpriteFrame(-40, -1, 66, 9, 1, 400)		// 1 PLAYER						- #1
	SpriteFrame(-40, -1, 66, 9, 92, 400)	// 1 PLAYER (Highlighted)		- #2
	SpriteFrame(-40, -1, 90, 9, 1, 410)		// 2 PLAYER VS					- #3
	SpriteFrame(-40, -1, 90, 9, 92, 410)	// 2 PLAYER VS (Highlighted)	- #4
	SpriteFrame(-60, -5, 16, 16, 104, 370)	// Sonic Icon					- #5
#endplatform
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Title/Title.gif")
	SpriteFrame(-49, -1, 98, 9, 1, 380)		// Press Button 	- #0
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
