1 <?xml version=
"1.0" encoding=
"UTF-8"?>
2 <!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
3 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en">
5 <meta content=
"text/html; charset=utf-8" http-equiv=
"Content-Type"/>
6 <link rel=
"stylesheet" href=
"style/pixelless.css" type=
"text/css"/>
7 <script type=
"text/javascript" src=
"script/util.js"></script>
8 <title>Finite State Automata
</title>
12 <div class=
"content header">
13 <h1>Finite State Automata
</h1>
25 <script type=
"text/javascript" language=
"JavaScript">
28 this.transitions = {};
37 Fsm.prototype.addTransition = function( from, to, event )
39 if( !(from in this.transitions) )
40 this.transitions[from] = {};
42 if( event in this.transitions[from] && this.transitions[from][event] != to )
43 throw new Error('Cannot add conflicting transition.');
45 this.transitions[from][event] = to;
50 Fsm.prototype.turnFrom = function( state )
52 return new TransitionHint(this, state);
55 Fsm.prototype.on = function( state, handler )
57 this.handlers[state] = handler;
60 Fsm.prototype.handle = function( event )
62 if( event in this.transitions[this.current] )
65 this.previous = this.current;
66 this.current = this.transitions[this.current][event];
68 if( this.current in this.handlers )
71 this.handlers[this.current].call(this.state, this.state);
75 if( console !== null )console.log(ex);
81 function TransitionHint( fsm, state )
84 this.startState = state;
87 TransitionHint.prototype.to = function( state )
89 return new Transition(this.fsm, this.startState, state);
92 function Transition( fsm, startState, endState )
95 this.startState = startState;
96 this.endState = endState;
99 Transition.prototype.on = function( event )
101 this.fsm.addTransition(this.startState, this.endState, event);
102 return new TransitionHint(this.fsm, this.endState);
105 var bot = new Fsm('seeking');
107 bot.turnFrom('seeking')
108 .to('attacking').on('target_encountered')
109 .to('fleing').on('health_low');
111 bot.on('attacking', function( state ) {
113 console.log('noooooo');
118 bot.handle('target_encountered');