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

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

private alias object.value0 : object.timer
private alias object.value1 : object.delay

private alias 0 : CLEDGER_ACTIVE
private alias 1 : CLEDGER_COLLAPSE
private alias 2 : CLEDGER_COLLAPSED
private alias 3 : CLEDGER_NONE
private alias 4 : CLEDGER_TILEDELAY
private alias 5 : CLEDGER_TILEFALL

// Player Aliases
private alias object.xpos : player.xpos
private alias object.ypos : player.ypos
private alias object.yvel : player.yvel


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

reserve function CLedgeRight_DebugDraw
reserve function CLedgeRight_DebugSpawn


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

private function CLedgeRight_DebugDraw
	DrawSprite(0)
end function


private function CLedgeRight_DebugSpawn
	CreateTempObject(TypeName[C Ledge Right], 0, object.xpos, object.ypos)
end function


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

event ObjectUpdate
	switch object.state
	case CLEDGER_ACTIVE
		break

	case CLEDGER_COLLAPSE
		if object.timer < 10
			object.timer++
		else
			object.frame = 1
			temp0 = 0
			temp3 = object.ypos
			temp3 -= 0x380000
			temp4 = 30
			while temp0 < 6
				temp1 = 0
				temp2 = object.xpos
				temp2 -= 0x280000
				while temp1 < 6
					CreateTempObject(TypeName[C Ledge Right], object.frame, temp2, temp3)
					object[tempObjectPos].state = CLEDGER_TILEDELAY
					object[tempObjectPos].delay = temp4
					object.frame++
					temp1++
					temp2 += 0x100000
					temp4 -= 4
				loop
				temp4 += 22
				temp0++
				temp3 += 0x100000
			loop
			PlaySfx(SfxName[Ledge Break L], false)
			SetSfxAttributes(SfxName[Ledge Break L], -1, -100)
			PlaySfx(SfxName[Ledge Break R], false)
			SetSfxAttributes(SfxName[Ledge Break R], -1, 100)
			object.timer = 0
			object.state++
		end if
		break

	case CLEDGER_COLLAPSED
		if object.timer < 20
			object.timer++
		else
			object.timer = 0
			object.state++
			ResetObjectEntity(object.entityPos, TypeName[Blank Object], 0, object.xpos, object.ypos)
		end if
		break

	case CLEDGER_NONE
		break

	case CLEDGER_TILEDELAY
		if object.timer < object.delay
			object.timer++
		else
			object.timer = 0
			object.state++
		end if
		break

	case CLEDGER_TILEFALL
		object.ypos += object.yvel
		object.yvel += 0x4000
		if object.outOfBounds == true
			object.type = TypeName[Blank Object]
		end if
		break

	end switch
	
	if object.state < CLEDGER_NONE
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if player[currentPlayer].yvel >= 0
				temp0 = player[currentPlayer].xpos
				temp0 -= object.xpos
				temp0 >>= 17
				temp0 += 24
				if temp0 < 0
					temp0 = 0
				end if
				if temp0 > 47
					temp0 = 47
				end if
				GetTableValue(temp1, temp0, CLedgeLeft_heightArray)
				temp1 -= 84
				temp0 = temp1
				temp0 += 32
				BoxCollisionTest(C_PLATFORM, object.entityPos, -48, temp1, 48, temp0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				if checkResult == true
					player[currentPlayer].ypos += 0x40000
					if object.state == CLEDGER_ACTIVE
						object.state = CLEDGER_COLLAPSE
					end if
				end if
			end if
		next
	end if
end event


event ObjectDraw
	switch object.state
	case CLEDGER_ACTIVE
	case CLEDGER_COLLAPSE
		DrawSprite(0)
		break

	case CLEDGER_TILEDELAY
	case CLEDGER_TILEFALL
		DrawSprite(object.propertyValue)
		break

	case CLEDGER_COLLAPSED
	case CLEDGER_NONE
	default
		break
		
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("GHZ/Objects.gif")
	
	// 0 - Full Platform Frame
	SpriteFrame(-48, -64, 96, 96, 1, 148)
	
	// All the Platform "Tile" Frames are done in a loop
	temp0 = 148
	while temp0 < 244 // sprY + height (148 + 96 = 244)
		temp1 = 1
		while temp1 < 97 // sprX + width (1 + 96 = 97)
			SpriteFrame(-8, -8, 16, 16, temp1, temp0)
			temp1 += 16
		loop
		temp0 += 16
	loop
	
	SetTableValue(TypeName[C Ledge Right], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(CLedgeRight_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(CLedgeRight_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
	
	if editor.showGizmos == true
		editor.drawingOverlay = true
		
		temp0 = -48; temp1 = -48; temp2 = 48; temp3 = 48;
		CallFunction(EditorHelpers_DrawHitbox)
		
		editor.drawingOverlay = false
	end if
end event


event RSDKLoad
	LoadSpriteSheet("GHZ/Objects.gif")
	SpriteFrame(-48, -64, 96, 96, 1, 148)

	// Although used by the object, it shouldn't be set by the editor
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
