The code below is equivalent. I can see pros and cons for both versions. Which is better: the short, clever way, or the long, ctrl+c way?
Short version:
character.on("key",function(key){
var action = ({
"a":{axis:"x",direction:-1},
"d":{axis:"x",direction:1},
"w":{axis:"y",direction:1},
"s":{axis:"y",direction:-1}})[key[1]],
stop = key[0]=="-";
if (action)
if (stop)
this.walkdir[action.axis] = 0;
else
this.walkdir[action.axis] = this.lookdir[action.axis] = action.direction;
});
Long version:
character.on("key",function(key){
switch (key){
case "+a":
this.walkdir.x = -1;
this.lookdir.x = -1;
break;
case "+d":
this.walkdir.x = 1;
this.lookdir.x = 1;
break;
case "+w":
this.walkdir.y = 1;
this.lookdir.y = 1;
break;
case "+s":
this.walkdir.y = -1;
this.lookdir.y = -1;
break;
case "-a":
if (this.walkdir.x == -1)
this.walkdir.x = 0;
break;
case "-d":
if (this.walkdir.x == 1)
this.walkdir.x = 0;
break;
case "-w":
if (this.walkdir.y == 1)
this.walkdir.y = 0;
break;
case "-s":
if (this.walkdir.y == -1)
this.walkdir.y = 0;
break;
case "space":
this.setStance("jumping");
break;
};
});