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

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

private alias object.value0 : object.timer
// Not sure why it skips from 0 to 4, guess there must've been a revision or smth
private alias object.value4 : object.attractTimer

private alias 0 : START_FLASH
private alias 1 : START_STARTGAME
private alias 2 : START_STARTLVLSEL
private alias 3 : START_STARTLVLSEL_OLD
private alias 4 : START_STARTATTRACT
private alias 5 : START_NOTIFYCALLBACK
private alias 6 : START_WAITFORCALLBACK
private alias 7 : START_FADETOEXIT
private alias 8 : START_EXIT


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

event ObjectUpdate
	switch object.state
	case START_FLASH
		CheckTouchRect(0, 0, screen.xsize, screen.ysize)
		
#platform: USE_STANDALONE
		if keyPress[0].start == true
			checkResult = true
		end if

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

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

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

#platform: USE_ORIGINS
		if input.pressButton == true
			checkResult = true
		end if
#endplatform

		if checkResult > -1
#platform: USE_STANDALONE
			if options.stageSelectFlag == true
				if checkResult > 0 // This is a fancy way of seeing if two (or more) fingers are on the screen
					object.state = START_STARTLVLSEL
				else
					object.state = START_STARTGAME
				end if
			else
				object.state = START_STARTGAME
			end if

			PlaySfx(SfxName[Menu Select], false)
			StopMusic()
#endplatform
			
#platform: USE_ORIGINS
			if options.stageSelectFlag == true
				if checkResult > 0
					object.state = START_STARTLVLSEL
					StopMusic()
				else
					object.state = START_NOTIFYCALLBACK
				end if
			else
				if keyPress[0].buttonB == true
					CallNativeFunction2(NotifyCallback, NOTIFY_BACK_TO_MAINMENU, 1)
				else
					object.state = START_NOTIFYCALLBACK
				end if
			end if
#endplatform
		else
			object.attractTimer++
			if object.attractTimer == 360
				object.state = START_STARTATTRACT
			end if
		end if
		break

	case START_STARTGAME
		object.timer += 8
		if object.timer == 320
			options.attractMode   = false
			player.lives 		  = 3
			player.continues 	  = 0
			player.score 		  = 0
			player.scoreBonus 	  = 50000
			lampPostID 			  = 0
			specialStage.listPos  = 0
			specialStage.emeralds = 0
			specialStage.nextZone = 0

			// New Game - to GHZ1 you go!
			stage.activeList 	  = REGULAR_STAGE
			stage.listPos 		  = 0

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

	case START_STARTLVLSEL
		object.timer += 8
		if object.timer == 320
			options.attractMode   = false
			player.lives 		  = 3
			player.continues 	  = 0
			player.score 		  = 0
			player.scoreBonus 	  = 50000
			lampPostID 			  = 0
			specialStage.listPos  = 0
			specialStage.emeralds = 0
			specialStage.nextZone = 0

			// Level Select!!
			stage.activeList 	  = PRESENTATION_STAGE
			stage.listPos 		  = 5

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

	case START_STARTLVLSEL_OLD // Unused state
		object.timer += 8
		if object.timer == 320
			// The Continue scene...?
			// This state seems to be a leftover from CD, where its level select code was pretty much the same as this, including going to stage list pos 4
			options.attractMode = false
			stage.listPos 		= 4
			LoadStage()
		end if
		SetScreenFade(0, 0, 0, object.timer)
		break

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

			if attract.stage == 3
				stage.activeList = SPECIAL_STAGE
				stage.listPos 	 = 0
			else
				stage.activeList = REGULAR_STAGE
				stage.listPos 	 = attract.stage
				stage.listPos 	*= 3
			end if
			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_NOTIFYCALLBACK
		game.continueFlag = false
		game.callbackResult = -1
		if game.playMode == BOOT_PLAYMODE_CLASSIC
			CallNativeFunction2(NotifyCallback, NOTIFY_CHARACTER_SELECT, 1)
		else
			CallNativeFunction2(NotifyCallback, NOTIFY_CHARACTER_SELECT, 0)
		end if
		object.state = START_WAITFORCALLBACK
		break
		
	case START_WAITFORCALLBACK
		if game.callbackResult > 0
			if game.continueFlag != false
				game.continueFlag = false
				object.state = START_FADETOEXIT
			else
				object.state = START_STARTGAME
			end if
			object.timer = 0
			StopMusic()
		else
			if game.callbackResult == 0
				object.state = START_FLASH
			end if
		end if
		break
		
	case START_FADETOEXIT
		object.timer += 4
		if object.timer >= 384
			object.state = START_EXIT
		end if
		SetScreenFade(0, 0, 0, object.timer)
		break
		
	case START_EXIT
		engine.state = RESET_GAME
		SetScreenFade(0, 0, 0, 255)
		break
#endplatform

	end switch
end event


event ObjectDraw
	object.animationTimer++
	if object.animationTimer > 59
		object.animationTimer = 0
	end if

	if object.animationTimer > 29
		DrawSpriteScreenXY(0, object.xpos, object.ypos)
	end if
end event


event ObjectStartup
#platform: USE_ORIGINS
	// Level Select?
	CallNativeFunction2(NotifyCallback, NOTIFY_LEVEL_SELECT_MENU, 0)
#endplatform
	
	LoadSpriteSheet("Title/Title.gif")
	
	// In the original S1 releases this was instead a engine.language switch for different languages, although all cases led to the same path
	
	switch engine.deviceType
	case STANDARD
		SpriteFrame(-48, -4, 96, 8, 1, 283)
		break

	case MOBILE
		SpriteFrame(-56, -4, 112, 8, 1, 292)
		break
		
	end switch
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Title/Title.gif")
	SpriteFrame(-48, -4, 96, 8, 1, 283)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event