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

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

private alias object.value0 : object.rotationSpeed
private alias object.value1 : object.length

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


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

reserve function RSpikeChain_DebugDraw
reserve function RSpikeChain_DebugSpawn


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

private value RSpikeChain_startDebugID = 0


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

private function RSpikeChain_DebugDraw
	DrawSprite(0)

	temp0 = debugMode[0].currentSelection
	temp0 -= RSpikeChain_startDebugID
	
	switch temp0
	case 0
	case 1
		temp0 = 4
		break

	case 2
	case 3
		temp0 = 2
		break
		
	end switch

	temp1 = object.ypos
	while temp0 > 0
		temp1 -= 0x100000
		DrawSpriteXY(1, object.xpos, temp1)
		temp0--
	loop
end function


private function RSpikeChain_DebugSpawn
	CreateTempObject(TypeName[R Spike Chain], object.direction, object.xpos, object.ypos)
	temp0 = debugMode[0].currentSelection
	temp0 -= RSpikeChain_startDebugID

	switch temp0
	case 0
		object[tempObjectPos].rotationSpeed = 5
		object[tempObjectPos].length = 4
		break

	case 1
		object[tempObjectPos].rotationSpeed = 2
		object[tempObjectPos].length = 4
		break

	case 2
		object[tempObjectPos].rotationSpeed = 2
		object[tempObjectPos].length = 2
		break

	case 3
		object[tempObjectPos].rotationSpeed = -2
		object[tempObjectPos].length = 2
		break
		
	end switch
end function


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

event ObjectUpdate
	object.angle += object.rotationSpeed
	if object.angle < 0
		object.angle += 0x200
	end if
	object.angle &= 0x1FF

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp0 = 0
		temp1 = 16
		temp2 = object.xpos
		temp3 = object.ypos

		while temp0 < object.length
			Cos(object.xpos, object.angle)
			object.xpos *= temp1
			object.xpos <<= 7
			object.xpos += temp2

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

			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
			temp0++
			temp1 += 16
		loop

		object.xpos = temp2
		object.ypos = temp3
	next
end event


event ObjectDraw
	DrawSprite(0)

	temp0 = 0
	temp1 = 16
	while temp0 < object.length
		Cos(temp2, object.angle)
		temp2 *= temp1
		temp2 <<= 7
		temp2 += object.xpos

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

		DrawSpriteXY(1, temp2, temp3)

		temp0++
		temp1 += 16
	loop
end event


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

	foreach (TypeName[R Spike Chain], arrayPos0, ALL_ENTITIES)
		switch object[arrayPos0].propertyValue
		case 0
			object[arrayPos0].rotationSpeed = 5
			object[arrayPos0].length = 4
			break
			
		case 1
			object[arrayPos0].rotationSpeed = 2
			object[arrayPos0].length = 4
			break

		case 2
			object[arrayPos0].rotationSpeed = 2
			object[arrayPos0].length = 2
			break

		case 3
			object[arrayPos0].rotationSpeed = -2
			object[arrayPos0].length = 2
			break
			
		end switch
	next

	SpriteFrame(-8, -8, 16, 16, 88, 138)
	SpriteFrame(-8, -8, 16, 16, 88, 138)

	temp0 = 0
	RSpikeChain_startDebugID = DebugMode_ObjCount
	while temp0 < 4
		SetTableValue(TypeName[R Spike Chain], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(RSpikeChain_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(RSpikeChain_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
	switch object.propertyValue
	default
	case 0
		object.rotationSpeed = 5
		object.length = 4
		break
		
	case 1
		object.rotationSpeed = 2
		object.length = 4
		break

	case 2
		object.rotationSpeed = 2
		object.length = 2
		break

	case 3
		object.rotationSpeed = -2
		object.length = 2
		break

	end switch

	DrawSprite(0)

	temp0 = 0
	temp1 = 16
	while temp0 < object.length
		Cos(temp2, object.angle)
		temp2 *= temp1
		temp2 <<= 7
		temp2 += object.xpos

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

		DrawSpriteXY(1, temp2, temp3)

		temp0++
		temp1 += 16
	loop
end event


event RSDKLoad
	LoadSpriteSheet("SYZ/Objects.gif")
	SpriteFrame(-8, -8, 16, 16, 88, 138)
	SpriteFrame(-8, -8, 16, 16, 88, 138)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Speed: Fast, Length: Long", 0)
	AddEnumVariable("Speed: Slow, Length: Long", 1)
	AddEnumVariable("Speed: Slow, Length: Short", 2)
	AddEnumVariable("Speed: Slow (Reverse), Length: Short", 3)
end event
