// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Invincibility 2 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 Invincibility2_frameTable4
	7, 4, 6, 4, 4, 6, 4, 7, 4, 6, 6, 4
end table

private table Invincibility2_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 Invincibility2_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 Invincibility2_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, Invincibility2_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, Invincibility2_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, Invincibility2_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, Invincibility2_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, Invincibility2_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, Invincibility2_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, Invincibility2_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, 18, 126)
	SpriteFrame(0, 0, 1, 1, 19, 126)
	SpriteFrame(-1, -1, 3, 3, 18, 125)
	SpriteFrame(-2, -2, 5, 5, 18, 129)
	SpriteFrame(-3, -3, 7, 7, 18, 135)
	SpriteFrame(-4, -4, 9, 9, 18, 143)
	SpriteFrame(-7, -7, 15, 15, 18, 153)
	SpriteFrame(-15, -15, 31, 31, 18, 169)
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
