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

//-------Aliases-------//
private alias 61 : TYPE_SPINYFLOOR

// Function declarations
reserve function SpinyFloor_Function144
reserve function SpinyFloor_DebugDraw
reserve function SpinyFloor_DebugSpawn

// Static Values

// Tables

function SpinyFloor_Function144
	object[arrayPos0].xpos = object[arrayPos0].value0
	object[arrayPos0].ypos = object[arrayPos0].value1
	object[arrayPos0].value2 = 256
	object[arrayPos0].value3 = 128
	object[arrayPos0].frame = 0
	object[arrayPos0].animationTimer = 0
	object[arrayPos0].state = 0
	if object[arrayPos0].propertyValue == 0
		object[arrayPos0].direction = FLIP_NONE
		object[arrayPos0].xvel = -0x4000
	else
		object[arrayPos0].direction = FLIP_X
		object[arrayPos0].xvel = 0x4000
	end if
	object[arrayPos0].priority = PRIORITY_ACTIVE_BOUNDS
end function


function SpinyFloor_DebugDraw
	DrawSprite(0)
end function


function SpinyFloor_DebugSpawn
	CreateTempObject(TypeName[Spiny Floor], temp0, object.xpos, object.ypos)
	object[tempObjectPos].value0 = object.xpos
	object[tempObjectPos].value1 = object.ypos
	arrayPos0 = object[tempObjectPos].entityPos
	object[tempObjectPos].propertyValue = object.direction
	CallFunction(SpinyFloor_Function144)
end function


event ObjectMain
	object.priority = PRIORITY_ACTIVE
	switch object.state
	case 0
		object.value2--
		if object.value2 <= 0
			object.value2 = 256
			object.value3 = 128
			FlipSign(object.xvel)
			object.direction ^= FLIP_X
		end if
		object.xpos += object.xvel
		object.animationTimer++
		if object.animationTimer == 10
			object.animationTimer = 0
			object.frame ^= 1
		end if
		if object.value3 == 0
			object.value5 = 0x7FFFFFFF
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -96, -256, 96, 256, currentPlayer, 1, 1, 1, 1)
				if checkResult == 1
					object.value3 = 40
					object.frame = 2
					object.state = 1
					temp0 = object.xpos
					temp0 -= object[currentPlayer].xpos
					Abs(temp0)
					if temp0 < object.value5
						object.value5 = temp0
						if object[currentPlayer].xpos > object.xpos
							object.value4 = 0x10000
						else
							object.value4 = -0x10000
						end if
					end if
				end if
			next
		else
			object.value3--
		end if
		break
	case 1
		object.value3--
		if object.value3 == 20
			PlaySfx(SfxName[Spiny Shot], 0)
			CreateTempObject(TypeName[Spiny Shot], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = object.value4
			object[tempObjectPos].yvel = -0x30000
			object[tempObjectPos].drawOrder = 2
		end if
		if object.value3 < 0
			object.value3 = 64
			object.frame = 0
			object.state = 0
		end if
		break
	end switch
	if object.outOfBounds == 1
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.value0
		object.ypos = object.value1
		if object.outOfBounds == 1
			arrayPos0 = object.entityPos
			CallFunction(SpinyFloor_Function144)
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if
	CallFunction(PlayerObject_EnemyPop)
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, object[currentPlayer].value40, object[currentPlayer].value38, object[currentPlayer].value41, object[currentPlayer].value39)
		if checkResult == 1
			CallFunction(PlayerObject_BadnikBreak)
		end if
	next
end event


event ObjectDraw
	DrawSprite(object.frame)
end event


event ObjectStartup
	LoadSpriteSheet("CPZ/Objects.gif")
	CheckCurrentStageFolder("Zone02")
	if checkResult == 1
		LoadSpriteSheet("CPZ/Objects.gif")
		SpriteFrame(-24, -11, 48, 23, 1, 1)
		SpriteFrame(-22, -11, 44, 23, 50, 1)
		SpriteFrame(-24, -11, 48, 23, 95, 1)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-24, -11, 48, 23, 131, 256)
		SpriteFrame(-22, -11, 44, 23, 180, 256)
		SpriteFrame(-24, -11, 48, 23, 225, 256)
	end if
	foreach (TypeName[Spiny Floor], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value0 = object[arrayPos0].xpos
		object[arrayPos0].value1 = object[arrayPos0].ypos
		CallFunction(SpinyFloor_Function144)
	next
	SetTableValue(TypeName[Spiny Floor], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(SpinyFloor_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(SpinyFloor_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
