read arrow keys and shoot
component install entity/arrows
- Should have minimum footprint. Uses 5 bits, 4 directional, 1 to shoot.
- Recent opposite arrows should take precedence over old ones.
- The arrows pressed at any point, after crazy fast combinations, should correctly evaluate to the desired direction.
- Should allow evaluating in a loop, instead of listening to events, but also provide events when needed.
- Should enforce writing a control function that will evaluate the correct state of the player's controls by only sharing those 5 bits at any given point.
var k = arrows();
document.body.onkeydown = k.onkeydown;
document.body.onkeyup = k.onkeyup;
function ctrl(){
k & k.left && left();
k & k.up && up();
k & k.right && right();
k & k.down && down();
k & k.shoot && shoot();
}
loop(ctrl);
Also, a real example.
left = 00001
up = 00010
right = 00100
down = 01000
shoot = 10000
MIT