Binds uses of config to config changes.
authordsc <david.schoonover@gmail.com>
Sat, 18 Dec 2010 00:04:22 +0000 (16:04 -0800)
committerdsc <david.schoonover@gmail.com>
Sat, 18 Dec 2010 00:04:22 +0000 (16:04 -0800)
src/Y/modules/y.config.cjs
src/Y/modules/y.event.cjs
src/tanks/game.cjs
src/tanks/map/pathmap.cjs
src/tanks/thing/bullet.cjs
src/tanks/thing/thing.cjs
src/tanks/ui/grid.cjs

index ff41a55..9e6e42d 100644 (file)
@@ -132,7 +132,26 @@ Y.YObject.subclass('Config', function(Config){
         
         getDefault : function getDefault(k, def){
             return getNested(this._defaults, k, def);
-        }
+        },
+        
+        /**
+         * Updates the selected keys on the target object whenever a set occurs.
+         * Also sets those keys on call.
+         */
+        updateOnChange : function updateOnChange(events, obj){
+            if ( !Y.isArray(events) )
+                events = events.split();
+            events = events.map(function events(key){
+                obj[key.split('.').pop()] = this.get(key);
+                if ( !Y(key).startsWith('set:') )
+                    key = 'set:'+key;
+                return key;
+            }, this);
+            this.addEventListener(events, function(evt){
+                obj[evt.data.key.split('.').pop()] = evt.data.newval;
+            });
+            return this;
+        },
         
     });
     
index 10068f8..7421fb8 100644 (file)
@@ -34,8 +34,13 @@ Y.YObject.subclass('Event', {
         return Qs[evt];
     },
     
-    addEventListener : function addEventListener(evt, fn){
-        this.getQueue(evt).push(fn.toFunction());
+    addEventListener : function addEventListener(evts, fn){
+        fn = fn.toFunction();
+        if ( !Y.isArray(evts) )
+            evts = evts.split();
+        evts.forEach(function addEvent(evt){
+            this.getQueue(evt).push(fn);
+        }, this);
         return this.target;
     },
     
index 0926019..135f487 100644 (file)
@@ -19,9 +19,9 @@ var Y         = require('Y').Y
 Game =
 exports['Game'] = 
 Y.subclass('Game', {
-    overlayPathmap : config.get('pathing.overlayPathmap'),
-    overlayAiPaths : config.get('pathing.overlayAiPaths'),
-    gameoverDelay  : config.get('game.gameoverDelay'),
+    overlayPathmap : null,
+    overlayAiPaths : null,
+    gameoverDelay  : null,
     
     gameover      : false,
     
@@ -228,3 +228,7 @@ Y.subclass('Game', {
     
 });
 
+config.updateOnChange(
+    ['pathing.overlayPathmap', 'pathing.overlayAiPaths', 'game.gameoverDelay'], 
+    Game.fn);
+
index 5e29503..71abcfe 100644 (file)
@@ -7,16 +7,19 @@ var Y        = require('Y').Y
 ,   config   = require('tanks/config').values
 ,   Vec      = math.Vec
 ,   Line     = math.Line
+
+,   GRID_SQUARE_SIZE = REF_SIZE
+,   GRID_SQUARE_MID_PT = new Vec(REF_SIZE/2, REF_SIZE/2)
 ,
 
+
+
 PathMap =
 exports['PathMap'] =
 QuadTree.subclass('PathMap', {
-    overlayAiPaths : config.get('pathing.overlayAiPaths'),
-    overlayPathmap : config.get('pathing.overlayPathmap'),
-    
-    gridSquareSize  : REF_SIZE,
-    gridSquareMidPt : new Vec(REF_SIZE/2, REF_SIZE/2),
+    // Config
+    overlayAiPaths : null,
+    overlayPathmap : null,
     
     
     
@@ -143,7 +146,7 @@ QuadTree.subclass('PathMap', {
      */
     grid : function grid(){
         if ( !this._grid ) {
-            var size = this.gridSquareSize
+            var size = GRID_SQUARE_SIZE
             ,   floor = Math.floor, ceil = Math.ceil
             ,   cols = ceil((this.width-2) /size)
             ,   rows = ceil((this.height-2)/size)
@@ -196,7 +199,7 @@ QuadTree.subclass('PathMap', {
             y = x.y;
             x = x.x;
         }
-        var floor = Math.floor, size = this.gridSquareSize;
+        var floor = Math.floor, size = GRID_SQUARE_SIZE;
         return new Vec(floor(x/size), floor(y/size));
     },
     
@@ -205,12 +208,12 @@ QuadTree.subclass('PathMap', {
             y = x.y;
             x = x.x;
         }
-        var floor = Math.floor, size = this.gridSquareSize;
+        var floor = Math.floor, size = GRID_SQUARE_SIZE;
         return new Vec(floor(x)*size, floor(y)*size);
     },
     
     path : function path(start, end, id){
-        var size = this.gridSquareSize, floor = Math.floor
+        var size = GRID_SQUARE_SIZE, floor = Math.floor
         ,   grid = this.grid()
         
         ,   startX = floor(start.x/size)
@@ -229,12 +232,12 @@ QuadTree.subclass('PathMap', {
         
         return path
                 .invoke('scale', size)
-                .invoke('add', this.gridSquareMidPt)
+                .invoke('add', GRID_SQUARE_MID_PT)
                 .end();
     },
     
     drawPath : function drawPath(id, start, path){
-        var size   = this.gridSquareSize, off
+        var size   = GRID_SQUARE_SIZE, off
         ,   w      = this.width-2,  h = this.height-2
         ,   el     = this.game.viewport
         ,   grid   = this.grid()
@@ -380,14 +383,16 @@ QuadTree.subclass('PathMap', {
     
 });
 
-var QT = QuadTree.prototype;
+config.updateOnChange(
+    ['pathing.overlayPathmap', 'pathing.overlayAiPaths'],
+    PathMap.fn);
 
 'set remove removeAll clear'
     .split(' ')
     .forEach(function(name){
-        PathMap.prototype[name] = function(){
+        PathMap.fn[name] = function(){
             delete this._grid;
-            return QT[name].apply(this, arguments);
+            return QuadTree.fn[name].apply(this, arguments);
         };
     });
 
index cf341d6..a6ed869 100644 (file)
@@ -14,7 +14,8 @@ var Y          = require('Y').Y
 Bullet =
 exports['Bullet'] =
 Thing.subclass('Bullet', {
-    traceTrajectories : true,
+    // Config
+    traceTrajectories : false,
     
     explosions : {
         timeBase : 400,     // base duration (ms)
@@ -132,7 +133,7 @@ Thing.subclass('Bullet', {
     render : function render(parent){
         this.remove();
         
-        if (config.get('pathing.traceTrajectories')) {
+        if (this.traceTrajectories) {
             var t = this.trajectory;
             this.tline = Line.fromPoints(t.x1,t.y1, t.x2,t.y2)
                 .attr('drawDefinitionPoints', true)
@@ -152,3 +153,5 @@ Thing.subclass('Bullet', {
     
 });
 
+config.updateOnChange('pathing.traceTrajectories', Bullet.fn);
+
index e0e634a..3931c44 100644 (file)
@@ -14,7 +14,8 @@ var Y           = require('Y').Y
 Thing =
 exports['Thing'] =
 new evt.Class('Thing', {
-    showAttackCooldown : config.get('ui.showAttackCooldown'),
+    // Config
+    showAttackCooldown : null,
     
     // Attributes
     stats: {
@@ -196,3 +197,6 @@ Y(Thing).extend({
         return stats;
     }
 });
+
+config.updateOnChange('ui.showAttackCooldown', Thing.fn);
+
index 624ae5f..ed8424f 100644 (file)
@@ -6,13 +6,16 @@ var Y = require('Y').Y
 Grid =
 exports['Grid'] =
 Rect.subclass('Grid', {
+    // Config
+    createGridTable  : null,
+    createGridCanvas : null,
+    
+    // Defaults
     _cssClasses : 'ezl layer shape rect grid',
     
     strokeStyle : '#6E6E6E',
     lineWidth   : 0.5,
     
-    createGridTable  : config.get('ui.createGridTable'),
-    createGridCanvas : config.get('ui.createGridCanvas'),
     
     
     
@@ -73,3 +76,6 @@ Rect.subclass('Grid', {
     }
     
 });
+
+config.updateOnChange(['ui.createGridTable', 'ui.createGridCanvas'], Grid.fn);
+