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.
137 lines
3.7 KiB
C#
137 lines
3.7 KiB
C#
|
|
using Fusion;
|
|
using StarterAssets;
|
|
using UnityEngine;
|
|
using UnityEngine.InputSystem;
|
|
|
|
/// <summary>
|
|
/// Tracks player input.
|
|
/// </summary>
|
|
[DefaultExecutionOrder(-10)]
|
|
public sealed class TopDownPlayerInput : NetworkBehaviour, IBeforeUpdate, IBeforeTick, INetworkInput
|
|
{
|
|
public NetworkedInput CurrentInput => _currentInput;
|
|
public NetworkedInput PreviousInput => _previousInput;
|
|
|
|
//===================================================
|
|
// PRIVATE FIELDS
|
|
//===================================================
|
|
private UICanvasControllerInput _mobileInput = null;
|
|
[Networked]
|
|
private NetworkedInput _currentInput { get; set; }
|
|
|
|
private bool _resetAccumulatedInput;
|
|
private NetworkedInput _accumulatedInput;
|
|
private NetworkedInput _previousInput;
|
|
|
|
//===================================================
|
|
// METHODS
|
|
//===================================================
|
|
public override void Spawned()
|
|
{
|
|
// Reset to default state.
|
|
_currentInput = default;
|
|
_previousInput = default;
|
|
_accumulatedInput = default;
|
|
_resetAccumulatedInput = default;
|
|
|
|
if (Object.HasInputAuthority == true)
|
|
{
|
|
Runner.GetComponent<ClientSessionHandler>().OnInputAction += OnInput;
|
|
|
|
if(Application.isMobilePlatform)
|
|
{
|
|
_mobileInput = Runner.GetComponent<ClientSessionHandler>().MobileInput;
|
|
}//if end
|
|
// else if(Application.isEditor)
|
|
// {
|
|
// Cursor.lockState = CursorLockMode.Locked;
|
|
// Cursor.visible = false;
|
|
// }//if end
|
|
}//if end
|
|
|
|
ReplicateToAll(false);
|
|
ReplicateTo(Object.InputAuthority, true);
|
|
}//Spawned() end
|
|
|
|
void IBeforeUpdate.BeforeUpdate()
|
|
{
|
|
if (HasInputAuthority == false)
|
|
return;
|
|
|
|
if (_resetAccumulatedInput == true)
|
|
{
|
|
_resetAccumulatedInput = false;
|
|
_accumulatedInput = default;
|
|
}//if end
|
|
|
|
if(Application.isMobilePlatform)
|
|
{
|
|
MobileInput();
|
|
}//if end
|
|
else
|
|
{
|
|
// if (Cursor.lockState != CursorLockMode.Locked)
|
|
// return;
|
|
|
|
StandaloneInput();
|
|
}//if end
|
|
|
|
}//BeforeUpdate() end
|
|
|
|
void IBeforeTick.BeforeTick()
|
|
{
|
|
if (Object == null)
|
|
return;
|
|
|
|
_previousInput = _currentInput;
|
|
NetworkedInput currentInput = _currentInput;
|
|
_currentInput = currentInput;
|
|
|
|
if (Object.InputAuthority != PlayerRef.None)
|
|
{
|
|
if (GetInput(out NetworkedInput input) == true)
|
|
_currentInput = input;
|
|
}//if end
|
|
}//BeforeTick() end
|
|
|
|
private void OnInput(NetworkRunner runner, NetworkInput networkInput)
|
|
{
|
|
networkInput.Set(_accumulatedInput);
|
|
_resetAccumulatedInput = true;
|
|
}//OnInput() end
|
|
|
|
private void StandaloneInput()
|
|
{
|
|
Keyboard keyboard = Keyboard.current;
|
|
if (keyboard != null)
|
|
{
|
|
Vector2 moveDirection = Vector2.zero;
|
|
|
|
if (keyboard.wKey.isPressed == true) { moveDirection += Vector2.up ; }
|
|
if (keyboard.sKey.isPressed == true) { moveDirection += Vector2.down ; }
|
|
if (keyboard.aKey.isPressed == true) { moveDirection += Vector2.left ; }
|
|
if (keyboard.dKey.isPressed == true) { moveDirection += Vector2.right; }
|
|
|
|
_accumulatedInput.MoveDirection = moveDirection.normalized;
|
|
_accumulatedInput.Sprint = keyboard.leftShiftKey.isPressed;
|
|
_accumulatedInput.JumpAction.Set(NetworkedInput.JUMP_BUTTON, keyboard.spaceKey.isPressed);
|
|
}//if end
|
|
|
|
if(Mouse.current != null)
|
|
_accumulatedInput.AttackAction.Set(NetworkedInput.ATTACK_BUTTON, Mouse.current.leftButton.isPressed);
|
|
}//StandaloneInput() end
|
|
|
|
private void MobileInput()
|
|
{
|
|
if(_mobileInput is null)
|
|
return;
|
|
|
|
_accumulatedInput.MoveDirection = _mobileInput.Move;
|
|
_accumulatedInput.Sprint = _mobileInput.Sprint;
|
|
_accumulatedInput.JumpAction.Set(NetworkedInput.JUMP_BUTTON, _mobileInput.Jump);
|
|
_accumulatedInput.AttackAction.Set(NetworkedInput.ATTACK_BUTTON, _mobileInput.Attack);
|
|
}//MobileInput() end
|
|
|
|
}//class end
|