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

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

private alias object.value0 : object.timer

private alias 0 : MUSICEVENT_FADETOBOSS
private alias 1 : MUSICEVENT_FADETOSTAGE
private alias 2 : MUSICEVENT_TRANSITION
private alias 3 : MUSICEVENT_FADETOBOSS_ACTION // This one and the next are the same as the actual ones in S1, the actual ones this time just do a check for vs.bossAttack to disable music transitions there
private alias 4 : MUSICEVENT_FADETOSTAGE_ACTION
private alias 5 : MUSICEVENT_FADEOUT

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

private alias 25 : SLOT_MUSICEVENT_CHANGE
private alias 26 : SLOT_MUSICEVENT_BOSS

// Track Aliases
private alias 0 : TRACK_STAGE
private alias 2 : TRACK_INVINCIBLE
private alias 4 : TRACK_BOSS


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

event ObjectUpdate
	switch object.propertyValue
	case MUSICEVENT_FADETOBOSS
		if vs.bossAttack == false
			object.propertyValue = MUSICEVENT_FADETOBOSS_ACTION
		else
			object.type = TypeName[Blank Object]
		end if
		break

	case MUSICEVENT_FADETOSTAGE
#platform: USE_ORIGINS
		if game.playMode == BOOT_PLAYMODE_BOSSRUSH
			object.propertyValue = MUSICEVENT_FADEOUT
		else
#endplatform
			if vs.bossAttack == false
				object.propertyValue = MUSICEVENT_FADETOSTAGE_ACTION
				if stage.activeList == BONUS_STAGE
					if stage.listPos > 0 // Checking to see if we're in a non-MBZ Boss Rush stage, this is intended for the 2013 Boss Rush but could work for the Origins Boss Rush too if the above game.playMode check was removed
						vs.bossAttack = true
						object.type = TypeName[Blank Object]
					end if
				end if
			else
				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_FADETOSTAGE
					CallFunction(SpeedUpMusic)
					stage.musicFlag = MUSICEVENT_FLAG_NOCHANGE
					break

				case MUSICEVENT_TRANSITION
					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

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

	case MUSICEVENT_FADETOSTAGE_ACTION
		if object.timer < 50
			object.timer++
			music.volume -= 2
		else
			PlayMusic(TRACK_STAGE)
			object.type = TypeName[Blank Object]
		end if
		break
		
#platform: USE_ORIGINS
	case MUSICEVENT_FADEOUT
		if object.timer < 50
			object.timer++
			music.volume -= 2
		else
			StopMusic()
			object.type = TypeName[Blank Object]
		end if
		break
#endplatform
	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)
	
	// While used by the object, it shouldn't be set by the editor
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
