From b8ce591b7090c372fc30314bc6767cbd99d7e4ea Mon Sep 17 00:00:00 2001 From: Jakob Feldmann Date: Fri, 5 Jan 2024 15:52:06 +0100 Subject: [PATCH] feat: working control scheme detection, with save and reset --- Assets/Input/InputActionMaps.cs | 14 +++ Assets/Input/InputActionMaps.inputactions | 5 ++ Assets/Prefabs/Player Blue.prefab | 3 +- Assets/Prefabs/Player Gray.prefab | 3 +- Assets/Scripts/Announcments.cs | 6 -- Assets/Scripts/GameManager.cs | 104 +++++++++++++++++----- Assets/Scripts/PlayerController.cs | 2 + 7 files changed, 105 insertions(+), 32 deletions(-) diff --git a/Assets/Input/InputActionMaps.cs b/Assets/Input/InputActionMaps.cs index d5660ff..aee1f17 100644 --- a/Assets/Input/InputActionMaps.cs +++ b/Assets/Input/InputActionMaps.cs @@ -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); diff --git a/Assets/Input/InputActionMaps.inputactions b/Assets/Input/InputActionMaps.inputactions index 49d45ca..18f5463 100644 --- a/Assets/Input/InputActionMaps.inputactions +++ b/Assets/Input/InputActionMaps.inputactions @@ -442,6 +442,11 @@ "isOR": false } ] + }, + { + "name": "New Player", + "bindingGroup": "New Player", + "devices": [] } ] } \ No newline at end of file diff --git a/Assets/Prefabs/Player Blue.prefab b/Assets/Prefabs/Player Blue.prefab index 80a9178..11cbeeb 100644 --- a/Assets/Prefabs/Player Blue.prefab +++ b/Assets/Prefabs/Player Blue.prefab @@ -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 diff --git a/Assets/Prefabs/Player Gray.prefab b/Assets/Prefabs/Player Gray.prefab index 2c96682..6bbf518 100644 --- a/Assets/Prefabs/Player Gray.prefab +++ b/Assets/Prefabs/Player Gray.prefab @@ -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 diff --git a/Assets/Scripts/Announcments.cs b/Assets/Scripts/Announcments.cs index 679bfe0..d89828d 100644 --- a/Assets/Scripts/Announcments.cs +++ b/Assets/Scripts/Announcments.cs @@ -11,12 +11,6 @@ public class Announcments : MonoBehaviour private float remainingTime; - void Start() - { - announcementText.enabled = false; - enabled = false; - } - void Update() { if (remainingTime > 0) diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index db8859f..b69c4f6 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -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) { - controlSchemeDetector.DisableDetection(); - currentState = GameState.Match; - StartMatch(); - return; + if (players.All(pc => PlayerHasActiveControls(pc.Value))) + { + controlSchemeDetector.DisableDetection(); + currentState = GameState.Match; + StartMatch(); + } } + } + 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); } - playerControlSchemes[playerId] = controlScheme; + 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]; diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index 46dffa9..445b5d1 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -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); }