Files
Ultimate-Fishing-Simulator-…/Assets/Scripts/Assembly-CSharp/UFS3/PlayerMovementController.cs
2026-03-04 09:37:33 +08:00

329 lines
8.5 KiB
C#

using ECM2;
using Obvious.Soap;
using SoapCustomVariable;
using UnityEngine;
namespace UFS3
{
public class PlayerMovementController : MonoBehaviour
{
public CameraViewTypeVariable CameraView;
public FSMVariable PlayerState;
public Vector2Variable MovementDirection;
public BoolVariable IsLeaveWaterPossible;
public BoolVariable IsRunPressed;
public BoolVariable IsRightButtonPressed;
public BoolVariable IsBobberZoomInPressed;
public FloatVariable MovementSpeed;
public FloatVariable LineTension;
public FloatVariable FlySpeed;
public BoolVariable IsFlyModeEnabled;
public BoolVariable IsUnderwaterCameraEnabled;
public BoolVariable isEquiped;
public ScriptableEventNoParam OnJump;
public ScriptableEventNoParam OnJumpStop;
[SerializeField]
private Transform _CameraTPPTarget;
private Character _Character;
private PlayerFishingGearController _equipment;
private bool _IsInVehicle = true;
[SerializeField]
private float _RotateTPPSpeed = 10f;
private CapsuleCollider _CapsuleCollider;
[SerializeField]
private WaterCheckComponent _SwimChecker;
[SerializeField]
private WaterCheckComponent _MidWaterChecker;
[SerializeField]
private Transform _LeaveWaterChecker;
private Vector3 _LeaveWaterPositon;
private LayerMask _LeaveWaterLayerMask => LayerMask.GetMask("Default", "Terrain");
private LayerMask _groundMask => LayerMask.GetMask("Terrain", "Default");
private void Awake()
{
_equipment = GetComponent<PlayerFishingGearController>();
_Character = GetComponent<Character>();
_CapsuleCollider = GetComponent<CapsuleCollider>();
}
private void OnEnable()
{
OnJump.OnRaised += OnJumpOnOnRaised;
OnJumpStop.OnRaised += OnJumpStopOnOnRaised;
PlayerState.OnValueChanged += PlayerStateOnOnValueChanged;
IsFlyModeEnabled.OnValueChanged += FlyModeEnable;
}
private void OnDisable()
{
OnJump.OnRaised -= OnJumpOnOnRaised;
OnJumpStop.OnRaised -= OnJumpStopOnOnRaised;
PlayerState.OnValueChanged -= PlayerStateOnOnValueChanged;
IsFlyModeEnabled.OnValueChanged -= FlyModeEnable;
}
private void FlyModeEnable(bool value)
{
PlayerState.Value = (value ? State.flyModeDebug : State.idle);
}
private void PlayerStateOnOnValueChanged(State state)
{
switch (PlayerState.PreviousValue)
{
case State.vehicle:
case State.vehicleFishing:
_CapsuleCollider.enabled = true;
_Character.EnableGroundConstraint(enable: true);
break;
case State.swiming:
IsLeaveWaterPossible.Value = false;
break;
}
switch (state)
{
case State.swiming:
_Character.SetMovementMode(Character.MovementMode.Swimming);
return;
case State.vehicle:
_Character.SetMovementMode(Character.MovementMode.None);
_CapsuleCollider.enabled = false;
return;
case State.idle:
case State.move:
_Character.SetMovementMode(Character.MovementMode.Walking);
_CapsuleCollider.enabled = true;
return;
case State.flyModeDebug:
_Character.SetMovementMode(Character.MovementMode.Flying);
return;
case State.vehicleFishing:
_CapsuleCollider.enabled = false;
_Character.SetMovementMode(Character.MovementMode.None);
return;
}
if (_IsInVehicle)
{
_CapsuleCollider.enabled = false;
}
}
private void OnJumpOnOnRaised()
{
if (IsUnderwaterCameraEnabled.Value)
{
return;
}
switch (PlayerState.Value)
{
case State.idle:
case State.move:
case State.flyModeDebug:
if (!isEquiped.Value)
{
_Character.Jump();
}
break;
case State.swiming:
if (IsLeaveWaterPossible.Value)
{
base.transform.position = _LeaveWaterPositon;
PlayerState.Value = State.idle;
}
break;
}
}
private void OnJumpStopOnOnRaised()
{
_Character.StopJumping();
}
private void Update()
{
if (PlayerState.Value != State.swiming && _SwimChecker.IsInWater())
{
PlayerState.Value = State.swiming;
}
switch (PlayerState.Value)
{
default:
_Character.SetMovementDirection(Vector2.zero);
break;
case State.idle:
if (MovementDirection.Value != Vector2.zero)
{
PlayerState.Value = State.move;
}
break;
case State.move:
if (MovementDirection.Value == Vector2.zero)
{
PlayerState.Value = State.idle;
}
MovementHandler(isEquiped.Value);
break;
case State.swiming:
UpdatePlayerBuoyancy();
MovementHandler(checkWaterBound: false);
CheckLeaveWater();
break;
case State.fishing:
case State.fight:
if (IsBobberZoomInPressed.Value && _equipment.EquipedFishingSet.attachedBobber != null)
{
Vector3 position = _equipment.EquipedFishingSet.attachedBobber.transform.position;
position.y = base.transform.position.y;
_Character.RotateTowards(position - base.transform.position, Time.deltaTime * 1f);
}
if (!IsRightButtonPressed.Value && !IsBobberZoomInPressed.Value)
{
MovementHandler();
}
else
{
_Character.SetMovementDirection(Vector3.zero);
}
break;
case State.flyModeDebug:
MovementHandler(checkWaterBound: false);
if (Input.GetKey(KeyCode.Space))
{
_Character.SetMovementDirection(Vector3.up * 10f * FlySpeed.Value * Time.deltaTime);
}
if (Input.GetKey(KeyCode.C))
{
_Character.SetMovementDirection(Vector3.down * 10f * FlySpeed.Value * Time.deltaTime);
}
break;
case State.vehicle:
break;
}
}
private void CheckLeaveWater()
{
if (!_MidWaterChecker.IsInWater() && !_SwimChecker.IsInWater())
{
PlayerState.Value = State.idle;
return;
}
bool value = false;
if (Physics.Raycast(_LeaveWaterChecker.position, Vector3.down, out var hitInfo, 1f, _LeaveWaterLayerMask))
{
value = true;
_LeaveWaterPositon = hitInfo.point;
}
IsLeaveWaterPossible.Value = value;
}
private void UpdatePlayerBuoyancy()
{
Vector3 velocity = _Character.velocity;
if (_SwimChecker.IsInWater())
{
velocity.y += 9f * Time.deltaTime;
velocity.y = Mathf.Clamp(velocity.y, -1f, 1f);
velocity.x = Mathf.MoveTowards(velocity.x, 0f, Time.deltaTime);
velocity.z = Mathf.MoveTowards(velocity.z, 0f, Time.deltaTime);
_Character.SetVelocity(velocity);
}
else
{
velocity.y *= 0.45f * Time.deltaTime;
_Character.SetVelocity(velocity);
}
}
private void MovementHandler(bool checkWaterBound = true)
{
if (IsUnderwaterCameraEnabled.Value)
{
return;
}
if (CameraView.Value == CameraViewType.TPP)
{
float num = (IsRunPressed.Value ? MovementSpeed.Value : (MovementSpeed.Value * 0.5f));
num = (IsFlyModeEnabled ? (num * (float)FlySpeed) : num);
Vector3 zero = Vector3.zero;
zero += Vector3.right * MovementDirection.Value.x;
zero += Vector3.forward * MovementDirection.Value.y;
zero = zero.relativeTo(_CameraTPPTarget, _Character.GetUpVector());
_Character.RotateTowards(zero, Time.deltaTime * _RotateTPPSpeed);
float value = Vector3.Dot(_Character.GetForwardVector(), zero);
Vector3 vector = _Character.GetForwardVector() * Mathf.Clamp01(value) * num;
if (checkWaterBound)
{
SetMovementDirectionWithRaycastCheck(vector);
}
else
{
_Character.SetMovementDirection(vector);
}
}
else
{
float num2 = (IsRunPressed.Value ? MovementSpeed.Value : (MovementSpeed.Value * 0.5f));
num2 = (IsFlyModeEnabled ? (num2 * (float)FlySpeed) : num2);
Vector3 vector2 = _Character.GetRightVector() * MovementDirection.Value.x * num2;
vector2 += _Character.GetForwardVector() * MovementDirection.Value.y * num2;
if (checkWaterBound)
{
SetMovementDirectionWithRaycastCheck(vector2);
}
else
{
_Character.SetMovementDirection(vector2);
}
}
if (checkWaterBound && (_Character.IsJumping() || _Character.IsFalling()))
{
Vector3 vector3 = _Character.velocity * Time.deltaTime;
RaycastHit hitInfo;
bool num3 = Physics.Raycast(new Ray(base.transform.position + Vector3.up + vector3, Vector3.down), out hitInfo, float.PositiveInfinity, _groundMask);
Vector3 vector4 = vector3;
Debug.Log("is jumping" + vector4.ToString());
if (num3 && hitInfo.point.y < -0.977f)
{
vector3.x = (vector3.z = 0f);
_Character.SetVelocity(Vector3.zero);
}
}
}
private void SetMovementDirectionWithRaycastCheck(Vector3 moveDir)
{
RaycastHit hitInfo;
bool flag = Physics.Raycast(new Ray(base.transform.position + Vector3.up + moveDir, Vector3.down), out hitInfo, float.PositiveInfinity, _groundMask);
_Character.SetMovementDirection((flag && hitInfo.point.y < -0.977f) ? Vector3.zero : moveDir);
}
}
}