-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStateMachine.java
More file actions
58 lines (49 loc) · 1.17 KB
/
Copy pathStateMachine.java
File metadata and controls
58 lines (49 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package org.firstinspires.ftc.teamcode;
//package com.github.pmtischler.base;
/**
* State machine manager.
* Simplifies the development of finite state machines.
*/
public class StateMachine {
/**
* A state in the state machine.
*/
public static interface State {
/**
* Called when the state first becomes the active state.
*/
public void start();
/**
* Called on each update.
* @return The next state to run.
*/
public State update();
}
/**
* Creates the state machine with the initial state.
* @param initial The initial state.
*/
public StateMachine(State initial) {
state = initial;
state.start();
}
/**
* Performs an update on the state machine.
*/
public void update() {
if (state == null) {
return;
}
State next = state.update();
if (next != null && state != next) {
next.start();
}
state = next;
}
// Gets the current state.
public State currentState() {
return state;
}
// The current state.
private State state;
}