fgm24/Assets/Scripts/Player/PlayerInput.cs

80 lines
2.4 KiB
C#
Raw Normal View History

2024-02-03 16:21:32 +01:00
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;
2024-02-03 16:58:14 +01:00
public Vector2 look;
2024-02-03 16:21:32 +01:00
public Gamepad controller { get; private set; }
public bool whipAttack;
2024-02-03 16:58:14 +01:00
public event Action<int> ropeLengthShrinken;
public event Action<int> ropeLengthExtend;
2024-02-03 16:21:32 +01:00
2024-02-03 21:35:06 +01:00
public bool useArrowKeys = false;
2024-02-03 16:21:32 +01:00
public int PlayerNum => playerNumber;
2024-02-03 20:01:12 +01:00
private void Start()
2024-02-03 16:21:32 +01:00
{
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);
2024-02-03 16:21:32 +01:00
if (controller != null)
{
movement.x = controller.leftStick.x.ReadValue();
movement.y = controller.leftStick.y.ReadValue();
2024-02-03 16:58:14 +01:00
look.x = controller.rightStick.x.ReadValue();
look.y = controller.rightStick.y.ReadValue();
2024-02-03 16:21:32 +01:00
whipAttack = controller.buttonWest.IsPressed();
2024-02-03 16:58:14 +01:00
if (controller.rightShoulder.IsPressed()) ropeLengthShrinken?.Invoke(playerNumber);
if (controller.leftShoulder.IsPressed()) ropeLengthExtend?.Invoke(playerNumber);
2024-02-03 16:21:32 +01:00
}
else
{
// wasd
2024-02-03 21:35:06 +01:00
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);
2024-02-03 21:35:06 +01:00
}
// arrows keys
2024-02-03 21:35:06 +01:00
else
{
movement.x = Input.GetAxisRaw("bruh2");
movement.y = Input.GetAxisRaw("bruh2_v");
2024-02-03 16:21:32 +01:00
whipAttack = Input.GetKey(KeyCode.L);
if (Input.GetKey(KeyCode.Comma)) ropeLengthShrinken?.Invoke(playerNumber);
if (Input.GetKey(KeyCode.Period)) ropeLengthExtend?.Invoke(playerNumber);
2024-02-03 21:35:06 +01:00
}
//Debug.Log($"player {playerNumber}: move {movement}");
2024-02-03 16:21:32 +01:00
}
}
}