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 ""isOR"": false
} }
] ]
},
{
""name"": ""New Player"",
""bindingGroup"": ""New Player"",
""devices"": []
} }
] ]
}"); }");
@ -627,6 +632,15 @@ public partial class @InputActionMaps: IInputActionCollection2, IDisposable
return asset.controlSchemes[m_Keyboard2SchemeIndex]; 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 public interface IPlayerActions
{ {
void OnThrust(InputAction.CallbackContext context); void OnThrust(InputAction.CallbackContext context);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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