Skip to content

Instantly share code, notes, and snippets.

@audionerd
Created November 7, 2012 19:49
Show Gist options
  • Select an option

  • Save audionerd/4033939 to your computer and use it in GitHub Desktop.

Select an option

Save audionerd/4033939 to your computer and use it in GitHub Desktop.
Cat Stativus
<html>
<head>
<title>Cat Stativus</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script src="https://raw.github.com/etgryphon/stativus/master/stativus.js"></script>
</head>
<body>
<div id="output">
<div class="cat-meow" style="display:none; white-space: pre; font-family: monospace">
/\__/\
/` '\
=== 0 0 ===
\ [] /
/ \
/ \
| |
\ || || /
\_oo__oo_/#######o
<a href="#" onclick="statechart.sendEvent('nextBehavior'); return false">sleep</a>
</div>
<div class="cat-sleep" style="display:none; white-space: pre; font-family: monospace">
/\__/\
/` '\
=== - - ===
\ __ /
/ \
/ \
| |
\ || || /
\_oo__oo_/#######o
<a href="#" onclick="statechart.sendEvent('nextBehavior'); return false">meow</a>
</div>
</div>
<script>
function Cat() {}
Cat.prototype.startMeowing = function() { $('.cat-meow').show() }
Cat.prototype.stopMeowing = function() { $('.cat-meow').hide() }
Cat.prototype.goToSleep = function() { $('.cat-sleep').show() }
Cat.prototype.wakeUp = function() { $('.cat-sleep').hide() }
var cat = new Cat()
var statechart = Stativus.createStatechart()
statechart.addState("meowing", {
enterState: function(){
cat.startMeowing()
},
exitState: function(){
cat.stopMeowing()
},
nextBehavior: function(){
this.goToState('sleeping');
}
});
statechart.addState("sleeping", {
enterState: function(){
cat.goToSleep()
},
exitState: function(){
cat.wakeUp()
},
nextBehavior: function(){
this.goToState('meowing');
}
});
// start meowing
statechart.initStates('meowing');
// ... and later, call nextBehavior to switch states
statechart.sendEvent('nextBehavior');
window.statechart = statechart;
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment