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

// ========================
// Aliases
// ========================

private alias object.value0 : object.stood
private alias object.value1 : object.collisionOffset.y
private alias object.value2 : object.timer
private alias object.value3 : object.centerPos.y
private alias object.value4 : object.stoodAngle

// Types
private alias 0 : FPILLAR_FALL
private alias 1 : FPILLAR_STATIC

// States
private alias 0 : FPILLAR_IDLE
private alias 1 : FPILLAR_FALLING_SOLID
private alias 2 : FPILLAR_FALLING
private alias 3 : FPILLAR_OFFSCREEN
private alias 4 : FPILLAR_NOFALL

// Player Aliases
private alias object.ypos : player.ypos
private alias object.gravity : player.gravity

private alias object.value17 : debugMode.currentSelection

// Path ID Aliases
private alias 0 : PATH_A


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

reserve function FallingPillar_DebugDraw
reserve function FallingPillar_DebugSpawn


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

private value FallingPillar_startDebugID = 0


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

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


private function FallingPillar_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= FallingPillar_startDebugID
	CreateTempObject(TypeName[Falling Pillar], temp0, object.xpos, object.ypos)
	object[tempObjectPos].centerPos.y = object[tempObjectPos].ypos
	object[tempObjectPos].state = temp0
	object[tempObjectPos].state <<= 2
end function


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

event ObjectUpdate
	if object.stood == true
		if object.timer == 0
			object.timer = 30
		end if

		if object.stoodAngle < 64
			object.stoodAngle += 4
		end if
	else
		if object.stoodAngle > 0
			object.stoodAngle -= 4
		end if
	end if
	temp0 = object.ypos
	temp0 &= 0xFFFF0000

	switch object.state
	case FPILLAR_IDLE
		temp1 = object.stoodAngle
		temp1 <<= 1
		Sin(temp2, temp1)
		temp2 <<= 9
		object.ypos = object.centerPos.y
		object.ypos += temp2
		if object.timer != 0
			object.timer--
			if object.timer == 0
				object.state++
				object.priority = PRIORITY_ACTIVE
			end if
		end if
		break

	case FPILLAR_FALLING_SOLID
		object.ypos += object.yvel
		object.yvel += 0x800

		ObjectTileCollision(CSIDE_FLOOR, 0, 65, PATH_A)
		if checkResult == true
			object.state++
		end if

		if object.outOfBounds == true
			object.ypos = object.centerPos.y
			object.yvel = 0
			object.timer = 0
			object.state = 3
		end if
		break
		// [Fallthrough]
	case FPILLAR_FALLING
		if object.outOfBounds == true
			object.ypos = object.centerPos.y
			object.yvel = 0
			object.timer = 0
			object.state++
		end if
		break

	case FPILLAR_OFFSCREEN
		if object.outOfBounds == true
			object.state = FPILLAR_IDLE
			object.priority = PRIORITY_BOUNDS
		end if
		break

	case FPILLAR_NOFALL
		break
		
	end switch

	object.collisionOffset.y = object.ypos
	object.collisionOffset.y &= 0xFFFF0000
	object.collisionOffset.y -= temp0

	object.stood = false
	if object.state != FPILLAR_OFFSCREEN
		temp1 = object.ypos
		object.ypos = temp0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -16, -32, 16, 65, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_TOP
				object.stood = true
				player[currentPlayer].ypos += object.collisionOffset.y
			end if

			if checkResult == COL_BOTTOM
				if player[currentPlayer].gravity == GRAVITY_GROUND
					CallFunction(Player_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")

	// Pillar frames
	SpriteFrame(-28, -32, 56, 56, 59, 42)
	SpriteFrame(-16, 24, 32, 8, 140, 80)
	SpriteFrame(-16, 32, 32, 37, 173, 38)

	// Add the Falling Pillars to the debug mode object list
	temp0 = 0
	FallingPillar_startDebugID = 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].centerPos.y = object[arrayPos0].ypos
		if object[arrayPos0].propertyValue == FPILLAR_STATIC
			object[arrayPos0].state = FPILLAR_NOFALL
		end if
	next
end event


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

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


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


event RSDKLoad
	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)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Falling", FPILLAR_FALL)
	AddEnumVariable("Static", FPILLAR_STATIC)
end event
