Liberi
An exergame built for kids with CP!
All Classes Functions Variables Properties Events
Controls.cs
1 using UnityEngine;
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5 using System.Linq;
6 
10 public enum ControlsAction : byte
11 {
15  Primary,
19  Secondary,
23  CycleNext,
27  CyclePrevious,
31  Menu,
35  Back
36 }
37 
41 public class Controls : MonoBehaviour
42 {
46  public static float Power
47  {
48  get { return _instance._power; }
49  }
50 
54  public static Vector3 Direction
55  {
56  get { return _instance._direction; }
57  }
58 
62  public static float RawCadence
63  {
64  get { return _instance._smoothCadence; }
65  }
66 
67  public AnimationCurve PowerCurve;
68 
69  static Controls _instance;
70 
74  public float MinPower = .05f;
75 
76  float _power;
77  float _smoothCadence;
78  Vector3 _direction;
79  Dictionary<int, bool> _actionPrevStates;
80  Dictionary<int, bool> _actionStates;
81  Dictionary<int, float> _actionHoldTimes;
82  Dictionary<int, List<IActionInfoProvider>> _actionInfoProviders;
83  static PlayerHealthProfile _health;
84 
90  public static void RegisterActionInfoProvider (ControlsAction action, IActionInfoProvider actionInfoProvider)
91  {
92  _instance._actionInfoProviders[(int)action].Insert(0, actionInfoProvider);
93  }
94 
100  public static void UnregisterActionInfoProvider (ControlsAction action, IActionInfoProvider actionInfoProvider)
101  {
102  _instance._actionInfoProviders[(int)action].Remove(actionInfoProvider);
103  }
104 
109  public static void InjectSmoothCadence (float cadence)
110  {
111  _instance._smoothCadence = cadence;
112 
113  if (_health != null)
114  {
115  float power = cadence / _health.CadenceCap;
116 
117  if (power < _instance.MinPower)
118  power = 0f;
119 
120  if (power > 1f)
121  power = 1f;
122 
123  //float precurve = power;
124  power = _instance.PowerCurve.Evaluate(power);
125 
126  _instance._power = power;
127 
128  //print("Raw: " + cadence + " Power: " + power + " PreCurve: " + precurve);
129  }
130  }
131 
136  public static void InjectDirection (Vector3 direction)
137  {
138  _instance._direction = direction.normalized;
139  }
140 
147  public static void InjectActionState (ControlsAction action, bool state)
148  {
149  if (state && !_instance._actionStates[(int)action])
150  Study.LogEvent(ControlsLogEvent.ActionDown, action);
151  else if (!state && _instance._actionStates[(int)action])
152  Study.LogEvent(ControlsLogEvent.ActionUp, action);
153 
154  _instance._actionStates[(int)action] = state;
155  }
156 
160  public static string GetActionCaption (ControlsAction action)
161  {
162  var providers = _instance._actionInfoProviders[(int)action];
163 
164  if (providers.Count != 0)
165  return providers[0].GetActionCaption(action);
166 
167  return "";
168  }
169 
173  public static bool IsActionEnabled (ControlsAction action)
174  {
175  var providers = _instance._actionInfoProviders[(int)action];
176 
177  if (providers.Count != 0)
178  return providers[0].IsActionEnabled(action);
179 
180  return false;
181  }
182 
186  public static bool GetAction (ControlsAction action)
187  {
188  return _instance._actionStates[(int)action];
189  }
190 
194  public static bool GetActionDown (ControlsAction action)
195  {
196  return _instance._actionStates[(int)action] && !_instance._actionPrevStates[(int)action];
197  }
198 
202  public static bool GetActionUp (ControlsAction action)
203  {
204  return !_instance._actionStates[(int)action] && _instance._actionPrevStates[(int)action];
205  }
206 
210  public static float GetActionHoldTime (ControlsAction action)
211  {
212  return _instance._actionHoldTimes[(int)action];
213  }
214 
219  public static void ResetHoldTime (ControlsAction action)
220  {
221  _instance._actionHoldTimes[(int)action] = 0;
222  }
223 
224  void Awake ()
225  {
226  _instance = this;
227 
228  _actionStates = new Dictionary<int, bool>();
229  _actionPrevStates = new Dictionary<int, bool>();
230  _actionHoldTimes = new Dictionary<int, float>();
231  _actionInfoProviders = new Dictionary<int, List<IActionInfoProvider>>();
232 
233  foreach (var action in Enum.GetValues(typeof(ControlsAction)))
234  {
235  _actionStates[(int)(ControlsAction)action] = false;
236  _actionPrevStates[(int)(ControlsAction)action] = false;
237  _actionHoldTimes[(int)(ControlsAction)action] = 0f;
238  _actionInfoProviders[(int)(ControlsAction)action] = new List<IActionInfoProvider>();
239  }
240  }
241 
242  void LateUpdate ()
243  {
244  foreach (var action in _actionStates.Keys)
245  {
246  _actionPrevStates[(int)action] = _actionStates[action];
247 
248  if (_actionStates[(int)action])
249  _actionHoldTimes[(int)action] += Time.deltaTime;
250  else
251  _actionHoldTimes[(int)action] = 0;
252  }
253  }
254 
255  void Start()
256  {
257  GameClient.Instance.ConnectedToWorldServer += OnConnectedToWorldServer;
258  GameClient.Instance.DisconnectedFromWorldServer += OnDisconnectedFromWorldServer;
259  }
260 
261  void OnConnectedToWorldServer()
262  {
263  _health = Game.LocalPlayerProfile.Health;
264  }
265 
266  void OnDisconnectedFromWorldServer()
267  {
268  _health = null;
269  }
270 }
static float Power
Returns an abstract power value in the range [0 ... 1].
Definition: Controls.cs:47
static bool GetAction(ControlsAction action)
Returns the current state of an action.
Definition: Controls.cs:186
static void UnregisterActionInfoProvider(ControlsAction action, IActionInfoProvider actionInfoProvider)
Unregister an action info provider.
Definition: Controls.cs:100
static void InjectActionState(ControlsAction action, bool state)
Injections an action state.
Definition: Controls.cs:147
float MinPower
The value below which power is simply considered zero.
Definition: Controls.cs:74
static float GetActionHoldTime(ControlsAction action)
Returns the amount of time the given action has been held down.
Definition: Controls.cs:210
static float RawCadence
Returns the raw pedaling cadence for logging use or other study reasons.
Definition: Controls.cs:63
static void InjectSmoothCadence(float cadence)
Injects a power value.
Definition: Controls.cs:109
static void InjectDirection(Vector3 direction)
Injects a direction. Call this from a device script.
Definition: Controls.cs:136
DisconnectedFromWorldServerHandler DisconnectedFromWorldServer
Event fired when the game client is disconnected from the world server.
Definition: GameClient.cs:69
static bool GetActionDown(ControlsAction action)
Checks if the given action has just been pressed down.
Definition: Controls.cs:194
static Vector3 Direction
Returns a normalized, abstract direction. Could also be zero.
Definition: Controls.cs:55
ConnectedToWorldServerHandler ConnectedToWorldServer
Event fired when the game client successfully connects to the world server.
Definition: GameClient.cs:61
static string GetActionCaption(ControlsAction action)
Returns the current caption for a given action.
Definition: Controls.cs:160
Interface for classes which provide information for an action.
static bool IsActionEnabled(ControlsAction action)
Checks if a given action is enabled.
Definition: Controls.cs:173
static GameClient Instance
Gets the sole instance of the game client.
Definition: GameClient.cs:136
Main game client class.
Definition: GameClient.cs:16
Class for managing study-related game components.
Definition: Study.cs:13
static void RegisterActionInfoProvider(ControlsAction action, IActionInfoProvider actionInfoProvider)
Register an action info provider.
Definition: Controls.cs:90
static bool GetActionUp(ControlsAction action)
Checks if the given action has just been released.
Definition: Controls.cs:202
static void LogEvent(Enum eventType, params object[] args)
Log a game event.
Definition: Study.cs:57
Controls class. Allows for controls queries and controls injection from devices.
Definition: Controls.cs:41
static void ResetHoldTime(ControlsAction action)
Resets the Hold Time of an action. Used to "consume" a held input, such as the 3 second hold for quit...
Definition: Controls.cs:219