Solo player
Example for solo game (keyboard + gamepad).
All joysticks will be attached to the keyboard (copying binds movements and events).
Hardware
Keyboard
using System.Collections;
using System.Collections.Generic;
using EODE.Wonderland;
using UnityEngine;
namespace MyGame {
public class KeyboardPlayer : KeyboardPlayer<ActionType> {
public KeyboardPlayer(Keyboard kb) : base(kb) { }
// associated joysticks
List<Kjoy> _joysticks = new List<Kjoy>();
// type helper
protected Keyboard Keyboard {
get { return _controller as Keyboard; }
}
public void Arrows(KeyCode up, KeyCode right, KeyCode down, KeyCode left) {
ArrowList = new KeyCode[] {
up,
right,
down,
left
};
Keyboard.Arrows(up, right, down, left);
}
public KeyCode[] ArrowList { get; protected set; }
public override void Update() {
base.Update();
// take joysticks position
foreach (var joy in _joysticks) {
var a = joy.GetAxis();
if (a != Vector2.zero) {
_player.Move = a;
}
}
}
public void Add(Joystick ctrl) {
var joy = new Kjoy(ctrl);
joy.OnPress += Player.Action;
joy.OnRelease += Player.ActionRelease;
_joysticks.Add(joy);
// bind all actions
foreach (var act in _actions.Keys) {
joy.Bind(act);
}
}
public void Remove(Joystick ctrl) {
_joysticks.RemoveAll(j => j.JoystickId == ctrl.Id);
}
// Bind copy
public override void Bind(ActionType actionType) {
base.Bind(actionType);
foreach (var joy in _joysticks) {
joy.Bind(actionType);
}
}
// Unbind copy
public override void Unbind(ActionType actionType) {
base.Unbind(actionType);
foreach (var joy in _joysticks) {
joy.Unbind(actionType);
}
}
}
}
Joystick
using System.Collections;
using System.Collections.Generic;
using EODE.Wonderland;
using UnityEngine;
namespace MyGame {
public class JoystickPlayer : JoystickPlayer<ActionType> {
public JoystickPlayer(Joystick joy) : base(joy) {
}
public event System.Action<ActionType> OnPress = delegate { };
public event System.Action<ActionType> OnRelease = delegate { };
public override void Bind(ActionType type) {
if (!_actions.ContainsKey(type)) {
_actions[type] = new actionEvent();
}
else {
_actions[type].press = delegate { };
_actions[type].release = delegate { };
_actions[type].pressed = false;
}
_actions[type].press += OnPress;
_actions[type].release += OnRelease;
}
public override void Unbind(ActionType type) {
if (_actions.ContainsKey(type)) {
_actions[type].press -= OnPress;
_actions[type].release -= OnRelease;
}
}
}
}
PlayersManager
...
Kboard_full _kbPlayer;
void Start() {
// config keyboard layout
KeyboardInfos.AutoTranslationEnabled = true;
// Listener on controller added or removed
OnControllerAdd += DoOnAddController;
OnControllerRemove += DoOnRemoveController;
// active keyboard
_kbPlayer = new Kboard_full(0);
AddController(_kbPlayer);
// joysticks events
JoysticksViewer.Events.OnConnect += ctrl => {
_kbPlayer.Add(ctrl);
};
JoysticksViewer.Events.OnDisconnect += ctrl => {
_kbPlayer.Remove(ctrl);
};
// Joysticks config
JoysticksViewer.Instance.SleepAfter = -1f;
JoysticksViewer.Instance.JoystickLimit = JoysticksViewer.JoystickType.x16;
// triggers already connected joysticks
JoysticksViewer.CallConnects();
}
...