// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Lose Ring 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.targetPlayer

private alias 0 : LOSERING_TYPE_HIGH
private alias 1 : LOSERING_TYPE_LOW

// Player aliases
private alias object.state  : player.state
private alias object.value0 : player.rings

// Game Mode Aliases
private alias 2 : MODE_TIMEATTACK

// Music Events
private alias 25 : SLOT_MUSICEVENT_CHANGE

private alias 2 : MUSICEVENT_TRANSITION

// Achievement Aliases
private alias 4 : ACHIEVEMENT_RINGKING

// Path ID Aliases
private alias 0 : PATH_A


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

event ObjectUpdate
	object.yvel += 0x1800
	object.xpos += object.xvel
	object.ypos += object.yvel

	if object.yvel >= 0
		ObjectTileCollision(CSIDE_FLOOR, 0, 8, PATH_A)
		if checkResult == true
			temp0 = object.yvel
			temp0 >>= 2
			object.yvel -= temp0
			FlipSign(object.yvel)
			if object.yvel > -0x10000
				object.yvel = -0x10000
			end if
		end if
	end if

	object.timer++
	if object.timer == 256
		object.type = TypeName[Blank Object]
		object.xvel = 0
		object.yvel = 0
		object.timer = 0
	else
		object.animationTimer += object.animationSpeed
		if object.animationTimer >= 0x100
			object.animationTimer -= 0x100
			object.frame++
			if object.frame == 8
				object.frame = 0
				if object.animationSpeed > 16
					object.animationSpeed -= 16
				end if
			end if
		end if

		if object.timer >= 240
			object.alpha -= 16
		end if
	end if

	if object.timer >= 64
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(player[currentPlayer].state, Player_State_Hurt)
			temp0 = checkResult
			CheckEqual(player[currentPlayer].state, Player_State_GotHit)
			temp0 |= checkResult
			if temp0 == false
				BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					object.type = TypeName[Ring Sparkle]
					if object.propertyValue == LOSERING_TYPE_HIGH
						object.drawOrder = 4
					else
						object.drawOrder = 2
					end if
					object.timer = 0
					object.targetPlayer = 0
					object.timer = 0
					object.frame = 0
					player[0].rings++
					if player[0].rings > 999
						// What the...?
						// this is meant to be "player[0].rings" right...? we dont need to set this obj's value0 to 999...
						player.rings = 999
					end if

					if player[0].rings >= ringExtraLife
						if options.gameMode != MODE_TIMEATTACK
#platform: USE_ORIGINS
							if game.coinMode == false
								player.lives++
							else
								CallNativeFunction2(NotifyCallback, NOTIFY_ADD_COIN, 1)
							end if
#endplatform
#platform: USE_STANDALONE
							player.lives++
#endplatform
							PlaySfx(SfxName[Life], false)
							PauseMusic()
							ResetObjectEntity(SLOT_MUSICEVENT_CHANGE, TypeName[Music Event], MUSICEVENT_TRANSITION, 0, 0)
							object[SLOT_MUSICEVENT_CHANGE].priority = PRIORITY_ACTIVE
							
							ringExtraLife += 100
							if ringExtraLife > 300
								ringExtraLife = 1000
							end if
						end if

						if stage.debugMode == false
							if player[0].rings >= 200
								// Grant the "Ring King" Achievement
								CallNativeFunction2(SetAchievement, ACHIEVEMENT_RINGKING, 100)
							end if
						end if
					end if

					if ringPan == 0
						PlaySfx(SfxName[Ring L], false)
						SetSfxAttributes(SfxName[Ring L], -1, -100)
						ringPan = 1
					else
						PlaySfx(SfxName[Ring R], false)
						SetSfxAttributes(SfxName[Ring R], -1, 100)
						ringPan = 0
					end if
				end if
			end if
		next
	end if
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_INK, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("Global/Items.gif")

	// Ring Frames, they're the same as the standard Ring object sprites
	SpriteFrame(-8, -8, 16, 16, 1, 1)
	SpriteFrame(-8, -8, 16, 16, 1, 18)
	SpriteFrame(-8, -8, 16, 16, 1, 35)
	SpriteFrame(-8, -8, 16, 16, 1, 52)
	SpriteFrame(-8, -8, 16, 16, 1, 69)
	SpriteFrame(-8, -8, 16, 16, 1, 86)
	SpriteFrame(-8, -8, 16, 16, 1, 103)
	SpriteFrame(-8, -8, 16, 16, 1, 120)
end event

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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Items.gif")
	SpriteFrame(-8, -8, 16, 16, 1, 1)
	
	// Although used by the object, it shouldn't be set here
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
