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

//-------Aliases-------//
private alias 54 : TYPE_BREAKBLOCK

// Function declarations
reserve function BreakBlock_DebugDraw
reserve function BreakBlock_DebugSpawn

// Static Values
public value BreakBlock_value39 = 0;
public value BreakBlock_value38 = 0;

// Tables
public table BreakBlock_table45
	-0x10000, -0x80000, 0x10000, -0x80000, -0xE000, -0x70000, 0xE000, -0x70000, -0xC000, -0x60000, 0xC000, -0x60000, -0xA000, -0x50000, 0xA000, -0x50000
	-0x8000, -0x40000, 0x8000, -0x40000
end table


function BreakBlock_DebugDraw
	temp0 = object.value17
	temp0 -= BreakBlock_value39
	DrawSprite(temp0)
end function


function BreakBlock_DebugSpawn
	temp0 = object.value17
	temp0 -= BreakBlock_value39
	temp0 |= 128
	CreateTempObject(TypeName[Break Block], temp0, object.xpos, object.ypos)
end function


event ObjectMain
	switch object.state
	case 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			temp1 = object.propertyValue
			temp1 &= 127
			if temp1 == 0
				temp0 = 4
			else
				temp0 = 5
				temp0 -= temp1
			end if
			temp0 <<= 3
			temp1 = temp0
			FlipSign(temp0)
			if object[currentPlayer].animation != ANI_JUMPING
				temp0 += BreakBlock_value38
			end if
			BoxCollisionTest(C_BOX, object.entityPos, -16, temp0, 16, temp1, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				if object[currentPlayer].animation == ANI_JUMPING
					GetBit(temp3, object.propertyValue, 7)
					CheckEqual(object[currentPlayer].collisionPlane, PATH_B)
					temp3 |= checkResult
					if temp3 == 1
						object[currentPlayer].yvel = 0
						object[currentPlayer].gravity = GRAVITY_AIR
						temp0 += 8
						temp0 <<= 16
						temp0 += object.ypos
						temp3 = object.propertyValue
						temp3 &= 127
						temp4 = 5
						temp4 += temp3
						temp5 = temp3
						temp5 <<= 2
						while temp3 < 5
							CreateTempObject(TypeName[Break Block], temp4, object.xpos, temp0)
							object[tempObjectPos].ixpos -= 8
							GetTableValue(object[tempObjectPos].xvel, temp5, BreakBlock_table45)
							temp5++
							GetTableValue(object[tempObjectPos].yvel, temp5, BreakBlock_table45)
							temp5++
							object[tempObjectPos].state = 1
							object[tempObjectPos].drawOrder = 5
							CreateTempObject(TypeName[Break Block], temp4, object.xpos, temp0)
							object[tempObjectPos].ixpos += 8
							GetTableValue(object[tempObjectPos].xvel, temp5, BreakBlock_table45)
							temp5++
							GetTableValue(object[tempObjectPos].yvel, temp5, BreakBlock_table45)
							temp5++
							object[tempObjectPos].state = 1
							object[tempObjectPos].drawOrder = 5
							temp0 += 0x100000
							temp3++
							temp4++
						loop
						CreateTempObject(TypeName[Object Score], object[currentPlayer].value19, object.xpos, object.ypos)
						object[tempObjectPos].drawOrder = 5
						object.propertyValue++
						object.ypos += 0x80000
						temp0 = object.propertyValue
						temp0 &= 127
						if temp0 >= 5
							object.type = TypeName[Blank Object]
						end if
						arrayPos0 = 22
						if object[currentPlayer].value16 == 0
							arrayPos0 += currentPlayer
						end if
						switch object[currentPlayer].value19
						case 0
							player.score += 100
							break
						case 1
							player.score += 200
							break
						case 2
							player.score += 500
							break
						case 3
						case 4
						case 5
						case 6
						case 7
						case 8
						case 9
						case 10
						case 11
						case 12
						case 13
						case 14
							player.score += 1000
							break
						case 15
							player.score += 10000
							break
						end switch
						if object[currentPlayer].gravity == GRAVITY_AIR
							if object[currentPlayer].value19 < 15
								object[currentPlayer].value19++
							end if
						end if
						PlaySfx(SfxName[Block Break], 0)
					end if
				end if
			else
				if checkResult == 4
					object[currentPlayer].yvel = 1
					object[currentPlayer].xvel = 0
					object[currentPlayer].speed = 0
				end if
			end if
		next
		break
	case 1
		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
end event


event ObjectDraw
	temp0 = object.propertyValue
	temp0 &= 127
	DrawSprite(temp0)
end event


event ObjectStartup
	CheckCurrentStageFolder("Zone05")
	if checkResult == 1
		LoadSpriteSheet("HTZ/Objects.gif")
		SpriteFrame(-16, -40, 32, 80, 69, 1)
		SpriteFrame(-16, -32, 32, 64, 69, 17)
		SpriteFrame(-16, -24, 32, 48, 69, 33)
		SpriteFrame(-16, -16, 32, 32, 69, 49)
		SpriteFrame(-16, -8, 32, 16, 69, 65)
		SpriteFrame(-8, -8, 16, 16, 69, 1)
		SpriteFrame(-8, -8, 16, 16, 69, 17)
		SpriteFrame(-8, -8, 16, 16, 69, 33)
		SpriteFrame(-8, -8, 16, 16, 69, 49)
		SpriteFrame(-8, -8, 16, 16, 69, 65)
		BreakBlock_value38 = 0
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-16, -40, 32, 80, 105, 687)
		SpriteFrame(-16, -32, 32, 64, 105, 703)
		SpriteFrame(-16, -24, 32, 48, 105, 719)
		SpriteFrame(-16, -16, 32, 32, 105, 735)
		SpriteFrame(-16, -8, 32, 16, 105, 751)
		SpriteFrame(-8, -8, 16, 16, 105, 687)
		SpriteFrame(-8, -8, 16, 16, 105, 703)
		SpriteFrame(-8, -8, 16, 16, 105, 719)
		SpriteFrame(-8, -8, 16, 16, 105, 735)
		SpriteFrame(-8, -8, 16, 16, 105, 751)
		BreakBlock_value38 = -8
	end if
	temp0 = 0
	BreakBlock_value39 = DebugMode_ObjCount
	while temp0 < 5
		SetTableValue(TypeName[Break Block], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(BreakBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(BreakBlock_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

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