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

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

private alias object.value0 : object.timer
private alias object.value1 : object.logoPos // Not set in this script, but set by the [Ending Control] that spawns this object
private alias object.value2 : object.tailFrame

// This object's states are pretty complex, a single object won't cycle through them all but instead each of the states appear under different scenarios
// I tried to note how each state plays into others, sorry if the explanation isn't any good though...

// These first two states are only encountered in the bad ending, when the player didn't get all emeralds
private alias  0 : ENDINGPOSE_INIT
private alias  1 : ENDINGPOSE_WAITING

// These next three states are when P1 poses, each pose frame is its own state
// These states are seen in both types of the ending
private alias  2 : ENDINGPOSE_POSE_START
private alias  3 : ENDINGPOSE_POSE_MID
private alias  4 : ENDINGPOSE_POSE_END

private alias  5 : ENDINGPOSE_HOLDEMERALDS
private alias  6 : ENDINGPOSE_EMERALDSSHINE
private alias  7 : ENDINGPOSE_LOOKUP
private alias  8 : ENDINGPOSE_EMERALDSFLASH_A // Fade-out
private alias  9 : ENDINGPOSE_EMERALDSFLASH_B // Fade-in
private alias 10 : ENDINGPOSE_LOOKAROUND
private alias 11 : ENDINGPOSE_LOOKLEFT
// (And then after looking left, P1 goes over to ENDINGPOSE_POSE_START)

// P2 Tails gets his own little states too, though they aren't as grand as P1's giant poses are
private alias 12 : ENDINGPOSE_P2_LOOKAROUND
private alias 13 : ENDINGPOSE_P2_LOOKLEFT
private alias 14 : ENDINGPOSE_P2_POSE_1
private alias 15 : ENDINGPOSE_P2_POSE_2


// Player Aliases
private alias object.state : player.state
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.speed : player.speed
private alias object.frame : player.frame
private alias object.direction : player.direction
private alias object.up : player.up
private alias object.drawOrder : player.drawOrder
private alias object.controlMode : player.controlMode

// Reserved Object Slot Aliases
private alias 1 : SLOT_PLAYER2


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

reserve function EndingPose_UpdateLogo


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

private function EndingPose_UpdateLogo
	if object.logoPos > 0
		object.logoPos -= 16
	end if
end function


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

event ObjectUpdate
	switch object.state
	case ENDINGPOSE_INIT
		object.state++
		if stage.playerListPos == PLAYER_TAILS_ALONE
			// A little extra setup for Tails's tails
			object.tailFrame = 36
		end if
		// [Fallthrough]
	case ENDINGPOSE_WAITING
		object.timer++
		if stage.playerListPos == PLAYER_TAILS_ALONE
			// Animate Tails's tails
			object.tailFrame = object.timer
			object.tailFrame >>= 3
			object.tailFrame %= 5
			object.tailFrame += 36
		end if

		if object.timer == 240
			currentPlayer = SLOT_PLAYER2
			while currentPlayer < playerCount
				ResetObjectEntity(currentPlayer, TypeName[Ending Pose], 0, player[currentPlayer].xpos, player[currentPlayer].ypos)
				player[currentPlayer].drawOrder = 6
				player[currentPlayer].state = ENDINGPOSE_P2_POSE_1
				player[currentPlayer].frame = 43
				player[currentPlayer].direction = FACING_LEFT
				
				currentPlayer++
			loop
			
			object.timer = 0
			switch stage.playerListPos
			case PLAYER_SONIC_ALONE
			case PLAYER_SONIC_AND_TAILS
				object.frame = 1
				break
				
			case PLAYER_TAILS_ALONE
				object.frame = 14
				break
				
			case PLAYER_KNUX_ALONE
				object.frame = 25
				break
				
			end switch
			
			object.state++
			object.tailFrame = 0
		else
			currentPlayer = SLOT_PLAYER2
			while currentPlayer < playerCount
				if player[currentPlayer].speed == 0
					player[currentPlayer].direction = FACING_LEFT
				end if
				
				currentPlayer++
			loop
		end if
		break

	case ENDINGPOSE_POSE_START
		CallFunction(EndingPose_UpdateLogo)
		object.timer++
		if object.timer == 12
			object.timer = 0
			switch stage.playerListPos
			case PLAYER_SONIC_ALONE
			case PLAYER_SONIC_AND_TAILS
				object.frame = 2
				object.ixpos = camera[0].xpos
				object.iypos = camera[0].ypos
				break
				
			case PLAYER_TAILS_ALONE
			case PLAYER_KNUX_ALONE
				break
				
			end switch
			
			object.state++
		end if
		break

	case ENDINGPOSE_POSE_MID
		CallFunction(EndingPose_UpdateLogo)
		object.timer++
		if object.timer == 4
			object.timer = -1560
			switch stage.playerListPos
			case PLAYER_SONIC_ALONE
			case PLAYER_SONIC_AND_TAILS
				object.frame = 3
				break
				
			case PLAYER_TAILS_ALONE
				object.frame = 15
				object.ixpos = camera[0].xpos
				object.iypos = camera[0].ypos
				break
				
			case PLAYER_KNUX_ALONE
				object.frame = 26
				object.ixpos = camera[0].xpos
				object.iypos = camera[0].ypos
				break
				
			end switch
			
			object.state++
		end if
		break

	case ENDINGPOSE_POSE_END
		if object.timer < 320
			object.timer += 12
		else
			// Roll the credits!
			stage.activeList = PRESENTATION_STAGE
			stage.listPos = 2
			
			lampPostID = 0
			recMilliSeconds = 0
			recSeconds = 0
			recMinutes = 0
			LoadStage()
		end if
		
		if object.timer > 0
			temp0 = object.timer
		else
			temp0 = 0
		end if
		SetScreenFade(0, 0, 0, temp0)
		break

	// In the good ending, the character starts on this state, skipping all the ones before
	case ENDINGPOSE_HOLDEMERALDS
		object.timer++
		if object.timer == 80
			object.timer = 0
			object.state++
		else
			switch stage.playerListPos
			case PLAYER_SONIC_ALONE
			case PLAYER_SONIC_AND_TAILS
				object.frame = 4
				break
				
			case PLAYER_TAILS_ALONE
				object.frame = 16
				break
				
			case PLAYER_KNUX_ALONE
				object.frame = 27
				break
				
			end switch

			currentPlayer = SLOT_PLAYER2
			while currentPlayer < playerCount
				if player[currentPlayer].speed == 0
					player[currentPlayer].direction = FACING_LEFT
				end if
				
				currentPlayer++
			loop
		end if
		break

	case ENDINGPOSE_EMERALDSSHINE
		object.frame = object.timer
		object.frame >>= 2
		object.frame &= 1
		
		switch stage.playerListPos
		case PLAYER_SONIC_ALONE
		case PLAYER_SONIC_AND_TAILS
			object.frame += 5
			break
			
		case PLAYER_TAILS_ALONE
			object.frame += 17
			break
			
		case PLAYER_KNUX_ALONE
			object.frame += 27
			break
			
		end switch

		object.timer++
		if object.timer == 80
			object.timer = 0
			switch stage.playerListPos
			case PLAYER_SONIC_ALONE
			case PLAYER_SONIC_AND_TAILS
				object.frame = 7
				break
				
			case PLAYER_TAILS_ALONE
				object.frame = 19
				break
				
			case PLAYER_KNUX_ALONE
				object.frame = 29
				break
				
			end switch

			currentPlayer = SLOT_PLAYER2
			while currentPlayer < playerCount
				player[currentPlayer].up = true
				player[currentPlayer].controlMode = CONTROLMODE_NONE
				
				currentPlayer++
			loop
			
			CreateTempObject(TypeName[Emeralds], 0, object.xpos, object.ypos)
			object[tempObjectPos].drawOrder = 6
			
			object.state++
		else
			currentPlayer = SLOT_PLAYER2
			while currentPlayer < playerCount
				if player[currentPlayer].speed == 0
					player[currentPlayer].direction = FACING_LEFT
				end if
				
				currentPlayer++
			loop
		end if
		break

	case ENDINGPOSE_LOOKUP
		// In this state, we wait for the Emeralds to increase our state
		break

	case ENDINGPOSE_EMERALDSFLASH_A
		if object.timer < 384
			object.timer += 6
		else
			object.timer = 256
			
			// Set all FG chunks to use their flowered versions
			SetTileLayerEntry(184, 0, 2, 2)
			SetTileLayerEntry(185, 0, 3, 2)
			SetTileLayerEntry(186, 0, 2, 3)
			SetTileLayerEntry(187, 0, 3, 3)
			SetTileLayerEntry(188, 0, 4, 2)
			SetTileLayerEntry(189, 0, 5, 2)
			SetTileLayerEntry(190, 0, 4, 3)
			SetTileLayerEntry(191, 0, 5, 3)
			
			object.state++
		end if

		SetScreenFade(208, 255, 224, object.timer)
		break

	case ENDINGPOSE_EMERALDSFLASH_B
		if object.timer > 0
			object.timer -= 16
			SetScreenFade(208, 255, 224, object.timer)
		else
			switch stage.playerListPos
			case PLAYER_SONIC_ALONE
			case PLAYER_SONIC_AND_TAILS
				object.frame = 8
				break
				
			case PLAYER_TAILS_ALONE
				object.frame = 20
				break
				
			case PLAYER_KNUX_ALONE
				object.frame = 30
				break
				
			end switch

			object.state++
			currentPlayer = SLOT_PLAYER2
			while currentPlayer < playerCount
				ResetObjectEntity(currentPlayer, TypeName[Ending Pose], 0, player[currentPlayer].xpos, player[currentPlayer].ypos)
				player[currentPlayer].drawOrder = 6
				player[currentPlayer].state = ENDINGPOSE_P2_LOOKAROUND
				player[currentPlayer].frame = 43
				player[currentPlayer].direction = FACING_LEFT
				
				currentPlayer++
			loop
		end if
		break

	case ENDINGPOSE_LOOKAROUND
		switch stage.playerListPos
		case PLAYER_SONIC_ALONE
		case PLAYER_SONIC_AND_TAILS
			object.frame = object.timer
			object.frame /= 6
			object.frame &= 1
			object.frame += 8
			break
			
		case PLAYER_TAILS_ALONE
			object.frame = object.timer
			object.frame /= 10
			object.frame &= 3
			object.frame += 20
			if object.timer >= 40
				object.frame = 23
			end if
			break
			
		case PLAYER_KNUX_ALONE
			object.frame = object.timer
			object.frame /= 10
			object.frame &= 3
			object.frame += 30
			if object.timer >= 40
				object.frame = 34
			end if
			break
			
		end switch

		object.timer++
		if object.timer == 42
			object.timer = 0
			object.state++
		end if
		break

	case ENDINGPOSE_LOOKLEFT
		object.timer++
		if object.timer == 60
			object.timer = 0
			switch stage.playerListPos
			case PLAYER_SONIC_ALONE
			case PLAYER_SONIC_AND_TAILS
				object.frame = 1
				break
				
			case PLAYER_TAILS_ALONE
				object.frame = 14
				break
				
			case PLAYER_KNUX_ALONE
				object.frame = 25
				break
				
			end switch

			object.state = ENDINGPOSE_POSE_START
		end if
		break

	// These remaining three states are for P2 Tails, looking busy while P1 (as a separate Ending Pose Object) gets to do the proper big poses front and center
	
	case ENDINGPOSE_P2_LOOKAROUND
		object.frame = object.timer
		object.frame /= 10
		object.frame &= 3
		object.frame += 20
		if object.timer >= 40
			object.frame = 23
		end if
		
		object.timer++
		if object.timer == 42
			object.timer = 0
			object.state++
		end if
		break

	case ENDINGPOSE_P2_LOOKLEFT
		object.timer++
		if object.timer == 60
			object.timer = 0
			object.frame = 43
			object.state++
		end if
		break

	case ENDINGPOSE_P2_POSE_1
		object.timer++
		if object.timer == 8
			object.frame++
			object.timer = 0
			object.state++
		end if
		break
		
	// ENDINGPOSE_P2_POSE_2 doesn't have a case since it doesn't do anything, its frame is set when exiting ENDINGPOSE_P2_POSE_1
		
	end switch
end event


event ObjectDraw
	if object.tailFrame != 0
		// Tails should have one of his tails sprites in tow
		DrawSprite(object.tailFrame)
	end if
	
	// The main player sprite
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
	
	// P1 should draw the logo, as well
	if object.entityPos == 0
		if object.state > 0
			temp0 = screen.xcenter
			temp0 -= 96
			temp0 -= object.logoPos
			if stage.player2Enabled == true
				if options.region == true
					// "Sonic the Hedgehog and Miles"
					temp1 = 42
				else
					// "Sonic the Hedgehog and Tails"
					temp1 = 41
				end if
			else
				temp1 = 10
				if stage.playerListPos < PLAYER_SONIC_AND_TAILS
					temp1 += stage.playerListPos
				end if
			end if

			DrawSpriteScreenXY(temp1, temp0, 80)
		end if
	end if
end event


event ObjectStartup
	LoadSpriteSheet("Ending/Objects.gif")
	
	// 0 - Sonic Idle Frame
	SpriteFrame(-14, -20, 30, 39, 28, 16)
	
	// 1-3 - Sonic's Final Pose Animation Frames
	SpriteFrame(-16, -16, 32, 35, 3, 220)
	SpriteFrame(-18, -27, 44, 71, 36, 184)
	SpriteFrame(-56, -68, 174, 134, 81, 121)
	
	// 4-9 - Sonic's Good Ending Animation Frames
	SpriteFrame(-10, -21, 24, 40, 3, 16)
	SpriteFrame(-10, -21, 24, 40, 3, 16)
	SpriteFrame(-14, -21, 28, 40, 3, 57)
	SpriteFrame(-16, -20, 29, 39, 3, 98)
	SpriteFrame(-16, -21, 32, 40, 3, 138)
	SpriteFrame(-16, -21, 32, 40, 3, 179)
	
	// 10 - "Sonic the Hedgehog"
	SpriteFrame(-48, 0, 96, 31, 256, 141)
	
	// 11 - "Miles 'Tails' Prower In Sonic the Hedgehog"
	SpriteFrame(-48, -17, 96, 48, 256, 182)
	
	// 12 - "Knuckles the Echidna in Sonic the Hedgehog"
	SpriteFrame(-48, -17, 96, 48, 256, 124)
	
	// 13-15 - Tails's Final Pose Animation Frames
	SpriteFrame(-10, -13, 24, 31, 288, 16)
	SpriteFrame(-19, -12, 33, 31, 254, 16)
	SpriteFrame(-55, -48, 111, 96, 400, 1)
	
	// 16-23 - Tails's Good Ending Animation Frames
	// (The two medium-sized transition frames next to the big sprite are unused)
	SpriteFrame(-12, -16, 27, 34, 313, 16)
	SpriteFrame(-12, -16, 27, 34, 59, 16)
	SpriteFrame(-15, -16, 30, 34, 87, 16)
	SpriteFrame(-15, -14, 31, 32, 118, 16)
	
	SpriteFrame(-12, -16, 25, 34, 150, 16)
	SpriteFrame(-12, -16, 25, 34, 176, 16)
	SpriteFrame(-12, -16, 25, 34, 202, 16)
	SpriteFrame(-12, -16, 25, 34, 228, 16)
	
	// 24-26 - Knuckles's Final Pose Animation Frames
	SpriteFrame(-14, -23, 27, 41, 325, 57)
	SpriteFrame(-16, -21, 28, 39, 296, 57)
	SpriteFrame(-32, -52, 64, 104, 448, 98)
	
	// 27-35 - Knuckles's Good Ending Animation Frames
	// (The two medium-sized transition frames next to the big sprite are unused)
	SpriteFrame(-12, -23, 24, 41, 32, 57)
	SpriteFrame(-16, -23, 28, 41, 57, 57)
	SpriteFrame(-18, -22, 45, 40, 86, 57)
	SpriteFrame(-16, -23, 33, 41, 132, 57)
	SpriteFrame(-16, -23, 33, 41, 166, 57)
	SpriteFrame(-16, -23, 33, 41, 200, 57)
	SpriteFrame(-16, -23, 33, 41, 200, 57)
	SpriteFrame(-16, -23, 27, 41, 268, 57)
	SpriteFrame(-16, -23, 33, 41, 234, 57)
	
	// 36-40 - Tails's Tails' Frames
	// Drawn using object.tailFrame, rather than the base object.frame
	SpriteFrame(-21, -6, 16, 24, 256, 99)
	SpriteFrame(-25, -6, 20, 24, 273, 99)
	SpriteFrame(-25, -6, 20, 24, 294, 99)
	SpriteFrame(-25, -6, 20, 24, 315, 99)
	SpriteFrame(-25, -6, 20, 24, 336, 99)
	
	// 41 - "Sonic the Hedgehog and Tails"
	SpriteFrame(-48, 0, 96, 40, 256, 141)
	
	// 42 - "Sonic the Hedgehog and Miles"
	SpriteFrame(-48, 0, 96, 40, 256, 199)
	
	// 43-44 - P2 Tails Pose Frames
	SpriteFrame(-10, -13, 22, 31, 58, 99)
	SpriteFrame(-20, -17, 36, 35, 44, 131)
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Ending/Objects.gif")
	SpriteFrame(-56, -68, 174, 134, 81, 121)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
