Advertisement
Guest User

Platform

a guest
Jun 30th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var xspan:Number = 15;
  2. var xLeft:Number;
  3. var xRight:Number;
  4. var footL:Number;
  5. var footM:Number;
  6. var footR:Number;
  7. var slope:Number;
  8. var onGround:Boolean = false;
  9. var xs:Number = 0;
  10. var ys:Number = 0;
  11. //
  12. var maxXs:Number = 8;
  13. var maxYs:Number = 15;
  14. var grav:Number = 1.5;
  15. var ACC:Number = 1;
  16. var maxCheckDist:Number = 60;
  17. //
  18. var keyL:Boolean = false;
  19. var keyR:Boolean = false;
  20. var keyU:Boolean = false;
  21. //
  22. onEnterFrame = function () {
  23.     //Controls
  24.     keyL = Key.isDown (Key.LEFT);
  25.     keyR = Key.isDown (Key.RIGHT);
  26.     keyU = Key.isDown (Key.UP);
  27.     //
  28.     _x += xs;
  29.     _y += ys;
  30.     findEdges ();
  31.     //GROUND STATE
  32.     if (onGround) {
  33.         hitbox._alpha = 100;
  34.         //Wall detection
  35.         if (hits (xRight, _y-10) and hits (xLeft, _y-10)) {
  36.             trace ("Walls on both sides at once: mind = blown");
  37.             break;
  38.         }
  39.         while (hits (xRight, _y-10)) {
  40.             _x--;
  41.             findEdges ();
  42.         }
  43.         while (hits (xLeft, _y-10)) {
  44.             _x++;
  45.             findEdges ();
  46.         }
  47.         //Running
  48.         if (keyL and !keyR) {
  49.             xAccel (-1);
  50.         } else if (keyR) {
  51.             xAccel (1);
  52.         } else {
  53.             //may change later to slowdown
  54.             xs = 0;
  55.         }
  56.         findSlope ();
  57.         //Slope handling
  58.         if (!onEdge ()) {
  59.             _y = (footL+footR)*0.5-hitbox._height*0.5;
  60.             if (slope>=1.1) {
  61.                 _x += slope*3;
  62.                 xs = Math.min (0, xs);
  63.                 findEdges ();
  64.                 findSlope ();
  65.                 _y = (footL+footR)*0.5-hitbox._height*0.5;
  66.             } else if (slope<=-1.1) {
  67.                 _x += slope*3;
  68.                 xs = Math.max (0, xs);
  69.                 findEdges ();
  70.                 findSlope ();
  71.                 _y = (footL+footR)*0.5-hitbox._height*0.5;
  72.             }
  73.         } else {
  74.             if (footM-_y>=20) {
  75.                 onGround = false;
  76.             }
  77.         }
  78.         //Jumping      
  79.         if (keyU) {
  80.             // and slope<=1.0 and slope>=-1.0) {
  81.             onGround = false;
  82.             ys = -17;
  83.             _y -= 2;
  84.             //if (Math.abs (slope)>=0.5) {
  85.             //xs += slope*5;
  86.             //}
  87.         }
  88.         //Otherwise ending ground state (i.e. walking off ledge)          
  89.         if (footM>=_y+hitbox._height) {
  90.             onGround = false;
  91.         }
  92.         //AIR STATE      
  93.     } else {
  94.         hitbox._alpha = 50;
  95.         //Horizontal movement
  96.         if (keyL and !keyR) {
  97.             xAccel (-0.5);
  98.         } else if (keyR) {
  99.             xAccel (0.5);
  100.         } else {
  101.             xs *= 0.93;
  102.         }
  103.         //Ceiling detection
  104.         while (hits (_x, _y-20)) {
  105.             _y++;
  106.             findEdges ();
  107.         }
  108.         //Wall detection
  109.         while (hits (xRight, _y-18)) {
  110.             _x--;
  111.             findEdges ();
  112.         }
  113.         while (hits (xLeft, _y-18)) {
  114.             _x++;
  115.             findEdges ();
  116.         }
  117.         while (hits (xRight, _y+5)) {
  118.             _x--;
  119.             findEdges ();
  120.         }
  121.         while (hits (xLeft, _y+5)) {
  122.             _x++;
  123.             findEdges ();
  124.         }
  125.         //
  126.         if (footM<=_y+hitbox._height*0.5) {
  127.             onGround = true;
  128.             ys = 0;
  129.             _y = footM-hitbox._height*0.5;
  130.         } else {
  131.             ys = Math.min (maxYs, ys+grav);
  132.         }
  133.         //
  134.     }
  135.     //trace ((footR-FootL));  
  136. };
  137. //
  138. //Figures out where the corners and edges of this MC are
  139. function drawLineDown (xarg:Number):Number {
  140.     var checkY:Number = _y;
  141.     while (!hits (xarg, checkY) and checkY-_y<maxCheckDist) {
  142.         checkY += ACC;
  143.     }
  144.     return (checkY);
  145. }
  146. //Figures out where the corners and edges of this MC are
  147. function findEdges () {
  148.     xLeft = _x-xspan;
  149.     xRight = _x+xspan;
  150.     footL = drawLineDown (xLeft);
  151.     footM = drawLineDown (_x);
  152.     footR = drawLineDown (xRight);
  153. }
  154. function onEdge ():Boolean {
  155.     slopeL = ((footM-footL)/xspan/4);
  156.     slopeR = ((footR-footM)/xspan/4);
  157.     return (Math.abs (slopeL-slopeR)>=0.5);
  158. }
  159. function findSlope () {
  160.     slope = ((footR-footL)/xspan/2);
  161.     _parent.slope = slope;
  162. }
  163. function xAccel (arg:Number) {
  164.     xs = Math.max (-maxXs, Math.min (maxXs, xs+arg));
  165. }
  166. //Checks if the given point is overlapping terrain.
  167. function hits (xa:Number, ya:Number):Boolean {//fTests ++;
  168.     return _parent.terrain.hitTest (xa+_parent._x, ya+_parent._y, true);
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement