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

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

private alias object.value1 : object.interval

// Player Aliases
private alias object.value17 : debugMode.currentSelection


// ========================
// Function Declarations
// ========================

reserve function ElectricOrb_DebugDraw
reserve function ElectricOrb_DebugSpawn


// ========================
// Static Values
// ========================

private value ElectricOrb_startDebugID = 0


// ========================
// Function Definitions
// ========================

private function ElectricOrb_DebugDraw
	temp0 = object.direction
	object.direction = debugMode.currentSelection
	object.direction -= ElectricOrb_startDebugID
	object.direction--
	object.direction <<= 1
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
	object.direction = temp0
end function


private function ElectricOrb_DebugSpawn
	temp0 = debugMode.currentSelection
	temp0 -= ElectricOrb_startDebugID
	temp0--
	temp0 <<= 1
	CreateTempObject(TypeName[Electric Orb], 0, object.xpos, object.ypos)
	object[tempObjectPos].direction = temp0
	object[tempObjectPos].drawOrder = 4
end function


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

event ObjectUpdate
	switch object.interval
	default
		temp0 = SBZSetup_electricOrbFrame
		// every 24 frames
		break

	case 2
		temp0 = SBZSetup_electricOrbFrame
		temp0 %= 12
		// every 12 frames
		break

	case 4
		temp0 = SBZSetup_electricOrbFrame
		temp0 %= 6
		// every 6 frames
		break

	end switch

	// temp0 = timer
	switch temp0
	case 0
		if SBZSetup_electricOrbTimer == 1
			temp0 = object.ixpos
			temp0 -= camera[0].xpos
			Abs(temp0)
			if temp0 < screen.xcenter
				temp0 = object.iypos
				temp0 -= camera[0].ypos
				Abs(temp0)
				if temp0 < screen.ycenter
					PlaySfx(SfxName[Electric Arc], false)
				end if
			end if
		end if
		break

	case 1
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_LightningHit)
			end if
		next
		break

	case 2
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -36, -8, 36, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_LightningHit)
			end if
		next
		break
		
	case 3
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -36, -8, 36, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_LightningHit)
			end if
		next
		break

	case 4
	case 5
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -64, -8, 64, 8, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				CallFunction(Player_LightningHit)
			end if
		next
		break

	end switch
end event


event ObjectDraw
	switch object.interval
	default
		DrawSpriteFX(SBZSetup_electricOrbFrame, FX_FLIP, object.xpos, object.ypos)
		break

	case 2
		temp0 = SBZSetup_electricOrbFrame
		temp0 %= 12
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
		
	case 4
		temp0 = SBZSetup_electricOrbFrame
		temp0 %= 6
		DrawSpriteFX(temp0, FX_FLIP, object.xpos, object.ypos)
		break
		
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")

	// Orb frames
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 75, 479)
	SpriteFrame(-36, -10, 76, 34, 92, 477)
	SpriteFrame(-36, -10, 76, 34, 169, 477)
	SpriteFrame(-64, -10, 132, 34, 246, 477)
	SpriteFrame(-64, -10, 132, 34, 379, 477)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	SpriteFrame(-8, -8, 16, 32, 58, 479)

	foreach (TypeName[Electric Orb], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].interval = object[arrayPos0].propertyValue
		object[arrayPos0].interval >>= 1
		object[arrayPos0].interval &= 3
		object[arrayPos0].interval <<= 1
		object[arrayPos0].propertyValue &= 1
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].direction = FLIP_Y
		else
			object[arrayPos0].direction = FACING_RIGHT
		end if
		object[arrayPos0].drawOrder = 4
	next

	temp0 = 0
	ElectricOrb_startDebugID = DebugMode_ObjCount
	ElectricOrb_startDebugID--
	while temp0 < 2
		SetTableValue(TypeName[Electric Orb], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(ElectricOrb_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(ElectricOrb_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // direction
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		case 1 // interval
			checkResult = object.propertyValue
			checkResult >>= 1
			checkResult &= 3
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // direction
			temp0 = editor.variableValue
			temp0 &= 1

			object.propertyValue &= 0xFE
			object.propertyValue |= temp0
			break
			
		case 1 // interval
			temp0 = editor.variableValue
			temp0 &= 3

			object.propertyValue &= 0xF1
			object.propertyValue |= temp0
			break
			
		end switch
	end if
end event


event RSDKDraw
	GetBit(object.direction, object.propertyValue, 0)
	object.direction <<= 1
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 32, 58, 479)
	
	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Up", 0)
	AddEnumVariable("Down", 1)
	
	AddEditorVariable("interval")
	SetActiveVariable("interval")
	AddEnumVariable("Every 24 Frames", 0)
	AddEnumVariable("Every 12 Frames", 1)
	AddEnumVariable("Every 6 Frames", 2)
end event
