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

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

private alias object.value0 : object.timer
private alias object.value3 : object.timer2

// States
private alias 0 : CREDITSCONTROL_SETUP
private alias 1 : CREDITSCONTROL_FADEIN
private alias 2 : CREDITSCONTROL_SHOWSCREEN
private alias 3 : CREDITSCONTROL_FADEOUT
private alias 4 : CREDITSCONTROL_FADEINTOEND
private alias 5 : CREDITSCONTROL_SHOWEND
private alias 6 : CREDITSCONTROL_FADETOMENU

// Priority
public alias 0 : PRIORITY_BOUNDS
public alias 1 : PRIORITY_ACTIVE
public alias 2 : PRIORITY_ALWAYS
public alias 3 : PRIORITY_XBOUNDS
public alias 4 : PRIORITY_XBOUNDS_DESTROY
public alias 5 : PRIORITY_INACTIVE
public alias 6 : PRIORITY_BOUNDS_SMALL
public alias 7 : PRIORITY_ACTIVE_SMALL

// Ink Effects
public alias 0 : INK_NONE
public alias 1 : INK_BLEND
public alias 2 : INK_ALPHA
public alias 3 : INK_ADD
public alias 4 : INK_SUB

// Flip Directions
public alias 0 : FLIP_NONE
public alias 1 : FLIP_X
public alias 2 : FLIP_Y
public alias 3 : FLIP_XY


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

event ObjectUpdate
	switch object.state
	case CREDITSCONTROL_SETUP
		object.timer = 320
		SetScreenFade(0x00, 0x00, 0x00, object.timer)
		object.state++
		if credits.screen == 0
			PlayMusic(0)
		end if
		break

	case CREDITSCONTROL_FADEIN
		if object.timer > 0
			object.timer -= 8
		else
			object.alpha = 0x80
			object.state++
		end if
		SetScreenFade(0x00, 0x00, 0x00, object.timer)
		break

	case CREDITSCONTROL_SHOWSCREEN
		object.timer++
		if object.timer == 356
			object.timer = 0
			object.state++
		end if
		break

	case CREDITSCONTROL_FADEOUT
		if object.timer < 256
			if object.timer2 == 21
				object.timer += 4
			else
				object.timer += 8
			end if

			SetScreenFade(0x00, 0x00, 0x00, object.timer)
		else
			object.timer2++
			if object.timer2 == 22
				object.state = CREDITSCONTROL_FADEINTOEND
				screen.yoffset += 256
			else
				object.state = CREDITSCONTROL_FADEIN
				if specialStage.emeralds >= 127
					if object.timer2 == 21
						screen.yoffset = 5120
						screen.yoffset += 1036
					else
						screen.yoffset += 256
					end if
				else
					screen.yoffset += 256
				end if
			end if

			SetScreenFade(0x00, 0x00, 0x00, 255)
		end if
		break

	case CREDITSCONTROL_FADEINTOEND
		if object.timer > 0
			object.timer -= 8
		else
			object.alpha = 0x80
			object.state++
		end if
		SetScreenFade(0x00, 0x00, 0x00, object.timer)
		break

	case CREDITSCONTROL_SHOWEND
		object.timer++
		if object.timer == 0x4B0
			object.timer = 0
			object.state++
		end if

		if keyPress[0].start == true
			object.timer = 0
			object.state++
		end if

		if object.timer > 60
			CheckTouchRect(0, 0, screen.xsize, screen.ysize)
			if checkResult > -1
				object.timer = 0
				object.state++
			end if
		end if
		break
		
	case CREDITSCONTROL_FADETOMENU
		if object.timer < 256
			object.timer += 8
			SetScreenFade(0x00, 0x00, 0x00, object.timer)
		else
			stage.activeList 	= PRESENTATION_STAGE
			stage.listPos 		= 0
			StopMusic()
			options.attractMode = false
#platform: USE_STANDALONE
			engine.state 		= 7
#endplatform
#platform: USE_ORIGINS
			engine.state 		= 8
#endplatform
			SetScreenFade(0x00, 0x00, 0x00, 0xFF)
#platform: USE_ORIGINS
			CallNativeFunction2(NotifyCallback, NOTIFY_STATS_MOVIE, 1)
#endplatform
		end if
		break
		
	end switch

	temp0 = screen.xcenter
	FlipSign(temp0)
	temp0 += 256
	screen.xoffset = temp0
end event


event ObjectStartup
	SetMusicTrack("Credits.ogg", 0, false)

	options.touchControls 	= false
	screen.yoffset 			= -8

	foreach (TypeName[Credits Control], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
	next
end event


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

event RSDKDraw
	DrawSprite(0)
end event


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