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

//-------Aliases-------//
private alias 64 : TYPE_SMALLPISTON

// Function declarations
reserve function SmallPiston_Function140
reserve function SmallPiston_DebugDraw
reserve function SmallPiston_DebugSpawn

// Static Values

// Tables

function SmallPiston_Function140
	object[arrayPos0].ypos = object[arrayPos0].value1
	object[arrayPos0].ypos += object[arrayPos0].value31
	object[arrayPos0].value0 = 90
	object[arrayPos0].value2 = 0
	object[arrayPos0].state = 0
	object[arrayPos0].priority = PRIORITY_ACTIVE_BOUNDS
end function


function SmallPiston_DebugDraw
	DrawSprite(0)
end function


function SmallPiston_DebugSpawn
	CreateTempObject(TypeName[Small Piston], 0, object.xpos, object.ypos)
	arrayPos0 = object[tempObjectPos].entityPos
	object[arrayPos0].value1 = object.ypos
	CallFunction(SmallPiston_Function140)
end function


event ObjectMain
	temp0 = object.ypos
	switch object.state
	case 0
		object.value0--
		if object.value0 == 0
			object.priority = PRIORITY_ACTIVE
			object.value0 = 8
			object.state = 1
		end if
		break
	case 1
		object.ypos += 0x80000
		object.value0--
		if object.value0 == 0
			object.value0 = 90
			object.state = 2
		end if
		break
	case 2
		object.value0--
		if object.value0 == 0
			object.value0 = 8
			object.state = 3
		end if
		break
	case 3
		object.ypos -= 0x80000
		object.value0--
		if object.value0 == 0
			object.value0 = 90
			object.state = 0
		end if
		break
	end switch
	temp1 = object.ypos
	temp1 -= temp0
	temp2 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		GetBit(temp0, object.value2, temp2)
		if temp0 == 1
			object[currentPlayer].ypos += temp1
		end if
		SetBit(object.value2, temp2, 0)
		BoxCollisionTest(C_BOX, object.entityPos, -16, -32, 16, 32, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
		switch checkResult
		case 1
			SetBit(object.value2, temp2, 1)
			break
		case 4
			if object[currentPlayer].gravity == GRAVITY_GROUND
				CallFunction(PlayerObject_Kill)
			end if
			break
		end switch
		temp2++
	next
	if object.outOfBounds == 1
		arrayPos0 = object.entityPos
		CallFunction(SmallPiston_Function140)
	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].value1 = object[arrayPos0].ypos
		CallFunction(SmallPiston_Function140)
	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

event RSDKDraw
	DrawSprite(0)
end event

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