refactor: moved a lot of player related scripts to the Reset.Units namespace
This commit is contained in:
75
Assets/Scripts/Core/PlayerManager.cs
Normal file
75
Assets/Scripts/Core/PlayerManager.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Reset.Core.Tools;
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.InputSystem.Users;
|
||||
|
||||
namespace Reset.Units{
|
||||
public static class PlayerManager{
|
||||
|
||||
public static GameObject Camera;
|
||||
public static GameObject Input;
|
||||
public static SessionManager Session;
|
||||
|
||||
private static GameObject _player;
|
||||
|
||||
public static GameObject Player{
|
||||
get{ return _player; }
|
||||
set{ _player = value; }
|
||||
}
|
||||
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
static void Reset(){
|
||||
Player = null;
|
||||
}
|
||||
|
||||
[RuntimeInitializeOnLoadMethod]
|
||||
static void PopulatePlayerSceneReferences(){
|
||||
try {
|
||||
Camera = GameObject.Find("CameraGroup");
|
||||
Input = GameObject.Find("InputManager");
|
||||
} catch (Exception e) {
|
||||
Console.WriteLine(e);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public static void AttachControllerToPlayer(InputDevice device){
|
||||
if (!Player) {
|
||||
throw new Exception(message: "There is no player to attach this new input device to.");
|
||||
}
|
||||
|
||||
InputUser playerUser = Player.GetComponent<PlayerInput>().user;
|
||||
|
||||
playerUser = InputUser.PerformPairingWithDevice(device, playerUser, InputUserPairingOptions.UnpairCurrentDevicesFromUser);
|
||||
Debug.Log($"Attached {device.displayName} to {Player}");
|
||||
}
|
||||
|
||||
public static GameObject FindNewPlayer(){
|
||||
var allPlayers = GameObject.FindGameObjectsWithTag("Player");
|
||||
for (int i = 0; i < allPlayers.Length; i++) {
|
||||
if (allPlayers[i].GetComponent<Player>() && allPlayers[i].GetComponent<NetworkObject>().IsLocalPlayer) {
|
||||
return allPlayers[i];
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void RequestNewController(){
|
||||
try {
|
||||
GameObject.Find("Input Selector").GetComponent<InputFinder>().AwaitNewInput();
|
||||
} catch (Exception e) {
|
||||
Debug.LogError($"Can't request a new controller: {e.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public static void ClearCurrentController(){
|
||||
InputUser playerUser = Player.GetComponent<PlayerInput>().user;
|
||||
playerUser.UnpairDevices();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user