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

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

private alias object.value0 : object.timer

// Tracks
private alias 0 : TRACK_STAGE
private alias 2 : TRACK_INVINCIBLE
private alias 4 : TRACK_BOSS

private alias 0 : MUSICEVENT_FADETOBOSS
private alias 1 : MUSICEVENT_FADETOSTAGE
private alias 2 : MUSICEVENT_TRANSITION
private alias 3 : MUSICEVENT_FADEOUT // New to Origins

private alias 0 : MUSICEVENT_FLAG_NOCHANGE
private alias 1 : MUSICEVENT_FLAG_SPEEDUP
private alias 2 : MUSICEVENT_FLAG_SLOWDOWN


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

event ObjectUpdate
#platform: USE_ORIGINS
	CallNativeFunction4(NotifyCallback, NOTIFY_DEBUGPRINT, 100, object.propertyValue, object.timer) // LOL
#endplatform

	switch object.propertyValue
#platform: USE_ORIGINS
	case MUSICEVENT_FADEOUT // yeah despite being numerically the final variant, the Origins devs just stuffed it up above at the top...
		if object.timer < 50
			object.timer++
			music.volume -= 2
		else
			StopMusic()
			music.volume = 100
			object.type = TypeName[Blank Object]
		end if
		break
#endplatform

	case MUSICEVENT_FADETOBOSS
		if object.timer < 50
			object.timer++
			music.volume -= 2
		else
			PlayMusic(TRACK_BOSS)
			object.type = TypeName[Blank Object]
		end if
		break

	case MUSICEVENT_FADETOSTAGE
#platform: USE_ORIGINS
		if game.playMode == BOOT_PLAYMODE_BOSSRUSH
			// In Boss Rush we don't wanna resume normal music afterwards, become a simple fade-out variant instead
			
			object.propertyValue = MUSICEVENT_FADEOUT
		else
#endplatform
			if object.timer < 50
				object.timer++
				music.volume += 2
			else
				PlayMusic(0)
				object.type = TypeName[Blank Object]
			end if
#platform: USE_ORIGINS
		end if
#endplatform
		break

	case MUSICEVENT_TRANSITION
		if object.timer < 240
			if music.volume > 0
				PauseMusic()
				music.volume = 0
			end if
			
			object.timer++
		else
			if music.volume == 0
				ResumeMusic()
				switch stage.musicFlag
				case MUSICEVENT_FLAG_NOCHANGE
					break

				case MUSICEVENT_FLAG_SPEEDUP
					CallFunction(SpeedUpMusic)
					stage.musicFlag = MUSICEVENT_FLAG_NOCHANGE
					break

				case MUSICEVENT_FLAG_SLOWDOWN
					CallFunction(SlowDownMusic)
					stage.musicFlag = MUSICEVENT_FLAG_NOCHANGE
					break
					
				end switch
				
				music.volume = 5
			else
				if music.volume < 100
					music.volume += 5
				else
					object.type = TypeName[Blank Object]
				end if
			end if
		end if
		break
		
	end switch
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
	
	// Although used by the object, it shouldn't be set here
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
