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


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

// Super States
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], false, object.xpos, object.ypos)
	object[tempObjectPos].propertyValue = false
	object[tempObjectPos].drawOrder = 4
end function


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

event ObjectUpdate
	if object.propertyValue == 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[Block 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 == 1
							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("SLZ/Objects.gif")

	arrayPos0 = 32
	while arrayPos0 < 0x420
		if object[arrayPos0].type == TypeName[Breakable Wall]
			// LOL its forced to 0 to make sure you dont spawn tiles accidently
			object[arrayPos0].propertyValue = false
			object[arrayPos0].drawOrder 	= 4
		end if
		arrayPos0++
	loop

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

	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("SLZ/Objects.gif")
	SpriteFrame(-16, -32, 32, 64, 34, 26)
	
	// I think it actually uses the same values as the GHZ one, but its always "middle", so idc
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
