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

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

private alias object.propertyValue : object.isDebris

// Player Aliases
private alias object.state : player.state
private alias object.xpos : player.xpos
private alias object.speed : player.speed
private alias object.direction : player.direction
private alias object.animation : player.animation

private alias 1 : SUPERSTATE_SUPER


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

reserve function BreakableWall_DebugDraw
reserve function BreakableWall_DebugSpawn


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

private function BreakableWall_DebugDraw
	DrawSprite(0)
end function


private function BreakableWall_DebugSpawn
	CreateTempObject(TypeName[Breakable Wall], 0, object.xpos, object.ypos)
	object[tempObjectPos].isDebris = false
	object[tempObjectPos].drawOrder = 4
end function


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

event ObjectUpdate
	if object.isDebris == true
		object.xpos += object.xvel
		object.ypos += object.yvel
		object.yvel += 0x7000

		if object.outOfBounds == true
			object.type = TypeName[Blank Object]
		end if
	else
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			checkResult = false
			if player[currentPlayer].state == Player_State_Roll
				if player[currentPlayer].speed >= 0x48000
					checkResult = true
				end if

				if player[currentPlayer].speed < -0x48000
					checkResult = true
				end if
			end if

			if stage.playerListPos == PLAYER_KNUX_ALONE
				checkResult = true
			end if

			if Player_superState == SUPERSTATE_SUPER
				checkResult = true
			end if

			if checkResult == true
				BoxCollisionTest(C_TOUCH, object.entityPos, -17, -32, 17, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					object.type = TypeName[Blank Object]
					PlaySfx(SfxName[Ledge Break], false)

					temp2 = object.xpos
					temp2 -= 0x80000

					temp3 = object.xpos
					temp3 += 0x80000
					temp4 = object.ypos
					temp4 -= 0x180000

					if player[currentPlayer].xpos < object.xpos
						CreateTempObject(TypeName[Breakable Wall], true, temp2, temp4)
						object[tempObjectPos].xvel = -0x60000
						object[tempObjectPos].yvel = -0x60000
						object[tempObjectPos].drawOrder = 4

						CreateTempObject(TypeName[Breakable Wall], true, temp3, temp4)
						object[tempObjectPos].xvel = -0x40000
						object[tempObjectPos].yvel = -0x50000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000

						CreateTempObject(TypeName[Breakable Wall], true, temp2, temp4)
						object[tempObjectPos].xvel = -0x80000
						object[tempObjectPos].yvel = -0x20000
						object[tempObjectPos].drawOrder = 4

						CreateTempObject(TypeName[Breakable Wall], true, temp3, temp4)
						object[tempObjectPos].xvel = -0x60000
						object[tempObjectPos].yvel = -0x10000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000

						CreateTempObject(TypeName[Breakable Wall], true, temp2, temp4)
						object[tempObjectPos].xvel = -0x80000
						object[tempObjectPos].yvel = 0x20000
						object[tempObjectPos].drawOrder = 4

						CreateTempObject(TypeName[Breakable Wall], true, temp3, temp4)
						object[tempObjectPos].xvel = -0x60000
						object[tempObjectPos].yvel = 0x10000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000

						CreateTempObject(TypeName[Breakable Wall], true, temp2, temp4)
						object[tempObjectPos].xvel = -0x60000
						object[tempObjectPos].yvel = 0x60000
						object[tempObjectPos].drawOrder = 4

						CreateTempObject(TypeName[Breakable Wall], true, temp3, temp4)
						object[tempObjectPos].xvel = -0x40000
						object[tempObjectPos].yvel = 0x50000
						object[tempObjectPos].drawOrder = 4
					else
						CreateTempObject(TypeName[Breakable Wall], true, temp2, temp4)
						object[tempObjectPos].xvel = 0x40000
						object[tempObjectPos].yvel = -0x50000
						object[tempObjectPos].drawOrder = 4

						CreateTempObject(TypeName[Breakable Wall], true, temp3, temp4)
						object[tempObjectPos].xvel = 0x60000
						object[tempObjectPos].yvel = -0x60000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000

						CreateTempObject(TypeName[Breakable Wall], true, temp2, temp4)
						object[tempObjectPos].xvel = 0x60000
						object[tempObjectPos].yvel = -0x10000
						object[tempObjectPos].drawOrder = 4

						CreateTempObject(TypeName[Breakable Wall], true, temp3, temp4)
						object[tempObjectPos].xvel = 0x80000
						object[tempObjectPos].yvel = -0x20000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000

						CreateTempObject(TypeName[Breakable Wall], true, temp2, temp4)
						object[tempObjectPos].xvel = 0x60000
						object[tempObjectPos].yvel = 0x10000
						object[tempObjectPos].drawOrder = 4

						CreateTempObject(TypeName[Breakable Wall], true, temp3, temp4)
						object[tempObjectPos].xvel = 0x80000
						object[tempObjectPos].yvel = 0x20000
						object[tempObjectPos].drawOrder = 4
						temp4 += 0x100000

						CreateTempObject(TypeName[Breakable Wall], true, temp2, temp4)
						object[tempObjectPos].xvel = 0x40000
						object[tempObjectPos].yvel = 0x50000
						object[tempObjectPos].drawOrder = 4

						CreateTempObject(TypeName[Breakable Wall], true, temp3, temp4)
						object[tempObjectPos].xvel = 0x60000
						object[tempObjectPos].yvel = 0x60000
						object[tempObjectPos].drawOrder = 4
					end if

					if player[currentPlayer].animation == ANI_GLIDING
						player[currentPlayer].animation = ANI_GLIDING_DROP
						player[currentPlayer].state = Player_State_GlideDrop
						if player[currentPlayer].direction == FACING_LEFT
							FlipSign(player[currentPlayer].speed)
						end if
					end if
				end if
			else
				BoxCollisionTest(C_SOLID, object.entityPos, -16, -32, 16, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			end if
		next
	end if
end event


event ObjectDraw
	DrawSprite(object.propertyValue)
end event


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

	arrayPos0 = 0x20
	while arrayPos0 < 0x420
		if object[arrayPos0].type == TypeName[Breakable Wall]
			object[arrayPos0].isDebris = false
			object[arrayPos0].drawOrder = 4
		end if
		
		arrayPos0++
	loop

	SpriteFrame(-16, -32, 32, 64, 443, 34)
	SpriteFrame(-8, -8, 16, 16, 443, 34)

	SetTableValue(TypeName[Breakable Wall], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(BreakableWall_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(BreakableWall_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


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