// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Moving Spikes Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 49 : TYPE_MOVINGSPIKES

// Function declarations
reserve function MovingSpikes_DebugDraw
reserve function MovingSpikes_DebugSpawn
reserve function MovingSpikes_Function121

// Static Values

// Tables

function MovingSpikes_DebugDraw
	DrawSprite(0)
end function


function MovingSpikes_DebugSpawn
	CreateTempObject(TypeName[Moving Spikes], 0, object.xpos, object.ypos)
	object[tempObjectPos].inkEffect = INK_BLEND
	object[tempObjectPos].drawOrder = 5
	if object.direction == FLIP_X
		object[tempObjectPos].xpos += 0x680000
		object[tempObjectPos].xvel = -0x10000
		object[tempObjectPos].value0 = 104
	else
		object[tempObjectPos].xpos -= 0x680000
		object[tempObjectPos].xvel = 0x10000
		object[tempObjectPos].value0 = -104
	end if
end function


function MovingSpikes_Function121
	temp0 = object.xpos
	temp0 >>= 16
	temp0 -= screen.cameraX
	Abs(temp0)
	temp0 -= 32
	if temp0 < screen.xcenter
		temp0 = object.ypos
		temp0 >>= 16
		temp0 -= screen.cameraY
		Abs(temp0)
		if temp0 < 160
			PlaySfx(SfxName[Clank], 0)
		end if
	end if
end function


event ObjectMain
	object.xpos += object.xvel
	if object.xvel > 0
		object.value0++
		if object.value0 == 104
			FlipSign(object.xvel)
			CallFunction(MovingSpikes_Function121)
		else
			foreach (TypeName[Moving Spikes], arrayPos0, ACTIVE_ENTITIES)
				if arrayPos0 != object.entityPos
					BoxCollisionTest(C_TOUCH, object.entityPos, 0, -40, 25, 40, arrayPos0, -25, -40, 0, 40)
					if checkResult == 1
						FlipSign(object.xvel)
						CallFunction(MovingSpikes_Function121)
					end if
				end if
			next
		end if
	else
		object.value0--
		if object.value0 == -104
			FlipSign(object.xvel)
			CallFunction(MovingSpikes_Function121)
		else
			foreach (TypeName[Moving Spikes], arrayPos0, ACTIVE_ENTITIES)
				if arrayPos0 != object.entityPos
					BoxCollisionTest(C_TOUCH, object.entityPos, -25, -40, 0, 40, arrayPos0, 0, -40, 25, 40)
					if checkResult == 1
						FlipSign(object.xvel)
						CallFunction(MovingSpikes_Function121)
					end if
				end if
			next
		end if
	end if
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -16, -32, 16, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		if checkResult == 1
			CallFunction(PlayerObject_Hit)
		end if
	next
end event


event ObjectDraw
	DrawSpriteFX(1, FX_INK, object.xpos, object.ypos)
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("OOZ/Objects.gif")
	SpriteFrame(-24, -40, 48, 80, 206, 26)
	SpriteFrame(8, -16, 4, 32, 239, 107)
	foreach (TypeName[Moving Spikes], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_INACTIVE
		object[arrayPos0].inkEffect = INK_BLEND
		object[arrayPos0].drawOrder = 5
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].xpos += 0x680000
			object[arrayPos0].xvel = -0x10000
			object[arrayPos0].value0 = 104
		else
			if object[arrayPos0].propertyValue == 0
				object[arrayPos0].xpos -= 0x680000
				object[arrayPos0].xvel = 0x10000
				object[arrayPos0].value0 = -104
			else
				object[arrayPos0].xvel = -0x10000
			end if
		end if
	next
	SetTableValue(TypeName[Moving Spikes], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(MovingSpikes_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(MovingSpikes_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
