// ----------------------------------
// 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


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

reserve function CFloor_DebugDraw
reserve function CFloor_DebugSpawn


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

private table CFloor_tileFramesFlipX
	1, 2, 3,  4,  5,  6
	7, 8, 9, 10, 11, 12
end table

private table CFloor_tileFramesNoFlip
	 6,  5,  4, 3, 2, 1
	12, 11, 10, 9, 8, 7
end table

private table CFloor_collapseTable
	30, 38, 46, 42, 34, 26
	 6, 14, 22, 18, 10,  2
end table


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

private function CFloor_DebugDraw
	DrawSprite(0)
end function


private function CFloor_DebugSpawn
	CreateTempObject(TypeName[C Floor], 0, object.xpos, object.ypos)
	object[tempObjectPos].collapseTable = CFloor_collapseTable
	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
			while temp0 < 2
				temp1 = 0
				temp2 = object.xpos
				if object.direction == FLIP_X
					temp2 -= 0x280000
				else
					temp2 += 0x280000
				end if

				while temp1 < 6
					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
				loop
				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 < 32
			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
		temp0 = object.xpos
		temp0 >>= 16
		temp0 -= camera[0].xpos
		Abs(temp0)
		temp1 = screen.xcenter
		temp1 += 128
		if temp0 > temp1
			object.state = CFLOOR_ACTIVE
			object.timer = 0
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_BOUNDS
			end if
		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, -48, -16, 48, 0, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
				else
					checkResult = COL_NONE
				end if
			else
				BoxCollisionTest(C_SOLID, object.entityPos, -48, -16, 48, 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
					if object.priority != PRIORITY_XBOUNDS_DESTROY
						object.priority = PRIORITY_ACTIVE
					end if
				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("HPZ/Objects.gif")
	SpriteFrame(-48, -16, 96, 32, 66, 223)
	SpriteFrame(-8, -8, 16, 16, 66, 223)
	SpriteFrame(-8, -8, 16, 16, 82, 223)
	SpriteFrame(-8, -8, 16, 16, 98, 223)
	SpriteFrame(-8, -8, 16, 16, 114, 223)
	SpriteFrame(-8, -8, 16, 16, 130, 223)
	SpriteFrame(-8, -8, 16, 16, 146, 223)
	SpriteFrame(-8, -8, 16, 16, 66, 239)
	SpriteFrame(-8, -8, 16, 16, 82, 239)
	SpriteFrame(-8, -8, 16, 16, 98, 239)
	SpriteFrame(-8, -8, 16, 16, 114, 239)
	SpriteFrame(-8, -8, 16, 16, 130, 239)
	SpriteFrame(-8, -8, 16, 16, 146, 239)

	foreach (TypeName[C Floor], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].collapseTable = CFloor_collapseTable
	next

	SetTableValue(TypeName[C Floor], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(CFloor_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(CFloor_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			checkResult &= 3
			break
			
		case 0 // type
			checkResult = object.propertyValue
			checkResult &= 3
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			object.propertyValue &= 3
			break
			
		case 0 // type
			object.propertyValue = editor.variableValue
			object.propertyValue &= 3
			break
			
		end switch
	end if
end event


event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("HPZ/Objects.gif")
	SpriteFrame(-48, -16, 96, 32, 66, 223)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Bottom (Top Solid)", 0)
	AddEnumVariable("Bottom (All Solid)", 2)
	// 1 & 3 go unused since direction is set via editor rather than as a propertyValue
end event
