Adds support for half-directional moves.
authordsc <david.schoonover@gmail.com>
Wed, 17 Nov 2010 10:22:08 +0000 (02:22 -0800)
committerdsc <david.schoonover@gmail.com>
Wed, 17 Nov 2010 10:22:08 +0000 (02:22 -0800)
src/Y/y-array.js
src/Y/y-class.js
src/Y/y-collection.js
src/Y/y-function.js
src/tanks/globals.js
src/tanks/main.js
src/tanks/map/loc.js
src/tanks/thing/tank.js
src/tanks/ui/player.js

index 8d421cd..46fa261 100644 (file)
@@ -1,85 +1,93 @@
 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;
+});
 
index 5f810b7..2922230 100644 (file)
@@ -162,6 +162,20 @@ Class.fn.subclass =
     };
 
 
+
+/**
+ * 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)
@@ -169,9 +183,35 @@ function bindAll(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
-});
index 0c9fab1..5cc6068 100644 (file)
@@ -123,18 +123,3 @@ YBase.subclass('YCollection', {
 });
 
 
-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;
-}
-
-
index 87aa5a9..f9d6984 100644 (file)
@@ -274,32 +274,15 @@ function limit(n){
 
 
 /** 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);
index 9605125..ad7eeb4 100644 (file)
@@ -24,6 +24,7 @@ var undefined
 ,   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
index 6764658..c51174f 100644 (file)
@@ -57,7 +57,7 @@ function setupUI(){
     setInterval(updateInfo, 1000);
     
     // Start the simulation!
-    // toggleGame();
+    toggleGame();
     updateInfo();
     
     // Fix grid-size on resize
index 4178962..43453a9 100644 (file)
@@ -34,7 +34,7 @@ Loc = new Y.Class('Loc', new math.Vec(0,0), {
     },
     
     moveByDir : function moveByDir(dir, v){
-        return this.moveByAngle(Loc.dirToAngle(dir), v);
+        return this.moveByAngle( Loc.dirToAngle(dir), v );
     },
     
     toSquare : function toSquare(){
@@ -69,12 +69,19 @@ Y(Loc).extend({
     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+"??");
         }
index 825d055..697ceac 100644 (file)
@@ -9,7 +9,7 @@ Tank = new Y.Class('Tank', Thing, {
     
     // 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
index 06c412f..e945e7e 100644 (file)
@@ -1,10 +1,20 @@
-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+''] ];
     }
 };
 
@@ -35,15 +45,6 @@ Player = new Y.Class('Player', {
             .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;
@@ -52,14 +53,19 @@ Player = new Y.Class('Player', {
     },
     
     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);
     },
     
@@ -82,14 +88,22 @@ Player = new Y.Class('Player', {
     },
     
     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;
     },