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

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

private alias object.value0  : object.frameTimer
private alias object.value1  : object.animTimer
private alias object.value18 : object.sortedDrawOrder

// Player aliases
private alias object.state : player.state
private alias object.xpos  : player.xpos
private alias object.ypos  : player.ypos

// Super States
private alias 1 : SUPERSTATE_SUPER


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

private table LightningShield_frameTable
	0, 1, 2, 3, 4, 3, 2, 1, 0, 5, 6, 7
end table

private table LightningShield_planeTable
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1
	1, 1, 1, 1, 1, 0, 0, 0
end table

private table LightningShield_directionTable
	0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1
	0, 0, 0, 0, 0, 0, 0, 0
end table

private table LightningShield_durationTable
	4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2
end table


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

event ObjectUpdate
	// Bug Details:
	// Unlike the other shields which set its draw order to -1, this one sets it to 7
	// This would act the same way... if your name isn't Origins and you didn't make draw order 7 an actual thing
	// Otherwise it'll end up drawing in front of every tile regardless of the player's draw order
	object.drawOrder = 7

	// Unload if the player shouldn't have a shield
	if player[-playerCount].state == Player_State_Death
		object.type = TypeName[Blank Object]
	end if
	if player[-playerCount].state == Player_State_Drown
		object.type = TypeName[Blank Object]
	end if

	// Update animation
	object.animationTimer--
	if object.animationTimer <= 0
	
		object.frameTimer++
		if object.frameTimer >= 12
			object.frameTimer = 0
		end if

		object.animTimer++
		if object.animTimer >= 24
			object.animTimer = 0
		end if

		GetTableValue(object.animationTimer, object.frameTimer, LightningShield_durationTable)
		GetTableValue(object.sortedDrawOrder, object.animTimer, LightningShield_planeTable)
		GetTableValue(object.direction, object.animTimer, LightningShield_directionTable)

	end if

	GetTableValue(object.frame, object.frameTimer, LightningShield_frameTable)
end event


event ObjectDraw
	if Player_superState != SUPERSTATE_SUPER
		DrawSpriteFX(object.frame, FX_FLIP, player[-playerCount].xpos, player[-playerCount].ypos)
	end if
end event


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

	// Sprite frames
	SpriteFrame(-8, -24, 31, 48, 191, 1)
	SpriteFrame(-1, -24, 24, 48, 223, 1)
	SpriteFrame(-8, -24, 31, 48, 174, 52)
	SpriteFrame(-8, -24, 32, 48, 206, 52)
	SpriteFrame(-8, -24, 16, 48, 239, 52)
	SpriteFrame(-24, -24, 48, 48, 1, 83)
	SpriteFrame(-24, -24, 48, 48, 50, 91)
	SpriteFrame(-24, -24, 48, 48, 99, 91)
	SpriteFrame(0, 0, 1, 1, 1, 1) // Empty frame
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
