QUEUE和'IF''ELSE''WHILE'语句从头开始和运行功能

时间:2014-10-03 02:21:13

标签: javascript if-statement while-loop queue game-engine

我在javascript中制作游戏引擎,并且需要一些方法来编写某些操作。

代码在这里:

<!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>

发生了一些事情,我需要在事件中运行操作。我调用eventActivate传递应该发生的事件。 translateAction函数读取此信息并调用设置操作的函数。我的逻辑基于一个级别包含事件,一个事件可以包含动作,每个不同的动作都包含原子。

示例:在某些时候,您调用eventActivate(1),这将推送堆栈上的相关事件。然后不时使用引擎并调用engine.runatomStack()来执行任何操作。

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

好的,所以我的引擎开始增长,现在我认为我需要添加IF / ELSE语句和WHILE / BREAK循环。我对实现有一些想法,但希望帮助使用此队列最有效。很抱歉,如果它重复,但无法使用Google找到帮助。

如果我有活动,我想的就是这样:

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

我不确定该怎么做,哪种方式效果最好......

链接到jsFiddle版本:http://jsfiddle.net/e3b0kocc/

1 个答案:

答案 0 :(得分:0)

确定,

我有一个可以解决一些问题的实现,但我认为如果这是一个问题我不能把if放在另一个内部。

<!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>

可以通过运行

进行测试
eventActivate(2)
engine.runatomStack()

为true修改evalCondition返回值将达到不同的结果。有人可以修改engine.if代码以允许内部if?