Bridges-C++ 3.5.0-dev2-1-ge3e57bf
Bridges(C++ API)
InputStateMachine.h
Go to the documentation of this file.
1#ifndef INPUTSTATEMACHINE_GAME_H
2#define INPUTSTATEMACHINE_GAME_H
3
4#include <functional>
5
6namespace bridges {
7 namespace game {
8
10 enum CycleState {
11 JUST_PRESSED,
12 STILL_PRESSED,
13 JUST_NOT_PRESSED,
14 STILL_NOT_PRESSED
15 };
16
17 enum FireState {
18 FIRE,
19 COOLDOWN,
20 NOT_PRESSED
21 };
22
23 CycleState s;
24 FireState f;
25 int cooldown;
26 int cooldownCounter;
27
28 std::function<bool()> rawState;
29
30 public:
31 InputStateMachine(std::function<bool()> rawStateFunc)
32 : s(STILL_NOT_PRESSED), f(NOT_PRESSED),
33 cooldown(30), cooldownCounter(0),
34 rawState(rawStateFunc) {
35
36 }
37
38 void update() {
39 bool currentState = rawState();
40
41 //update cycle state
42 switch (s) {
43 case JUST_PRESSED:
44 if (currentState) {
45 s = STILL_PRESSED;
46 }
47 else {
48 s = JUST_NOT_PRESSED;
49 }
50 break;
51 case STILL_PRESSED:
52 if (currentState) {
53 s = STILL_PRESSED;
54 }
55 else {
56 s = JUST_NOT_PRESSED;
57 }
58 break;
59 case JUST_NOT_PRESSED:
60 if (currentState) {
61 s = JUST_PRESSED;
62 }
63 else {
64 s = STILL_NOT_PRESSED;
65 }
66 break;
67 case STILL_NOT_PRESSED:
68 if (currentState) {
69 s = JUST_PRESSED;
70 }
71 else {
72 s = STILL_NOT_PRESSED;
73 }
74 break;
75 }
76
77 //update firestate
78 switch (f) {
79 case FIRE:
80 if (currentState) {
81 f = COOLDOWN;
82 cooldownCounter = cooldown;
83 }
84 else {
85 f = NOT_PRESSED;
86 }
87 break;
88 case COOLDOWN:
89 if (currentState) {
90 cooldownCounter --;
91 if (cooldownCounter == 0)
92 f = FIRE;
93 }
94 else {
95 f = NOT_PRESSED;
96 }
97 break;
98 case NOT_PRESSED:
99 if (currentState) {
100 f = FIRE;
101 }
102 else {
103 f = NOT_PRESSED;
104 }
105 break;
106 }
107
108 }
109
110 bool justPressed() const {
111 return s == JUST_PRESSED;
112 }
113 bool stillPressed() const {
114 return s == STILL_PRESSED;
115 }
116 bool justNotPressed() const {
117 return s == JUST_NOT_PRESSED;
118 }
119 bool stillNotPressed() const {
120 return s == STILL_NOT_PRESSED;
121 }
122
123 void setFireCooldown(int c) {
124 if (c <= 0) {
125 throw "cooldown must be positive";
126 }
127 cooldown = c;
128 }
129
130 bool fire() const {
131 return f == FIRE;
132 }
133
134 };
135
136 }
137}
138
139#endif
Definition: InputStateMachine.h:9
void update()
Definition: InputStateMachine.h:38
void setFireCooldown(int c)
Definition: InputStateMachine.h:123
bool justPressed() const
Definition: InputStateMachine.h:110
bool fire() const
Definition: InputStateMachine.h:130
bool justNotPressed() const
Definition: InputStateMachine.h:116
InputStateMachine(std::function< bool()> rawStateFunc)
Definition: InputStateMachine.h:31
bool stillNotPressed() const
Definition: InputStateMachine.h:119
bool stillPressed() const
Definition: InputStateMachine.h:113
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4