Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var xspan:Number = 15;
- var xLeft:Number;
- var xRight:Number;
- var footL:Number;
- var footM:Number;
- var footR:Number;
- var slope:Number;
- var onGround:Boolean = false;
- var xs:Number = 0;
- var ys:Number = 0;
- //
- var maxXs:Number = 8;
- var maxYs:Number = 15;
- var grav:Number = 1.5;
- var ACC:Number = 1;
- var maxCheckDist:Number = 60;
- //
- var keyL:Boolean = false;
- var keyR:Boolean = false;
- var keyU:Boolean = false;
- //
- onEnterFrame = function () {
- //Controls
- keyL = Key.isDown (Key.LEFT);
- keyR = Key.isDown (Key.RIGHT);
- keyU = Key.isDown (Key.UP);
- //
- _x += xs;
- _y += ys;
- findEdges ();
- //GROUND STATE
- if (onGround) {
- hitbox._alpha = 100;
- //Wall detection
- if (hits (xRight, _y-10) and hits (xLeft, _y-10)) {
- trace ("Walls on both sides at once: mind = blown");
- break;
- }
- while (hits (xRight, _y-10)) {
- _x--;
- findEdges ();
- }
- while (hits (xLeft, _y-10)) {
- _x++;
- findEdges ();
- }
- //Running
- if (keyL and !keyR) {
- xAccel (-1);
- } else if (keyR) {
- xAccel (1);
- } else {
- //may change later to slowdown
- xs = 0;
- }
- findSlope ();
- //Slope handling
- if (!onEdge ()) {
- _y = (footL+footR)*0.5-hitbox._height*0.5;
- if (slope>=1.1) {
- _x += slope*3;
- xs = Math.min (0, xs);
- findEdges ();
- findSlope ();
- _y = (footL+footR)*0.5-hitbox._height*0.5;
- } else if (slope<=-1.1) {
- _x += slope*3;
- xs = Math.max (0, xs);
- findEdges ();
- findSlope ();
- _y = (footL+footR)*0.5-hitbox._height*0.5;
- }
- } else {
- if (footM-_y>=20) {
- onGround = false;
- }
- }
- //Jumping
- if (keyU) {
- // and slope<=1.0 and slope>=-1.0) {
- onGround = false;
- ys = -17;
- _y -= 2;
- //if (Math.abs (slope)>=0.5) {
- //xs += slope*5;
- //}
- }
- //Otherwise ending ground state (i.e. walking off ledge)
- if (footM>=_y+hitbox._height) {
- onGround = false;
- }
- //AIR STATE
- } else {
- hitbox._alpha = 50;
- //Horizontal movement
- if (keyL and !keyR) {
- xAccel (-0.5);
- } else if (keyR) {
- xAccel (0.5);
- } else {
- xs *= 0.93;
- }
- //Ceiling detection
- while (hits (_x, _y-20)) {
- _y++;
- findEdges ();
- }
- //Wall detection
- while (hits (xRight, _y-18)) {
- _x--;
- findEdges ();
- }
- while (hits (xLeft, _y-18)) {
- _x++;
- findEdges ();
- }
- while (hits (xRight, _y+5)) {
- _x--;
- findEdges ();
- }
- while (hits (xLeft, _y+5)) {
- _x++;
- findEdges ();
- }
- //
- if (footM<=_y+hitbox._height*0.5) {
- onGround = true;
- ys = 0;
- _y = footM-hitbox._height*0.5;
- } else {
- ys = Math.min (maxYs, ys+grav);
- }
- //
- }
- //trace ((footR-FootL));
- };
- //
- //Figures out where the corners and edges of this MC are
- function drawLineDown (xarg:Number):Number {
- var checkY:Number = _y;
- while (!hits (xarg, checkY) and checkY-_y<maxCheckDist) {
- checkY += ACC;
- }
- return (checkY);
- }
- //Figures out where the corners and edges of this MC are
- function findEdges () {
- xLeft = _x-xspan;
- xRight = _x+xspan;
- footL = drawLineDown (xLeft);
- footM = drawLineDown (_x);
- footR = drawLineDown (xRight);
- }
- function onEdge ():Boolean {
- slopeL = ((footM-footL)/xspan/4);
- slopeR = ((footR-footM)/xspan/4);
- return (Math.abs (slopeL-slopeR)>=0.5);
- }
- function findSlope () {
- slope = ((footR-footL)/xspan/2);
- _parent.slope = slope;
- }
- function xAccel (arg:Number) {
- xs = Math.max (-maxXs, Math.min (maxXs, xs+arg));
- }
- //Checks if the given point is overlapping terrain.
- function hits (xa:Number, ya:Number):Boolean {//fTests ++;
- return _parent.terrain.hitTest (xa+_parent._x, ya+_parent._y, true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement