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

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

private alias object.value0 : object.amplitude.x
private alias object.value1 : object.amplitude.y

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


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

reserve function HVSpikeBall_DebugDraw
reserve function HVSpikeBall_DebugSpawn

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

private value HVSpikeBall_startDebugID = 0


private function HVSpikeBall_DebugDraw
	DrawSprite(0)
end function


private function HVSpikeBall_DebugSpawn
	CreateTempObject(TypeName[HV Spike Ball], object.direction, object.xpos, object.ypos)
	temp0 = debugMode[0].currentSelection
	temp0 -= HVSpikeBall_startDebugID

	if temp0 == 0
		if object.direction == FLIP_NONE
			object[tempObjectPos].amplitude.x = 0x1800
			object[tempObjectPos].amplitude.y = 0
			object[tempObjectPos].xpos -= 0x300000
		else
			object[tempObjectPos].amplitude.x = -0x1800
			object[tempObjectPos].amplitude.y = 0
			object[tempObjectPos].xpos -= 0x300000
		end if
	else
		if object.direction == FLIP_NONE
			object[tempObjectPos].amplitude.x = 0
			object[tempObjectPos].amplitude.y = 0x1800
			object[tempObjectPos].ypos -= 0x300000
		else
			object[tempObjectPos].amplitude.x = 0
			object[tempObjectPos].amplitude.y = -0x1800
			object[tempObjectPos].ypos -= 0x500000
		end if
	end if
end function


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

event ObjectUpdate
	object.angle = SYZSetup_oscillation
	object.angle *= 0x200
	object.angle /= 356
	
	temp0 = object.xpos
	temp1 = object.ypos

	Cos(object.xpos, object.angle)
	object.xpos *= object.amplitude.x
	object.xpos += temp0

	Sin(object.ypos, object.angle)
	object.ypos *= object.amplitude.y
	object.ypos += temp1

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		if checkResult == true
			CallFunction(Player_Hit)
		end if
	next

	object.xpos = temp0
	object.ypos = temp1
end event


event ObjectDraw
	Cos(temp0, object.angle)
	temp0 *= object.amplitude.x
	temp0 += object.xpos

	Sin(temp1, object.angle)
	temp1 *= object.amplitude.y
	temp1 += object.ypos

	DrawSpriteXY(0, temp0, temp1)
end event


event ObjectStartup
	LoadSpriteSheet("SYZ/Objects.gif")
	SpriteFrame(-24, -24, 48, 48, 61, 178)

	foreach (TypeName[HV Spike Ball], arrayPos0, ALL_ENTITIES)
		switch object[arrayPos0].propertyValue
		case 0
			object[arrayPos0].amplitude.x = 0x1800
			object[arrayPos0].amplitude.y = 0
			object[arrayPos0].xpos -= 0x300000
			break

		case 1
			object[arrayPos0].amplitude.x = -0x1800
			object[arrayPos0].amplitude.y = 0
			object[arrayPos0].xpos -= 0x300000
			break

		case 2
			object[arrayPos0].amplitude.x = 0
			object[arrayPos0].amplitude.y = 0x1800
			object[arrayPos0].ypos -= 0x300000
			break
			
		case 3
			object[arrayPos0].amplitude.x = 0
			object[arrayPos0].amplitude.y = -0x1800
			object[arrayPos0].ypos -= 0x500000
			break
			
		end switch
	next

	temp0 = 0
	HVSpikeBall_startDebugID = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[HV Spike Ball], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(HVSpikeBall_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(HVSpikeBall_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 // type
			checkResult = object.propertyValue
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // type
			object.propertyValue = editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)

	if editor.showGizmos == true
		editor.drawingOverlay = true

		temp6 = object.xpos
		temp7 = object.ypos

		switch object.propertyValue
		case 0
			object.amplitude.x = 0x1800
			object.amplitude.y = 0
			object.xpos -= 0x300000
			object.angle = 0x180
			break
			
		case 1
			object.amplitude.x = -0x1800
			object.amplitude.y = 0
			object.xpos -= 0x300000
			object.angle = 0x80
			break
			
		case 2
			object.amplitude.x = 0
			object.amplitude.y = 0x1800
			object.ypos -= 0x300000
			object.angle = 0x180
			break
			
		case 3
			object.amplitude.x = 0
			object.amplitude.y = -0x1800
			object.ypos -= 0x500000
			object.angle = 0x80
			break
			
		end switch

		arrayPos0 = 0
		object.angle = 0x80
		while arrayPos0 < 2
			Cos(temp0, object.angle)
			temp0 *= object.amplitude.x
			temp0 += object.xpos

			Sin(temp1, object.angle)
			temp1 *= object.amplitude.y
			temp1 += object.ypos

			object.inkEffect = INK_BLEND
			DrawSpriteFX(0, FX_INK, temp0, temp1)

			object.angle += 0x100
			arrayPos0++
		loop

		object.xpos = temp6
		object.ypos = temp7
		
		editor.drawingOverlay = false
	end if
end event


event RSDKLoad
	LoadSpriteSheet("SYZ/Objects.gif")
	SpriteFrame(-24, -24, 48, 48, 61, 178)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Right, Then Left", 0)
	AddEnumVariable("Left, Then Right", 1)
	AddEnumVariable("Down, Then Up", 2)
	AddEnumVariable("Up, Then Down", 3)
end event
