// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: DER Target 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.sfxTimer
private alias object.value2 : object.sfxDelay // Gradually decreases in order to make the SFX play more and more often
private alias object.value3 : object.reticleFrame

// States
private alias 0 : DERTARGET_INIT
private alias 1 : DERTARGET_TRACKING
private alias 2 : DERTARGET_TARGETTED // No longer moving, this is where the DER's gonna land

// Player aliases
private alias 0 : SLOT_PLAYER1
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos


// ========================
// Tables
// ========================

private table DERTarget_xpos_backups
	0, 0, 0, 0
end table

private table DERTarget_ypos_backups
	0, 0, 0, 0
end table


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

event ObjectUpdate
	switch object.state
	case DERTARGET_INIT
		object.timer = 160
		object.sfxDelay = 24
		object.sfxTimer = 24

		object.xpos = player[SLOT_PLAYER1].xpos
		object.ypos = player[SLOT_PLAYER1].ypos

		// Set all the table values to just be P1's pos right now
		SetTableValue(player[SLOT_PLAYER1].xpos, 0, DERTarget_xpos_backups)
		SetTableValue(player[SLOT_PLAYER1].ypos, 0, DERTarget_ypos_backups)
		SetTableValue(player[SLOT_PLAYER1].xpos, 1, DERTarget_xpos_backups)
		SetTableValue(player[SLOT_PLAYER1].ypos, 1, DERTarget_ypos_backups)
		SetTableValue(player[SLOT_PLAYER1].xpos, 2, DERTarget_xpos_backups)
		SetTableValue(player[SLOT_PLAYER1].ypos, 2, DERTarget_ypos_backups)
		SetTableValue(player[SLOT_PLAYER1].xpos, 3, DERTarget_xpos_backups)
		SetTableValue(player[SLOT_PLAYER1].ypos, 3, DERTarget_ypos_backups)

		object.drawOrder = 5
		object.priority = PRIORITY_ACTIVE
		object.state = DERTARGET_TRACKING
		break

	case DERTARGET_TRACKING
		object.timer--
		if object.timer < 0
			object.state++
			object.timer = 64
			object.sfxTimer = 4

			object.xpos = player[SLOT_PLAYER1].xpos
			object.ypos = player[SLOT_PLAYER1].ypos
		else
			// In order to make the target have a delay when following the player,
			// there's this whole table to store old player pos's

			// Make this target's position be where the player was 3 cycles ago
			GetTableValue(object.xpos, 3, DERTarget_xpos_backups)
			GetTableValue(object.ypos, 3, DERTarget_ypos_backups)

			// Shift all the values by 1 around the table
			GetTableValue(temp0, 2, DERTarget_xpos_backups)
			GetTableValue(temp1, 2, DERTarget_ypos_backups)
			SetTableValue(temp0, 3, DERTarget_xpos_backups)
			SetTableValue(temp1, 3, DERTarget_ypos_backups)
			GetTableValue(temp0, 1, DERTarget_xpos_backups)
			GetTableValue(temp1, 1, DERTarget_ypos_backups)
			SetTableValue(temp0, 2, DERTarget_xpos_backups)
			SetTableValue(temp1, 2, DERTarget_ypos_backups)
			GetTableValue(temp0, 0, DERTarget_xpos_backups)
			GetTableValue(temp1, 0, DERTarget_ypos_backups)
			SetTableValue(temp0, 1, DERTarget_xpos_backups)
			SetTableValue(temp1, 1, DERTarget_ypos_backups)

			// And then put the _actual_ player position at the front of the table
			SetTableValue(player[SLOT_PLAYER1].xpos, 0, DERTarget_xpos_backups)
			SetTableValue(player[SLOT_PLAYER1].ypos, 0, DERTarget_ypos_backups)
		end if

		object.sfxTimer--
		if object.sfxTimer < 0
			object.sfxDelay--
			object.sfxTimer = object.sfxDelay
			PlaySfx(SfxName[Beep], false)
		end if
		break

	case DERTARGET_TARGETTED
		object.timer--
		if object.timer < 0
			// Object [-6] is the Death Egg Robot, make it land where its target shows
			object[-6].timer = 1
			object[-6].xpos = object.xpos

			// Reset this object, not needed anymore
			object.state = 0
			object.type = TypeName[Blank Object]
		else
			object.sfxTimer--

			if object.sfxTimer < 0
				object.sfxTimer = 4
				PlaySfx(SfxName[Beep], false)
			end if
		end if
		break

	end switch

	// Update the object's animation
	object.animationTimer++
	if object.animationTimer == 4
		object.animationTimer = 0
		object.frame++
		if object.frame >= 5
			object.frame = 0
		end if
		object.reticleFrame ^= 1
	end if
end event


event ObjectDraw
	DrawSprite(object.frame)

	if object.state == 2
		// Draw the inner reticle
		temp0 = object.reticleFrame
		temp0 += 5
		DrawSprite(temp0)
	end if
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone12")
	if checkResult == true
		LoadSpriteSheet("DEZ/Objects.gif")

		// 0-4 - Target Frames
		SpriteFrame(-53, -53, 106, 106, 249, 149)
		SpriteFrame(-45, -45, 90, 90, 158, 165)
		SpriteFrame(-37, -37, 74, 74, 82, 181)
		SpriteFrame(-29, -29, 58, 58, 23, 197)
		SpriteFrame(-21, -21, 42, 42, 356, 213)
		
		// 5-6 - Inner Reticle Frames
		SpriteFrame(-8, -8, 16, 16, 356, 196)
		SpriteFrame(-8, -8, 16, 16, 373, 196)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		
		// 0-4 - Target Frames
		SpriteFrame(-53, -53, 106, 106, 761, 149)
		SpriteFrame(-45, -45, 90, 90, 670, 165)
		SpriteFrame(-37, -37, 74, 74, 594, 181)
		SpriteFrame(-29, -29, 58, 58, 535, 197)
		SpriteFrame(-21, -21, 42, 42, 868, 213)
		
		// 5-6 - Inner Reticle Frames
		SpriteFrame(-8, -8, 16, 16, 868, 196)
		SpriteFrame(-8, -8, 16, 16, 885, 196)
	end if
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone12")
	if checkResult == true
		LoadSpriteSheet("DEZ/Objects.gif")
		SpriteFrame(-21, -21, 42, 42, 356, 213)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-21, -21, 42, 42, 868, 213)
	end if

	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
