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

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

private alias object.value0  : object.timer
private alias object.value18 : object.sortedDrawOrder

private alias -1 : DRAWORDER_PLAYER

private alias object.state : player.state
private alias object.xpos  : player.xpos
private alias object.ypos  : player.ypos

// Flame shield states
private alias 0 : FIRESHIELD_IDLE_SETUP
private alias 1 : FIRESHIELD_IDLE
private alias 2 : FIRESHIELD_DASH_SETUP
private alias 3 : FIRESHIELD_DASH

// Super States
private alias 1 : SUPERSTATE_SUPER


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

// Passive/idle ver - just staying around the player normally
private table FireShield_idleFrameTable
	0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 6, 7, 4, 5, 2, 3, 0, 1
end table

// Active - During flame dash
private table FireShield_dashFrameTable
	10, 11, 12, 10, 12, 13, 10, 11, 12, 10, 12, 13, 0
end table


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

event ObjectUpdate
	object.drawOrder = DRAWORDER_PLAYER

	// 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

	switch object.state
	case FIRESHIELD_IDLE_SETUP
		object.animationTimer = 0
		object.timer = 0
		object.sortedDrawOrder = 0
		object.direction = FLIP_NONE
		GetTableValue(object.frame, object.timer, FireShield_idleFrameTable)
		object.state++
		// Fall through to below
	case FIRESHIELD_IDLE
		object.animationTimer++

		if object.animationTimer >= 3
			object.animationTimer = 1
			object.timer++
			object.sortedDrawOrder ^= 1

			if object.timer >= 10
				object.direction = FLIP_Y
				if object.timer >= 18
					object.timer = 0
					object.direction = FLIP_NONE
				end if
			end if

			GetTableValue(object.frame, object.timer, FireShield_idleFrameTable)
		end if
		break

	case FIRESHIELD_DASH_SETUP
		object.animationTimer = 0
		object.timer = 0
		object.sortedDrawOrder = 0
		GetTableValue(object.frame, object.timer, FireShield_dashFrameTable)
		object.state++
		// Fall through again
	case FIRESHIELD_DASH
		object.animationTimer++

		if object.animationTimer >= 3

			object.animationTimer = 1
			object.timer++

			if object.timer >= 12
				object.state = FIRESHIELD_IDLE_SETUP
			end if

			GetTableValue(object.frame, object.timer, FireShield_dashFrameTable)
		end if
		break
		
	end switch
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")

	// Flame shield frames
	SpriteFrame(-16, 9, 37, 15, 131, 52)
	SpriteFrame(-20, -24, 39, 15, 131, 68)
	SpriteFrame(-20, 3, 40, 21, 148, 101)
	SpriteFrame(-23, -24, 47, 23, 189, 101)
	SpriteFrame(-24, -9, 48, 29, 148, 125)
	SpriteFrame(-24, -19, 48, 27, 197, 125)
	SpriteFrame(-24, -15, 48, 28, 1, 132)
	SpriteFrame(-24, -21, 48, 34, 50, 140)
	SpriteFrame(-23, -17, 46, 32, 99, 140)
	SpriteFrame(-24, -22, 48, 42, 1, 161)
	SpriteFrame(-4, -21, 20, 43, 64, 175)
	SpriteFrame(-24, -23, 48, 46, 85, 175)
	SpriteFrame(-32, -19, 56, 38, 135, 175)
	SpriteFrame(-40, -24, 63, 47, 192, 166)
	SpriteFrame(0, 0, 1, 1, 1, 1) // (Empty)
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
