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

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

private alias object.propertyValue : object.length

private alias object.value0 : object.swingAngle
private alias object.value1 : object.drawPos.x
private alias object.value2 : object.drawPos.y

private alias object.value17 : debugMode.currentSelection

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

reserve function SwingingSpike_DebugDraw
reserve function SwingingSpike_DebugSpawn

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

private value SwingingSpike_startDebugID = 0

private function SwingingSpike_DebugDraw
	temp7 = debugMode[0].currentSelection
	temp7 -= SwingingSpike_startDebugID
	DrawSprite(0)
	
	temp0 = 0
	temp1 = 16
	while temp0 < temp7
		Cos(temp2, 128)
		temp2 *= temp1
		temp2 <<= 7
		temp2 += object.xpos
		Sin(temp3, 128)
		temp3 *= temp1
		temp3 <<= 7
		temp3 += object.ypos
		DrawSpriteXY(1, temp2, temp3)

		temp0++
		temp1 += 16
	loop

	temp1 -= 8
	Cos(temp2, 128)
	temp2 *= temp1
	temp2 <<= 7
	temp2 += object.xpos
	Sin(temp3, 128)
	temp3 *= temp1
	temp3 <<= 7
	temp3 += object.ypos
	DrawSpriteXY(2, temp2, temp3)
end function


private function SwingingSpike_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= SwingingSpike_startDebugID
	CreateTempObject(TypeName[Swinging Spike], temp0, object.xpos, object.ypos)
	object[tempObjectPos].direction = object.direction
end function


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

event ObjectUpdate
	GetTableValue(object.swingAngle, 12, StageSetup_oscillationTable)
	object.swingAngle >>= 8
	if object.direction == 1
		object.swingAngle ^= 255
		object.swingAngle += 129
	end if
	object.swingAngle <<= 1
	temp1 = object.length
	temp1++
	temp1 <<= 4
	temp1 -= 8

	Cos(object.drawPos.x, object.swingAngle)
	object.drawPos.x *= temp1
	object.drawPos.x <<= 7
	object.drawPos.x += object.xpos

	Sin(object.drawPos.y, object.swingAngle)
	object.drawPos.y *= temp1
	object.drawPos.y <<= 7
	object.drawPos.y += object.ypos
	
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.drawPos.x
		object.ypos = object.drawPos.y
		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
		object.xpos = temp0
		object.ypos = temp1
	next
end event


event ObjectDraw
	temp0 = 0
	temp1 = 16
	while temp0 < object.length
		Cos(temp2, object.swingAngle)
		temp2 *= temp1
		temp2 <<= 7
		temp2 += object.xpos
		Sin(temp3, object.swingAngle)
		temp3 *= temp1
		temp3 <<= 7
		temp3 += object.ypos
		DrawSpriteXY(1, temp2, temp3)
		temp0++
		temp1 += 16
	loop

	DrawSpriteXY(2, object.drawPos.x, object.drawPos.y)
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-15, -23, 30, 30, 92, 31)
	SpriteFrame(-8, -8, 16, 16, 65, 106)
	SpriteFrame(-24, -24, 48, 48, 397, 182)

	temp0 = 0
	SwingingSpike_startDebugID = DebugMode_ObjCount
	SwingingSpike_startDebugID--
	while temp0 < 8
		SetTableValue(TypeName[Swinging Spike], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(SwingingSpike_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(SwingingSpike_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


event RSDKDraw
	DrawSprite(0)

	temp0 = 0
	temp1 = 16
	while temp0 < object.length
		Cos(temp2, 128)
		temp2 *= temp1
		temp2 <<= 7
		temp2 += object.xpos
		Sin(temp3, 128)
		temp3 *= temp1
		temp3 <<= 7
		temp3 += object.ypos
		DrawSpriteXY(1, temp2, temp3)
		temp0++
		temp1 += 16
	loop
	temp1 -= 8
	Cos(temp2, 128)
	temp2 *= temp1
	temp2 <<= 7
	temp2 += object.xpos
	Sin(temp3, 128)
	temp3 *= temp1
	temp3 <<= 7
	temp3 += object.ypos
	DrawSpriteXY(2, temp2, temp3)
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-15, -23, 30, 30, 92, 31)
	SpriteFrame(-8, -8, 16, 16, 65, 106)
	SpriteFrame(-24, -24, 48, 48, 397, 182)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "length")
end event
