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

//-------Aliases-------//
private alias 47 : TYPE_BREAKABLEPILLAR

// Function declarations
reserve function BreakablePillar_DebugDraw
reserve function BreakablePillar_DebugSpawn

// Static Values
private value BreakablePillar_value36 = 0;

// Tables
public table BreakablePillar_table40
	-0x20000, -0x20000, -0x1C000, -0x1C000, 0x1C000, -0x1C000, 0x20000, -0x20000, -0x18000, -0x18000, 0x18000, -0x18000, -0x14000, -0x14000, 0x14000, -0x14000
	-0x10000, -0x10000, 0x10000, -0x10000, -0xC000, -0xC000, 0xC000, -0xC000, -0x8000, -0x8000, 0x8000, -0x8000
end table

function BreakablePillar_DebugDraw
	EditFrame(0, -28, -24, 56, 32, 59, 42)
	DrawSprite(0)
	temp0 = object[0].value17
	temp0 -= BreakablePillar_value36
	temp0++
	DrawSprite(temp0)
end function


function BreakablePillar_DebugSpawn
	temp0 = object[0].value17
	temp0 -= BreakablePillar_value36
	temp0++
	CreateTempObject(TypeName[Breakable Pillar], temp0, object.xpos, object.ypos)
	object[tempObjectPos].value0 = 32
end function


event ObjectMain
	switch object.state
	case 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_TOUCH, object.entityPos, -40, -256, 40, 256, currentPlayer, -1, -1, 1, 1)
			if checkResult == 1
				object.priority = PRIORITY_ACTIVE
				object.state++
			end if
		next
		break
	case 1
		object.value0 += 2
		if object.value0 == 80
			object.state = 2
		end if
		break
	case 2
		break
	case 3
		break
	case 4
		object.value1--
		if object.value1 < 0
			object.state++
		end if
		break
	case 5
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x1800
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
		end if
		break
	end switch
	if object.state < 4
		temp0 = object.value0
		temp0 -= 8
		FlipSign(temp0)
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.state < 3
				BoxCollisionTest(C_BOX, object.entityPos, -16, temp0, 16, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					object[currentPlayer].animation = ANI_JUMPING
					if object[currentPlayer].state == PlayerObject_KnuxGlideLeft
						FlipSign(object[currentPlayer].xvel)
						object[currentPlayer].speed = object[currentPlayer].xvel
					end if
					object[currentPlayer].state = PlayerObject_HandleRolling
					object[currentPlayer].gravity = GRAVITY_AIR
					PlaySfx(SfxName[Ledge Break], 0)
					temp5 = 0
					temp0 = object.value0
					temp0 >>= 5
					temp1 = object.value0
					temp1 &= 31
					if temp1 != 0
						temp0++
					end if
					temp3 = object.value0
					FlipSign(temp3)
					temp3 += 16
					temp3 <<= 16
					temp3 += object.ypos
					temp2 = object.xpos
					temp2 -= 0x180000
					temp1 = 3
					while temp1 < 7
						CreateTempObject(TypeName[Breakable Pillar], temp1, temp2, temp3)
						GetTableValue(object[tempObjectPos].xvel, temp5, BreakablePillar_table40)
						temp5++
						GetTableValue(object[tempObjectPos].yvel, temp5, BreakablePillar_table40)
						temp5++
						object[tempObjectPos].state = 4
						temp2 += 0x100000
						temp1++
					loop
					temp3 += 0x100000
					temp4 = 4
					while temp0 >= 0
						temp1 = 7
						temp2 = object.xpos
						temp2 -= 0x80000
						while temp1 < 9
							CreateTempObject(TypeName[Breakable Pillar], temp1, temp2, temp3)
							GetTableValue(object[tempObjectPos].xvel, temp5, BreakablePillar_table40)
							temp5++
							GetTableValue(object[tempObjectPos].yvel, temp5, BreakablePillar_table40)
							temp5++
							object[tempObjectPos].value1 = temp4
							object[tempObjectPos].state = 4
							temp2 += 0x100000
							temp1++
						loop
						temp3 += 0x100000
						temp4 += 4
						temp0--
					loop
					temp3 = object.ypos
					temp3 += 0x100000
					if object.propertyValue == 1
						temp6 = 0
					else
						temp6 = 2
					end if
					temp6 += 9
					temp1 = 0
					temp2 = object.xpos
					temp2 -= 0x80000
					while temp1 < 2
						CreateTempObject(TypeName[Breakable Pillar], temp6, temp2, temp3)
						GetTableValue(object[tempObjectPos].xvel, temp5, BreakablePillar_table40)
						temp5++
						GetTableValue(object[tempObjectPos].yvel, temp5, BreakablePillar_table40)
						temp5++
						object[tempObjectPos].value1 = temp4
						object[tempObjectPos].state = 4
						temp2 += 0x100000
						temp6++
						temp1++
					loop
					object.state = 3
				end if
			end if
		next
		temp0 = object.ixpos
		temp0 -= screen.cameraX
		Abs(temp0)
		temp0 -= 184
		if temp0 >= screen.xcenter
			object.value0 = 32
			object.value1 = 0
			object.state = 0
			object.priority = PRIORITY_ACTIVE_BOUNDS
		end if
	end if
end event


event ObjectDraw
	if object.state != 3
		temp0 = object.value0
		temp0 -= 8
		FlipSign(temp0)
		EditFrame(0, -28, temp0, 56, object.value0, 59, 42)
		DrawSprite(0)
		DrawSprite(object.propertyValue)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("ARZ/Objects.gif")
	SpriteFrame(-28, -24, 56, 32, 59, 42)
	SpriteFrame(-16, 8, 32, 24, 223, 137)
	SpriteFrame(-16, 8, 32, 16, 71, 130)
	SpriteFrame(-4, -8, 12, 16, 59, 42)
	SpriteFrame(-8, -8, 16, 16, 71, 42)
	SpriteFrame(-8, -8, 16, 16, 87, 42)
	SpriteFrame(-8, -8, 12, 16, 103, 42)
	SpriteFrame(-8, -8, 16, 16, 71, 58)
	SpriteFrame(-8, -8, 16, 16, 87, 58)
	SpriteFrame(-8, -8, 16, 24, 223, 137)
	SpriteFrame(-8, -8, 16, 24, 239, 137)
	SpriteFrame(-8, -8, 16, 16, 71, 130)
	SpriteFrame(-8, -8, 16, 16, 87, 130)
	temp0 = 0
	BreakablePillar_value36 = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[Breakable Pillar], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(BreakablePillar_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(BreakablePillar_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
	foreach (TypeName[Breakable Pillar], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value0 = 32
		object[arrayPos0].propertyValue++
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

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