Hardware
This directory contains all of your controller types (joystick, keyboard, touch interface, Network interface...).
Add Joysticks and Keyboard
The inheritance is required to specify your ActionType.
KeyboardPlayer
using System.Collections;
using System.Collections.Generic;
using EODE.Wonderland;
using UnityEngine;
namespace MyProject {
public class KeyboardPlayer : KeyboardPlayer<ActionType> {
public KeyboardPlayer(Keyboard kb) : base(kb) { }
// type helper
protected Keyboard Keyboard {
get { return _controller as Keyboard; }
}
}
}
JoystickPlayer
using System.Collections;
using System.Collections.Generic;
using EODE.Wonderland;
using UnityEngine;
namespace MyProject {
public class JoystickPlayer : JoystickPlayer<ActionType> {
public JoystickPlayer(Joystick joy) : base(joy) {
// remove controller if not used
joy.OnAsleep += () => {
PlayersManager.Instance.RemoveController(GetControllerUid());
};
// auto add controller (disabled with a wake up button)
/* joy.OnWakeUp += () => {
PlayersManager.Instance.AddController(this);
}; */
}
}
}
Create your own
The PlayerController
This is the connection between the controller (IController, the event listener) and the player (IPlayer).
PlayerControllerBase<KeyType
, ActionType> : KeyType is the type of received events. KeyCode
for a Keyboard or int
for a Joystick. It could be short
for network...
The code of the KeyboardPlayer
:
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace EODE.Wonderland {
public class KeyboardPlayer<ActionType> : PlayerControllerBase<KeyCode, ActionType> where ActionType : struct, IComparable, IFormattable {
public KeyboardPlayer(Keyboard kb) : base(kb) { }
public override void RefreshKeyBridge() {
_controller.RefreshKeyBridge(KeyBridge.getKeyList());
}
public override void Update() {
_player.Move = _controller.Axis1;
_controller.Update();
}
public override int JoystickId {
get { return _controller.Id; }
protected set { }
}
public override string Name {
get { return (_player != null ? "o" : "x") + " - Keyboard player " + JoystickId; }
protected set { }
}
}
}
The Controller
The events listener.
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace EODE.Wonderland {
public class Keyboard : IController<KeyCode> {
// global unique id (for all controllers)
private int _uid = -1;
public int GetUid() {
return _uid;
}
public void RefreshKeyBridge(List<KeyCode> keyList) {
... // the key/actions dictionnary is refreshed. Use this method if you need a keycode list to check
}
public Vector2 Axis1 {
get { ... }
private set { }
}
// Keyboard uid
private int _id = -1;
public int Id {
get { return _id; }
private set { }
}
public string Name {
get { return "Keyboard"; }
private set { }
}
public Keyboard(int id) {
_id = id;
_uid = Controller.GetUniqId();
}
...
System.Action<KeyCode> _onButtonPressed = delegate { };
public event System.Action<KeyCode> OnButtonPressed {
add {
_onButtonPressed += value;
}
remove {
_onButtonPressed -= value;
}
}
System.Action<KeyCode> _onButtonRelease = delegate { };
public event System.Action<KeyCode> OnButtonRelease {
add {
_onButtonRelease += value;
}
remove {
_onButtonRelease -= value;
}
}
public void Update() {
...
}
...
}
}