// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Small Piston 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.y
private alias object.value2  : object.stoodPlayers
private alias object.value31 : object.wrapOffset.y

// States
private alias 0 : SMALLPISTON_IDLE_UP
private alias 1 : SMALLPISTON_MOVEDOWN
private alias 2 : SMALLPISTON_IDLE_DOWN
private alias 3 : SMALLPISTON_MOVEUP

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


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

reserve function SmallPiston_Setup
reserve function SmallPiston_DebugDraw
reserve function SmallPiston_DebugSpawn

private function SmallPiston_Setup
	object[arrayPos0].ypos 		   	= object[arrayPos0].startPos.y
	object[arrayPos0].ypos 		   += object[arrayPos0].wrapOffset.y
	object[arrayPos0].timer 		= 90
	object[arrayPos0].stoodPlayers = 0
	object[arrayPos0].state 		= SMALLPISTON_IDLE_UP
	object[arrayPos0].priority 		= PRIORITY_BOUNDS
end function


private function SmallPiston_DebugDraw
	DrawSprite(0)
end function


private function SmallPiston_DebugSpawn
	CreateTempObject(TypeName[Small Piston], 0, object.xpos, object.ypos)
	arrayPos0 = object[tempObjectPos].entityPos
	object[arrayPos0].startPos.y = object.ypos
	CallFunction(SmallPiston_Setup)
end function


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

event ObjectUpdate
	temp0 = object.ypos
	switch object.state
	case SMALLPISTON_IDLE_UP
		object.timer--
		if object.timer == 0
			object.priority = PRIORITY_ACTIVE
			object.timer = 8
			object.state = SMALLPISTON_MOVEDOWN
		end if
		break

	case SMALLPISTON_MOVEDOWN
		object.ypos += 0x80000

		object.timer--
		if object.timer == 0
			object.timer = 90
			object.state = SMALLPISTON_IDLE_DOWN
		end if
		break

	case SMALLPISTON_IDLE_DOWN
		object.timer--
		if object.timer == 0
			object.timer = 8
			object.state = SMALLPISTON_MOVEUP
		end if
		break

	case SMALLPISTON_MOVEUP
		object.ypos -= 0x80000

		object.timer--
		if object.timer == 0
			object.timer = 90
			object.state = SMALLPISTON_IDLE_UP
		end if
		break
		
	end switch

	temp1 = object.ypos
	temp1 -= temp0

	temp2 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		GetBit(temp0, object.stoodPlayers, temp2)
		if temp0 == true
			player[currentPlayer].ypos += temp1
		end if

		SetBit(object.stoodPlayers, temp2, false)
		BoxCollisionTest(C_SOLID, object.entityPos, -16, -32, 16, 32, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
		switch checkResult
		case COL_TOP
			SetBit(object.stoodPlayers, temp2, true)
			break

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

	if object.outOfBounds == true
		arrayPos0 = object.entityPos
		CallFunction(SmallPiston_Setup)
	end if
end event


event ObjectDraw
	DrawSprite(0)
end event


event ObjectStartup
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-16, -32, 32, 64, 1, 70)

	foreach (TypeName[Small Piston], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		CallFunction(SmallPiston_Setup)
	next

	SetTableValue(TypeName[Small Piston], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(SmallPiston_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(SmallPiston_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("MPZ/Objects.gif")
	SpriteFrame(-16, -32, 32, 64, 1, 70)
	
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
