Making an engine to run with custom game code
Thursday, October 2, 2014
So, I'm working on my game and decided to share some simplified version of my code here. I'm making a game engine in javascript, and needed some way to code some actions.
The code is here, you can save it as example.html (http://jsfiddle.net/e3b0kocc/):
Something happens, and I need to run the actions inside an event. I call eventActivate passing the event that should happen. The function translateAction read this information and calls the function that set up the actions. My logic is based that a level contain events, an event can contain actions, and each different action contain atoms.
So, for example, at some point you call eventActivate(1) and that will push the relative events on the stack. Then from time to time the engine is used and calls engine.runatomStack() to execute whatever is there. Below is an example using f12 to call developer console and insert javascript (used Firefox).
//engine.atomStack is Array [ ]
eventActivate(2)
//engine.atomStack is Array [ Array[2], Array[2] ]
engine.runatomStack()
//prints:
// "executed action 2, params 5 4" example.html:18
// "executed action 2, params 2 4" example.html:18
//engine.atomStack is Array [ ]
I decided to make this post because I asked a question on stackoverflow and had to make a simpler version of my code to show there and thought that it could be useful to show here too!
The code is here, you can save it as example.html (http://jsfiddle.net/e3b0kocc/):
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 | <!DOCTYPE html> <html> <body> <script> var engine={}, actions={}; engine.atomStack=new Array(); engine.runatomStack = function(){ while(engine.atomStack.length > 0){ var actionToRun = engine.atomStack.shift(); actionToRun[0](actionToRun[1]); } }; engine.action1 = function( param ) { //execute something param[0] console.log("executed action 1, param " + param[0] ); } engine.action2 = function( param ) { //execute something param[0] and param[1] console.log("executed action 2, params " + param[0] + " " + param[1] ); } actions.action1 = function( param ) { //do something param[0] var params = param.split(';'); engine.atomStack.push([engine.action1,params]); } actions.action2 = function( param ) { //do something param[0] and param[1] var params = param.split(';'); params[1]=parseInt(params[1],10)+2 engine.atomStack.push([engine.action2,params]); } translateActions = function(action, param) { actions[action](param); }; eventActivate = function(event) { for (var i = 0; i < events[event].length ; i++) { var action = events[event][i]; var actionAndParam = action.split('|'); translateActions(actionAndParam[0],actionAndParam[1]); } }; events = { 1: ["action1|5","action2|2;2","action1|2"], 2: ["action2|5;2","action2|2;2"], 3: ["action2|5;2","action1|2"] }; </script> </body> </html> |
Something happens, and I need to run the actions inside an event. I call eventActivate passing the event that should happen. The function translateAction read this information and calls the function that set up the actions. My logic is based that a level contain events, an event can contain actions, and each different action contain atoms.
So, for example, at some point you call eventActivate(1) and that will push the relative events on the stack. Then from time to time the engine is used and calls engine.runatomStack() to execute whatever is there. Below is an example using f12 to call developer console and insert javascript (used Firefox).
//engine.atomStack is Array [ ]
eventActivate(2)
//engine.atomStack is Array [ Array[2], Array[2] ]
engine.runatomStack()
//prints:
// "executed action 2, params 5 4" example.html:18
// "executed action 2, params 2 4" example.html:18
//engine.atomStack is Array [ ]
I decided to make this post because I asked a question on stackoverflow and had to make a simpler version of my code to show there and thought that it could be useful to show here too!