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

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

private alias object.value0 : object.targetRing

// TODO: primitive names
private alias 0 : WATCHLOSERING_AWAITHURT
private alias 1 : WATCHLOSERING_FINDRING
private alias 2 : WATCHLOSERING_WATCHRING

// Player Aliases
private alias object.state : player.state


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

event ObjectUpdate
	switch object.state
	case WATCHLOSERING_AWAITHURT
		if player[0].state == Player_State_GotHit
			object.state = WATCHLOSERING_FINDRING
		end if
		break
		
	case WATCHLOSERING_FINDRING
		foreach (TypeName[Lose Ring], arrayPos0, ACTIVE_ENTITIES)
			object.targetRing = arrayPos0
		next
		
		if object.targetRing != 0
			object.state = WATCHLOSERING_WATCHRING
		end if
		break
		
	case WATCHLOSERING_WATCHRING
		arrayPos0 = object.targetRing
		if object[arrayPos0].type == TypeName[Ring Sparkle]
			// The Player grabbed the ring, now find the next ring
			object.targetRing = 0
			object.state = WATCHLOSERING_FINDRING
		else
			if object[arrayPos0].type == TypeName[Blank Object]
				// The Player wasn't fast enough and the Ring despawned, kill the Player
				object.targetRing = 0
				object.state = WATCHLOSERING_FINDRING

				game.forceKillPlayer = true
			end if
		end if
		break
		
	end switch
end event


event ObjectStartup
	// Normally I'd say like "Cycle through all WatchLoseRing Objects in the stage and set them to active" but like...
	foreach (TypeName[WatchLoseRing], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_ACTIVE
	end if // ???pardon? an end if??? It's supposed to be a `next` instead...
	
	// As it is now, it seems like this foreach only really finds the first WatchLoseRing Object and then just stops after that...
	// It doesn't matter too much though, as only one (or none) should ever be placed into a scene
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Items.gif")
	SpriteFrame(-8, -8, 16, 16, 1, 1)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
