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

// ========================
// Aliases
// ========================

private alias 0 : BREAKBLOCK_BLOCK
private alias 1 : BREAKBLOCK_DEBRIS

// Player Aliases
private alias object.yvel : player.yvel
private alias object.gravity : player.gravity
private alias object.animation : player.animation
private alias object.value19 : player.badnikBonus

// Achievement Aliases
private alias 3 : ACHIEVEMENT_BLOCKBUSTER


// ========================
// Function Declarations
// ========================

reserve function BreakBlock_DebugDraw
reserve function BreakBlock_DebugSpawn


// ========================
// Function Definitions
// ========================

private function BreakBlock_DebugDraw
	DrawSprite(0)
end function


private function BreakBlock_DebugSpawn
	CreateTempObject(TypeName[Break Block], 0, object.xpos, object.ypos)
end function


// ========================
// Events
// ========================

event ObjectUpdate
	switch object.state
	case BREAKBLOCK_BLOCK
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if player[currentPlayer].animation == ANI_JUMPING
				BoxCollisionTest(C_SOLID, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == COL_TOP
					player[currentPlayer].yvel = -0x30000
					player[currentPlayer].gravity = GRAVITY_AIR

					CreateTempObject(TypeName[Object Score], player[currentPlayer].badnikBonus, object.xpos, object.ypos)
					object[tempObjectPos].drawOrder = 4

					CreateTempObject(TypeName[Break Block], 0, object.xpos, object.ypos)
					object[tempObjectPos].ixpos -= 8
					object[tempObjectPos].iypos -= 8
					object[tempObjectPos].xvel = -0x20000
					object[tempObjectPos].yvel = -0x20000
					object[tempObjectPos].state = BREAKBLOCK_DEBRIS
					object[tempObjectPos].drawOrder = 5

					CreateTempObject(TypeName[Break Block], 0, object.xpos, object.ypos)
					object[tempObjectPos].ixpos += 8
					object[tempObjectPos].iypos -= 8
					object[tempObjectPos].xvel = 0x20000
					object[tempObjectPos].yvel = -0x20000
					object[tempObjectPos].state = BREAKBLOCK_DEBRIS
					object[tempObjectPos].drawOrder = 5

					CreateTempObject(TypeName[Break Block], 0, object.xpos, object.ypos)
					object[tempObjectPos].ixpos -= 8
					object[tempObjectPos].iypos += 8
					object[tempObjectPos].xvel = -0x10000
					object[tempObjectPos].yvel = -0x10000
					object[tempObjectPos].state = BREAKBLOCK_DEBRIS
					object[tempObjectPos].drawOrder = 5

					CreateTempObject(TypeName[Break Block], 0, object.xpos, object.ypos)
					object[tempObjectPos].ixpos += 8
					object[tempObjectPos].iypos += 8
					object[tempObjectPos].xvel = 0x10000
					object[tempObjectPos].yvel = -0x10000
					object[tempObjectPos].state = BREAKBLOCK_DEBRIS
					object[tempObjectPos].drawOrder = 5

					object.type = TypeName[Blank Object]

					switch player[currentPlayer].badnikBonus
					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
						if stage.debugMode == false
							// Grant the "Block Buster" Achievement
							CallNativeFunction2(SetAchievement, ACHIEVEMENT_BLOCKBUSTER, 100)
						end if
						break

					end switch

					if player[currentPlayer].gravity == GRAVITY_AIR
						if player[currentPlayer].badnikBonus < 15
							player[currentPlayer].badnikBonus++
						end if
					end if

					PlaySfx(SfxName[Block Break], false)
				end if
			else
				BoxCollisionTest(C_SOLID, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			end if
		next
		break

	case BREAKBLOCK_DEBRIS
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x3800
		if object.outOfBounds == true
			object.type = TypeName[Blank Object]
		end if
		break
		
	end switch
end event


event ObjectDraw
	DrawSprite(object.state)
end event


event ObjectStartup
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 317, 363)
	SpriteFrame(-8, -8, 16, 16, 317, 363)

	SetTableValue(TypeName[Break Block], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(BreakBlock_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(BreakBlock_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("MZ/Objects.gif")
	SpriteFrame(-16, -16, 32, 32, 317, 363)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
