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

//-------Aliases-------//
private alias 62 : TYPE_SPINYWALL

// Function declarations
reserve function SpinyWall_Function147
reserve function SpinyWall_DebugDraw
reserve function SpinyWall_DebugSpawn

// Static Values

// Tables

function SpinyWall_Function147
	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
	object[arrayPos0].direction = object[arrayPos0].propertyValue
	object[arrayPos0].yvel = -0x4000
	object[arrayPos0].priority = PRIORITY_ACTIVE_BOUNDS
end function


function SpinyWall_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


function SpinyWall_DebugSpawn
	CreateTempObject(TypeName[Spiny Wall], 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(SpinyWall_Function147)
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.yvel)
			object.direction ^= FLIP_Y
		end if
		object.ypos += object.yvel
		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 = 0x30000
						else
							object.value4 = -0x30000
						end if
					end if
				end if
			next
		else
			object.value3--
		end if
		break
	case 1
		object.value3--
		if object.value3 == 20
			CreateTempObject(TypeName[Spiny Shot], 0, object.xpos, object.ypos)
			object[tempObjectPos].xvel = object.value4
			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(SpinyWall_Function147)
		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
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("CPZ/Objects.gif")
	CheckCurrentStageFolder("Zone02")
	if checkResult == 1
		LoadSpriteSheet("CPZ/Objects.gif")
		SpriteFrame(-12, -24, 23, 48, 1, 25)
		SpriteFrame(-12, -22, 23, 44, 25, 25)
		SpriteFrame(-12, -24, 23, 48, 49, 25)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-12, -24, 23, 48, 99, 501)
		SpriteFrame(-12, -22, 23, 44, 99, 550)
		SpriteFrame(-12, -24, 23, 48, 99, 595)
	end if
	foreach (TypeName[Spiny Wall], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value0 = object[arrayPos0].xpos
		object[arrayPos0].value1 = object[arrayPos0].ypos
		CallFunction(SpinyWall_Function147)
	next
	SetTableValue(TypeName[Spiny Wall], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(SpinyWall_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(SpinyWall_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
