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

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

private alias object.value0 : object.moveDistance
private alias object.value1 : object.movePos
private alias object.value2 : object.timer

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

private alias object.value17 : debugMode.currentSelection


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

reserve function Piston_DebugDraw
reserve function Piston_DebugSpawn


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

private value Piston_startDebugID = 0


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

private table Piston_moveDistances
	0x380000, 0x400000, 0x600000
end table


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

private function Piston_DebugDraw
	temp0 = debugMode[0].currentSelection
	temp0 -= Piston_startDebugID
	DrawSprite(0)
end function


private function Piston_DebugSpawn
	temp0 = debugMode[0].currentSelection
	temp0 -= Piston_startDebugID
	CreateTempObject(TypeName[Piston], temp0, object.xpos, object.ypos)
	GetTableValue(object[tempObjectPos].moveDistance, object[tempObjectPos].propertyValue, Piston_moveDistances)
end function


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

event ObjectUpdate
	if object.timer != 0
		object.timer--
		object.yvel = 0
	else
		if object.direction == FLIP_NONE
			object.yvel = 0x80000
			object.movePos += object.yvel

			if object.movePos >= object.moveDistance
				object.direction ^= FLIP_X
				if object.propertyValue != 0
					object.timer = 60
				end if
			end if
		else
			if object.propertyValue != 0
				object.yvel = -0x80000
			else
				object.yvel = -0x10000
			end if

			object.movePos += object.yvel
			if object.movePos <= 0
				object.direction ^= FLIP_X
				object.timer = 60
			end if
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_SOLID2, object.entityPos, -28, -32, 28, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		
		switch checkResult
		case COL_TOP
			player[currentPlayer].ypos += object.yvel
			break

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

	object.ypos += object.yvel
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-28, -32, 56, 64, 132, 1)

	foreach (TypeName[Piston], arrayPos0, ALL_ENTITIES)
		GetTableValue(object[arrayPos0].moveDistance, object[arrayPos0].propertyValue, Piston_moveDistances)
	next

	temp0 = 0
	Piston_startDebugID = DebugMode_ObjCount
	while temp0 < 3
		SetTableValue(TypeName[Piston], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(Piston_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(Piston_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event


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

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


event RSDKDraw
	DrawSprite(0)

	if editor.showGizmos == true
		editor.drawingOverlay = true
		
		temp0 = object.propertyValue
		temp0 %= 3
		GetTableValue(object.moveDistance, temp0, Piston_moveDistances)

		temp1 = object.ypos
		temp1 += object.moveDistance
		
		object.inkEffect = INK_BLEND
		DrawSpriteFX(0, FX_INK, object.xpos, temp1)

		editor.drawingOverlay = false
	end if
end event


event RSDKLoad
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-28, -32, 56, 64, 132, 1)

	AddEditorVariable("moveDist")
	SetActiveVariable("moveDist")
	AddEnumVariable("56 Pixels", 0)
	AddEnumVariable("64 Pixels", 1)
	AddEnumVariable("96 Pixels", 2)
end event
