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

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

private alias object.value0 : object.spinAngle
private alias object.value1 : object.spinSpeed
private alias object.value2 : object.length
private alias object.value3 : object.amplitude
private alias object.value4 : object.drawPos.x
private alias object.value5 : object.drawPos.y

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


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

reserve function RotatingSpike_DebugDraw
reserve function RotatingSpike_DebugSpawn


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

private value RotatingSpike_startDebugID = 0


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

private function RotatingSpike_DebugDraw
	temp7 = debugMode[0].currentSelection
	temp7 -= RotatingSpike_startDebugID
	temp6 = object.direction
	temp6 <<= 8

	DrawSprite(0)
	temp0 = 0
	temp1 = 16
	while temp0 < temp7
		Cos(temp2, temp6)
		temp2 *= temp1
		temp2 <<= 7
		temp2 += object.xpos

		Sin(temp3, temp6)
		temp3 *= temp1
		temp3 <<= 7
		temp3 += object.ypos

		DrawSpriteXY(1, temp2, temp3)

		temp0++
		temp1 += 16
	loop

	Cos(temp2, temp6)
	temp2 *= temp1
	temp2 <<= 7
	temp2 += object.xpos

	Sin(temp3, temp6)
	temp3 *= temp1
	temp3 <<= 7
	temp3 += object.ypos

	DrawSpriteXY(2, temp2, temp3)
end function


private function RotatingSpike_DebugSpawn
	CreateTempObject(TypeName[Rotating Spike], 0, object.xpos, object.ypos)

	object[tempObjectPos].spinAngle = object.direction
	object[tempObjectPos].spinAngle <<= 16

	if object.direction == FLIP_NONE
		object[tempObjectPos].spinSpeed = 256
	else
		object[tempObjectPos].spinSpeed = -256
	end if

	object[tempObjectPos].length = debugMode[0].currentSelection
	object[tempObjectPos].length -= RotatingSpike_startDebugID
	object[tempObjectPos].length++

	object[tempObjectPos].amplitude = object[tempObjectPos].length
	object[tempObjectPos].amplitude <<= 4
end function


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

event ObjectUpdate
	object.spinAngle += object.spinSpeed
	object.spinAngle &= 0x1FFFF
	temp0 = object.spinAngle
	temp0 >>= 8

	Cos(object.drawPos.x, temp0)
	object.drawPos.x *= object.amplitude
	object.drawPos.x <<= 7
	object.drawPos.x += object.xpos

	Sin(object.drawPos.y, temp0)
	object.drawPos.y *= object.amplitude
	object.drawPos.y <<= 7
	object.drawPos.y += object.ypos
	
	temp0 = object.xpos
	temp1 = object.ypos
	object.xpos = object.drawPos.x
	object.ypos = object.drawPos.y

	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_Hit)
		end if
	next

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


event ObjectDraw
	DrawSprite(0)

	temp0 = 0
	temp1 = 16
	temp4 = object.spinAngle
	temp4 >>= 8
	while temp0 < object.length
		Cos(temp2, temp4)
		temp2 *= temp1
		temp2 <<= 7
		temp2 += object.xpos
		Sin(temp3, temp4)
		temp3 *= temp1
		temp3 <<= 7
		temp3 += object.ypos
		DrawSpriteXY(1, temp2, temp3)
		temp0++
		temp1 += 16
	loop

	temp1 -= 8
	DrawSpriteXY(2, object.drawPos.x, object.drawPos.y)
end event


event ObjectStartup
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 84, 173)
	SpriteFrame(-8, -8, 16, 16, 101, 173)
	SpriteFrame(-16, -16, 32, 32, 84, 190)

	foreach (TypeName[Rotating Spike], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].spinAngle = object[arrayPos0].direction
		object[arrayPos0].spinAngle <<= 15
		object[arrayPos0].direction = FLIP_NONE // this object doesn't really use dir for anything btw

		object[arrayPos0].spinSpeed = object[arrayPos0].propertyValue
		object[arrayPos0].spinSpeed &= 0xF0
		if object[arrayPos0].spinSpeed >= 0x80
			object[arrayPos0].spinSpeed -= 0x100
		end if
		object[arrayPos0].spinSpeed <<= 4

		object[arrayPos0].length = object[arrayPos0].propertyValue
		object[arrayPos0].length &= 15
		object[arrayPos0].amplitude = object[arrayPos0].length
		object[arrayPos0].amplitude <<= 4
	next

	temp0 = 0
	RotatingSpike_startDebugID = DebugMode_ObjCount
	RotatingSpike_startDebugID--
	while temp0 < 4
		SetTableValue(TypeName[Rotating Spike], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(RotatingSpike_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(RotatingSpike_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

			// TODO: these following two, aren't they the same?
		case 0 // spinSpeed
			checkResult = object.propertyValue
			checkResult >>= 4
			checkResult &= 0x0F
			break
			
		case 1 // length
			checkResult = object.propertyValue
			checkResult &= 0xF0
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // spinSpeed
			editor.variableValue &= 0x0F
			
			object.propertyValue &= 0xF0
			object.propertyValue |= editor.variableValue
			break
			
		case 1 // length
			editor.variableValue &= 0x0F
			editor.variableValue <<= 4

			object.propertyValue &= 0x0F
			object.propertyValue |= editor.variableValue
			break
			
		end switch
	end if
end event


event RSDKDraw
	object.spinAngle = object.direction
	object.spinAngle <<= 15

	object.length = object.propertyValue
	object.length &= 15
	object.amplitude = object.length
	object.amplitude <<= 4

	temp0 = object.spinAngle
	temp0 >>= 8

	Cos(object.drawPos.x, temp0)
	object.drawPos.x *= object.amplitude
	object.drawPos.x <<= 7
	object.drawPos.x += object.xpos

	Sin(object.drawPos.y, temp0)
	object.drawPos.y *= object.amplitude
	object.drawPos.y <<= 7
	object.drawPos.y += object.ypos

	DrawSprite(0)

	temp0 = 0
	temp1 = 16
	temp4 = object.spinAngle
	temp4 >>= 8
	while temp0 < object.length
		Cos(temp2, temp4)
		temp2 *= temp1
		temp2 <<= 7
		temp2 += object.xpos
		
		Sin(temp3, temp4)
		temp3 *= temp1
		temp3 <<= 7
		temp3 += object.ypos
		
		DrawSpriteXY(1, temp2, temp3)
		temp0++
		temp1 += 16
	loop

	temp1 -= 8
	DrawSpriteXY(2, object.drawPos.x, object.drawPos.y)
end event


event RSDKLoad
	LoadSpriteSheet("LZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 84, 173)
	SpriteFrame(-8, -8, 16, 16, 101, 173)
	SpriteFrame(-16, -16, 32, 32, 84, 190)

	AddEditorVariable("spinSpeed")
	SetActiveVariable("spinSpeed")

	AddEditorVariable("length")
	SetActiveVariable("length")
	
	// TODO: looks like val0 (a pre-set angle) is a thing too? found one in m003
	// that's just a 1 though, so maybe not..?
end event
