QUEUE and "IF" "ELSE" "WHILE" from scratch and functions performed

I am building a game engine in javascript and need some way to code some actions.

The code is here:

<!DOCTYPE html> <html> <body> <script>
var engine={}, actions={}; engine.atomStack=new Array();
events = {
    1: ["action1|5","action2|2;2","action1|2"],
    2: ["action2|5;2","action2|2;2"],
    3: ["action2|5;2","action1|2"] };

engine.runatomStack = function(){
    while(engine.atomStack.length > 0){
        var actionToRun = engine.atomStack.shift();
        actionToRun[0](actionToRun[1]);  } };

eventActivate = function(event) {
    for (var i = 0; i < events[event].length ; i++) {
        var actionAndParam = events[event][i].split('|');
        translateActions(actionAndParam[0],actionAndParam[1]);  } };

engine.action1 = function( param ) {
    console.log("executed action 1, param "+param[0]); }

engine.action2 = function( param ) {
    console.log("executed action 2, params "+param[0]+" "+param[1]); }

actions.action1 = function( param ) {
    var params = param.split(';');
    engine.atomStack.push([engine.action1,params]); }

actions.action2 = function( param ) {
    var params = param.split(';');
    params[1]=parseInt(params[1],10)+2
    engine.atomStack.push([engine.action2,params]); }

translateActions = function(action, param) { actions[action](param); };
</script> </body> </html>

      

Something is happening and I need to trigger actions inside the event. I am calling eventActivate passing the event that should happen. The translateAction function reads this information and calls a function that sets the actions. My logic is based on the fact that a level contains events, an event can contain actions, and every other action contains atoms.

Example: at some point you call eventActivate (1) and this will push relative events on the stack. Then from time to time the engine is used and calls engine.runatomStack () to execute whatever is there.

//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 [  ]

      

Ok, so my engine starts to grow and that's it, and now I think I need to add IF / ELSE statements and WHILE / BREAK loops. I have some ideas for implementation, but I need help on what works best with this queue. Sorry if it's duplicated but couldn't find help using google.

I thought something like if I had events:

4: ["action2|5;2","IF|condition","action2|2;2","END|"]
5: ["action2|5;2","IF|condition","action2|2;2","ELSE|","action1|2","END|"]

      

I'm not sure exactly how to go, which works best ...

Link to jsFiddle version: http://jsfiddle.net/e3b0kocc/

+3


source to share


1 answer


Good,

I have an implementation that might solve some things, but I think I can't put an if inside another if that's the problem.

<!DOCTYPE html> <html> <body> <script>
var engine={}, actions={}; engine.atomStack=new Array();
events = {
    1: ["action1|5","action2|2;2","action1|2"],
    2: ["action2|5;2","if|true","action1|5","else|","action2|2;2","end|"],
    3: ["action2|5;2","action1|2"] };

engine.runatomStack = function(){
    while(engine.atomStack.length > 0){
        var actionToRun = engine.atomStack.shift();
        actionToRun[0](actionToRun[1]);  } };

eventActivate = function(event) {
    for (var i = 0; i < events[event].length ; i++) {
        var actionAndParam = events[event][i].split('|');
        translateActions(actionAndParam[0],actionAndParam[1]);  } };

evalCondition = function( param ){
    return false
}

engine.if = function( param ) {
    if ( evalCondition(param)) {
        var removeActions = false
        for (var i = 0; i < engine.atomStack.length ; i++) {
            if(engine.atomStack[i][0] == engine.else) {
                removeActions = true
            }
            if(engine.atomStack[i][0] == engine.end) {
                return
            }
            if(removeActions == true){
                engine.atomStack.splice(i)
            }

        }
    } else {
        var actionToRun =[0,0]
        while(engine.atomStack.length > 0 && 
              actionToRun[0] != engine.end &&
              actionToRun[0] != engine.else ){
        var actionToRun = engine.atomStack.shift();
        }
    }
}

engine.end = function () {}

engine.else = function () {}

engine.action1 = function( param ) {
    console.log("executed action 1, param "+param[0]); }

engine.action2 = function( param ) {
    console.log("executed action 2, params "+param[0]+" "+param[1]); }

actions.action1 = function( param ) {
    var params = param.split(';');
    engine.atomStack.push([engine.action1,params]); }

actions.if = function( param ) {
    var params = param.split(';');
    engine.atomStack.push([engine.if,params]); }

actions.else = function( param ) {
    engine.atomStack.push([engine.else,'']); }

actions.end = function( param ) {
    engine.atomStack.push([engine.end,'']); }

actions.action2 = function( param ) {
    var params = param.split(';');
    params[1]=parseInt(params[1],10)+2
    engine.atomStack.push([engine.action2,params]); }

translateActions = function(action, param) { actions[action](param); };
</script> </body> </html>

      



It can be tested by running

eventActivate(2)
engine.runatomStack()

      

Changing the return value of evalCondition to true will produce different results. Can someone modify the engine.if code to allow if inside if?

0


source







All Articles