// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: C Floor 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 object.value2 : object.collapseTable

private alias 0 : CFLOOR_ACTIVE
private alias 1 : CFLOOR_COLLAPSE
private alias 2 : CFLOOR_COLLAPSED
private alias 3 : CFLOOR_NONE
private alias 4 : CFLOOR_TILEDELAY
private alias 5 : CFLOOR_TILEFALL

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

private alias object.value17 : debugMode.currentSelection


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

reserve function CFloor_DebugDraw
reserve function CFloor_DebugSpawn


// ========================
// Static Values
// ========================

private value CFloor_debugStartID = 0


// ========================
// Tables
// ========================

private table CFloor_tileFramesFlipX
	1, 2, 3, 4
	5, 6, 7, 8
end table

private table CFloor_tileFramesNoFlip
	4, 3, 2, 1
	8, 7, 6, 5
end table

private table CFloor_collapseTableL
	30, 22, 14, 6
	26, 18, 10, 2
end table

private table CFloor_collapseTableB
	22, 30, 26, 18
	 6, 14, 10, 2
end table


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

private function CFloor_DebugDraw
	DrawSprite(0)
end function


private function CFloor_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= CFloor_debugStartID
	CreateTempObject(TypeName[C Floor], 0, object.xpos, object.ypos)
	if temp0 == 0
		object[tempObjectPos].collapseTable = CFloor_collapseTableB
	else
		object[tempObjectPos].collapseTable = CFloor_collapseTableL
	end if
	GetBit(temp0, object.direction, 0)
	if temp0 == FACING_RIGHT
		object[tempObjectPos].direction = FLIP_NONE
	else
		object[tempObjectPos].direction = FLIP_X
	end if
end function


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

event ObjectUpdate
	switch object.state
	default
	case CFLOOR_ACTIVE
		break

	case CFLOOR_COLLAPSE
		if object.timer < 10
			object.timer++
		else
			temp5 = 0
			temp0 = 0
			temp3 = object.ypos
			temp3 -= 0x80000
			temp4 = 10
			while temp0 < 2
				temp1 = 0
				temp2 = object.xpos
				if object.direction == FLIP_X
					temp2 -= 0x180000
				else
					temp2 += 0x180000
				end if

				while temp1 < 4
					if object.direction == FLIP_X
						GetTableValue(temp6, temp5, CFloor_tileFramesFlipX)
					else
						GetTableValue(temp6, temp5, CFloor_tileFramesNoFlip)
					end if
					
					CreateTempObject(TypeName[C Floor], temp6, temp2, temp3)
					object[tempObjectPos].state = CFLOOR_TILEDELAY
					GetTableValue(object[tempObjectPos].delay, temp5, object.collapseTable)
					temp5++
					temp6++
					temp1++
					if object.direction == FLIP_X
						temp2 += 0x100000
					else
						temp2 -= 0x100000
					end if
					temp4 += 4
				loop
				temp4 -= 26
				object.frame += 2
				temp0++
				temp3 += 0x100000
			loop
			PlaySfx(SfxName[Ledge Break], false)
			object.timer = 0
			object.state++
		end if
		break

	case CFLOOR_COLLAPSED
		if object.timer < 20
			object.timer++
		else
			object.timer = 0
			object.state++
		end if
		break

	case CFLOOR_NONE
		break

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

	case CFLOOR_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 < CFLOOR_TILEDELAY
		if object.outOfBounds == true
			object.state = CFLOOR_ACTIVE
			object.timer = 0
			object.priority = PRIORITY_BOUNDS
		end if
	end if

	if object.state < CFLOOR_NONE
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.propertyValue < 2
				if player[currentPlayer].yvel >= 0
					BoxCollisionTest(C_PLATFORM, object.entityPos, -32, -16, 32, 0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				else
					checkResult = COL_NONE
				end if
			else
				BoxCollisionTest(C_SOLID, object.entityPos, -32, -16, 32, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			end if

			if checkResult == COL_TOP
				if object.state == CFLOOR_ACTIVE
					object.state = CFLOOR_COLLAPSE
					object.priority = PRIORITY_ACTIVE
				end if
			end if
		next
	end if
end event


event ObjectDraw
	switch object.state
	case CFLOOR_ACTIVE
	case CFLOOR_COLLAPSE
		DrawSprite(0)
		break

	case CFLOOR_COLLAPSED
	case CFLOOR_NONE
		break

	case CFLOOR_TILEDELAY
	case CFLOOR_TILEFALL
		DrawSprite(object.propertyValue)
		break
		
	end switch
end event


event ObjectStartup
	LoadSpriteSheet("ARZ/Objects.gif")
	SpriteFrame(-32, -16, 64, 32, 126, 112)
	SpriteFrame(-8, -8, 16, 16, 126, 112)
	SpriteFrame(-8, -8, 16, 16, 142, 112)
	SpriteFrame(-8, -8, 16, 16, 158, 112)
	SpriteFrame(-8, -8, 16, 16, 174, 112)
	SpriteFrame(-8, -8, 16, 16, 126, 128)
	SpriteFrame(-8, -8, 16, 16, 142, 128)
	SpriteFrame(-8, -8, 16, 16, 158, 128)
	SpriteFrame(-8, -8, 16, 16, 174, 128)

	foreach (TypeName[C Floor], arrayPos0, ALL_ENTITIES)
		GetBit(temp5, object[arrayPos0].propertyValue, 0)
		if temp5 == false
			object[arrayPos0].collapseTable = CFloor_collapseTableB
		else
			object[arrayPos0].collapseTable = CFloor_collapseTableL
		end if
	next

	temp0 = 0
	CFloor_debugStartID = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[C Floor], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(CFloor_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(CFloor_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			checkResult &= 1
			break
			
		case 0 // type
			GetBit(checkResult, object.propertyValue, 0)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 1
			break
			
		case 0 // type
			CheckNotEqual(editor.variableValue, 0)
			SetBit(object.propertyValue, 0, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("ARZ/Objects.gif")
	SpriteFrame(-32, -16, 64, 32, 126, 112)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Bottom", 0)
	AddEnumVariable("Left", 1)
end event
