From: dsc Date: Mon, 8 Nov 2010 08:53:07 +0000 (-0800) Subject: Bullet reflections now work! X-Git-Url: http://git.less.ly:3516/?a=commitdiff_plain;h=31e54b12a3b4d2edd707307a6b2ac4067afdc50d;p=tanks.git Bullet reflections now work! --- diff --git a/index.php b/index.php index 40593b9..2a76380 100644 --- a/index.php +++ b/index.php @@ -43,6 +43,11 @@ $scripts = array( "src/portal/layer.js", "src/portal/shape.js", + + "src/portal/math/math.js", + "src/portal/math/vec.js", + "src/portal/math/line.js", + "src/portal/util/quadtree.js", "src/portal/util/rbtree.js", "src/portal/util/eventloop.js", diff --git a/src/Y/core.js b/src/Y/core.js index 19e33ae..493e4bf 100644 --- a/src/Y/core.js +++ b/src/Y/core.js @@ -1,6 +1,6 @@ // Generic Collection Functions -function notSelfOrWrapped(fn){ +function notWrapped(fn){ var self = arguments.callee.caller; return fn && fn !== self && fn.__wraps__ !== self; } @@ -10,7 +10,7 @@ function reduce(o, fn, acc, cxt){ return acc; // fn = Function.toFunction(fn); - if ( notSelfOrWrapped(o.reduce) ) + if ( notWrapped(o.reduce) ) return o.reduce.apply(o, [fn, acc, cxt]); cxt = cxt || o; @@ -20,31 +20,39 @@ function reduce(o, fn, acc, cxt){ return acc; } -function set(o, key, value, def){ - if ( o && notSelfOrWrapped(o.set) ) - return o.set.apply(o, slice.call(arguments,1)); +function set(o, key, value, def){ if ( o && key !== undefined ) o[key] = (value !== undefined ? value : def); - return o; } -function attr(o, key, value, def){ - if ( o && notSelfOrWrapped(o.attr) ) - return o.attr.apply(o, slice.call(arguments,1)); - +function dset(o, key, value, def){ + if ( o && notWrapped(o.set) ) + return o.set.apply(o, slice.call(arguments,1)); + else + return set(o, key, value, def); +} + +function attr(o,key,value,def){ if ( !o || key === undefined ) return o; - if ( isPlainObject(key) ) + if ( Y.isPlainObject(key) ) return extend(o, key); if ( value !== undefined || def !== undefined ){ - return set(o, key, value, def); + return dset(o, key, value, def); } else return o[key]; } +function dattr(o, key, value, def){ + if ( o && notWrapped(o.attr) ) + return o.attr.apply(o, slice.call(arguments,1)); + else + return attr(o, key, value, def); +} + function extend( A, B ){ return slice.call(arguments,1).reduce(extend._extendall, A); } @@ -52,6 +60,6 @@ extend._extendall = function _extendall(A, donor){ return reduce(donor, extend._set, A); }; extend._set = function _set(o, v, k){ - return attr(o, k, v, o[k]); + return dattr(o, k, v, o[k]); }; diff --git a/src/Y/y-core.js b/src/Y/y-core.js index 995751b..dced427 100644 --- a/src/Y/y-core.js +++ b/src/Y/y-core.js @@ -1,7 +1,7 @@ Y.reduce = reduce; -Y.set = set; -Y.attr = attr; +Y.set = dset; +Y.attr = dattr; Y.extend = extend; Y.isFunction = isFunction; diff --git a/src/Y/y-op.js b/src/Y/y-op.js index 5017606..ca5064d 100644 --- a/src/Y/y-op.js +++ b/src/Y/y-op.js @@ -36,17 +36,24 @@ Y.op = { zrshift: function(x,y){ return x >>> y; }, // values - nop: function(){}, + nop: function(x){}, I: function(x){ return x; }, K: function(k){ return function(){ return k; }; }, val: function(def,o){ return o !== undefined ? o : def; }, ok: function(o){ return o !== undefined && o !== null; }, - // values & accessors - has: function(k,o){ return k in o; }, - get: function(k,o){ return o[k] }, - getdef: function(def,k,o){ return (k in o ? o[k] : def); }, - set: function(o,k,v){ if (o && k !== undefined) o[k] = v; return o; }, + // reduce-ordered values & accessors + khas: function(k,o){ return k in o; }, + kget: function(k,o){ return o[k] }, + defkget: function(def,k,o){ return (k in o ? o[k] : def); }, + vkset: function(o,v,k){ if (o && k !== undefined) o[k] = v; return o; }, + + // curry-ordered values & accessors + has: function(o,k){ return k in o; }, + get: function(o,k){ return o[k] }, + getdef: function(o,k,def){ return (k in o ? o[k] : def); }, + set: set, + attr: attr, method: function(name){ var args = Y(arguments,1); return function(obj){ diff --git a/src/portal/layer.js b/src/portal/layer.js index f88500a..db019dd 100644 --- a/src/portal/layer.js +++ b/src/portal/layer.js @@ -1,5 +1,14 @@ (function($, undefined){ +var CONTEXT_ATTRS = Y([ + 'globalAlpha', 'globalCompositeOperation', + 'strokeStyle', 'fillStyle', + 'lineWidth', 'lineCap', 'lineJoin', 'miterLimit', + 'shadowOffsetX', 'shadowOffsetY', 'shadowBlur', 'shadowColor' +]); + + + Layer = new Y.Class('Layer', { _cssClasses : 'portal layer', @@ -118,6 +127,29 @@ Layer = new Y.Class('Layer', { /// Attributes /// + + attr : function attr(key, value, def){ + if (!key) return this; + + if ( Y.isPlainObject(key) ) { + for (var k in key) + this.attr(k, key[k]); + return this; + + // } else if ( CONTEXT_ATTRS.has(key) ) { + // var r = Y.attr(this.ctx, key, value, def); + // + // if (r === this.ctx) { + // // This implies we set a property + // this.dirty = true; + // return this; + // } else + // return r; + // + } else + return Y.op.attr(this, key, value, def); + }, + /** * Changes the layer's width and then updates the canvas. */ @@ -291,19 +323,6 @@ Layer = new Y.Class('Layer', { /// Drawing Functions /// - // for debugging - point : function point(x,y, color){ - var ctx = this.ctx; - this._openPath(ctx); - - var r = 2; - ctx.arc(x+r,y+r, r, 0, Math.PI*2, false); - ctx.fillStyle = color || '#FFFFFF'; - ctx.fill(); - - this._closePath(ctx); - return this; - }, /** * @param {CanvasDrawingContext2D} [ctx=this.ctx] Forces context to use rather than the layer's own. * @param {Boolean} [force=false] Forces redraw. @@ -322,7 +341,8 @@ Layer = new Y.Class('Layer', { }, _openPath : function _openPath(ctx){ - var w = this.canvasWidth + var self = this + , w = this.canvasWidth , h = this.canvasHeight , neg = this.negBleed ; @@ -337,6 +357,14 @@ Layer = new Y.Class('Layer', { // ctx.scale(this.absScaleX, this.absScaleY); + // Set context attributes + CONTEXT_ATTRS.forEach(function(name){ + if (self[name] === undefined) + delete ctx[name]; + else + ctx[name] = self[name]; + }); + return this; }, @@ -365,6 +393,22 @@ Layer = new Y.Class('Layer', { return this; }, + // for debugging + point : function point(x,y, color){ + var ctx = this.ctx; + // this._openPath(ctx); + + var r = 2; + ctx.beginPath(); + ctx.arc(x,y, r, 0, Math.PI*2, false); + ctx.fillStyle = color || '#FFFFFF'; + ctx.fill(); + ctx.closePath(); + + // this._closePath(ctx); + return this; + }, + @@ -439,6 +483,7 @@ $(function(){ $('