fgm24/Assets/Scripts/Player/PlayerInput.cs

80 lines
2.4 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 bool useArrowKeys = false;
public int PlayerNum => playerNumber;
private void Start()
{
controller = Gamepad.all.ElementAtOrDefault(playerNumber);
if (controller == null)
{
Debug.LogWarning($"No Gamepad found for player {playerNumber + 1}");
}
}
private void Update()
{
// check in update to switch if controller is connected/disconnected mid game
controller = Gamepad.all.ElementAtOrDefault(playerNumber);
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
{
// wasd
if (!useArrowKeys)
{
movement.x = Input.GetAxisRaw("bruh");
movement.y = Input.GetAxisRaw("bruh_v");
whipAttack = Input.GetKey(KeyCode.R);
if (Input.GetKey(KeyCode.E)) ropeLengthShrinken?.Invoke(playerNumber);
if (Input.GetKey(KeyCode.Q)) ropeLengthExtend?.Invoke(playerNumber);
}
// arrows keys
else
{
movement.x = Input.GetAxisRaw("bruh2");
movement.y = Input.GetAxisRaw("bruh2_v");
whipAttack = Input.GetKey(KeyCode.L);
if (Input.GetKey(KeyCode.Comma)) ropeLengthShrinken?.Invoke(playerNumber);
if (Input.GetKey(KeyCode.Period)) ropeLengthExtend?.Invoke(playerNumber);
}
//Debug.Log($"player {playerNumber}: move {movement}");
}
}
}