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

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

private alias object.value1 : object.gravityStrength

// States
private alias 0 : BREAKGEM_INTACT
private alias 1 : BREAKGEM_SHARD

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

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

reserve function BreakGem_DebugDraw
reserve function BreakGem_DebugSpawn


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

private function BreakGem_DebugDraw
	DrawSprite(0)
end function


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


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

event ObjectUpdate
	switch object.state
	case BREAKGEM_INTACT
		object.inkEffect = INK_ALPHA
		temp0 = oscillation
		temp0 <<= 4
		Sin(temp1, temp0)
		temp1 >>= 5
		temp1 += 0xE0
		object.alpha = temp1

		// Cycle through all players
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			// Is the player rolling?
			if player[currentPlayer].animation == ANI_JUMPING
				// Check if the gem should be broken
				BoxCollisionTest(C_SOLID, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == COL_TOP
					// Player has rolled into gem, break the gem into fragments
					player[currentPlayer].yvel = -0x30000
					player[currentPlayer].gravity = GRAVITY_AIR

					CreateTempObject(TypeName[Break Gem], 6, object.xpos, object.ypos)
					object[tempObjectPos].ixpos -= 24
					object[tempObjectPos].iypos += 8
					object[tempObjectPos].xvel = -0x18000
					object[tempObjectPos].yvel = -0x1C000
					object[tempObjectPos].state = BREAKGEM_SHARD
					object[tempObjectPos].drawOrder = 5
					object[tempObjectPos].gravityStrength = 0x2800

					CreateTempObject(TypeName[Break Gem], 7, object.xpos, object.ypos)
					object[tempObjectPos].ixpos -= 8
					object[tempObjectPos].iypos += 8
					object[tempObjectPos].xvel = -0xC000
					object[tempObjectPos].yvel = -0x20000
					object[tempObjectPos].state = BREAKGEM_SHARD
					object[tempObjectPos].drawOrder = 5
					object[tempObjectPos].gravityStrength = 0x2800
					
					CreateTempObject(TypeName[Break Gem], 8, object.xpos, object.ypos)
					object[tempObjectPos].ixpos += 8
					object[tempObjectPos].iypos += 8
					object[tempObjectPos].xvel = 0xC000
					object[tempObjectPos].yvel = -0x20000
					object[tempObjectPos].state = BREAKGEM_SHARD
					object[tempObjectPos].drawOrder = 5
					object[tempObjectPos].gravityStrength = 0x2800
					
					CreateTempObject(TypeName[Break Gem], 9, object.xpos, object.ypos)
					object[tempObjectPos].ixpos += 24
					object[tempObjectPos].iypos += 8
					object[tempObjectPos].xvel = 0x18000
					object[tempObjectPos].yvel = -0x1C000
					object[tempObjectPos].state = BREAKGEM_SHARD
					object[tempObjectPos].drawOrder = 5
					object[tempObjectPos].gravityStrength = 0x2800
					
					CreateTempObject(TypeName[Break Gem], 10, object.xpos, object.ypos)
					object[tempObjectPos].ixpos -= 24
					object[tempObjectPos].iypos -= 4
					object[tempObjectPos].xvel = -0x18000
					object[tempObjectPos].yvel = -0x1C000
					object[tempObjectPos].state = BREAKGEM_SHARD
					object[tempObjectPos].drawOrder = 5
					object[tempObjectPos].gravityStrength = 0x2800
					
					CreateTempObject(TypeName[Break Gem], 11, object.xpos, object.ypos)
					object[tempObjectPos].ixpos -= 8
					object[tempObjectPos].iypos += 2
					object[tempObjectPos].xvel = -0xC000
					object[tempObjectPos].yvel = -0x20000
					object[tempObjectPos].state = BREAKGEM_SHARD
					object[tempObjectPos].drawOrder = 5
					object[tempObjectPos].gravityStrength = 0x2800
					
					CreateTempObject(TypeName[Break Gem], 10, object.xpos, object.ypos)
					object[tempObjectPos].ixpos += 8
					object[tempObjectPos].iypos += 6
					object[tempObjectPos].xvel = 0xC000
					object[tempObjectPos].yvel = -0x20000
					object[tempObjectPos].state = BREAKGEM_SHARD
					object[tempObjectPos].drawOrder = 5
					object[tempObjectPos].gravityStrength = 0x2800
					
					CreateTempObject(TypeName[Break Gem], 11, object.xpos, object.ypos)
					object[tempObjectPos].ixpos += 24
					object[tempObjectPos].iypos -= 4
					object[tempObjectPos].xvel = 0x18000
					object[tempObjectPos].yvel = -0x1C000
					object[tempObjectPos].state = BREAKGEM_SHARD
					object[tempObjectPos].drawOrder = 5
					object[tempObjectPos].gravityStrength = 0x2800
					
					CreateTempObject(TypeName[Break Gem], 2, object.xpos, object.ypos)
					object[tempObjectPos].ixpos -= 24
					object[tempObjectPos].iypos -= 8
					object[tempObjectPos].xvel = -0x20000
					object[tempObjectPos].yvel = -0x20000
					object[tempObjectPos].state = BREAKGEM_SHARD
					object[tempObjectPos].drawOrder = 5
					object[tempObjectPos].gravityStrength = 0x3800
					
					CreateTempObject(TypeName[Break Gem], 3, object.xpos, object.ypos)
					object[tempObjectPos].ixpos -= 8
					object[tempObjectPos].iypos -= 8
					object[tempObjectPos].xvel = -0x10000
					object[tempObjectPos].yvel = -0x28000
					object[tempObjectPos].state = BREAKGEM_SHARD
					object[tempObjectPos].drawOrder = 5
					object[tempObjectPos].gravityStrength = 0x3800
					
					CreateTempObject(TypeName[Break Gem], 4, object.xpos, object.ypos)
					object[tempObjectPos].ixpos += 8
					object[tempObjectPos].iypos -= 8
					object[tempObjectPos].xvel = 0x10000
					object[tempObjectPos].yvel = -0x28000
					object[tempObjectPos].state = BREAKGEM_SHARD
					object[tempObjectPos].drawOrder = 5
					object[tempObjectPos].gravityStrength = 0x3800
					
					CreateTempObject(TypeName[Break Gem], 5, object.xpos, object.ypos)
					object[tempObjectPos].ixpos += 24
					object[tempObjectPos].iypos -= 8
					object[tempObjectPos].xvel = 0x20000
					object[tempObjectPos].yvel = -0x20000
					object[tempObjectPos].state = BREAKGEM_SHARD
					object[tempObjectPos].drawOrder = 5
					object[tempObjectPos].gravityStrength = 0x3800

					// Erase this object
					object.type = TypeName[Blank Object]

					// TODO: is there supposed to be any weird variable ID stuff here?
					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
						break
					end switch

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

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

					PlaySfx(SfxName[Block Break], false)
				end if
			else
				// Simply interact with the player as a box
				BoxCollisionTest(C_SOLID, object.entityPos, -24, -16, 24, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			end if
		next
		break

	case BREAKGEM_SHARD
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += object.gravityStrength

		if object.outOfBounds == true
			object.type = TypeName[Blank Object]
		end if
		break

	end switch

end event


event ObjectDraw
	if object.state == BREAKGEM_INTACT
		DrawSpriteFX(0, FX_INK, object.xpos, object.ypos)
		DrawSprite(1)
	else
		DrawSprite(object.propertyValue)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("HPZ/Objects.gif")

	// Sprite frames

	// Gem frames
	SpriteFrame(-32, -16, 64, 32, 1, 223)
	SpriteFrame(-32, 1, 64, 15, 115, 207)

	// Shard frames
	SpriteFrame(-8, -8, 16, 16, 115, 173)
	SpriteFrame(-8, -8, 16, 16, 132, 173)
	SpriteFrame(-8, -8, 16, 16, 149, 173)
	SpriteFrame(-8, -8, 16, 16, 166, 173)
	SpriteFrame(-8, -8, 16, 16, 115, 190)
	SpriteFrame(-8, -8, 16, 16, 132, 190)
	SpriteFrame(-8, -8, 16, 16, 149, 190)
	SpriteFrame(-8, -8, 16, 16, 166, 190)

	SpriteFrame(-4, -4, 8, 8, 183, 173)
	SpriteFrame(-4, -4, 8, 8, 183, 182)

	// Add this object to the debug mode object list
	SetTableValue(TypeName[Break Gem], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(BreakGem_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(BreakGem_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-32, -16, 64, 32, 1, 223)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
