PlayersManager
The main user management and gameObject creation code. This example is a 1 controller = 1 player.
Base
Add a JoystickViewer component at the same time.
using System.Collections;
using System.Collections.Generic;
using EODE.Wonderland;
using UnityEngine;
namespace MyProject {
[RequireComponent(typeof(JoysticksViewer))]
public class PlayersManager : PlayersManager<ActionType> {
}
}
Attach Joysticks
Connect JoysticksViewer to PlayersManager. The joystick type is a Kjoy.
void Start() {
AutoAttachJoystick<Kjoy>();
}
or make it yourself
JoysticksViewer.Events.OnConnect += ctrl => {
AddController(new Kjoy(ctrl));
};
JoysticksViewer.Events.OnDisconnect += ctrl => RemoveController(ctrl.GetUid());
JoysticksViewer.CallConnects(); // already connected joysticks
AutoAssignPlayers(); // if controllers already found, assign players
Create players
void Start() {
OnControllerAdd += DoOnAddController;
OnControllerRemove += DoOnRemoveController;
AutoAttachJoystick<Kjoy>();
}
/// <summary>
/// When controller is added.
/// </summary>
/// <param name="uid">Uid of the "hard" controller</param>
void DoOnAddController(int uid) {
var player = CreatePlayer();
// connect controller hard and soft
AssignPlayer(player.Number, uid);
}
/// <summary>
/// When controller is removed. Called only if controller(hard) is linked with a controller(soft)
/// </summary>
/// <param name="uid">Uid of the "hard" controller</param>
/// <param name="controllerId">Id of the "soft" controller</param>
void DoOnRemoveController(int uid, int controllerId) {
// remove ghost players
RemoveUnassignedPlayers();
}
/// <summary>
/// Create and return a new player
/// Player id is "MaxPlayerId+1"
/// ! if you use this without a link to a hard controller, the player is automatically detached from players manager
/// </summary>
public InputsPlayer CreatePlayer() {
// create the gameobject
var goPlayer = new GameObject();
goPlayer.transform.SetParent(transform);
var player = goPlayer.AddComponent<Player>();
player.InputsPlayer.Number = GetLastPlayerNumber() + 1;
goPlayer.name = "Player " + player.InputsPlayer.Number;
// add player to active list controller
Players.Add(player.InputsPlayer);
return player.InputsPlayer;
}
Multiple keyboard example
Press + or - on the keyboard to add and remove players.
using System.Collections;
using System.Collections.Generic;
using EODE.Wonderland;
using UnityEngine;
namespace MyProject {
[RequireComponent(typeof(JoysticksViewer))]
public class PlayersManager : PlayersManager<ActionType> {
const string _storageFile = "ControllersConfig";
private class Config {
public int keyboardNumber = 1;
}
private Config _config = new Config();
void Start() {
// prepare keyboard and joysticks
if (Storage.FileExists(_storageFile)) {
_config = Storage.ReadJsonFile<Config>(_storageFile);
}
// Listener on controller added or removed
OnControllerAdd += DoOnAddController;
OnControllerRemove += DoOnRemoveController;
// auto attach joysticks using Joystick type Moonlight.Inputs.Joystick
AutoAttachJoystick<Kjoy>();
// manualy attach keyboards
switch (_config.keyboardNumber) {
case (0):
break;
case (1):
Keyboards1();
break;
case (2):
Keyboards2();
break;
case (3):
Keyboards3();
break;
default:
_config.keyboardNumber = 1;
Storage.SaveJsonFile(_storageFile, _config);
break;
}
}
float _changeKbNumDelay = 0f;
protected override void Update() {
base.Update();
if (_changeKbNumDelay + 2f > Time.fixedTime) {
return;
}
// change keyboard number
// up
if (Input.GetKeyUp(KeyCode.KeypadPlus)) {
_changeKbNumDelay = Time.fixedTime;
var n = KeyboardNumber;
if (n == 0) Keyboards1();
if (n == 1) Keyboards2();
if (n == 2) Keyboards3();
_config.keyboardNumber = KeyboardNumber;
Storage.SaveJsonFile(_storageFile, _config);
}
// down
else if (Input.GetKeyUp(KeyCode.KeypadMinus)) {
_changeKbNumDelay = Time.fixedTime;
var n = KeyboardNumber;
if (n == 1) Keyboards0();
if (n == 2) Keyboards1();
if (n == 3) Keyboards2();
_config.keyboardNumber = KeyboardNumber;
Storage.SaveJsonFile(_storageFile, _config);
}
}
/// <summary>
/// When controller is added.
/// </summary>
/// <param name="uid">Uid of the "hard" controller</param>
void DoOnAddController(int uid) {
var player = CreatePlayer();
// link controller hard and soft
AssignPlayer(player.Number, uid);
}
/// <summary>
/// When controller is removed. Called only if controller(hard) is linked with a controller(soft)
/// </summary>
/// <param name="uid">Uid of the "hard" controller</param>
/// <param name="controllerId">Id of the "soft" controller</param>
void DoOnRemoveController(int uid, int controllerId) {
// remove ghost players
RemoveUnassignedPlayers();
}
/// <summary>
/// Create and return a new player
/// Player id is "MaxPlayerId+1"
/// ! if you use this without link to a hard controller, the player is automatically detached from controllers manager
/// </summary>
Moonlight.InputsPlayer CreatePlayer() {
// create the gameobject
var goPlayer = new GameObject();
goPlayer.transform.SetParent(transform);
var player = goPlayer.AddComponent<Player>();
player.InputsPlayer.Number = GetLastPlayerNumber() + 1;
goPlayer.name = "Player " + player.InputsPlayer.Number;
// add player to active list controller
Players.Add(player.InputsPlayer);
return player.InputsPlayer;
}
#region keyboard modes
int[] _keyboardIds = new int[] {-1, -1, -1 };
int KeyboardNumber {
get {
for (var i = 0; i < 3; ++i) {
if (_keyboardIds[i] < 0) {
return i;
}
}
return 3;
}
}
void CreateKb(int n, KeyboardPlayer kb) {
// switch controller
if (_keyboardIds[n] >= 0 && _controllers.ContainsKey(_keyboardIds[n])) {
var playerNum = _controllers[_keyboardIds[n]].Player.Number;
_controllers.Remove(_keyboardIds[n]);
_controllers.Add(kb.GetControllerUid(), kb);
AssignPlayer(playerNum, kb.GetControllerUid());
_keyboardIds[n] = kb.GetControllerUid();
} else {
_keyboardIds[n] = AddController(kb);
}
}
void RemoveKb(int n) {
if (_keyboardIds[n] >= 0) {
RemoveController(_keyboardIds[n]);
_keyboardIds[n] = -1;
}
}
public void Keyboards0() {
RemoveKb(0);
RemoveKb(1);
RemoveKb(2);
RemoveUnassignedPlayers();
}
public void Keyboards1() {
CreateKb(0, new Kboard_full(0));
RemoveKb(1);
RemoveKb(2);
RemoveUnassignedPlayers();
}
public void Keyboards2() {
CreateKb(0, new Kboard_2_left(0));
CreateKb(1, new Kboard_2_right(1));
RemoveKb(2);
RemoveUnassignedPlayers();
}
public void Keyboards3() {
CreateKb(0, new Kboard_3_left(0));
CreateKb(1, new Kboard_3_center(1));
CreateKb(2, new Kboard_3_right(2));
RemoveUnassignedPlayers();
}
#endregion
}
}
With A Waiting list
using System.Collections;
using System.Collections.Generic;
using EODE.Wonderland;
using UnityEngine;
namespace MyProject {
[RequireComponent(typeof(JoysticksViewer))]
public class PlayersManager : PlayersManager<ActionType> {
WaitingList _waitingList = new WaitingList();
public int Seats = 0;
public bool Locked {
get {
return _waitingList.Locked;
}
set {
_waitingList.Locked = value;
}
}
...
protected override void Update() {
base.Update();
if (_waitingList.SeatsLeft != Seats - AssignedPlayerCount()) {
_waitingList.SeatsLeft = Seats - AssignedPlayerCount();
}
...
}
public void ResetPlayers() {
_waitingList = new WaitingList();
_waitingList.Locked = true;
UnassignAll();
foreach (var controller in _controllers) {
DoOnAddController(controller.Key);
}
}
void DoOnAddController(int uid) {
// tmp lock list
var lk = _waitingList.Locked;
_waitingList.Locked = true;
var player = CreatePlayer();
// link controller hard and soft
AssignPlayer(player.Number, uid);
player.Enabled = false; // disable again because when Controller is assigned to player, player.Enable() is called.
// creates player
_waitingList.Locked = lk;
}
...
Moonlight.InputsPlayer CreatePlayer() {
var ip = new InputsPlayer();
ip.Number = GetLastPlayerNumber() + 1;
ip.Enabled = false;
_waitingList.Add(() => {
if (ip.Controller == null || ip.Enabled) {
// controller has been removed
Players.Remove(ip);
return true;
}
ip.Enabled = true;
// create the gameobject
var goPlayer = Instantiate(PlayerPrefab);
goPlayer.transform.SetParent(transform);
var playerComponent = goPlayer.GetOrAddComponent<Player>();
playerComponent.InputsPlayer = ip;
goPlayer.name = "Player " + playerComponent.InputsPlayer.Number;
return true;
});
// add player to active list controller
Players.Add(ip);
return ip;
}
...
}
}
/// config
GameManager.Instance.PlayersManager.Seats = Config.MaxPlayersNumber;
/// Join State
public class Warmup : StateBehaviour {
public override Task Enter() {
GameManager.Instance.PlayersManager.Locked = false;
return base.Enter();
}
public override Task Exit() {
GameManager.Instance.PlayersManager.Locked = true;
return base.Exit();
}
}