// ----------------------------------
// 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 52 : TYPE_CFLOOR

// Function declarations
reserve function CFloor_DebugDraw
reserve function CFloor_DebugSpawn

// Static Values

// Tables
public table CFloor_table46
	1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
end table

public table CFloor_table47
	6, 5, 4, 3, 2, 1, 12, 11, 10, 9, 8, 7
end table

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

function CFloor_DebugDraw
	DrawSprite(0)
end function


function CFloor_DebugSpawn
	CreateTempObject(TypeName[C Floor], 0, object.xpos, object.ypos)
	object[tempObjectPos].value2 = CFloor_table0
	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
			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_table46)
					else
						GetTableValue(temp6, temp5, CFloor_table47)
					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
				loop
				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 < 32
			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
		temp0 = object.xpos
		temp0 >>= 16
		temp0 -= screen.cameraX
		Abs(temp0)
		temp1 = screen.xcenter
		temp1 += 128
		if temp0 > temp1
			object.state = 0
			object.value0 = 0
			if object.priority != PRIORITY_XBOUNDS_DESTROY
				object.priority = PRIORITY_ACTIVE_BOUNDS
			end if
		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, -48, -16, 48, 0, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				else
					checkResult = 0
				end if
			else
				BoxCollisionTest(C_BOX, object.entityPos, -48, -16, 48, 16, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			end if
			if checkResult == 1
				if object.state == 0
					object.state = 1
					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 0
	case 1
		DrawSprite(0)
		break
	case 4
	case 5
		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].value2 = CFloor_table0
	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

event RSDKDraw
	DrawSprite(0)
end event

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