Fixes the literal corner case.
authordsc <david.schoonover@gmail.com>
Wed, 10 Nov 2010 15:05:33 +0000 (07:05 -0800)
committerdsc <david.schoonover@gmail.com>
Wed, 10 Nov 2010 15:05:33 +0000 (07:05 -0800)
src/Y/type.js
src/Y/y-core.js
src/Y/y-function.js
src/tanks/globals.js
src/tanks/main.js
src/tanks/map/pathmap.js
src/tanks/thing/bullet.js
src/tanks/thing/thing.js

index 11d7360..7735fdf 100644 (file)
@@ -41,4 +41,7 @@ function isPlainObject( obj ){
     return key === undefined || hasOwn.call( obj, key );
 }
 
+function isA(a, b){
+    return (a instanceof b) || (type_of(a) === b);
+}
 
index ec51cba..5bcfddb 100644 (file)
@@ -4,6 +4,7 @@ Y.set    = dset;
 Y.attr   = dattr;
 Y.extend = extend;
 
+Y.isA           = isA;
 Y.isString      = isString;
 Y.isNumber      = isNumber;
 Y.isFunction    = isFunction;
index bc08bf2..5428275 100644 (file)
@@ -302,6 +302,7 @@ Y(Y.reduce);
 Y(Y.set);
 Y(Y.attr);
 Y(Y.extend);
+Y.isA = Y(Y.isA).curry();
 Y.reduce(YFunction.prototype, function(_,fn,name){
     YFunction(fn);
 });
index 4d2d098..9605125 100644 (file)
@@ -30,3 +30,13 @@ var undefined
 
 ;
 
+if (!window.console) {
+    console = {
+        log   : Y.op.noop,
+        info  : Y.op.noop,
+        warn  : Y.op.noop,
+        error : Y.op.noop,
+        dir   : Y.op.noop,
+        debug : Y.op.noop
+    };
+}
index e141c2a..93782b2 100644 (file)
@@ -22,6 +22,9 @@ function main(){
     T.fireProjectile(6*sq,1.5*sq);
     T.fireProjectile(0.5*sq,0.5*sq);
     
+    T.setLocation(3*sq,3*sq);
+    T.fireProjectile(1*sq,1*sq);
+    
     T.shape.hide();
     LBT.pathmap.removeBlocker(T);
     
index c7b0008..8633bf2 100644 (file)
@@ -16,6 +16,7 @@ PathMap = new Y.Class('PathMap', QuadTree, {
     },
     
     addBlocker : function addBlocker(obj){
+        this.removeBlocker(obj);
         var bb = obj.boundingBox;
         if (obj.blocking && bb)
             obj.region = this.set(bb.x1,bb.y1, bb.x2,bb.y2, obj);
@@ -30,13 +31,6 @@ PathMap = new Y.Class('PathMap', QuadTree, {
         return obj;
     },
     
-    updateBlocker : function updateBlocker(obj){
-        this.removeBlocker(obj);
-        this.addBlocker(obj);
-        return obj;
-    },
-    
-    
     attemptMove : function attemptMove(obj, trj, to){
         var wall, blocker, what, bb = obj.boundingBox
         ,   maxW = this.width,  maxH = this.height
@@ -74,7 +68,13 @@ PathMap = new Y.Class('PathMap', QuadTree, {
         // }
         
         // Check for pathmap collisions
-        blocker = this.get(x1,y1, x2,y2).remove(obj).shift();
+        var blockers = this.get(x1,y1, x2,y2).remove(obj);
+        if (blockers.size() > 1){
+            console.log('multiple blockers! '+blockers._o.slice(0));
+            return { 'ok':false, 'corner':true };
+        }
+        
+        blocker = blockers.shift();
         if ( blocker ) {
             what = blocker+' on the ';
             
index eb1d488..e169d96 100644 (file)
@@ -6,13 +6,10 @@ Bullet = new Y.Class('Bullet', Thing, {
      */
     init : function initBullet(owner, x,y){
         this.owner = owner;
-        
-        var loc = owner.loc
-        ,   trj = this.trajectory = new math.Line(loc.x,loc.y, x,y);
         Thing.init.call(this, owner.game, owner.align);
-        
-        this.setLocation(trj.x1, trj.y1);
-        trj.setTScale(this.stats.move * REF_SIZE/1000);
+        var loc = owner.loc;
+        this.trajectory = new math.Line(loc.x,loc.y, x,y, this.stats.move*REF_SIZE/1000);
+        this.setLocation(loc.x,loc.y);
     },
     blocking : false,
     elapsed : 0,
@@ -32,6 +29,21 @@ Bullet = new Y.Class('Bullet', Thing, {
     
     createCooldowns : Y.op.nop,
     
+    // setLocation : function setLocation(x,y){
+    //     var trj = this.trajectory;
+    //     this.trajectory = new math.Line(x,y, trj.x2,trj.y2, trj.tdist);
+    //     this.elapsed = 0;
+    //     return Thing.prototype.setLocation.call(this, x,y);
+    // },
+    
+    setTarget : function setTarget(x,y){
+        var loc = this.loc
+        ,   trj = this.trajectory;
+        this.trajectory = new math.Line(loc.x,loc.y, x,y, trj.tdist);
+        this.elapsed = 0;
+        return this;
+    },
+    
     act : function act(){
         this.elapsed += ELAPSED;
         if (!this.dead)
@@ -48,40 +60,48 @@ Bullet = new Y.Class('Bullet', Thing, {
         
         // Something obstructs us -- time to reflect!
         if (!test.ok) {
-            to = test.to;
-            var wall = test.wall, p2 = trj.p2
-            ,   bb = this.boundingBox
-            ,   goal = wall.isWithin(p2,bb) ? trj.near(p2.x+2*REF_SIZE, p2.y+2*REF_SIZE) : p2
-            ,   _ng, ng = _ng = math.reflect(goal, wall)
-            ,   x1 = bb.x1, y1 = bb.y1
-            ,   tx = to.x,  ty = to.y
-            ,   cmp = Y.op.cmp
-            ;
             
-            // Are we pointed the wrong direction? Rotate 180 degrees around the bullet
-            if ( cmp(tx,x1) !== cmp(goal.x,x1) || cmp(ty,y1) !== cmp(goal.y,y1) )
-                ng = new math.Vec(tx - ng.x + tx, ty - ng.y + ty);
+            // Literal corner case :P
+            if (test.corner) {
+                var to = this.loc, ng = trj.p1;
+                // this.trajectory = new math.Line(loc.x,loc.y, p1.x,p1.y, trj.tdist);
+                
+                
+            // Normal reflection against one line
+            } else {
+                to = test.to;
+                var wall = test.wall, p2 = trj.p2
+                ,   bb = this.boundingBox
+                ,   goal = wall.isWithin(p2,bb) ? trj.near(p2.x+2*REF_SIZE, p2.y+2*REF_SIZE) : p2
+                ,   _ng, ng = _ng = math.reflect(goal, wall)
+                ,   x1 = bb.x1, y1 = bb.y1
+                ,   tx = to.x,  ty = to.y
+                ,   cmp = Y.op.cmp
+                ;
+                
+                // Are we pointed the wrong direction? Rotate 180 degrees around the bullet
+                if ( cmp(tx,x1) !== cmp(goal.x,x1) || cmp(ty,y1) !== cmp(goal.y,y1) )
+                    ng = new math.Vec(tx - ng.x + tx, ty - ng.y + ty);
+            }
             
-            this.trajectory = new math.Line(
-                    to.x,to.y, ng.x,ng.y,
-                    this.stats.move * REF_SIZE/1000 );
+            this.trajectory = new math.Line(to.x,to.y, ng.x,ng.y, trj.tdist);
             
-            // console.log('['+this._depth+' '+TICKS+'] '+this+' reflected by', test.what);
-            // console.log('  to='+to+',  goal='+goal+(p2.equals(goal) ? '' : ' <~ '+p2));
-            // console.log('  --> new goal='+ng+(ng.equals(_ng) ? '' : ' <~ '+_ng));
-            // console.log('  --> trajectory='+this.trajectory);
+            if (this._depth) {
+                console.log('['+this._depth+' '+TICKS+'] '+this+' reflected by', test.what);
+                console.log('  to='+to+',  goal='+goal+(p2.equals(goal) ? '' : ' <~ '+p2));
+                console.log('  --> new goal='+ng+(ng.equals(_ng) ? '' : ' <~ '+_ng));
+                console.log('  --> trajectory='+this.trajectory);
+            }
             
             this.render(this.game.level).draw();
             this.elapsed = ELAPSED;
             
-            if (this._depth++ < 3)
+            if (this._depth++ < 5)
                 return this.move();
             
-            console.log('['+this._depth+' '+TICKS+'] '+this+' reflected by', test.what);
-            console.log('  to='+to+',  goal='+goal+(p2.equals(goal) ? '' : ' <~ '+p2));
-            console.log('  --> new goal='+ng+(ng.equals(_ng) ? '' : ' <~ '+_ng));
-            console.log('  --> trajectory='+this.trajectory);
-            throw new Error('Reflection error!');
+            console.log('Reflection limit reached!');
+            this._depth = 0;
+            return this;
         }
         
         // if (this._depth > 0)
index 49e6252..d274472 100644 (file)
@@ -50,7 +50,7 @@ Thing = new Evt.Class('Thing', {
         loc = this.loc = new Loc(x,y);
         if (this.shape) this.shape.position(x,y);
         this.boundingBox = new Loc.Rect(x,y, x+this.width,y+this.height);
-        return loc;
+        return this;
     },