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;
+ },
});
, 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,
*/
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)
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));
},
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)
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()
});
-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);
};
});
Bullet =
exports['Bullet'] =
Thing.subclass('Bullet', {
- traceTrajectories : true,
+ // Config
+ traceTrajectories : false,
explosions : {
timeBase : 400, // base duration (ms)
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)
});
+config.updateOnChange('pathing.traceTrajectories', Bullet.fn);
+