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

// This Object is used in several missions where Badniks are to respawn
// It should be paired with one of the Revive[*] Badnik objects as well
// Do note, however, that this Object name's itself varies between zones - sometimes it's Revive Enemy while other times it's ReviveEnemy, note the space (or lack thereof) but it doesn't really matter

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

// To avoid overlap with an object's normal values, this Object's values start descending
private alias object.value47 : object.backupType
private alias object.value46 : object.backupState

private alias object.value45 : object.timeCheckpoint

// A pair of other backup values for the object, only used by Caterkillers it seems
private alias object.value44 : object.backupvalueA
private alias object.value43 : object.backupvalueB

// And then this value starts all the way at the beginning, unlike the others
// An object's timer doesn't really need to be preserved between respawns though, so I suppose overlapping here is fine
private alias object.value0  : object.target

// States
private alias 0: REVIVE_CHECKENEMY
private alias 1: REVIVE_STORETIME
private alias 2: REVIVE_IDLE
private alias 3: REVIVE_REVIVEENEMY


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

reserve function ReviveEnemy_CheckOutOfRange
reserve function ReviveEnemy_GetTotalTime


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

private function ReviveEnemy_CheckOutOfRange
	// This function is called to see if the current Object is out of range, and safe to be restored
	// It returns the result in checkResult
	
	temp1 = object.ixpos
	temp1 -= screen.xoffset
	CheckLower(temp1, 0)
	temp0 = checkResult
	
	CheckGreater(temp1, screen.xsize)
	temp0 |= checkResult
	
	temp1 = object.iypos
	temp1 -= screen.yoffset
	CheckLower(temp1, 0)
	temp0 |= checkResult
	
	CheckGreater(temp1, screen.ysize)
	checkResult |= temp0
end function


private function ReviveEnemy_GetTotalTime
	// This function gets the current total time, in seconds
	// Return value is temp0
	
	temp0 = stage.minutes
	temp0 *= 60
	temp0 += stage.seconds
end function


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

event ObjectUpdate
	switch object.state
	case REVIVE_CHECKENEMY
		// Focus on the badnik that spawned this object
		arrayPos0 = object.target
		
		if object[arrayPos0].type == TypeName[Blank Object] // See if the badnik's been destroyed
			// If it is, then turn it into a Revive Enemy Object and copy loads of values
			
			object[arrayPos0].type = TypeName[Revive Enemy]
			object[arrayPos0].xpos = object.xpos
			object[arrayPos0].ypos = object.ypos
			object[arrayPos0].propertyValue = object.propertyValue
			object[arrayPos0].priority = PRIORITY_ACTIVE
			object[arrayPos0].state = REVIVE_STORETIME
			object[arrayPos0].backupType = object.backupType
			object[arrayPos0].backupState = object.backupState
			object[arrayPos0].backupvalueB = object.backupvalueB
			object[arrayPos0].backupvalueA = object.backupvalueA
		end if
		
		object.type = TypeName[Blank Object]
		break
		
	case REVIVE_STORETIME
		CallFunction(ReviveEnemy_CheckOutOfRange)
		if checkResult == true
			// Store the current time
			CallFunction(ReviveEnemy_GetTotalTime)
			object.timeCheckpoint = temp0
			
			// Onwards!
			object.state = REVIVE_IDLE
			object.priority = PRIORITY_BOUNDS
		end if
		break
		
	case REVIVE_IDLE
		// See if it's been 20 seconds since this Enemy was defeated
		CallFunction(ReviveEnemy_GetTotalTime)
		temp0 -= object.timeCheckpoint
		if temp0 >= 20
			// If it has, then try to restore
			
			object.state = REVIVE_REVIVEENEMY
		end if
		break
		
	case REVIVE_REVIVEENEMY
		// It is worth noting, the Object still has default priority when calling this function
		
		CallFunction(ReviveEnemy_CheckOutOfRange)
		if checkResult == true
			// Now, we can restore this Badnik to what it used to be
			// We're doing it via object.type rather than ResetObjectEntity because we want to preserve this Object's values
			
			object.type = object.backupType
			object.state = object.backupState
		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)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
