var
YArray =
Y.YArray =
-YCollection.subclass('YArray', {
- 'init': function(o){
+YCollection.subclass('YArray', function(YArray){
+ this.init = function(o){
YCollection.init.call(this, o || []);
- }
-});
-
-YArray.prototype.merge =
-YArray.prototype.concat =
-function concat( donor ){
- var A = this._o;
- new Y(arguments).forEach(function( donor ){
- A = A.concat(donor);
- });
- return Y(A);
-};
-
-YArray.prototype.remove =
-function remove(v){
- var idx = this.indexOf(v);
- if ( idx != -1 )
- this.splice(idx, 1);
- return this;
-};
-
-YArray.prototype.last =
-function last(){
- var A = this._o;
- return A[ A.length-1 ];
-};
-
-YArray.prototype.unique =
-function unique(){
- return this.filter(function(v, i){
- // Executes in the context of the new array, so
- // `this.indexOf` is checking what we've already
- // collected.
- return (this.indexOf(v) === -1);
- });
-};
-
-// Like map, but produces a dict
-// Y(["Foo?", "R&D"]).generate(encodeURIComponent)
-// -> Y({"Foo?":"Foo%3F", "R&D":"R%26D"})
-YArray.prototype.generate =
-function generate(fn, acc){
- var args = Y(arguments),
- fn = args.shift(),
- acc = args.shift();
- return this.reduce(function(acc, v, i){
- return acc.attr(v, fn.apply( this, [v].concat(args) ));
- }, Y(acc || {}));
-};
-
-// Y([ ["foo", 1], ["bar", 2] ]).toDict()
-// -> Y({foo:1, bar:2})
-YArray.prototype.toDict =
-function toDict(){
+ };
-};
-
-
-
-YArray.prototype.clone =
-function clone(){
- return Y(this._o.slice(0));
-};
-
-YArray.prototype.size =
-function size(){
- return this._o.length;
-};
-
-YArray.prototype.toString =
-function toString(){
- return this.className + "(" + (this._o || "") + ")";
-};
-
-
-YCollection.chainDelegates(YArray, 'push', 'unshift', 'sort', 'splice', 'reverse');
-Y.mixinNames(YArray, Array, ['reduce', 'map', 'forEach', 'filter', 'slice'], true, true);
-Y.mixinNames(YArray, Array, ['indexOf', 'lastIndexOf', 'shift', 'pop', 'join'], true, false);
+ this.merge =
+ this.concat =
+ function concat( donor ){
+ var A = this._o;
+ new Y(arguments).forEach(function( donor ){
+ A = A.concat(donor);
+ });
+ return Y(A);
+ };
+
+ this.remove =
+ function remove(v){
+ var idx = this.indexOf(v);
+ if ( idx != -1 )
+ this.splice(idx, 1);
+ return this;
+ };
+
+ this.last =
+ function last(){
+ var A = this._o;
+ return A[ A.length-1 ];
+ };
+
+ this.unique =
+ function unique(){
+ return this.reduce(
+ function(acc, v, i){
+ if (acc.indexOf(v) === -1)
+ acc.push(v);
+ return acc;
+
+ }, new Y.YArray() );
+
+ // return this.filter(function(v, i){
+ // // Executes in the context of the new array, so
+ // // `this.indexOf` is checking what we've already
+ // // collected.
+ // return (this.indexOf(v) === -1);
+ // });
+ };
+
+ // Like map, but produces a dict
+ // Y(["Foo?", "R&D"]).generate(encodeURIComponent)
+ // -> Y({"Foo?":"Foo%3F", "R&D":"R%26D"})
+ this.generate =
+ function generate(fn, acc){
+ var args = Y(arguments),
+ fn = args.shift(),
+ acc = args.shift();
+ return this.reduce(function(acc, v, i){
+ return acc.attr(v, fn.apply( this, [v].concat(args) ));
+ }, Y(acc || {}));
+ };
+
+ // Y([ ["foo", 1], ["bar", 2] ]).toDict()
+ // -> Y({foo:1, bar:2})
+ this.toDict =
+ function toDict(){
+
+ };
+
+ this.clone =
+ function clone(){
+ return Y(this._o.slice(0));
+ };
+
+ this.size =
+ function size(){
+ return this._o.length;
+ };
+
+ this.toString =
+ function toString(){
+ return "Y[" + (this._o || "") + "]";
+ };
+
+
+ chainDelegates(YArray, 'push', 'unshift', 'sort', 'splice', 'reverse');
+ mixinNames(YArray, Array, ['reduce', 'map', 'forEach', 'filter', 'slice'], true, true);
+ mixinNames(YArray, Array, ['indexOf', 'lastIndexOf', 'shift', 'pop', 'join'], true, false);
+
+ return this;
+});
};
+
+/**
+ * Root-class for all Y-objects.
+ */
+function YBase(){}
+YBase = Y.YBase = new Class("YBase", {
+ __y__ : true
+});
+
+
+
+
+/// Other Class Utilities ///
+
Y.bindAll = bindAll;
function bindAll(o){
for (var k in o)
return o;
}
+// Y.chainDelegates = chainDelegates;
+function chainDelegates(type, name){
+ var names = Y(arguments, 1),
+ proto = type.prototype;
+ names.forEach(function(name){
+ proto[name] = function(){
+ var o = this._o || this;
+ o[name].apply(o, Y(arguments));
+ return this;
+ };
+ });
+ return type;
+}
+
+// Y.mixinNames = mixinNames;
+function mixinNames(o, Donor, names, override, yWrap){
+ var target = ( isFunction(o) ? o.prototype : o)
+ , proto = ( isFunction(Donor) ? Donor.prototype : Donor);
+
+ // Need a closure to capture the name
+ names.forEach(function(name){
+ if ( isFunction(proto[name]) && (override || !target[name]) )
+ target[name] = function(){
+ var r = proto[name].apply(this._o || target, arguments);
+ return (yWrap ? Y(r) : r);
+ };
+ });
+
+ return o;
+}
-function YBase(){}
-YBase = Y.YBase = new Class("YBase", {
- __y__ : true
-});
});
-YCollection.chainDelegates = chainDelegates;
-function chainDelegates(type, name){
- var names = Y(arguments, 1),
- proto = type.prototype;
- names.forEach(function(name){
- proto[name] = function(){
- var o = this._o || this;
- o[name].apply(o, Y(arguments));
- return this;
- };
- });
- return type;
-}
-
-
/** Returns the declared name of a function. */
-YFunction.prototype.getname = Y.getname = getname;
-function getname( fn ){
- if ( !fn && isFunction(this) )
- fn = this;
- if ( !isFunction(fn) )
- return fn;
- else
- return fn.className || fn.name || (fn+'').match( /function\s*([^\(]*)\(/ )[1] || '';
-}
-
-Y.mixinNames = mixinNames;
-function mixinNames(o, Donor, names, override, yWrap){
- var target = ( isFunction(o) ? o.prototype : o)
- , proto = ( isFunction(Donor) ? Donor.prototype : Donor);
-
- // Need a closure to capture the name
- names.forEach(function(name){
- if ( isFunction(proto[name]) && (override || !target[name]) )
- target[name] = function(){
- var r = proto[name].apply(this._o || target, arguments);
- return (yWrap ? Y(r) : r);
- };
- });
-
- return o;
-}
+YFunction.prototype.getName = Y.getName
+ function getName( fn ){
+ if ( !fn && isFunction(this) )
+ fn = this;
+ if ( !isFunction(fn) )
+ return fn;
+ else
+ return fn.className || fn.name || (fn+'').match( /function\s*([^\(]*)\(/ )[1] || '';
+ }
YFunction(Y);
, SQUARETH = REF_SIZE * SECONDTH // Amount of square/second covered in this tick
, PI = Math.PI
+, QUARTER_PI = PI/4
, HALF_PI = PI/2
, PI_AND_HALF = PI + HALF_PI
, TWO_PI = PI*2
setInterval(updateInfo, 1000);
// Start the simulation!
- // toggleGame();
+ toggleGame();
updateInfo();
// Fix grid-size on resize
},
moveByDir : function moveByDir(dir, v){
- return this.moveByAngle(Loc.dirToAngle(dir), v);
+ return this.moveByAngle( Loc.dirToAngle(dir), v );
},
toSquare : function toSquare(){
UP : 'up', DOWN : 'down',
RIGHT : 'right', LEFT : 'left',
+ UP_RIGHT : 'right up', UP_LEFT : 'left up',
+ DOWN_RIGHT : 'down right', DOWN_LEFT : 'down left',
+
dirToAngle : function dirToAngle(dir){
switch (dir) {
- case Loc.RIGHT: return 0;
- case Loc.UP: return PI_AND_HALF;
- case Loc.LEFT: return PI;
- case Loc.DOWN: return HALF_PI;
+ case Loc.RIGHT: return 0;
+ case Loc.DOWN_RIGHT: return QUARTER_PI;
+ case Loc.DOWN: return HALF_PI;
+ case Loc.DOWN_LEFT: return HALF_PI+QUARTER_PI;
+ case Loc.LEFT: return PI;
+ case Loc.UP_LEFT: return PI+QUARTER_PI;
+ case Loc.UP: return PI_AND_HALF;
+ case Loc.UP_RIGHT: return PI_AND_HALF+QUARTER_PI;
default: throw new Error("wtf direction is "+dir+"??");
}
// Attributes
stats: {
- move : 0.5, // move speed (squares/sec)
+ move : 1.0, // move speed (squares/sec)
rotate : HALF_PI, // rotation speed (radians/sec)
power : 1, // attack power
-Action = {
- MOVE : 'move',
- ATTACK : 'attack',
-
- moveDirFromKey : {
+Key = {
+ _dirFromKey : {
37: "left", 38: "up", 39: "right", 40: "down",
65: "left", 87: "up", 68: "right", 83: "down"
+ },
+
+ _inverse : {
+ left : "right", right : "left",
+ up : "down", down : "up"
+ },
+
+ getDir : function getDir(key){
+ return Key._dirFromKey[key+''];
+ },
+
+ getInverseDir : function getInverseDir(key){
+ return Key._inverse[ Key._dirFromKey[key+''] ];
}
};
.bind('mouseup', this.mouseup);
},
- // queueMove : function queueMove(dir){
- // var self = this;
- // return function(evt){
- // self.action = Actions.MOVE;
- // self.target = dir;
- // return false;
- // };
- // },
-
updateMeta : function updateMeta(evt){
this.shift = evt.shiftKey;
this.alt = evt.altKey;
},
keydown : function keydown(evt){
- var k = evt.which+'';
- if ( !this.activeKeys.has(k) )
- this.activeKeys.push(k);
+ var dir = Key.getDir(evt.which)
+ , inv = Key.getInverseDir(evt.which);
+
+ if ( dir && !this.activeKeys.has(dir) ) {
+ this.activeKeys.remove(inv);
+ this.activeKeys.push(dir);
+ }
this.updateMeta(evt);
},
keyup : function keyup(evt){
- this.activeKeys.remove(evt.which+'');
+ var dir = Key.getDir(evt.which);
+ if (dir) this.activeKeys.remove(dir);
this.updateMeta(evt);
},
},
act : function act(){
- if (this.tank.dead) return this;
+ if (this.tank.dead || !this.activeKeys.size() )
+ return this.tank;
+
+ var dir = this.activeKeys
+ .unique()
+ .sort()
+ .join(' ');
+
+ if (dir) this.move(dir);
- var active = this.activeKeys;
- for (var key in Action.moveDirFromKey)
- if ( active.has(key) ) {
- this.move( Action.moveDirFromKey[key] );
- return this.tank;
- }
+ // var active = this.activeKeys;
+ // for (var key in Key._dirFromKey)
+ // if ( active.has(key) ) {
+ // this.move( Key._dirFromKey[key] );
+ // return this.tank;
+ // }
return this.tank;
},