You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
HighGroundRoyaleFusion/Assets/_Scripts/Offline/TopDownPlayerOffline.cs

121 lines
5.3 KiB
C#

using TMPro;
using UnityEngine;
using DG.Tweening;
using Sirenix.OdinInspector;
using Fusion.Addons.SimpleKCC;
[HideMonoScript]
public sealed class TopDownPlayerOffline : MonoBehaviour
{
//===================================================
// FIELDS
//===================================================
[Title("TOP DOWN PLAYER OFFLINE", titleAlignment: TitleAlignments.Centered)]
[SerializeField] KCCOffline _kcc = null;
[SerializeField] PlayerInput _input = null;
[SerializeField] AnimationController _animator = null;
[SerializeField] TMP_Text _userText = null;
//===================================================
// PRIVATE FIELDS
//===================================================
[SerializeField] MovementData _moveData = new();
//===================================================
// PROPERTIES
//===================================================
public float Speed => _moveData.Speed;
public KCCOffline KCC => _kcc;
public PlayerInput Input => _input;
private MovementSettings MoveSettings => GameData.Instance.MovementSettings;
//===================================================
// METHODS
//===================================================
private void Awake()
{
if(_kcc == null)
_kcc = GetComponent<KCCOffline>();
if(_input == null)
_input = GetComponent<PlayerInput>();
if (_moveData.MainCamera == null)
_moveData.MainCamera = GameObject.FindGameObjectWithTag("MainCamera");
if(_animator == null)
_animator = GetComponent<AnimationController>();
if(GameManager.Instance)
_userText.text = GameManager.Instance.UserName;
}//Spawned() end
private void FixedUpdate()
{
if(Input == null)
return;
_moveData.TargetSpeed = Input.CurrentInput.Sprint == true ? MoveSettings.SprintSpeed : MoveSettings.MoveSpeed;
if(Input.CurrentInput.MoveDirection != Vector2.zero)
{
_moveData.TargetRotation = KCC.GetLookRotation(false, true).y;
_moveData.TargetRotation = Mathf.Atan2(Input.CurrentInput.MoveDirection.x, Input.CurrentInput.MoveDirection.y) * Mathf.Rad2Deg + _moveData.MainCamera.transform.eulerAngles.y;
_moveData.TargetRotationMove = _moveData.TargetRotation + _moveData.MainCamera.transform.eulerAngles.y;
_moveData.TargetRotation = Mathf.SmoothDampAngle(transform.eulerAngles.y, _moveData.TargetRotation, ref _moveData.RotationVelocity, MoveSettings.RotationSmooth);
KCC.SetLookRotation(new Vector2(0f, _moveData.TargetRotation));
}//if end
else
_moveData.TargetRotationMove = 0f;
if(Input.CurrentInput.MoveDirection != Vector2.zero)
{
_moveData.InputDirection = Quaternion.Euler(0.0f, _moveData.TargetRotationMove, 0.0f) * Vector3.forward;
_moveData.InputDirection.x += Input.CurrentInput.MoveDirection.x;
_moveData.InputDirection.z += Input.CurrentInput.MoveDirection.y;
}//if end
else
_moveData.InputDirection = Vector3.zero;
// It feels better when the player falls quicker.
KCC.SetGravity(KCC.RealVelocity.y >= 0.0f ? MoveSettings.UpGravity : MoveSettings.DownGravity);
_moveData.MovementSpeed = Input.CurrentInput.MoveDirection.magnitude;
if(_moveData.MovementSpeed > 0.01f)
_moveData.Speed = Mathf.Lerp(_moveData.Speed, _moveData.MovementSpeed > 0.01f ? _moveData.TargetSpeed : 0f, Time.fixedDeltaTime * 10f);
else
_moveData.Speed = 0f;
_moveData.DesiredMoveVelocity = _moveData.InputDirection * _moveData.Speed;
if (KCC.ProjectOnGround(_moveData.DesiredMoveVelocity, out Vector3 projectedDesiredMoveVelocity) == true)
_moveData.DesiredMoveVelocity = Vector3.Normalize(projectedDesiredMoveVelocity) * _moveData.Speed;
if (_moveData.DesiredMoveVelocity == Vector3.zero)
_moveData.Acceleration = KCC.IsGrounded == true ? MoveSettings.GroundDeceleration : MoveSettings.AirDeceleration;
else
_moveData.Acceleration = KCC.IsGrounded == true ? MoveSettings.GroundAcceleration : MoveSettings.AirAcceleration;
_moveData.MoveVelocity = Vector3.Lerp(_moveData.MoveVelocity, _moveData.DesiredMoveVelocity, _moveData.Acceleration * Time.fixedDeltaTime);
KCC.Move(_moveData.MoveVelocity, _moveData.JumpImpulse);
Animate();
}//FixedUpdateNetwork() end
private void Animate()
{
_animator.Animate(KCC.IsGrounded, Speed, Time.deltaTime);
if (KCC.IsGrounded && Input.CurrentInput.AttackAction.WasPressed(Input.PreviousInput.AttackAction, NetworkedInput.ATTACK_BUTTON) == true)
_animator.Attack();
if (KCC.IsGrounded && Input.CurrentInput.JumpAction.WasPressed(Input.PreviousInput.JumpAction, NetworkedInput.JUMP_BUTTON) == true)
{
DOTween.To(()=> _moveData.JumpImpulse, x=> _moveData.JumpImpulse = x, MoveSettings.JumpImpulsePeak, 0.25f).SetEase(Ease.InExpo).OnComplete(()=>
DOTween.To(()=> _moveData.JumpImpulse, x=> _moveData.JumpImpulse = x, 0f, 0.25f).SetEase(Ease.InExpo));
_animator.Jump();
}//if end
}//Render() end
}//class end