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

// This object seems to be unused, but it's just a basic roll object that doesn't really do all that much

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

private alias object.value0 : object.inTube // I dont really know what the point of this variable is but alright I guess

private alias 0 : TUBESWITCH_R_ENTRY
private alias 1 : TUBESWITCH_L_ENTRY

// Player Aliases
private alias object.state     : player.state
private alias object.speed     : player.speed
private alias object.xvel      : player.xvel
private alias object.yvel      : player.yvel
private alias object.gravity   : player.gravity
private alias object.direction : player.direction
private alias object.animation : player.animation
private alias object.value34   : player.collisionDisabled


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

event ObjectUpdate
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if player[currentPlayer].collisionDisabled == true
			player[currentPlayer].collisionDisabled = false
		end if

		CheckEqual(player[currentPlayer].state, Player_State_Death)
		temp0 = checkResult
		CheckEqual(player[currentPlayer].state, Player_State_LedgePullUp)
		temp0 |= checkResult
		CheckEqual(player[currentPlayer].state, Player_State_Climb)
		temp0 |= checkResult
		if temp0 == false
			BoxCollisionTest(C_TOUCH, object.entityPos, -16, -16, 16, 16, currentPlayer, C_BOX, C_BOX, C_BOX, C_BOX)
			if checkResult == true
				if player[currentPlayer].yvel < 0
					player[currentPlayer].yvel = 0
				end if

				switch object.propertyValue
				case TUBESWITCH_R_ENTRY // Enter from the right, exit from the left
					if player[currentPlayer].xvel > 0
						if player[currentPlayer].animation != ANI_JUMPING
							if player[currentPlayer].state != Player_State_TubeRoll
								PlaySfx(SfxName[Rolling], false)
							end if
						end if

						if player[currentPlayer].state == Player_State_GlideLeft
							FlipSign(player[currentPlayer].speed)
						end if

						player[currentPlayer].direction = FACING_RIGHT
						player[currentPlayer].state 	= Player_State_TubeRoll
						player[currentPlayer].animation = ANI_JUMPING
					else
						if player[currentPlayer].gravity == GRAVITY_GROUND
							if player[currentPlayer].xvel > -0x20000
								player[currentPlayer].speed = -0x20000
							end if
						end if

						player[currentPlayer].state = Player_State_Roll
						player[currentPlayer].animation = ANI_JUMPING
					end if
					break

				case TUBESWITCH_L_ENTRY // Enter from the left, exit from the right
					if player[currentPlayer].xvel < 0
						if player[currentPlayer].animation != ANI_JUMPING
							if player[currentPlayer].state != Player_State_TubeRoll
								PlaySfx(SfxName[Rolling], false)
							end if
						end if

						if player[currentPlayer].state == Player_State_GlideLeft
							FlipSign(player[currentPlayer].speed)
						end if

						player[currentPlayer].direction = FACING_LEFT
						player[currentPlayer].state 	= Player_State_TubeRoll
						player[currentPlayer].animation = ANI_JUMPING
					else
						if player[currentPlayer].gravity == GRAVITY_GROUND
							if player[currentPlayer].xvel < 0x20000
								player[currentPlayer].speed = 0x20000
							end if
						end if

						player[currentPlayer].state = Player_State_Roll
						player[currentPlayer].animation = ANI_JUMPING
					end if
					break
				end switch
				
				object.inTube = true
			else
				object.inTube = false
			end if
		end if
	next
end event


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

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


event RSDKDraw
	temp0 = object.xpos
	temp0 -= 0x80000
	temp1 = object.ypos
	temp1 -= 0x80000

	DrawSpriteXY(0, temp0, temp1)

	temp0 += 0x100000
	DrawSpriteXY(0, temp0, temp1)

	temp1 += 0x100000
	DrawSpriteXY(0, temp0, temp1)

	temp0 -= 0x100000
	DrawSpriteXY(0, temp0, temp1)
end event


event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-8, -8, 16, 16, 239, 239)

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Enter From Right, Exit From Left", 0)
	AddEnumVariable("Enter From Left, Exit From Right", 1)
end event
