From ac6046ec99a710db08608076e13f70898f609d67 Mon Sep 17 00:00:00 2001 From: dsc Date: Fri, 24 Dec 2010 11:52:34 -0800 Subject: [PATCH] Adds properties for reflectivity and inventory; fills in Item collision code. --- src/ezl/index.cjs | 9 +- src/ezl/layer.cjs | 653 -------------------------------------- src/ezl/layer/layer.cjs | 651 +++++++++++++++++++++++++++++++++++++ src/ezl/shape/circle.cjs | 3 +- src/ezl/shape/line.cjs | 2 +- src/ezl/shape/shape.cjs | 3 +- src/ezl/util/astar.cjs | 2 +- src/ezl/util/binaryheap.cjs | 155 --------- src/ezl/util/tree/binaryheap.cjs | 155 +++++++++ src/ezl/widget/cooldown.cjs | 2 +- src/tanks/calc.cjs | 5 - src/tanks/map/level.cjs | 2 +- src/tanks/map/pathmap.cjs | 2 +- src/tanks/map/traversal.cjs | 7 +- src/tanks/thing/bullet.cjs | 4 +- src/tanks/thing/item.cjs | 80 ++++- src/tanks/thing/player.cjs | 5 + src/tanks/thing/tank.cjs | 16 +- src/tanks/thing/thing.cjs | 8 +- src/tanks/thing/wall.cjs | 20 +- src/tanks/ui/pathmapui.cjs | 70 +++-- www/css/lttl.css | 32 +- www/deps.html | 11 +- 23 files changed, 992 insertions(+), 905 deletions(-) delete mode 100644 src/ezl/layer.cjs create mode 100644 src/ezl/layer/html.cjs create mode 100644 src/ezl/layer/index.cjs create mode 100644 src/ezl/layer/layer.cjs create mode 100644 src/ezl/layer/text.cjs delete mode 100644 src/ezl/util/binaryheap.cjs create mode 100644 src/ezl/util/tree/binaryheap.cjs delete mode 100644 src/tanks/abilities/buff.cjs delete mode 100644 src/tanks/abilities/index.cjs delete mode 100644 src/tanks/calc.cjs create mode 100644 src/tanks/effects/buff.cjs create mode 100644 src/tanks/effects/index.cjs diff --git a/src/ezl/index.cjs b/src/ezl/index.cjs index 0b952af..112295b 100644 --- a/src/ezl/index.cjs +++ b/src/ezl/index.cjs @@ -6,12 +6,13 @@ var Y = require('Y').Y; Y.extend(exports, { - 'Layer' : require('ezl/layer').Layer, - + 'math' : require('ezl/math'), 'loc' : require('ezl/loc'), + 'util' : require('ezl/util'), + 'loop' : require('ezl/loop'), - 'math' : require('ezl/math'), + + 'layer' : require('ezl/layer'), 'shape' : require('ezl/shape'), - 'util' : require('ezl/util'), 'widget' : require('ezl/widget') }); diff --git a/src/ezl/layer.cjs b/src/ezl/layer.cjs deleted file mode 100644 index 21f150f..0000000 --- a/src/ezl/layer.cjs +++ /dev/null @@ -1,653 +0,0 @@ -//#ensure "jquery" - -var undefined -, Y = require('Y').Y -, Vec = require('ezl/math/vec').Vec -, loc = require('ezl/loc') -, Loc = loc.Loc -, BoundingBox = loc.BoundingBox -, Animation = require('ezl/loop/fx').Animation -, -CONTEXT_ATTRS = Y('globalAlpha globalCompositeOperation strokeStyle fillStyle lineWidth lineCap lineJoin miterLimit shadowOffsetX shadowOffsetY shadowBlur shadowColor'.split(' ')), -FAUX_ACCESSORS = Y('width height position stroke fill origin rotate scale translate title'.split(' ')) -,_X = 0, _Y = 1 -, - - - -Layer = -exports['Layer'] = -Y.subclass('Layer', { - _cssClasses : 'ezl layer', - - canvas : null, - parent : null, - children : null, - - dirty : true, - ctx : null, - animActive : null, - animQueue : null, - - - layerWidth : 0, canvasWidth : 0, - layerHeight : 0, canvasHeight : 0, - - x: 0, y: 0, loc : null, // Position relative to parent - - // Bleeds are marks outside the declared layer-size - negBleed : null, // Loc - posBleed : null, // Loc - - // Transforms - _origin : null, // rotational origin - transform : null, // Object - - /// Defaults /// - - useCanvasScaling : false, // Default to CSS3 scaling - alwaysClearDrawing : true, // Whether to clear the canvas content before redraw - alwaysClearAttrs : false, // Whether to remove all canvas attributes (CONTEXT_ATTRS) - // and transforms (scale, rotate, translate) and reset defaults before redraw - - originX : 0, - originY : 0, - - - - /// Setup /// - - init : function init(){ - this.children = new Y.YArray(); - this.animActive = new Y.YArray(); - this.animQueue = new Y.YArray(); - - this.loc = new Loc(0,0); - this.negBleed = new Loc(0,0); - this.posBleed = new Loc(0,0); - - this.boundingBox = new BoundingBox(0,0, 0,0, this.originX,this.originY); - this._origin = this.boundingBox.origin; - - this.transform = { - rotate : 0, - scale : new Loc(1.0,1.0), - translate : new Loc(0,0) // translates canvas - }; - - this.canvas = jQuery(''); - this.ctx = this.canvas[0].getContext('2d'); - - this.layer = jQuery('
') - .addClass(this._cssClasses) - .append(this.canvas); - - this.layer[0].layer = - this.canvas[0].layer = this; - - this.tick = this.tick.bind(this); - }, - - /// Scene Graph Heirarchy /// - - /** @param {Layer} child */ - append : function append(child){ - new Y(arguments).invoke('appendTo', this); - return this; - }, - - /** @param {Layer} parent */ - appendTo : function appendTo(parent){ - if (!parent) return this; - - // Always ensure we detach from the DOM and redraw this node - this.remove(); - this.dirty = true; - - // Layer? Add self as new child, fix DOM - if ( parent instanceof Layer ) { - this.parent = parent; - parent.children.push(this); - parent.layer.append(this.layer); - - // Otherwise: Attach to a DOM node as a new root layer node, leave parent null - } else - $(parent).append(this.layer); - - return this; - }, - - /** - * Removes this layer from its parent and the DOM. - */ - remove : function remove(){ - if (this.parent) - this.parent.children.remove(this); - this.parent = null; - this.layer.remove(); - }, - - /** - * Clears this layer and destroys all children. - */ - empty : function empty(ctx){ - this.children.invoke('remove'); - this.clear(ctx); - return this; - }, - - /** - * @return The root of the scene graph. - */ - root : function root(){ - if (this.parent) - return this.parent.root(); - else - return this; - }, - - - - - - /// Attributes /// - - // set : function set(key, value, def){ - // if ( key === undefined || (value === undefined && def === undefined) ) - // return this; - // - // value = (value !== undefined ? value : def); - // if ( FAUX_ACCESSORS.has(key) ) - // this[key](value); - // else - // this[key] = value; - // - // return this; - // }, - - attr : Y.attr.methodize(), - - size : function size(w,h){ - if (w === undefined && h === undefined) - return new Vec(this.layerWidth,this.layerHeight); - - this.layerWidth = w; - this.layerHeight = h; - - var bb = this.boundingBox.resize(w,h) - , nb = this.negBleed, pb = this.posBleed - - // HTMLCanvas.{width,height} is a long - , cw = this.canvasWidth = Math.ceil(w + nb.x + pb.x) - , ch = this.canvasHeight = Math.ceil(h + nb.y + pb.y) - ; - - this.layer.css({ - 'width' : w, 'height' : h, - 'left' : bb.x1, 'top' : bb.y1 - }); - this.canvas.css({ - 'width' : cw, 'height' : ch, - 'margin-left' : -nb.x, 'top' : -nb.y - }); - var el = this.canvas[0]; - el.width = cw; - el.height = ch; - - return this; - }, - - /** - * Changes the layer's width and then updates the canvas. - */ - width : function width(w){ - if (w === undefined) - return this.layerWidth; - - this.layerWidth = w; - - var bb = this.boundingBox.resize(w, this.layerHeight) - // , ro = bb.relOrigin - , nb = this.negBleed - , cw = this.canvasWidth = Math.ceil(w + nb.x + this.posBleed.x); // HTMLCanvas.width is a long - - this.layer.css({ - 'width' : w, - 'left' : bb.x1, - // 'margin-left' : -ro.x - }); - - this.canvas.css({ - 'width' : cw, - 'margin-left' : -nb.x - }); - this.canvas[0].width = cw; - - return this; - }, - - height : function height(h){ - if (h === undefined) - return this.layerHeight; - - this.layerHeight = h; - - var bb = this.boundingBox.resize(this.layerWidth, h) - // , ro = bb.relOrigin - , nb = this.negBleed - , ch = this.canvasHeight = Math.ceil(h + nb.y + this.posBleed.y); // HTMLCanvas.height is a long - - this.layer.css({ - 'height' : h, - 'top' : bb.y1, - // 'margin-top' : -ro.y - }); - - this.canvas.css({ - 'height' : ch, - 'margin-top' : -nb.y - }); - this.canvas[0].height = ch; - - return this; - }, - - /** - * position() -> {Loc} - * Gets position of the layer relative to the parent. - * @return {Loc} - */ - /** - * position(x,y) -> {this} - * Sets the position of this node, and then returns it. - * @param {Number|String|undefined} x - * @param {Number|String|undefined} y If omitted, this method must be invoked with `undefined` as the first argument. - * @return {this} - */ - /** - * position(pos) -> {this} - * Sets the position of this node, and then returns it. - * @param {Object} pos An object with "x" and/or "y" properties as in `position(x,y)`. - * @return {this} - */ - position : function position(x,y){ - if (x === undefined && y === undefined) - return this.layer.position(); - - if ( x instanceof Array ) { - y = x[_Y]; x = x[_X]; - } else if ( Y.isPlainObject(x) ){ - y = ('top' in x ? x.top : x.y); - x = ('left' in x ? x.left : x.x); - } - - var bbox = this.boundingBox.relocate(x,y); - this.css({ - 'left' : bbox.x1, - 'top' : bbox.y1 - }); - return this; - }, - - stroke : function stroke(style, width){ - if (style === undefined && width === undefined) - return this.strokeStyle; - - if (width !== undefined) - this.lineWidth = width; - - this.dirty = true; - this.strokeStyle = style; - return this; - }, - - fill : function fill(style){ - if (style === undefined) - return this.fillStyle; - - this.dirty = true; - this.fillStyle = style; - return this; - }, - - hide : makeDelegate('hide'), - show : makeDelegate('show'), - - /** CSS properties */ - css : makeDelegate('css'), - - /** Position relative to document. */ - offset : makeDelegate('offset'), - - - - /// Transformations /// - - /** - * Gets and sets the origin-location for this shape, used for - * position and rotation. Defaults to the midpoint. - */ - origin : function origin(x,y){ - var o = this._origin; - if (arguments.length === 0) - return o.absolute(this.layerWidth, this.layerHeight); - - if ( x instanceof Array ) { - y = x[_Y]; x = x[_X]; - } - o.x = x; - o.y = y; - return this._applyTransforms(); - }, - - /** - * Rotates this layer by r radians. - */ - rotate : function rotate(r){ - var t = this.transform; - if (r === undefined) - return t.rotate; - - t.rotate = r; - return this._applyTransforms(); - }, - - /** - * Scales this layer by (sx,sy), and then applies scaling relatively - * to all sublayers (preserving knowledge of their individual scaling). - */ - scale : function scale(sx,sy){ - var ts = this.transform.scale; - if (arguments.length === 0) - return ts; - - if ( sx instanceof Array ) { - sy = sx[_Y]; sx = sx[_X]; - } - ts.x = sx; - ts.y = sy; - this.dirty = true; - return this._applyTransforms(); - }, - - /** - * Translates draw calls by (x,y) within this layer only. This allows you to - * functionally move the origin of the coordinate system for this layer. - */ - translate : function translate(tx,ty){ - var tt = this.transform.translate; - if (arguments.length === 0) - return tt; - - if ( tx instanceof Array ) { - ty = tx[_Y]; tx = tx[_X]; - } - tt.x = tx; - tt.y = ty; - this.dirty = true; - return this; - }, - - /** - * @private - */ - _applyTransforms : function _applyTransforms(){ - var t = this.transform, tfns = []; - - if (t.rotate !== 0) - tfns.push('rotate('+t.rotate+'rad)'); - - if (!this.useCanvasScaling && (t.scale.x !== 1 || t.scale.y !== 1)) - tfns.push('scale('+t.scale.x+','+t.scale.y+')'); - - var trans = (tfns.length ? tfns.join(' ') : 'none') - , o = this._origin.toUnits('px') - , origin = o.x+' '+o.y ; - this.layer.css(['', '-moz-', '-webkit-'] - .reduce( - function(values, prefix){ - values[prefix+'transform'] = trans; - values[prefix+'transform-origin'] = origin; - return values; - }, - {}) ); - return this; - }, - - - - - - - - /// Iterators /// - - invoke : function invoke(name){ - var args = Y(arguments,1); - // this[name].apply(this, args); - // this.children.invoke.apply(this.children, ['invoke', name].concat(args)); - return this._invoke(name, args); - }, - _invoke : function _invoke(name, args){ - this[name].apply(this, args); - this.children.invoke('_invoke', name, args); - return this; - }, - - setAll : function setAll(k,v){ - this[k] = v; - this.children.invoke('setAll', k,v); - return this; - }, - - /** - * Reduce "up", across this and parents, inner to outer: - * acc = fn.call(context || node, acc, node) - */ - reduceup : function reduceup(fn, acc, context){ - acc = fn.call(context || this, acc, this); - return ( this.parent ? this.parent.reduceup(fn, acc, context) : acc ); - }, - - /** - * Reduce "down", across this and children, depth-first: - * acc = fn.call(context || node, acc, node) - */ - reduce : function reduce(fn, acc, context){ - acc = fn.call(context || this, acc, this); - return this.children.reduce(function(acc, child){ - return child.reduce(fn, acc, context); - }, acc); - }, - - - - - - - /// Drawing Functions /// - - /** - * @param {CanvasDrawingContext2D} [ctx=this.ctx] Forces context to use rather than the layer's own. - * @param {Boolean} [force=false] Forces redraw. - */ - draw : function draw(ctx, force){ - var _ctx = ctx || this.ctx; - - if ( this.dirty || force ){ - this.dirty = false; - this._openPath(_ctx); - this.render(_ctx); - this._closePath(_ctx); - } - - this.children.invoke('draw', ctx, force); - return this; - }, - - _openPath : function _openPath(ctx){ - var self = this - , neg = this.negBleed - , t = this.transform - , w = this.canvasWidth, h = this.canvasHeight ; - - ctx.beginPath(); - - // TODO: only alwaysClearAttrs should reset transforms? - // if (this.alwaysClearAttrs) - ctx.setTransform(1,0,0,1,0,0); - - if (this.alwaysClearDrawing) - ctx.clearRect(-w,-h, 2*w,2*h); - - // if (this.alwaysClearAttrs) { - if (this.useCanvasScaling && (t.scale.x !== 1 || t.scale.y !== 1)) - ctx.scale(t.scale.x,t.scale.y); - - ctx.translate(neg.x, neg.y); - ctx.translate(t.translate.x, t.translate.y); - // } - - // Set context attributes - CONTEXT_ATTRS.forEach(function(name){ - if (this[name] !== undefined) - ctx[name] = this[name]; - else if (this.alwaysClearAttrs) - delete ctx[name]; - }, this); - - return this; - }, - - /** To be implemented by subclasses. */ - render : function render(ctx){ return this; }, - - _closePath : function _closePath(ctx){ - ctx.closePath(); - return this; - }, - - /** - * Clears this layer and optionally all children. - */ - clear : function clear(ctx, clearChildren){ - var w = this.canvas.width() - , h = this.canvas.height(); - ctx = ctx || this.ctx; - ctx.beginPath(); - ctx.setTransform(1,0,0,1,0,0); - ctx.clearRect(-w,-h, 2*w,2*h); - ctx.closePath(); - if (clearChildren) - this.children.invoke('clear'); - return this; - }, - - /** - * @param {Number} duration Duration (ms) of animation. - * @param {Object} props Object whose keys are the property-names to animate - * paired with the target value. Animated properties can also be relative. - * If a value is supplied with a leading += or -= sequence of characters, - * then the target value is computed by adding or subtracting the given - * number from the current value of the property. - * @param {Object} [options] Animation options: - * * {Number} [delay=0] Milliseconds to wait before beginning animation. - * * {Boolean} [queue=true] Indicates whether to place the animation - * in the effects queue to run when other animations have finished. - * If false, the animation will begin after `delay` milliseconds; - * likewise, if `delay` is non-zero `queue` defaults to `false` - * instead. - * * {String|Function|Object} [easing='linear'] Name of easing function - * used to calculate each step value, or a function, or a map of - * properties to either of the above. - * * {Function} [complete] Function called on animation completion. - * * {Function} [step] Function called after each step of the animation. - * @param {Float} [now] Time (ms) to use as the start-time. If omitted, the - * current time is used. - */ - animate : function animate(duration, props, options, now) { - var A = new Animation(this, duration, props, options); - if ( A.options.queue && this.animActive.length ) - this.animQueue.push(A); - else - this.animActive.push(A.start(now || new Date().getTime())); - return this; - }, - - tick : function tick(elapsed, now){ - if (elapsed instanceof Event) { - var d = evt.data; - now = d.now; - elapsed = d.elapsed; - } - - this.animActive = this.animActive.filter(function(anim){ - return anim.tick(elapsed, now); - }); - - var childrenRunning = this.children.invoke('tick', elapsed, now).some(Y.op.I) - , running = !!this.animActive.length; - if ( !running && this.animQueue.length ) - this.animActive.push( this.animQueue.shift().start(now) ); - - return childrenRunning || running; - }, - - /// Debuggging /// - - // 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; - }, - - title : function title(val){ - if (val === undefined) - return this.layer.attr('title'); - - this.layer.attr('title', val); - return this; - }, - - - - /// Misc /// - toString : function toString(){ - var pos = ((this.layer && this.layer.parent()[0]) ? this.position() : {top:NaN, left:NaN}); - return this.className+'['+pos.left+','+pos.top+']( children='+this.children.size()+' )'; - } -}); - -function makeDelegate(name, dirties, prop){ - prop = prop || 'layer'; - return function delegate(){ - if (dirties && arguments.length) - this.dirty = true; - - var target = this[prop] - , result = target[name].apply(target, arguments); - - return (result !== target ? result : this); - }; -} - -// Install CSS styles -$(function(){ - $('