// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: C Floor Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 58 : TYPE_CFLOOR

// Function declarations
reserve function CFloor_DebugDraw
reserve function CFloor_DebugSpawn

// Static Values
public value CFloor_value40 = 0;

// Tables
public table CFloor_table42
	1, 2, 3, 4, 5, 6, 7, 8
end table

public table CFloor_table43
	4, 3, 2, 1, 8, 7, 6, 5
end table

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

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

function CFloor_DebugDraw
	DrawSprite(0)
end function


function CFloor_DebugSpawn
	temp0 = object[0].value17
	temp0 -= CFloor_value40
	CreateTempObject(TypeName[C Floor], 0, object.xpos, object.ypos)
	if temp0 == 0
		object[tempObjectPos].value2 = CFloor_table1
	else
		object[tempObjectPos].value2 = CFloor_table0
	end if
	GetBit(temp0, object.direction, 0)
	if temp0 == 0
		object[tempObjectPos].direction = FLIP_NONE
	else
		object[tempObjectPos].direction = FLIP_X
	end if
end function


event ObjectMain
	switch object.state
	case 1
		if object.value0 < 10
			object.value0++
		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_table42)
					else
						GetTableValue(temp6, temp5, CFloor_table43)
					end if
					CreateTempObject(TypeName[C Floor], temp6, temp2, temp3)
					object[tempObjectPos].state = 4
					GetTableValue(object[tempObjectPos].value1, temp5, object.value2)
					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], 0)
			object.value0 = 0
			object.state++
		end if
		break
	case 2
		if object.value0 < 20
			object.value0++
		else
			object.value0 = 0
			object.state++
		end if
		break
	case 4
		if object.value0 < object.value1
			object.value0++
		else
			object.value0 = 0
			object.state++
		end if
		break
	case 5
		object.ypos += object.yvel
		object.yvel += 0x4000
		if object.outOfBounds == 1
			object.type = TypeName[Blank Object]
		end if
		break
	end switch
	if object.state < 4
		if object.outOfBounds == 1
			object.state = 0
			object.value0 = 0
			object.priority = PRIORITY_ACTIVE_BOUNDS
		end if
	end if
	if object.state < 3
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			if object.propertyValue < 2
				if object[currentPlayer].yvel >= 0
					BoxCollisionTest(C_PLATFORM, object.entityPos, -32, -16, 32, 0, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				else
					checkResult = 0
				end if
			else
				BoxCollisionTest(C_BOX, object.entityPos, -32, -16, 32, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			end if
			if checkResult == 1
				if object.state == 0
					object.state = 1
					object.priority = PRIORITY_ACTIVE
				end if
			end if
		next
	end if
end event


event ObjectDraw
	switch object.state
	case 0
	case 1
		DrawSprite(0)
		break
	case 4
	case 5
		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 == 0
			object[arrayPos0].value2 = CFloor_table1
		else
			object[arrayPos0].value2 = CFloor_table0
		end if
	next
	temp0 = 0
	CFloor_value40 = 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

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
