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.

87 lines
2.8 KiB
C#

2 weeks ago
//Shady
using UnityEngine;
using StarterAssets;
using Sirenix.OdinInspector;
using UnityEngine.InputSystem;
[HideMonoScript]
public class PlayerInput : MonoBehaviour
{
//===================================================
// FIELDS
//===================================================
[Title("PLAYER INPUT", titleAlignment: TitleAlignments.Centered)]
[SerializeField] UICanvasControllerInput _mobileInput = null;
//===================================================
// PRIVATE FIELDS
//===================================================
private NetworkedInput _currentInput = default;
private NetworkedInput _previousInput = default;
//===================================================
// PROPERTIES
//===================================================
public NetworkedInput CurrentInput => _currentInput;
public NetworkedInput PreviousInput => _previousInput;
//===================================================
// METHODS
//===================================================
private void Awake()
{
_currentInput = default;
_previousInput = default;
if(Application.isMobilePlatform)
{
_mobileInput = OfflineGameManager.Instance.MobileInput;
_mobileInput.gameObject.SetActive(true);
}//if end
}//Start() end
private void Update() => _previousInput = _currentInput;
private void FixedUpdate()
{
if(Application.isMobilePlatform)
MobileInput();
else
StandaloneInput();
}//Update() 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; }
_currentInput.MoveDirection = moveDirection.normalized;
_currentInput.Sprint = keyboard.leftShiftKey.isPressed;
_currentInput.JumpAction.Set(NetworkedInput.JUMP_BUTTON, keyboard.spaceKey.isPressed);
// if(keyboard.spaceKey.isPressed)
// Debug.Log("YES JUMP");
}//if end
if(Mouse.current != null)
_currentInput.AttackAction.Set(NetworkedInput.ATTACK_BUTTON, Mouse.current.leftButton.isPressed);
}//StandaloneInput() end
private void MobileInput()
{
if(_mobileInput is null)
return;
_currentInput.MoveDirection = _mobileInput.Move;
_currentInput.Sprint = _mobileInput.Sprint;
_currentInput.JumpAction.Set(NetworkedInput.JUMP_BUTTON, _mobileInput.Jump);
_currentInput.AttackAction.Set(NetworkedInput.ATTACK_BUTTON, _mobileInput.Attack);
}//MobileInput() end
}//class end