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

//-------Aliases-------//
private alias 49 : TYPE_FALLINGPILLAR

// Function declarations
reserve function FallingPillar_DebugDraw
reserve function FallingPillar_DebugSpawn

// Static Values
private value FallingPillar_value37 = 0;

// Tables

function FallingPillar_DebugDraw
	DrawSprite(0)
	DrawSprite(1)
	DrawSprite(2)
end function


function FallingPillar_DebugSpawn
	temp0 = object[0].value17
	temp0 -= FallingPillar_value37
	CreateTempObject(TypeName[Falling Pillar], temp0, object.xpos, object.ypos)
	object[tempObjectPos].value3 = object[tempObjectPos].ypos
	object[tempObjectPos].state = temp0
	object[tempObjectPos].state <<= 2
end function


event ObjectMain
	if object.value0 == 1
		if object.value2 == 0
			object.value2 = 30
		end if
		if object.value4 < 64
			object.value4 += 4
		end if
	else
		if object.value4 > 0
			object.value4 -= 4
		end if
	end if
	temp0 = object.ypos
	temp0 &= -0x10000
	switch object.state
	case 0
		temp1 = object.value4
		temp1 <<= 1
		Sin(temp2, temp1)
		temp2 <<= 9
		object.ypos = object.value3
		object.ypos += temp2
		if object.value2 != 0
			object.value2--
			if object.value2 == 0
				object.state++
				object.priority = PRIORITY_ACTIVE
			end if
		end if
		break
	case 1
		object.ypos += object.yvel
		object.yvel += 0x800
		ObjectTileCollision(CSIDE_FLOOR, 0, 65, 0)
		if checkResult == 1
			object.state++
		end if
		if object.outOfBounds == 1
			object.ypos = object.value3
			object.yvel = 0
			object.value2 = 0
			object.state = 3
		end if
		break
	case 2
		if object.outOfBounds == 1
			object.ypos = object.value3
			object.yvel = 0
			object.value2 = 0
			object.state++
		end if
		break
	case 3
		if object.outOfBounds == 1
			object.state = 0
			object.priority = PRIORITY_ACTIVE_BOUNDS
		end if
		break
	case 4
		break
	end switch
	object.value1 = object.ypos
	object.value1 &= -0x10000
	object.value1 -= temp0
	object.value0 = 0
	if object.state != 3
		temp1 = object.ypos
		object.ypos = temp0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_BOX, object.entityPos, -16, -32, 16, 65, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object.value0 = 1
				object[currentPlayer].ypos += object.value1
			end if
			if checkResult == 4
				if object[currentPlayer].gravity == GRAVITY_GROUND
					CallFunction(PlayerObject_Kill)
				end if
			end if
		next
		object.ypos = temp1
	end if
end event


event ObjectDraw
	DrawSprite(0)
	DrawSprite(1)
	DrawSprite(2)
end event


event ObjectStartup
	LoadSpriteSheet("ARZ/Objects.gif")
	SpriteFrame(-28, -32, 56, 56, 59, 42)
	SpriteFrame(-16, 24, 32, 8, 140, 80)
	SpriteFrame(-16, 32, 32, 37, 173, 38)
	temp0 = 0
	FallingPillar_value37 = DebugMode_ObjCount
	while temp0 < 2
		SetTableValue(TypeName[Falling Pillar], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(FallingPillar_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(FallingPillar_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
	foreach (TypeName[Falling Pillar], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].value3 = object[arrayPos0].ypos
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].state = 4
		end if
	next
end event

event RSDKDraw
	DrawSprite(0)
end event

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