// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: Plasma Ball 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.targetPos.x
private alias object.value2 : object.animTimerMax
private alias object.value3 : object.animSpeed
private alias object.value4 : object.frameTable

private alias 0 : PLASMABALL_SETUP
private alias 1 : PLASMABALL_MOVETOPOS
private alias 2 : PLASMABALL_CHARGING
private alias 3 : PLASMABALL_ACTIVE

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


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

private table PlasmaBall_chargingFrames
	0, 8, 1, 9, 6, 7, 0, 8, 1, 9, 6, 7, 2, 3, 4, 5
end table

private table PlasmaBall_attackingFrames
	6, 1, 7, 1
end table


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

event ObjectUpdate
	switch object.state
	case PLASMABALL_SETUP
		object.frameTable = PlasmaBall_chargingFrames
		object.animTimerMax = 63
		object.animSpeed = 2
		object.xvel = object.targetPos.x
		object.xvel -= object.xpos
		object.xvel >>= 4
		object.state++
		break

	case PLASMABALL_MOVETOPOS
		if object.alpha < 192
			object.alpha += 16
		end if

		object.xpos += object.xvel
		if object.xpos <= object.targetPos.x
			object.timer = 180
			object.state++
		end if
		break

	case PLASMABALL_CHARGING
		object.timer--
		if object.timer <= 0
			object.animationTimer = 0
			object.alpha = 128
			object.frameTable = PlasmaBall_attackingFrames
			object.animTimerMax = 7
			object.animSpeed = 1
			object.xvel = player[0].xpos
			object.xvel -= object.xpos
			object.xvel >>= 8
			object.yvel = 0x14000
			object.timer = 133
			object.state++
		end if
		break

	case PLASMABALL_ACTIVE
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.timer--
		if object.timer <= 0
			object.type = TypeName[Blank Object]
		end if
		break
		
	end switch

	object.animationTimer++
	object.animationTimer &= object.animTimerMax
	temp0 = object.animationTimer
	temp0 >>= object.animSpeed
	GetTableValue(object.frame, temp0, object.frameTable)

	if object.frameTable == PlasmaBall_attackingFrames
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -12, -12, 12, 12, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_LightningHit)
			end if
		next
	end if
end event


event ObjectDraw
	if object.frameTable == PlasmaBall_attackingFrames
		DrawSpriteXY(object.frame, object.xpos, object.ypos)
		DrawSpriteFX(5, FX_INK, object.xpos, object.ypos)
	else
		DrawSpriteFX(object.frame, FX_INK, object.xpos, object.ypos)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 2, 397)
	SpriteFrame(-12, -12, 24, 24, 35, 397)
	SpriteFrame(-5, -5, 10, 10, 60, 397)
	SpriteFrame(-7, -7, 14, 14, 71, 397)
	SpriteFrame(-8, -8, 16, 16, 86, 397)
	SpriteFrame(-12, -12, 24, 24, 103, 397)
	SpriteFrame(-7, -7, 14, 15, 128, 397)
	SpriteFrame(-7, -7, 14, 15, 145, 397)
	SpriteFrame(-16, -16, 32, 32, 2, 430)
	SpriteFrame(-12, -12, 24, 24, 35, 430)

	foreach (TypeName[Plasma Ball], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].inkEffect = INK_ADD
		object[arrayPos0].alpha = 0x80
	next
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 2, 397)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
