NonConForm State Engine for Java Script. The first example

3 10 2014

I started to port my NonConForm library from the Falcon to Python a while ago. Now I started to port it to JavaScript as well. Today the state engine, the heart of the gaming menu framework is finished.

Here the first example:


-------------------------------------------------
declare states
-------------------------------------------------
var stGame = new NcfState("Game");
var tFinishFromGame = new NcfTransition("FinishFromGame","End",function() {});
stGame.addTransition(tFinishFromGame);
Ncf.addState(stGame);
-------------------------------------------------
define view
-------------------------------------------------
Ncf.getState("Game").setView(
function() {
Ncf.log("View");
Ncf.transit("FinishFromGame");
}
)
-------------------------------------------------
define controller
-------------------------------------------------
Ncf.getState("Game").setController(
function() {
Ncf.log("Controller");
Ncf.transit("FinishFromGame");
}
)
-------------------------------------------------
start engine
-------------------------------------------------
Ncf.setDebugMode(true);
Ncf.init(50,"Game");

and here is the output

initialize NonConForm
—————————–
start NonConForm state engine
—————————–
Leaving:Start
|——————Transit to——————|:Game
Entering:Game
NonConForm main loop started
Main Loop
View
Controller
Leaving:Game
|——————Transit to——————|:End
—————————–
finishing NonConForm
—————————–
NonConForm main loop stopped





Java Script Keyboard Input

3 07 2013

Today, I found a nice little Java Script library called keypress.js. It handles Keyboard events. I used it to rewrite the keyboard handling in “Heimholung” my actual project.

Before keypress I had these handlers:

addEventListener(“keydown”, function (e) {
keysDown[e.keyCode] = true;
}, false);addEventListener(“keyup”, function (e) {
delete keysDown[e.keyCode];
}, false);

Now I use keypress:

keypress.register_combo({
“keys”: “down”,
“on_keydown” : function(e) {
keysDown[e.keyCode]=true;
},

“on_keyup” : function(e) {
delete keysDown[e.keyCode];
},

“prevent_repeat”    : true,
});

 

After a while I got everything working. Most important was to have a function to wait until a key is pressed:

 

keypress.register_combo({
“keys”: “down”,
“on_keyup”: function(e) {
keypress.reset();
nextStateInit();
currentState.state=nextState;
},
“prevent_repeat”    : true,
});