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

// ========================
// 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
private alias 2 : TUBESWITCH_RWALL_ENTRY
private alias 3 : TUBESWITCH_LWALL_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.collisionMode	: player.collisionMode
private alias object.angle 			: player.angle
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

				case 2
					if player[currentPlayer].gravity == GRAVITY_AIR
						if player[currentPlayer].yvel > 0
							player[currentPlayer].animation = ANI_JUMPING
							player[currentPlayer].state = Player_State_TubeRoll
							player[currentPlayer].gravity = GRAVITY_GROUND
							player[currentPlayer].speed = player[currentPlayer].yvel
							player[currentPlayer].collisionMode = CMODE_RWALL
							player[currentPlayer].angle = 0x40
							PlaySfx(SfxName[Rolling], false)
						end if
					end if
					break

				case 3
					if player[currentPlayer].gravity == GRAVITY_AIR
						if player[currentPlayer].yvel > 0
							player[currentPlayer].animation = ANI_JUMPING
							player[currentPlayer].state = Player_State_TubeRoll
							player[currentPlayer].gravity = GRAVITY_GROUND
							player[currentPlayer].speed = player[currentPlayer].yvel
							FlipSign(player[currentPlayer].speed)
							player[currentPlayer].collisionMode = CMODE_LWALL
							player[currentPlayer].angle = 0xC0
							PlaySfx(SfxName[Rolling], false)
						end if
					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
	DrawSprite(0)
end event


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

	AddEditorVariable("type")
	SetActiveVariable("type")
	AddEnumVariable("Enter From Right, Exit From Left", 0)
	AddEnumVariable("Enter From Left, Exit From Right", 1)
	AddEnumVariable("Enter From Top, Stick to RWall", 2)
	AddEnumVariable("Enter From Top, Stick to LWall", 3)
end event
