feat: working control scheme detection, with save and reset

This commit is contained in:
Jakob Feldmann 2024-01-05 15:52:06 +01:00
parent 730fe6f183
commit b8ce591b70
7 changed files with 105 additions and 32 deletions

View File

@ -464,6 +464,11 @@ public partial class @InputActionMaps: IInputActionCollection2, IDisposable
""isOR"": false
}
]
},
{
""name"": ""New Player"",
""bindingGroup"": ""New Player"",
""devices"": []
}
]
}");
@ -627,6 +632,15 @@ public partial class @InputActionMaps: IInputActionCollection2, IDisposable
return asset.controlSchemes[m_Keyboard2SchemeIndex];
}
}
private int m_NewPlayerSchemeIndex = -1;
public InputControlScheme NewPlayerScheme
{
get
{
if (m_NewPlayerSchemeIndex == -1) m_NewPlayerSchemeIndex = asset.FindControlSchemeIndex("New Player");
return asset.controlSchemes[m_NewPlayerSchemeIndex];
}
}
public interface IPlayerActions
{
void OnThrust(InputAction.CallbackContext context);

View File

@ -442,6 +442,11 @@
"isOR": false
}
]
},
{
"name": "New Player",
"bindingGroup": "New Player",
"devices": []
}
]
}

View File

@ -186,7 +186,7 @@ MonoBehaviour:
m_ActionId: c8819d8c-8dc4-4eb6-ae07-132d6fffbed4
m_ActionName: Player/Boost[/Keyboard/ctrl,/Keyboard/shift,/XInputControllerWindows/buttonEast,/XInputControllerWindows/leftShoulder]
m_NeverAutoSwitchControlSchemes: 1
m_DefaultControlScheme: Controller
m_DefaultControlScheme: New Player
m_DefaultActionMap: Player
m_SplitScreenIndex: -1
m_Camera: {fileID: 0}
@ -303,6 +303,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
cameraOperator: {fileID: 0}
playerId: 2
thrustAcceleration: 1500
steerVelocity: 300
normalMaxVelocity: 25

View File

@ -132,7 +132,7 @@ MonoBehaviour:
m_ActionId: c8819d8c-8dc4-4eb6-ae07-132d6fffbed4
m_ActionName: Player/Boost[/Keyboard/ctrl,/Keyboard/shift,/XInputControllerWindows/buttonEast,/XInputControllerWindows/leftShoulder]
m_NeverAutoSwitchControlSchemes: 1
m_DefaultControlScheme: Controller
m_DefaultControlScheme: New Player
m_DefaultActionMap: Player
m_SplitScreenIndex: -1
m_Camera: {fileID: 0}
@ -249,6 +249,7 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
cameraOperator: {fileID: 0}
playerId: 1
thrustAcceleration: 1500
steerVelocity: 300
normalMaxVelocity: 25

View File

@ -11,12 +11,6 @@ public class Announcments : MonoBehaviour
private float remainingTime;
void Start()
{
announcementText.enabled = false;
enabled = false;
}
void Update()
{
if (remainingTime > 0)

View File

@ -1,13 +1,9 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using Palmmedia.ReportGenerator.Core.Reporting.Builders;
using UnityEditor.Experimental.Licensing;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Users;
using UnityEngine.InputSystem.Utilities;
using UnityEngine.Rendering;
using UnityEngine.SceneManagement;
using static InputActionMaps;
@ -26,12 +22,34 @@ public class GameManager : MonoBehaviour
void Awake()
{
if (GM == null)
if (GM != null)
{
GM.zone = zone;
GM.announcements = announcements;
Destroy(gameObject);
}
else
{
GM = this;
DontDestroyOnLoad(gameObject);
}
}
// void OnEnable()
// {
// SceneManager.sceneLoaded += OnSceneLoaded;
// }
// void OnDisable()
// {
// SceneManager.sceneLoaded -= OnSceneLoaded;
// }
// void OnSceneLoaded(Scene scene, LoadSceneMode mode)
// {
// StartControlAssignment();
// }
void Update()
{
if (restartMatchTime > 0)
@ -42,18 +60,22 @@ public class GameManager : MonoBehaviour
{
restartMatchTime = 0;
StartNewMatch();
enabled = false;
}
}
// Start is called before the first frame update
void Start()
{
if (currentState == GameState.End)
{
return;
}
currentState = GameState.Starting;
controlSchemeDetector = new ControlSchemeDetection();
controlSchemeDetector.ControlSchemeDetected += AssignPlayerControls;
controlSchemeDetector.ControlSchemeDetected += DetectPlayerControls;
controlSchemeDetector.EnableDetection();
announcements.AnnounceText("You both press a key \n on your controll scheme of choice \n to start the match");
enabled = false;
announcements.AnnounceText("You both press a key \n on your control scheme of choice \n to start the match");
}
void StartMatch()
@ -74,52 +96,65 @@ public class GameManager : MonoBehaviour
if (!go.CompareTag("Player") || currentState == GameState.End)
return;
currentState = GameState.End;
zone.onPlayZoneExited -= CheckLosingCondition;
Destroy(go);
announcements.AnnounceText(go.name + " has lost the match", 2f);
restartMatchTime = 2.2f;
players.Clear();
enabled = true;
}
private void StartNewMatch()
{
string currentSceneName = SceneManager.GetActiveScene().name;
players.Clear();
SceneManager.LoadScene(currentSceneName);
currentState = GameState.Starting;
}
public void RegisterPlayer(PlayerController pc)
{
if (!players.ContainsKey(pc.instanceID))
if (!players.ContainsKey(pc.playerId))
{
players[pc.instanceID] = pc;
players[pc.playerId] = pc;
}
DetectPlayerControls(this);
}
private void AssignPlayerControls(object sender, string controlScheme)
private void DetectPlayerControls(object sender, string controlScheme = "New Player")
{
if (players.Count < minPlayerCount)
{
return;
}
AssignPlayerControls(controlScheme);
if (players.Count <= playerControlSchemes.Count)
{
if (players.All(pc => PlayerHasActiveControls(pc.Value)))
{
controlSchemeDetector.DisableDetection();
currentState = GameState.Match;
StartMatch();
return;
}
}
}
private void AssignPlayerControls(string controlScheme = "New Player")
{
foreach (int playerId in players.Keys)
{
if (playerControlSchemes.ContainsValue(controlScheme))
{
return;
}
if (playerControlSchemes.ContainsKey(playerId))
PlayerController pc = players[playerId];
if (PlayerHasActiveControls(pc))
{
continue;
}
PlayerController pc = players[playerId];
if (playerControlSchemes.ContainsKey(playerId))
{
controlScheme = playerControlSchemes[playerId];
}
else if (playerControlSchemes.ContainsValue(controlScheme))
{
return;
}
if (controlScheme.Contains("Keyboard"))
{
pc.playerInput.SwitchCurrentControlScheme(controlScheme, Keyboard.current);
@ -128,9 +163,26 @@ public class GameManager : MonoBehaviour
{
pc.playerInput.SwitchCurrentControlScheme(controlScheme, Gamepad.current);
}
if (controlScheme != "New Player")
{
playerControlSchemes[playerId] = controlScheme;
}
}
}
private bool PlayerHasActiveControls(PlayerController pc)
{
if (pc == null)
{
return false;
}
string currentScheme = pc.playerInput.currentControlScheme;
if (currentScheme != null && currentScheme != "New Player")
{
return true;
}
return false;
}
}
public class ControlSchemeDetection : IPlayerActions
@ -175,6 +227,10 @@ public class ControlSchemeDetection : IPlayerActions
public void readControlScheme(InputAction.CallbackContext context)
{
if (!context.canceled || context.performed)
{
return;
}
int bindingIndex = context.action.GetBindingIndexForControl(context.control);
InputBinding binding = context.action.bindings[bindingIndex];
string controlScheme = binding.groups.Split(';')[0];

View File

@ -6,6 +6,7 @@ using static AffectingForcesManager;
public class PlayerController : MonoBehaviour
{
[SerializeField] public int playerId = 1;
// Private variables
[SerializeField] private CameraOperator cameraOperator;
[SerializeField] private float thrustAcceleration = 400;
@ -246,6 +247,7 @@ public class PlayerController : MonoBehaviour
if (context.phase == InputActionPhase.Performed)
{
Debug.Log("reload triggered");
Destroy(GameManager.GM);
string currentSceneName = SceneManager.GetActiveScene().name;
SceneManager.LoadScene(currentSceneName);
}