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

// This Object is used in Mission "Super Sonic Finish" - M045 - Mission_Zone09
// This Object is also used in its follow-up DLC Mission, "Extreme! Super Sonic Finish" - M104 - DLC_Zone06
// It's to be paired with the SetRingCount object, or else Sonic will detransform immediately since he has no rings and all

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

// States
private alias 0 : SUPERSONIC_TRANSFORM
private alias 1 : SUPERSONIC_NOSUPERCHECK
private alias 2 : SUPERSONIC_STATIC
private alias 3 : SUPERSONIC_PLAYMUSIC
private alias 4 : SUPERSONIC_FACERIGHT

// Player Aliases
private alias object.type		: player.type
private alias object.state		: player.state
private alias object.visible	: player.visible
private alias object.xpos		: player.xpos
private alias object.ypos		: player.ypos
private alias object.direction	: player.direction
private alias object.value1		: player.timer
private alias object.value7		: player.invincibleTimer
private alias object.value8		: player.blinkTimer
private alias object.value34	: player.collisionDisabled

// Super State Aliases
private alias 0 : SUPERSTATE_NONE
private alias 1 : SUPERSTATE_SUPER
private alias 2 : SUPERSTATE_FADEOUT
private alias 3 : SUPERSTATE_END

// Track Aliases
private alias 7 : TRACK_SUPER


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

event ObjectUpdate
	switch object.state
	case SUPERSONIC_TRANSFORM
		currentPlayer = 0
		object.type = player[0].type // Set ourselves to Player Object for the function we're about to call
		CallFunction(Player_TryTransform)
		object.type = TypeName[SuperSonic] // We're done pretending to be Sonic, set ourselves back
		object.state = SUPERSONIC_NOSUPERCHECK
		break
		
	case SUPERSONIC_NOSUPERCHECK
		if Player_superState == SUPERSTATE_NONE
			if stage.timeEnabled == true
				// You turned back into regular Sonic. Test failed.
				game.forceKillPlayer = true
				object.state = SUPERSONIC_STATIC
			end if
		end if
		break
		
	case SUPERSONIC_STATIC
		break
		
	// These following two states are pretty much unused
	case SUPERSONIC_PLAYMUSIC
		PlayMusic(TRACK_SUPER)
		object.state = SUPERSONIC_FACERIGHT
		break
		
	case SUPERSONIC_FACERIGHT // Why is this a separate state???
		player[0].direction = FACING_RIGHT
		object.state = SUPERSONIC_NOSUPERCHECK
		break
		
	end switch
end event


event ObjectStartup
	temp0 = false
	foreach (TypeName[SuperSonic], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		
		if object[arrayPos0].propertyValue != 0
			// Do note - every instance of a SuperSonic object in the game has a property value of 0
			
			player[arrayPos0].state = SUPERSONIC_PLAYMUSIC
			temp0 = true
		end if
	next
	
	if temp0 != false
		// This is another stubbed out/unused method of making the Player start as Super Sonic
		// It doesn't work quite as it should, the version used in ObjectUpdate is used instead
		
		currentPlayer = 0
		arrayPos0 = currentPlayer
		arrayPos0 += playerCount
		ResetObjectEntity(arrayPos0, TypeName[Super Spark], 0, player[0].xpos, player[0].ypos)
		object[arrayPos0].priority = PRIORITY_ACTIVE
		
		Player_superState = SUPERSTATE_SUPER
		player[currentPlayer].invincibleTimer = 60
		player[currentPlayer].blinkTimer = 0
		player[currentPlayer].visible = true
		CallFunction(Player_UpdatePhysicsState)
		
		if stage.playerListPos == PLAYER_SONIC_ALONE
			// This animation file is being loaded to the [SuperSonic] object type, it's supposed to be
			// for the [Player Object] type though
			LoadAnimation("SuperSonic.ani")
		end if
		
		player[currentPlayer].state = Player_State_Air_NoDropDash
		player[currentPlayer].collisionDisabled = true
		player[currentPlayer].timer = 0
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Items.gif")
	SpriteFrame(-24, -24, 48, 48, 207, 149)
	
	// Well technically it does something, but even if it is set, the ObjectUpdate version of the routine will run the right after anyways, so...
	// May as well keep its label as "unused"
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
