// ----------------------------------
// RSDK Project: Sonic 1
// Script Description: H Crush Girder 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.startPos.x
private alias object.value2 : object.startPos.y
private alias object.value3 : object.nextState

private alias 0 : HCRUSHGIRDER_MOVE_RIGHT
private alias 1 : HCRUSHGIRDER_MOVE_DOWN
private alias 2 : HCRUSHGIRDER_MOVE_LEFT
private alias 3 : HCRUSHGIRDER_MOVE_UP
private alias 4 : HCRUSHGIRDER_DELAY

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


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

reserve function HCrushGirder_DebugDraw
reserve function HCrushGirder_DebugSpawn


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

private function HCrushGirder_DebugDraw
	DrawSprite(0)
end function


private function HCrushGirder_DebugSpawn
	CreateTempObject(TypeName[H Crush Girder], 0, object.xpos, object.ypos)
	object[tempObjectPos].startPos.x = object.xpos
	object[tempObjectPos].startPos.y = object.ypos
end function


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

event ObjectUpdate
	switch object.state
	case HCRUSHGIRDER_MOVE_RIGHT
		object.timer++
		if object.timer == 96
			object.timer = 6
			object.nextState = HCRUSHGIRDER_MOVE_DOWN
			object.state = HCRUSHGIRDER_DELAY
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -96, -24, 96, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				player[currentPlayer].xpos += 0x10000
				break

			case COL_LEFT
				player[currentPlayer].pushing = false
				break
			end switch
		next

		object.xpos += 0x10000
		break

	case HCRUSHGIRDER_MOVE_DOWN
		object.timer++
		if object.timer == 48
			object.timer = 6
			object.nextState = HCRUSHGIRDER_MOVE_LEFT
			object.state = HCRUSHGIRDER_DELAY
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -96, -24, 96, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				player[currentPlayer].ypos += 0x10000
				break

			case COL_BOTTOM
				if player[currentPlayer].gravity == 0
					CallFunction(Player_Kill)
				end if
				break
			end switch
		next

		object.ypos += 0x10000
		break

	// actually moves left and up a bit
	case HCRUSHGIRDER_MOVE_LEFT
		object.timer++
		if object.timer == 96
			object.timer = 6
			object.nextState = HCRUSHGIRDER_MOVE_UP
			object.state = HCRUSHGIRDER_DELAY
		end if

		object.ypos -= 0x4000
		object.xpos -= 0x10000
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -96, -24, 96, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				player[currentPlayer].xpos -= 0x10000
				break

			case COL_BOTTOM
				break
			end switch
		next
		break

	case HCRUSHGIRDER_MOVE_UP
		object.timer++
		if object.timer == 24
			object.timer = 6
			object.nextState = HCRUSHGIRDER_MOVE_RIGHT
			object.state = HCRUSHGIRDER_DELAY
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -96, -24, 96, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			switch checkResult
			case COL_TOP
				player[currentPlayer].ypos -= 0x10000
				break

			case COL_BOTTOM
				break
			end switch
		next
		object.ypos -= 0x10000
		break

	case HCRUSHGIRDER_DELAY
		if object.timer > 0
			object.timer--
		else
			object.state = object.nextState
		end if

		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			BoxCollisionTest(C_SOLID, object.entityPos, -96, -24, 96, 24, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == COL_BOTTOM
				if player[currentPlayer].gravity == 0
					CallFunction(Player_Kill)
				end if
			end if
		next
		break
		
	end switch
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-96, -24, 192, 48, 319, 1)

	foreach (TypeName[H Crush Girder], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
	next

	SetTableValue(TypeName[H Crush Girder], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(HCrushGirder_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(HCrushGirder_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-96, -24, 192, 48, 319, 1)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
