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

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

private alias object.state  : object.isSetup

private alias object.value0  : object.starPos0.x
private alias object.value1  : object.starPos1.x
private alias object.value2  : object.starPos2.x
private alias object.value3  : object.starPos3.x
private alias object.value4  : object.starPos4.x
private alias object.value5  : object.starPos5.x
private alias object.value6  : object.starPos6.x
private alias object.value7  : object.starPos7.x

private alias object.value8  : object.starPos0.y
private alias object.value9  : object.starPos1.y
private alias object.value10 : object.starPos2.y
private alias object.value11 : object.starPos3.y
private alias object.value12 : object.starPos4.y
private alias object.value13 : object.starPos5.y
private alias object.value14 : object.starPos6.y
private alias object.value15 : object.starPos7.y

private alias object.value16 : object.starAngle0
private alias object.value17 : object.starAngle1

// value18 is unused

private alias object.value19 : object.starFrame0
private alias object.value20 : object.starFrame1

// Player Aliases
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.direction : player.direction

private alias object.value7 : player.invincibleTimer

private alias -1 : DRAWORDER_PLAYER


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

private table Invincibility_frameTable4
	7, 4, 6, 4, 4, 6, 4, 7, 4, 6, 6, 4
end table

private table Invincibility_frameTable3
	2, 3, 4, 5, 6, 7, 6, 5, 4, 3
	2, 3, 4, 5, 6, 7, 6, 5, 4, 3
end table

private table Invincibility_frameTable2
	1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2
	1, 2, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2
end table

private table Invincibility_frameTable1
	0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1
	0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1
end table


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

event ObjectUpdate
	if object.isSetup == false
		object.starPos7.x = player[-playerCount].xpos
		object.starPos6.x = player[-playerCount].xpos
		object.starPos5.x = player[-playerCount].xpos
		object.starPos4.x = player[-playerCount].xpos
		object.starPos3.x = player[-playerCount].xpos
		object.starPos2.x = player[-playerCount].xpos
		object.starPos1.x = player[-playerCount].xpos
		
		object.starPos0.x = player[-playerCount].xpos
		object.starPos7.y = player[-playerCount].ypos
		object.starPos6.y = player[-playerCount].ypos
		object.starPos5.y = player[-playerCount].ypos
		object.starPos4.y = player[-playerCount].ypos
		object.starPos3.y = player[-playerCount].ypos
		object.starPos2.y = player[-playerCount].ypos
		object.starPos1.y = player[-playerCount].ypos
		object.starPos0.y = player[-playerCount].ypos

		object.starAngle0 = 180
		object.starAngle1 = 0
		object.isSetup = true
		object.inkEffect = INK_ALPHA
		object.alpha = 255
		object.drawOrder = DRAWORDER_PLAYER
	end if
	
	object.starFrame0++
	object.starFrame0 %= 12
	object.starFrame1++
	object.starFrame1 %= 10

	object.starPos7.x = object.starPos6.x
	object.starPos6.x = object.starPos5.x
	object.starPos5.x = object.starPos4.x
	object.starPos4.x = object.starPos3.x
	object.starPos3.x = object.starPos2.x
	object.starPos2.x = object.starPos1.x
	object.starPos1.x = object.starPos0.x
	object.starPos0.x = player[-playerCount].xpos

	object.starPos7.y = object.starPos6.y
	object.starPos6.y = object.starPos5.y
	object.starPos5.y = object.starPos4.y
	object.starPos4.y = object.starPos3.y
	object.starPos3.y = object.starPos2.y
	object.starPos2.y = object.starPos1.y
	object.starPos1.y = object.starPos0.y
	object.starPos0.y = player[-playerCount].ypos
	
	// Make the sparkles fly in the Player's direction
	if player[-playerCount].direction == FACING_RIGHT
		object.starAngle0 += 144
		object.starAngle0 &= 0x1FF
		object.starAngle1 += 16
		object.starAngle1 &= 0x1FF
	else
		object.starAngle0 -= 144
		object.starAngle0 &= 0x1FF
		object.starAngle1 -= 16
		object.starAngle1 &= 0x1FF
	end if

	if player[-playerCount].invincibleTimer < 32
		object.alpha = player[-playerCount].invincibleTimer
		object.alpha <<= 3
	end if
end event


event ObjectDraw
	temp2 = object.starAngle1
	temp2 += 116
	Cos(temp0, temp2)
	Sin(temp1, temp2)
	temp0 <<= 11
	temp1 <<= 11
	temp0 += object.starPos7.x
	temp1 += object.starPos7.y
	GetTableValue(object.frame, object.starFrame0, Invincibility_frameTable1)
	DrawSpriteFX(object.frame, FX_INK, temp0, temp1)

	temp2 = object.starAngle1
	temp2 += 372
	Cos(temp0, temp2)
	Sin(temp1, temp2)
	temp0 <<= 11
	temp1 <<= 11
	temp0 += object.starPos7.x
	temp1 += object.starPos7.y
	temp2 = object.starFrame0
	temp2 += 6
	GetTableValue(object.frame, temp2, Invincibility_frameTable1)
	DrawSpriteFX(object.frame, FX_INK, temp0, temp1)

	Cos(temp0, object.starAngle1)
	Sin(temp1, object.starAngle1)
	temp0 <<= 11
	temp1 <<= 11
	temp0 += object.starPos5.x
	temp1 += object.starPos5.y
	GetTableValue(object.frame, object.starFrame0, Invincibility_frameTable2)
	DrawSpriteFX(object.frame, FX_INK, temp0, temp1)

	temp2 = object.starAngle1
	temp2 += 256
	Cos(temp0, temp2)
	Sin(temp1, temp2)
	temp0 <<= 11
	temp1 <<= 11
	temp0 += object.starPos5.x
	temp1 += object.starPos5.y
	temp2 = object.starFrame0
	temp2 += 6
	GetTableValue(object.frame, temp2, Invincibility_frameTable2)
	DrawSpriteFX(object.frame, FX_INK, temp0, temp1)

	temp2 = object.starAngle1
	temp2 += 168
	Cos(temp0, temp2)
	Sin(temp1, temp2)
	temp0 <<= 11
	temp1 <<= 11
	temp0 += object.starPos3.x
	temp1 += object.starPos3.y
	GetTableValue(object.frame, object.starFrame1, Invincibility_frameTable3)
	DrawSpriteFX(object.frame, FX_INK, temp0, temp1)

	temp2 = object.starAngle1
	temp2 += 424
	Cos(temp0, temp2)
	Sin(temp1, temp2)
	temp0 <<= 11
	temp1 <<= 11
	temp0 += object.starPos3.x
	temp1 += object.starPos3.y
	temp2 = object.starFrame1
	temp2 += 5
	GetTableValue(object.frame, temp2, Invincibility_frameTable3)
	DrawSpriteFX(object.frame, FX_INK, temp0, temp1)

	Cos(temp0, object.starAngle0)
	Sin(temp1, object.starAngle0)
	temp0 <<= 11
	temp1 <<= 11
	temp0 += player[-playerCount].xpos
	temp1 += player[-playerCount].ypos
	GetTableValue(object.frame, object.starFrame0, Invincibility_frameTable4)
	DrawSpriteFX(object.frame, FX_INK, temp0, temp1)

	temp2 = object.starAngle0
	temp2 += 256
	Cos(temp0, temp2)
	Sin(temp1, temp2)
	temp0 <<= 11
	temp1 <<= 11
	temp0 += player[-playerCount].xpos
	temp1 += player[-playerCount].ypos
	DrawSpriteFX(object.frame, FX_INK, temp0, temp1)
end event


event ObjectStartup
	LoadSpriteSheet("Global/Items.gif")
	
	SpriteFrame(0, 0, 1, 1, 232, 42)
	SpriteFrame(0, 0, 1, 1, 233, 42)
	SpriteFrame(-1, -1, 3, 3, 232, 41)
	SpriteFrame(-2, -2, 5, 5, 232, 35)
	SpriteFrame(-3, -3, 7, 7, 232, 27)
	SpriteFrame(-4, -4, 9, 9, 232, 17)
	SpriteFrame(-7, -7, 15, 15, 232, 1)
	SpriteFrame(-15, -15, 31, 31, 200, 1)
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Items.gif")
	SpriteFrame(0, 0, 1, 1, 232, 42)
	
	// It's set by the player object upon death, but it never seems to get used otherwise
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
