Skip to content

Instantly share code, notes, and snippets.

@unstoppablecarl
Created June 14, 2016 17:30
Show Gist options
  • Save unstoppablecarl/f0228917851defb9e76eae228c26570e to your computer and use it in GitHub Desktop.
Save unstoppablecarl/f0228917851defb9e76eae228c26570e to your computer and use it in GitHub Desktop.
'use strict';
var findFirstWithKey = function(bindings, key) {
for (var i = 0; i < bindings.length; i++) {
var val = bindings[i][key];
if (val) {
return val;
}
}
return false;
};
var findFirst = function(bindings, func) {
for (var i = 0; i < bindings.length; i++) {
var val = bindings[i];
if (func(val)) {
return val;
}
}
return false;
};
var makeMultiBinding = function(input, bindings) {
bindings = bindings.map(function(binding) {
return input.keyboard.addKey(binding);
});
var out = {
upDuration: function(duration) {
return findFirst(bindings, function(key) {
return key.upDuration(duration);
});
},
downDuration: function(duration) {
return findFirst(bindings, function(key) {
return key.downDuration(duration);
});
}
};
Object.defineProperties(out, {
isUp: {
get: function() {
return !!findFirstWithKey(bindings, 'isUp')
}
},
isDown: {
get: function() {
return !!findFirstWithKey(bindings, 'isDown')
}
},
duration: {
get: function() {
return findFirstWithKey(bindings, 'isDown').duration;
}
},
repeats: {
get: function() {
return findFirstWithKey(bindings, 'isDown').repeats;
}
},
timeDown: {
get: function() {
return findFirstWithKey(bindings, 'isDown').timeDown;
}
},
timeUp: {
get: function() {
return findFirstWithKey(bindings, 'isUp').timeUp;
}
},
});
return out;
};
var bindInputKeys = function(input, keys) {
_.each(keys, function(bindings, key) {
keys[key] = makeMultiBinding(input, bindings);
});
return keys;
};
var k = Phaser.KeyCode;
var keys = {
jump: [k.SPACEBAR, k.W],
up: [k.UP, k.W],
left: [k.LEFT, k.A],
right: [k.RIGHT, k.D],
down: [k.DOWN, k.S],
};
var actions = bindInputKeys(game.input, keys);
if(actions.jump.isDown){
// jump
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment