// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: SHC Splash
// Script Author: LaveSIime
// based on Mania's implementation by SuperSonic16, LJSTAR and RubberDuckyCooly
// ----------------------------------


//-------Aliases-------//
private alias object.value0 : splash.fadeTimer
private alias object.value1 : splash.frameTimer

// States
private alias 0 : SPLASH_SETUP
private alias 1 : SPLASH_FADEIN
private alias 2 : SPLASH_SHC
private alias 3 : SPLASH_FADEOUT

// If you have a custom title screen, this function is all that needs to be changed
// This function is called after the splash is over to decide what to do next, by default it'll proceed with 
function Splash_NextPhase
	LoadPalette("SpecialStage.act", 0, 0, 0, 100)
	CreateTempObject(TypeName[Palette Police], saved.graphics, 0, 0)
	ResetObjectEntity(object.entityPos, TypeName[Sonic Team], 0, object.xpos, object.ypos)
end function

// Everything hereon is the splash code, you shouldn't need to change this

// Array of every frame and what sprite to use
private table Splash_SpriteTable
	2, 1, 0, 1, 2, 1, 0, 2, 0, 1, 2, 0, 2, 1, 0, 1, 0, 1, 2, 1, 0, 2, 0, 2, 1, 0, 2, 0, 1, 2, 0, 2, 0, 2, 1, 0, 1, 0, 2, 0, 1, 2, 0, 2, 1, 0, 1, 2, 1, 2, 1, 0, 2, 0, 1, 2
end table

// Array of how long every frame lasts for
private table Splash_frameDelay
	64, 1, 1, 1, 1, 1, 2, 1, 2, 1, 3, 1, 4, 3, 1, 2, 3, 2, 4, 3, 3, 4, 64, 4, 2, 3, 2, 3, 2, 2, 3, 2, 3, 2, 2, 3, 2, 64, 2, 3, 2, 2, 3, 2, 2, 3, 2, 2, 2, 2, 2, 3, 2, 3, 2, 360
end table

event ObjectMain
	switch object.state
	case SPLASH_SETUP
		// Initialization phase
		splash.fadeTimer = 320
		SetScreenFade(0, 0, 0, splash.fadeTimer)
		object.state++
		Callfunction(SkipScreen)			
		break
	case SPLASH_FADEIN
		// Fade-in phase
		if splash.fadeTimer > 0
			splash.fadeTimer -= 8
		else
			object.state++
		end if
		SetScreenFade(0, 0, 0, splash.fadeTimer)
		Callfunction(SkipScreen)			
		break
	case SPLASH_SHC
		// Main phase

		// Move to next frame in accordance with the table
		GetTableValue(temp0, object.frame, Splash_frameDelay)
		splash.frameTimer++
		if splash.frameTimer == temp0
			object.frame += 1
			splash.frameTimer = 0
		end if

		// Play the jingle when the logo starts to flash
		if object.frame == 1
			if splash.fadeTimer == 0
				PlayMusic(10)
			end if
		end if

		// Fade out when on the last frame
		if object.frame == 55
			splash.fadeTimer = 0
			object.state++
		end if
		Callfunction(SkipScreen)		
		break
	case SPLASH_FADEOUT
		// Fade-out phase
		if splash.fadeTimer < 256
			splash.fadeTimer += 8
			SetScreenFade(0, 0, 0, splash.fadeTimer)
		else
			Callfunction(Splash_NextPhase)
			SetScreenFade(0, 0, 0, 255)
		end if
		break
	end switch
end event

function SkipScreen	
	CheckTouchRect(0, 0, screen.xsize, screen.ysize)
	if checkResult > -1
		inputPress.start = 1
	end if
	if inputPress.start == 1
		object.state = SPLASH_FADEOUT
	end if
end function

event ObjectDraw
	GetTableValue(temp0, object.frame, Splash_SpriteTable)
	DrawSpriteScreenXY(temp0, screen.xcenter, screen.ycenter)
end event

event ObjectStartup
	LoadSpriteSheet("Title/Splash.gif")
	SpriteFrame(-160, -112, 320, 224, 0, 0)
	SpriteFrame(-160, -112, 320, 224, 0, 224)
	SpriteFrame(-160, -112, 320, 224, 0, 448)
	LoadPalette("Splash.act", 0, 50, 50, 61)
	SetMusicTrack("Splash.ogg", 10, 0)
end event


event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
