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

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

private alias object.value1 : object.palCycleTimer
private alias object.value2 : object.palCycleTablePos
private alias object.value3 : object.paletteTimer

// Player Aliases
private alias object.gravity : player.gravity
private alias object.animation : player.animation
private alias object.xvel : player.xvel
private alias object.yvel : player.yvel
private alias object.speed : player.speed
private alias object.up : player.up
private alias object.down : player.down
private alias object.ypos : player.ypos

// Game Modes
private alias 0 : MODE_NOSAVE
private alias 1 : MODE_SAVEGAME
private alias 2 : MODE_TIMEATTACK

// Tracks
private alias 0 : TRACK_STAGE
private alias 1 : TRACK_ACTFINISH
private alias 2 : TRACK_INVINCIBLE
private alias 3 : TRACK_CONTINUE
private alias 4 : TRACK_BOSS
private alias 5 : TRACK_GAMEOVER
private alias 6 : TRACK_DROWNING
private alias 7 : TRACK_SUPER

// Reserved object slots
private alias 10 : SLOT_ZONESETUP
private alias 25 : SLOT_MUSICEVENT_CHANGE

// Music Events
private alias  0 : MUSICEVENT_FADETOBOSS
private alias  1 : MUSICEVENT_FADETOSTAGE
private alias  2 : MUSICEVENT_TRANSITION

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

// Music Loops
private alias 99874 : MUSIC_LOOP_WFZ
private alias 80074 : MUSIC_LOOP_WFZ_F

private alias 38679 : MUSIC_LOOP_INV
private alias 30897 : MUSIC_LOOP_INV_F


// ========================
// Function Declarations
// ========================

reserve function WFZSetup_State_Clinging

reserve function WFZSetup_SpeedUpMusic
reserve function WFZSetup_SlowDownMusic


// ========================
// Static Values
// ========================

public value WFZSetup_scrollVelocity.x = 0
public value WFZSetup_scrollVelocity.y = 0

public value WFZSetup_hPropellerFrame = 0
public value WFZSetup_hPropellerTimer = 0
public value WFZSetup_vPropellerFrame = 0
public value WFZSetup_vPropellerTimer = 0


// ========================
// Tables
// ========================

private table WFZSetup_palCycleTable
	0xE0E040, 0xE0A020, 0xE06000, 0xC02000
	0xE0E0E0, 0xE0E040, 0xE0A020, 0xE06000
	0xE0E040, 0xE0A020, 0xE06000, 0xC02000
	0xE0A020, 0xE06000, 0xC02000, 0x802000
end table


// ========================
// Function Definitions
// ========================

// Custom state given to the player in this stage only, by the Wind Current objects
// Identical to the (unused) Player_State_Clinging function in the Player Object script
public function WFZSetup_State_Clinging
	player.gravity = GRAVITY_AIR
	if player.animation != ANI_CLINGING
		player.xvel = -0x80000
		player.speed = -0x80000
	else
		player.xvel = 0
		player.speed = 0
	end if

	if player.up == true
		player.ypos -= 0x10000
	else
		if player.down == true
			player.ypos += 0x10000
		end if
	end if

	player.yvel = 0
end function


private function WFZSetup_SpeedUpMusic
	CheckEqual(object[SLOT_MUSICEVENT_CHANGE].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[SLOT_MUSICEVENT_CHANGE].propertyValue, MUSICEVENT_TRANSITION)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, MUSICEVENT_FLAG_NOCHANGE)
	temp0 &= checkResult
	if temp0 == false
		switch music.currentTrack
		case TRACK_STAGE
			SetMusicTrack("Invincibility_F.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV_F)
			SwapMusicTrack("WingFortress_F.ogg", TRACK_STAGE, MUSIC_LOOP_WFZ_F, 8000)
			break

		case TRACK_INVINCIBLE
			SetMusicTrack("WingFortress_F.ogg", TRACK_STAGE, MUSIC_LOOP_WFZ_F)
			SwapMusicTrack("Invincibility_F.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV_F, 8000)
			break

		case TRACK_BOSS
		case TRACK_DROWNING
		case TRACK_SUPER
			SetMusicTrack("WingFortress_F.ogg", TRACK_STAGE, MUSIC_LOOP_WFZ_F)
			SetMusicTrack("Invincibility_F.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV_F)
			break
			
		end switch
	else
		stage.musicFlag = MUSICEVENT_FLAG_SPEEDUP
	end if
end function


private function WFZSetup_SlowDownMusic
	CheckEqual(object[SLOT_MUSICEVENT_CHANGE].type, TypeName[Music Event])
	temp0 = checkResult
	CheckEqual(object[SLOT_MUSICEVENT_CHANGE].propertyValue, MUSICEVENT_TRANSITION)
	temp0 &= checkResult
	CheckEqual(stage.musicFlag, MUSICEVENT_FLAG_NOCHANGE)
	temp0 &= checkResult
	if temp0 == false
		switch music.currentTrack
		case TRACK_STAGE
			SetMusicTrack("Invincibility.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV)
			SwapMusicTrack("WingFortress.ogg", TRACK_STAGE, MUSIC_LOOP_WFZ, 12500)
			break

		case TRACK_INVINCIBLE
			SetMusicTrack("WingFortress.ogg", TRACK_STAGE, MUSIC_LOOP_WFZ)
			SwapMusicTrack("Invincibility.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV, 12500)
			break

		case TRACK_BOSS
		case TRACK_DROWNING
		case TRACK_SUPER
			SetMusicTrack("WingFortress.ogg", TRACK_STAGE, MUSIC_LOOP_WFZ)
			SetMusicTrack("Invincibility.ogg", TRACK_INVINCIBLE, MUSIC_LOOP_INV)
			break
			
		end switch
	else
		stage.musicFlag = MUSICEVENT_FLAG_SLOWDOWN
	end if
end function


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

event ObjectUpdate
	object.palCycleTimer++
	if object.palCycleTimer == 2
		object.palCycleTimer = 0

		GetTableValue(temp0, object.palCycleTablePos, WFZSetup_palCycleTable)
		SetPaletteEntry(0, 167, temp0)
		object.palCycleTablePos++
		object.palCycleTablePos &= 15

		GetTableValue(temp0, object.palCycleTablePos, WFZSetup_palCycleTable)
		SetPaletteEntry(0, 168, temp0)
		object.palCycleTablePos++
		object.palCycleTablePos &= 15

		GetTableValue(temp0, object.palCycleTablePos, WFZSetup_palCycleTable)
		SetPaletteEntry(0, 169, temp0)
		object.palCycleTablePos++
		object.palCycleTablePos &= 15

		GetTableValue(temp0, object.palCycleTablePos, WFZSetup_palCycleTable)
		SetPaletteEntry(0, 170, temp0)
		object.palCycleTablePos++
		object.palCycleTablePos &= 15
	end if

	object.paletteTimer++
	if object.paletteTimer == 6
		object.paletteTimer = 0
		RotatePalette(0, 192, 195, true)
	end if

	WFZSetup_hPropellerFrame = WFZSetup_hPropellerTimer
	WFZSetup_hPropellerFrame >>= 1
	WFZSetup_hPropellerTimer++
	WFZSetup_hPropellerTimer %= 12

	WFZSetup_vPropellerFrame = WFZSetup_vPropellerTimer
	WFZSetup_vPropellerFrame >>= 1
	WFZSetup_vPropellerTimer++
	WFZSetup_vPropellerTimer %= 6
end event


event ObjectStartup
	SetMusicTrack("WingFortress.ogg", TRACK_STAGE, MUSIC_LOOP_WFZ)

	SpeedUpMusic = WFZSetup_SpeedUpMusic
	SlowDownMusic = WFZSetup_SlowDownMusic

	animalType1 = TypeName[Locky]
	animalType2 = TypeName[Wocky]

	// Reset the boss flash colour to just be a solid black for now
	SetPaletteEntry(0, 196, 0x000000)

	object[SLOT_ZONESETUP].type = TypeName[WFZ Setup]
	object[SLOT_ZONESETUP].priority = PRIORITY_ACTIVE

	WFZSetup_scrollVelocity.x = 0
	WFZSetup_scrollVelocity.y = 0
	
	WFZSetup_hPropellerFrame = 0
	WFZSetup_hPropellerTimer = 0
	WFZSetup_vPropellerFrame = 0
	WFZSetup_vPropellerTimer = 0
	
	// This level has a special cutscene and all for the main game, remove Animal Capsules outside of TA so that we can watch it
	foreach (TypeName[Animal Prison], arrayPos0, ALL_ENTITIES)
		if options.gameMode != MODE_TIMEATTACK
			ResetObjectEntity(arrayPos0, TypeName[Blank Object], 0, 0, 0)
		end if
	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
