Commits

Mike Meyer committed a352cba

Collision/profiling changes.

Comments (0)

Files changed (2)

src/ld17/Gameplay.as

                                                    level.height - Game.stageHeight );
 
             fpsCounter = Game.layers.uiLayer.Add(FpsCounter) as FpsCounter;
+
+            Game.cheats.AddCheatCode('p', DebugTrace);
         }
 
         /**
         {
             Game.cameraFollowTarget = null;
             Game.layers.RemoveAll();
+            Game.cheats.RemoveCheatCode('p', DebugTrace);
             super.End();
         }
+
+        private function DebugTrace():void
+        {
+            trace("Profiling Info:  ");
+            trace("---- A ----");
+            trace("  times:", Profiler.SectionA.times);
+            trace("  avg:",   Profiler.SectionA.avg);
+            trace("  min:",   Profiler.SectionA.min);
+            trace("  max:",   Profiler.SectionA.max);
+            trace("  total:", Profiler.SectionA.total);
+            trace("---- B ----");
+            trace("  times:", Profiler.SectionB.times);
+            trace("  avg:",   Profiler.SectionB.avg);
+            trace("  min:",   Profiler.SectionB.min);
+            trace("  max:",   Profiler.SectionB.max);
+            trace("  total:", Profiler.SectionB.total);
+            trace("---- C ----");
+            trace("  times:", Profiler.SectionC.times);
+            trace("  avg:",   Profiler.SectionC.avg);
+            trace("  min:",   Profiler.SectionC.min);
+            trace("  max:",   Profiler.SectionC.max);
+            trace("  total:", Profiler.SectionC.total);
+            trace("---- D ----");
+            trace("  times:", Profiler.SectionD.times);
+            trace("  avg:",   Profiler.SectionD.avg);
+            trace("  min:",   Profiler.SectionD.min);
+            trace("  max:",   Profiler.SectionD.max);
+            trace("  total:", Profiler.SectionD.total);
+        }
     }
 
 }

src/ld17/Player.as

     import flash.display.DisplayObject;
     import flash.display.Sprite;
     import flash.geom.Rectangle;
+    import stego.collision.CollisionEvent;
+    import stego.collision.CollisionInfo;
     import stego.constants.Keyboard;
     import stego.core.GameInterface;
     import stego.core.GameObject;
+    import stego.debug.Profiler;
     /**
      * ...
      * @author Mike Meyer
     {
         protected var initted:Boolean = false;
 
+        protected static const COEFFICIENT_OF_RESTITUTION:Number = 0.5;
+
         protected static const HEAD_LENGTH:Number = 40;
-        protected static const HEAD_WIDTH:Number  = 25;
+        protected static const HEAD_WIDTH:Number  = 20;
 
-        private static const NUM_SECTIONS:Number = 10;
+        private static const NUM_SECTIONS:Number = 16;
 
         protected var babyLeviathanBehavior:BabyLeviathanBehavior;
         protected var collisionHack:Sprite;
             {
                 initted = true;
 
+                MakeTail();
+
                 head = new Sprite();
                 this.addChild(head);
-                head.graphics.beginFill(Palette.BLACK);
-                head.graphics.moveTo(  0.875 * HEAD_LENGTH, -0.125 * HEAD_WIDTH);
-                head.graphics.lineTo(  0.875 * HEAD_LENGTH,  0.125 * HEAD_WIDTH);
-                head.graphics.lineTo(    0.5 * HEAD_LENGTH,   0.25 * HEAD_WIDTH);
-                head.graphics.lineTo(  0.375 * HEAD_LENGTH,  0.375 * HEAD_WIDTH);
-                head.graphics.lineTo( -0.125 * HEAD_LENGTH,    0.5 * HEAD_WIDTH);
-                head.graphics.lineTo(      0 * HEAD_LENGTH,      0 * HEAD_WIDTH);
-                head.graphics.lineTo( -0.125 * HEAD_LENGTH,   -0.5 * HEAD_WIDTH);
-                head.graphics.lineTo(  0.375 * HEAD_LENGTH, -0.375 * HEAD_WIDTH);
-                head.graphics.lineTo(    0.5 * HEAD_LENGTH,  -0.25 * HEAD_WIDTH);
-                head.graphics.lineTo(  0.875 * HEAD_LENGTH, -0.125 * HEAD_WIDTH);
+                head.graphics.lineStyle(1, Palette.BLACK);
+                head.graphics.beginFill(Palette.DARK_GREEN);
+                head.graphics.moveTo(  0.875 * HEAD_LENGTH, -0.02 * HEAD_WIDTH);
+                head.graphics.lineTo(  0.875 * HEAD_LENGTH,  0.17 * HEAD_WIDTH);
+                head.graphics.lineTo(    0.5 * HEAD_LENGTH,  0.33 * HEAD_WIDTH);
+                head.graphics.lineTo(  0.375 * HEAD_LENGTH,  0.50 * HEAD_WIDTH);
+                head.graphics.lineTo(  0.125 * HEAD_LENGTH,  0.33 * HEAD_WIDTH);
+                head.graphics.lineTo( -0.125 * HEAD_LENGTH,   0.5 * HEAD_WIDTH);
+                head.graphics.lineTo(      0 * HEAD_LENGTH,     0 * HEAD_WIDTH);
+                head.graphics.lineTo( -0.125 * HEAD_LENGTH,  -0.5 * HEAD_WIDTH);
+                head.graphics.lineTo(  0.125 * HEAD_LENGTH, -0.33 * HEAD_WIDTH);
+                head.graphics.lineTo(  0.375 * HEAD_LENGTH,  -0.5 * HEAD_WIDTH);
+                head.graphics.lineTo(    0.5 * HEAD_LENGTH, -0.33 * HEAD_WIDTH);
+                head.graphics.lineTo(  0.875 * HEAD_LENGTH, -0.02 * HEAD_WIDTH);
                 head.graphics.endFill();
 
-                MakeTail();
+                // eyes
+                head.graphics.lineStyle(1, Palette.LIGHT_GREEN);
+                head.graphics.moveTo( 0.4375 * HEAD_LENGTH,  0.25 * HEAD_WIDTH );
+                head.graphics.lineTo(  0.375 * HEAD_LENGTH,  0.28 * HEAD_WIDTH );
+                head.graphics.moveTo( 0.4375 * HEAD_LENGTH, -0.25 * HEAD_WIDTH );
+                head.graphics.lineTo(  0.375 * HEAD_LENGTH, -0.28 * HEAD_WIDTH );
 
                 collisionHack = new Sprite();
                 collisionHack.graphics.beginFill(Palette.BLACK, 0.0);
         /**
          * Get the collision bounds for collision testing
          */
-        override public function GetCollisionBounds(targetCoordinateSpace:DisplayObject = null):Rectangle
+        override public function GetCollisionBounds():Rectangle
         {
-            if (null == targetCoordinateSpace)
+            if (null == hackCachedBounds)
             {
-                targetCoordinateSpace = root;
+                hackCachedBounds = collisionHack.getBounds(stage);
+                hackCachedBounds.offset(-x,-y);
             }
 
-            // my collision code doesn't handle rotating things or other cases where bounding boxes change
-            return collisionHack.getBounds(targetCoordinateSpace);
+            donotNewMe.x = hackCachedBounds.x + x;
+            donotNewMe.y = hackCachedBounds.y + y;
+            donotNewMe.width = hackCachedBounds.width;
+            donotNewMe.height = hackCachedBounds.height;
+            return donotNewMe;
+        }
+
+        /**
+         * override to deal with collisions
+         */
+        override public function CollisionOccurred(ci:CollisionInfo, object:GameObject):void
+        {
+            dispatchEvent(new CollisionEvent(ci, object));
+
+            if (!CanPassThrough(ci, object))
+            {
+                var collisionBox:Rectangle = this.GetCollisionBounds();
+                var offsetIntoCollisionBoxFromLeft:Number = 0.5 * collisionBox.width;
+                var offsetIntoCollisionBoxFromTop:Number = 0.5 * collisionBox.height;
+                var offsetIntoCollisionBoxFromRight:Number = -0.5 * collisionBox.width;
+                var offsetIntoCollisionBoxFromBottom:Number = -0.5 * collisionBox.height;
+
+                if ( 0.0 < ci.normal.x )
+                {
+                    this.x = ci.location.x + offsetIntoCollisionBoxFromLeft;
+                }
+                else if ( 0.0 > ci.normal.x )
+                {
+                    this.x = ci.location.x + offsetIntoCollisionBoxFromRight;
+                }
+                else
+                {
+                    this.x = ci.location.x;
+                }
+
+                if ( 0.0 < ci.normal.y )
+                {
+                    this.y = ci.location.y + offsetIntoCollisionBoxFromTop;
+                }
+                else if ( 0.0 > ci.normal.y )
+                {
+                    this.y = ci.location.y + offsetIntoCollisionBoxFromBottom;
+                }
+                else
+                {
+                    this.y = ci.location.y;
+                }
+
+                if (ci.normal.x > 0.0)
+                {
+                    dx = COEFFICIENT_OF_RESTITUTION * Math.abs(dx);
+                }
+                else if (ci.normal.x < 0.0)
+                {
+                    dx = -COEFFICIENT_OF_RESTITUTION * Math.abs(dx);
+                }
+                if (ci.normal.y > 0.0)
+                {
+                    dy = COEFFICIENT_OF_RESTITUTION * Math.abs(dy);
+                }
+                else if (ci.normal.y < 0.0)
+                {
+                    dy = -COEFFICIENT_OF_RESTITUTION * Math.abs(dy);
+                }
+            }
         }
     }
 }