|
|
@ -1,4 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
using Fusion;
|
|
|
|
using Fusion;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine;
|
|
|
|
using DG.Tweening;
|
|
|
|
using DG.Tweening;
|
|
|
@ -7,103 +6,104 @@ using Fusion.Addons.SimpleKCC;
|
|
|
|
[DefaultExecutionOrder(-5)]
|
|
|
|
[DefaultExecutionOrder(-5)]
|
|
|
|
public sealed class TopDownPlayer : NetworkBehaviour
|
|
|
|
public sealed class TopDownPlayer : NetworkBehaviour
|
|
|
|
{
|
|
|
|
{
|
|
|
|
//===================================================
|
|
|
|
//===================================================
|
|
|
|
// FIELDS
|
|
|
|
// FIELDS
|
|
|
|
//===================================================
|
|
|
|
//===================================================
|
|
|
|
[SerializeField] KCC _kcc = null;
|
|
|
|
[SerializeField] KCC _kcc = null;
|
|
|
|
[SerializeField] TopDownPlayerInput _input = null;
|
|
|
|
[SerializeField] TopDownPlayerInput _input = null;
|
|
|
|
[SerializeField] AnimationController _animator = null;
|
|
|
|
[SerializeField] AnimationController _animator = null;
|
|
|
|
|
|
|
|
|
|
|
|
//===================================================
|
|
|
|
//===================================================
|
|
|
|
// PRIVATE FIELDS
|
|
|
|
// PRIVATE FIELDS
|
|
|
|
//===================================================
|
|
|
|
//===================================================
|
|
|
|
private MovementData _moveData = new();
|
|
|
|
private MovementData _moveData = new();
|
|
|
|
[Networked]
|
|
|
|
[Networked]
|
|
|
|
private int _jumpCount { get; set; }
|
|
|
|
private int _jumpCount { get; set; }
|
|
|
|
private int _lastVisibleJump;
|
|
|
|
private int _lastVisibleJump;
|
|
|
|
[Networked]
|
|
|
|
[Networked]
|
|
|
|
private int _attackCount { get; set; }
|
|
|
|
private int _attackCount { get; set; }
|
|
|
|
private int _lastVisibleAttack;
|
|
|
|
private int _lastVisibleAttack;
|
|
|
|
|
|
|
|
|
|
|
|
//===================================================
|
|
|
|
//===================================================
|
|
|
|
// PROPERTIES
|
|
|
|
// PROPERTIES
|
|
|
|
//===================================================
|
|
|
|
//===================================================
|
|
|
|
public float Speed => _moveData.Speed;
|
|
|
|
public float Speed => _moveData.Speed;
|
|
|
|
public KCC KCC => _kcc;
|
|
|
|
public KCC KCC => _kcc;
|
|
|
|
public TopDownPlayerInput Input => _input;
|
|
|
|
public TopDownPlayerInput Input => _input;
|
|
|
|
private MovementSettings MoveSettings => GameData.Instance.MovementSettings;
|
|
|
|
private MovementSettings MoveSettings => GameData.Instance.MovementSettings;
|
|
|
|
|
|
|
|
|
|
|
|
//===================================================
|
|
|
|
//===================================================
|
|
|
|
// METHODS
|
|
|
|
// METHODS
|
|
|
|
//===================================================
|
|
|
|
//===================================================
|
|
|
|
public override void Spawned()
|
|
|
|
public override void Spawned()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
base.Spawned();
|
|
|
|
base.Spawned();
|
|
|
|
|
|
|
|
|
|
|
|
_lastVisibleJump = _jumpCount;
|
|
|
|
_lastVisibleJump = _jumpCount;
|
|
|
|
_lastVisibleAttack = _attackCount;
|
|
|
|
_lastVisibleAttack = _attackCount;
|
|
|
|
|
|
|
|
|
|
|
|
if(_kcc == null)
|
|
|
|
if(_kcc == null)
|
|
|
|
_kcc = GetComponent<KCC>();
|
|
|
|
_kcc = GetComponent<KCC>();
|
|
|
|
|
|
|
|
|
|
|
|
if(_input == null)
|
|
|
|
if(_input == null)
|
|
|
|
_input = GetComponent<TopDownPlayerInput>();
|
|
|
|
_input = GetComponent<TopDownPlayerInput>();
|
|
|
|
|
|
|
|
|
|
|
|
if (_moveData.MainCamera == null)
|
|
|
|
if (_moveData.MainCamera == null)
|
|
|
|
_moveData.MainCamera = GameObject.FindGameObjectWithTag("MainCamera");
|
|
|
|
_moveData.MainCamera = GameObject.FindGameObjectWithTag("MainCamera");
|
|
|
|
|
|
|
|
|
|
|
|
if(_animator == null)
|
|
|
|
if(_animator == null)
|
|
|
|
_animator = GetComponent<AnimationController>();
|
|
|
|
_animator = GetComponent<AnimationController>();
|
|
|
|
}//Spawned() end
|
|
|
|
}//Spawned() end
|
|
|
|
|
|
|
|
|
|
|
|
public override void FixedUpdateNetwork()
|
|
|
|
public override void FixedUpdateNetwork()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if(Input == null)
|
|
|
|
if(Input == null)
|
|
|
|
return;
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
_moveData.TargetSpeed = Input.CurrentInput.Sprint == true ? MoveSettings.SprintSpeed : MoveSettings.MoveSpeed;
|
|
|
|
_moveData.TargetSpeed = Input.CurrentInput.Sprint == true ? MoveSettings.SprintSpeed : MoveSettings.MoveSpeed;
|
|
|
|
|
|
|
|
|
|
|
|
if(Input.CurrentInput.MoveDirection != Vector2.zero)
|
|
|
|
if(Input.CurrentInput.MoveDirection != Vector2.zero)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_moveData.TargetRotation = KCC.GetLookRotation(false, true).y;
|
|
|
|
Vector3 moveDirection = new Vector3(Input.CurrentInput.MoveDirection.x, 0.0f, Input.CurrentInput.MoveDirection.y);
|
|
|
|
_moveData.TargetRotation = Mathf.Atan2(Input.CurrentInput.MoveDirection.x, Input.CurrentInput.MoveDirection.y) * Mathf.Rad2Deg + _moveData.MainCamera.transform.eulerAngles.y;
|
|
|
|
_moveData.TargetRotation = Quaternion.LookRotation(moveDirection).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);
|
|
|
|
_moveData.TargetRotation = Mathf.SmoothDampAngle(transform.eulerAngles.y, _moveData.TargetRotation, ref _moveData.RotationVelocity, MoveSettings.RotationSmooth);
|
|
|
|
|
|
|
|
KCC.SetLookRotation(new Vector2(0f, _moveData.TargetRotation));
|
|
|
|
KCC.SetLookRotation(new Vector2(0f, _moveData.TargetRotation));
|
|
|
|
}//if end
|
|
|
|
}//if end
|
|
|
|
else
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
_moveData.TargetRotationMove = 0f;
|
|
|
|
_moveData.TargetRotationMove = 0f;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(Input.CurrentInput.MoveDirection != Vector2.zero)
|
|
|
|
if(Input.CurrentInput.MoveDirection != Vector2.zero)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_moveData.InputDirection = Quaternion.Euler(0.0f, _moveData.TargetRotationMove, 0.0f) * Vector3.forward;
|
|
|
|
_moveData.InputDirection = Quaternion.Euler(0.0f, _moveData.TargetRotation, 0.0f) * Vector3.forward;
|
|
|
|
_moveData.InputDirection.x += Input.CurrentInput.MoveDirection.x;
|
|
|
|
}
|
|
|
|
_moveData.InputDirection.z += Input.CurrentInput.MoveDirection.y;
|
|
|
|
|
|
|
|
}//if end
|
|
|
|
|
|
|
|
else
|
|
|
|
else
|
|
|
|
|
|
|
|
{
|
|
|
|
_moveData.InputDirection = Vector3.zero;
|
|
|
|
_moveData.InputDirection = Vector3.zero;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (KCC.IsGrounded && Input.CurrentInput.AttackAction.WasPressed(Input.PreviousInput.AttackAction, NetworkedInput.ATTACK_BUTTON) == true)
|
|
|
|
|
|
|
|
_attackCount++;
|
|
|
|
if (KCC.IsGrounded && Input.CurrentInput.AttackAction.WasPressed(Input.PreviousInput.AttackAction, NetworkedInput.ATTACK_BUTTON) == true)
|
|
|
|
|
|
|
|
_attackCount++;
|
|
|
|
|
|
|
|
|
|
|
|
if (KCC.IsGrounded && Input.CurrentInput.JumpAction.WasPressed(Input.PreviousInput.JumpAction, NetworkedInput.JUMP_BUTTON) == true)
|
|
|
|
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, MoveSettings.JumpImpulsePeak, 0.25f).SetEase(Ease.InExpo).OnComplete(()=>
|
|
|
|
DOTween.To(()=> _moveData.JumpImpulse, x=> _moveData.JumpImpulse = x, 0f, 0.25f).SetEase(Ease.InExpo));
|
|
|
|
DOTween.To(()=> _moveData.JumpImpulse, x=> _moveData.JumpImpulse = x, 0f, 0.25f).SetEase(Ease.InExpo));
|
|
|
|
|
|
|
|
|
|
|
|
_jumpCount++;
|
|
|
|
_jumpCount++;
|
|
|
|
}//if end
|
|
|
|
}//if end
|
|
|
|
|
|
|
|
|
|
|
|
// It feels better when the player falls quicker.
|
|
|
|
// It feels better when the player falls quicker.
|
|
|
|
KCC.SetGravity(KCC.RealVelocity.y >= 0.0f ? MoveSettings.UpGravity : MoveSettings.DownGravity);
|
|
|
|
KCC.SetGravity(KCC.RealVelocity.y >= 0.0f ? MoveSettings.UpGravity : MoveSettings.DownGravity);
|
|
|
|
_moveData.MovementSpeed = Input.CurrentInput.MoveDirection.magnitude;
|
|
|
|
_moveData.MovementSpeed = Input.CurrentInput.MoveDirection.magnitude;
|
|
|
|
|
|
|
|
|
|
|
|
if(_moveData.MovementSpeed > 0.01f)
|
|
|
|
if(_moveData.MovementSpeed > 0.01f)
|
|
|
|
_moveData.Speed = Mathf.Lerp(_moveData.Speed, _moveData.MovementSpeed > 0.01f ? _moveData.TargetSpeed : 0f, Runner.DeltaTime * 10f);
|
|
|
|
_moveData.Speed = Mathf.Lerp(_moveData.Speed, _moveData.MovementSpeed > 0.01f ? _moveData.TargetSpeed : 0f, Runner.DeltaTime * 10f);
|
|
|
|
else
|
|
|
|
else
|
|
|
|
_moveData.Speed = 0f;
|
|
|
|
_moveData.Speed = 0f;
|
|
|
|
|
|
|
|
|
|
|
|
_moveData.DesiredMoveVelocity = _moveData.InputDirection * _moveData.Speed;
|
|
|
|
_moveData.DesiredMoveVelocity = _moveData.InputDirection * _moveData.Speed;
|
|
|
|
|
|
|
|
|
|
|
|
if (KCC.ProjectOnGround(_moveData.DesiredMoveVelocity, out Vector3 projectedDesiredMoveVelocity) == true)
|
|
|
|
if (KCC.ProjectOnGround(_moveData.DesiredMoveVelocity, out Vector3 projectedDesiredMoveVelocity) == true)
|
|
|
|
_moveData.DesiredMoveVelocity = Vector3.Normalize(projectedDesiredMoveVelocity) * _moveData.Speed;
|
|
|
|
_moveData.DesiredMoveVelocity = Vector3.Normalize(projectedDesiredMoveVelocity) * _moveData.Speed;
|
|
|
@ -113,23 +113,23 @@ public sealed class TopDownPlayer : NetworkBehaviour
|
|
|
|
else
|
|
|
|
else
|
|
|
|
_moveData.Acceleration = KCC.IsGrounded == true ? MoveSettings.GroundAcceleration : MoveSettings.AirAcceleration;
|
|
|
|
_moveData.Acceleration = KCC.IsGrounded == true ? MoveSettings.GroundAcceleration : MoveSettings.AirAcceleration;
|
|
|
|
|
|
|
|
|
|
|
|
_moveData.MoveVelocity = Vector3.Lerp(_moveData.MoveVelocity, _moveData.DesiredMoveVelocity, _moveData.Acceleration * Runner.DeltaTime);
|
|
|
|
_moveData.MoveVelocity = Vector3.Lerp(_moveData.MoveVelocity, _moveData.DesiredMoveVelocity, _moveData.Acceleration * Runner.DeltaTime);
|
|
|
|
KCC.Move(_moveData.MoveVelocity, _moveData.JumpImpulse);
|
|
|
|
KCC.Move(_moveData.MoveVelocity, _moveData.JumpImpulse);
|
|
|
|
}//FixedUpdateNetwork() end
|
|
|
|
}//FixedUpdateNetwork() end
|
|
|
|
|
|
|
|
|
|
|
|
public override void Render()
|
|
|
|
public override void Render()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_animator.Animate(KCC.IsGrounded, Speed, Runner.DeltaTime);
|
|
|
|
_animator.Animate(KCC.IsGrounded, Speed, Runner.DeltaTime);
|
|
|
|
|
|
|
|
|
|
|
|
if (_lastVisibleJump < _jumpCount)
|
|
|
|
if (_lastVisibleJump < _jumpCount)
|
|
|
|
_animator.Jump();
|
|
|
|
_animator.Jump();
|
|
|
|
|
|
|
|
|
|
|
|
_lastVisibleJump = _jumpCount;
|
|
|
|
_lastVisibleJump = _jumpCount;
|
|
|
|
|
|
|
|
|
|
|
|
if (_lastVisibleAttack < _attackCount)
|
|
|
|
if (_lastVisibleAttack < _attackCount)
|
|
|
|
_animator.Attack();
|
|
|
|
_animator.Attack();
|
|
|
|
|
|
|
|
|
|
|
|
_lastVisibleAttack = _attackCount;
|
|
|
|
_lastVisibleAttack = _attackCount;
|
|
|
|
}//Render() end
|
|
|
|
}//Render() end
|
|
|
|
|
|
|
|
|
|
|
|
}//class end
|
|
|
|
}//class end
|
|
|
|