Skip to content

Commit

Permalink
Merge pull request #3 from djalmaaraujo/konami
Browse files Browse the repository at this point in the history
Complete remake of Konami code. New structure, supporting multiple instances + init first key fail support and more
  • Loading branch information
zenorocha committed Jul 13, 2012
2 parents 625f295 + c533469 commit 2695dfe
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions js/konami.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,45 @@
YUI.add('konami', function (Y) {
Y.Event.define('konami', {
on: function (node, subscription, notifier) {
var pressedKeys = [],
konamiKeys = "38,38,40,40,37,39,37,39,66,65";

subscription._handle = Y.on('keydown', function(e) {
pressedKeys.push( e.keyCode );
if ( pressedKeys.toString().indexOf( konamiKeys ) >= 0 ){
notifier.fire(e);
}
});
},

detach: function (node, subscription, notifier) {
subscription._handle.detach();
}
});
}, '0.0.2', { requires: ['event'] });
Y.Event.define('konami', {
_konamiSequence: [38,38,40,40,37,39,37,39,66,65],

on: function (node, sub, notifier) {
var instance = this;

sub._firstLength = 0;
sub._index = 0;
sub._handle = Y.on('keydown', Y.rbind(instance._handleKeyDown, instance, sub, notifier));
},

_handleKeyDown: function(e, sub, notifier) {
var instance = this,
index = sub._index,
konamiKeys = this._konamiSequence,
keyCode = e.keyCode;

if (sub._firstLength === 0) {
while (konamiKeys[0] === konamiKeys[++sub._firstLength]);
}

if (keyCode === konamiKeys[index]) {
if (++index === konamiKeys.length) {
notifier.fire(e);
index = 0;
}
}
else {

if ((index === sub._firstLength) && (keyCode === konamiKeys[0])) {
return;
}

index = 0;
}

sub._index = index;
},

detach: function (node, sub, notifier) {
sub._handle.detach();
}
});
}, '1.0', { requires: ['event-synthetic'] });

0 comments on commit 2695dfe

Please sign in to comment.