fgm24/Assets/Scripts/Player/PlayerInput.cs

56 lines
1.6 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System;
using UnityEngine;
using UnityEngine.InputSystem;
public class PlayerInput : MonoBehaviour
{
[SerializeField] private int playerNumber;
public Vector2 movement;
public Vector2 look;
public Gamepad controller { get; private set; }
public bool whipAttack;
public event Action<int> ropeLengthShrinken;
public event Action<int> ropeLengthExtend;
public int PlayerNum => playerNumber;
private void Awake()
{
controller = Gamepad.all.ElementAtOrDefault(playerNumber);
if (controller == null)
{
Debug.LogWarning($"No Gamepad found for player {playerNumber + 1}");
}
}
private void Update()
{
if (controller != null)
{
movement.x = controller.leftStick.x.ReadValue();
movement.y = controller.leftStick.y.ReadValue();
look.x = controller.rightStick.x.ReadValue();
look.y = controller.rightStick.y.ReadValue();
whipAttack = controller.buttonWest.IsPressed();
if (controller.rightShoulder.IsPressed()) ropeLengthShrinken?.Invoke(playerNumber);
if (controller.leftShoulder.IsPressed()) ropeLengthExtend?.Invoke(playerNumber);
}
else
{
movement.x = Input.GetAxisRaw("Horizontal");
movement.y = Input.GetAxisRaw("Vertical");
whipAttack = Input.GetKey(KeyCode.B);
}
//Debug.Log($"player {playerNumber}: move {movement}");
}
}