// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Belt Platform 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.stoodPlayers

// States
private alias 0 : BELTPLATFORM_INACTIVE
private alias 1 : BELTPLATFORM_APPEARING
private alias 2 : BELTPLATFORM_MOVING
private alias 3 : BELTPLATFORM_DISAPPEARING

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


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

event ObjectUpdate
	switch object.state
	case BELTPLATFORM_INACTIVE
		break

	case BELTPLATFORM_APPEARING
		object.animationTimer++
		if object.animationTimer == 4
			object.animationTimer = 0
			object.frame--
			if object.frame == 0
				object.state = BELTPLATFORM_MOVING
			end if
		end if
		break

	case BELTPLATFORM_MOVING
		object.ypos += object.yvel
		
		object.timer--
		if object.timer == 0
			object.state = BELTPLATFORM_DISAPPEARING
		end if

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

			SetBit(object.stoodPlayers, temp2, false)
			BoxCollisionTest(C_PLATFORM, object.entityPos, -24, -4, 24, 4, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				SetBit(object.stoodPlayers, temp2, true)
			end if
			temp2++
		next
		break

	case BELTPLATFORM_DISAPPEARING
		object.animationTimer++
		if object.animationTimer == 4
			object.animationTimer = 0
			object.frame++
			if object.frame == 3
				object.state = BELTPLATFORM_INACTIVE
			end if
		end if
		break
		
	end switch

	temp0 = object.xpos
	temp0 >>= 16
	temp0 -= camera[0].xpos
	Abs(temp0)
	temp0 -= 128
	if temp0 > screen.xcenter
		object.state 	= BELTPLATFORM_INACTIVE
		object.priority = PRIORITY_BOUNDS
	end if
end event


event ObjectDraw
	if object.state > BELTPLATFORM_INACTIVE
		DrawSprite(object.frame)
	end if
end event


event ObjectStartup
	LoadSpriteSheet("SCZ/Objects.gif")
	SpriteFrame(-24, -4, 48, 8, 80, 177)
	SpriteFrame(-24, -3, 48, 6, 80, 186)
	SpriteFrame(-24, -2, 48, 4, 80, 193)
end event


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

event RSDKDraw
	DrawSprite(0)
end event


event RSDKLoad
	LoadSpriteSheet("SCZ/Objects.gif")
	SpriteFrame(-24, -4, 48, 8, 80, 177)
	
	// In the scene it's normally set to 1, it doesn't seem to do anything though?
	SetVariableAlias(ALIAS_VAR_PROPVAL, "unused")
end event
