home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Corrida / drifting.swf / scripts / __Packages / Vehicle.as
Encoding:
Text File  |  2005-08-23  |  16.1 KB  |  522 lines

  1. class Vehicle extends MovieClip
  2. {
  3.    var currentSpeed;
  4.    var heading;
  5.    var direction;
  6.    var directionLatency;
  7.    var steeringPosition;
  8.    var carNumber;
  9.    var startX;
  10.    var startY;
  11.    var BRAKE_POWER;
  12.    var MAX_SPEED;
  13.    var MAX_SPEED_REVERSE;
  14.    var ACCELERATION;
  15.    var REVERSE_ACCELERATION;
  16.    var ROAD_WIPEOUT_THRESHOLD;
  17.    var GRASS_WIPEOUT_THRESHOLD;
  18.    var tyres;
  19.    var turningLeft;
  20.    var turningRight;
  21.    var accelerating;
  22.    var braking;
  23.    var wipeOutThreshold;
  24.    var lostIt;
  25.    var steeringResponse;
  26.    var STEERING_RATE_OF_CHANGE = 1;
  27.    var STEERING_MAX = 8;
  28.    var STEERING_RANDOM_ELEMENT = 2;
  29.    var FRICTION = 0.1;
  30.    var STEERING_SNAP = 10;
  31.    var SKIDMARK_THRESHOLD = 50;
  32.    var SKIDMARK_LIMIT = 500;
  33.    var SKIDMARK_ADJUSTMENT = 8;
  34.    var MOUSE_DISTANCE_LIMIT = 200;
  35.    var WIPE_OUT_FRICTION = 0.2;
  36.    var SPEED_BLEED_STRENGTH = 0.3;
  37.    var SPEED_BLEED_MAX = 60;
  38.    var MULTIPLIER_TIMEOUT = 30;
  39.    var DRIFT = 3;
  40.    var drifting = false;
  41.    var onGrass = false;
  42.    var onBump = false;
  43.    var onOil = false;
  44.    var x = 0;
  45.    var y = 0;
  46.    var targetSpeed = 0;
  47.    var currentSkidmarkDepth = 0;
  48.    var active = false;
  49.    var lastGateHit = 0;
  50.    var driftTimer = 0;
  51.    var noDriftTimer = 0;
  52.    var reversing = false;
  53.    var loseIt = false;
  54.    var BUMP_WIPEOUT_THRESHOLD = 5;
  55.    function Vehicle()
  56.    {
  57.       super();
  58.       this.currentSpeed = 0;
  59.       this.heading = 0;
  60.       this.direction = 0;
  61.       this.directionLatency = 0;
  62.       this.steeringPosition = 0;
  63.       this.gotoAndStop(this.carNumber);
  64.       this.startX = this._x;
  65.       this.startY = this._y;
  66.       this.BRAKE_POWER = _root.carData_array[this.carNumber].BRAKE_POWER;
  67.       this.MAX_SPEED = _root.carData_array[this.carNumber].MAX_SPEED;
  68.       this.MAX_SPEED_REVERSE = _root.carData_array[this.carNumber].MAX_SPEED_REVERSE;
  69.       this.ACCELERATION = _root.carData_array[this.carNumber].ACCELERATION;
  70.       this.REVERSE_ACCELERATION = _root.carData_array[this.carNumber].REVERSE_ACCELERATION;
  71.       this.ROAD_WIPEOUT_THRESHOLD = _root.carData_array[this.carNumber].ROAD_WIPEOUT_THRESHOLD;
  72.       this.GRASS_WIPEOUT_THRESHOLD = _root.carData_array[this.carNumber].GRASS_WIPEOUT_THRESHOLD;
  73.       this.tyres = _root.carData_array[this.carNumber].TYRES;
  74.    }
  75.    function updatePosition()
  76.    {
  77.       var newXPosition = this._x + Math.sin(this.direction * 3.141592653589793 / 180) * this.currentSpeed;
  78.       var newYPosition = this._y - Math.cos(this.direction * 3.141592653589793 / 180) * this.currentSpeed;
  79.       this._x = newXPosition;
  80.       this._y = newYPosition;
  81.       this.x = this._x;
  82.       this.y = this._y;
  83.       this._rotation = this.heading;
  84.       this.turningLeft = false;
  85.       this.turningRight = false;
  86.       if(!_root.ghostPlayback)
  87.       {
  88.          if(Key.isDown(37))
  89.          {
  90.             this.turningLeft = true;
  91.          }
  92.          if(Key.isDown(39))
  93.          {
  94.             this.turningRight = true;
  95.          }
  96.          if(Key.isDown(38))
  97.          {
  98.             if(_root.gameTicks == 1)
  99.             {
  100.                _root.engineUp_sound.start(audioStartPointUp,1);
  101.             }
  102.             if(this.accelerating == false)
  103.             {
  104.                var acceleratorHit = true;
  105.             }
  106.             else
  107.             {
  108.                var acceleratorHit = false;
  109.             }
  110.             this.accelerating = true;
  111.          }
  112.          else
  113.          {
  114.             if(this.accelerating == true)
  115.             {
  116.                var acceleratorRelease = true;
  117.             }
  118.             else
  119.             {
  120.                var acceleratorRelease = false;
  121.             }
  122.             this.accelerating = false;
  123.          }
  124.          if(Key.isDown(40))
  125.          {
  126.             this.braking = true;
  127.          }
  128.          else
  129.          {
  130.             this.braking = false;
  131.          }
  132.       }
  133.       else
  134.       {
  135.          if(_root.keyPlayback_array[_root.gameTicks] & 1)
  136.          {
  137.             this.turningLeft = true;
  138.          }
  139.          if(_root.keyPlayback_array[_root.gameTicks] & 2)
  140.          {
  141.             this.turningRight = true;
  142.          }
  143.          if(_root.keyPlayback_array[_root.gameTicks] & 4)
  144.          {
  145.             if(this.accelerating == false)
  146.             {
  147.                var acceleratorHit = true;
  148.             }
  149.             else
  150.             {
  151.                var acceleratorHit = false;
  152.             }
  153.             this.accelerating = true;
  154.          }
  155.          else
  156.          {
  157.             if(this.accelerating == true)
  158.             {
  159.                var acceleratorRelease = true;
  160.             }
  161.             else
  162.             {
  163.                var acceleratorRelease = false;
  164.             }
  165.             this.accelerating = false;
  166.          }
  167.          if(_root.keyPlayback_array[_root.gameTicks] & 8)
  168.          {
  169.             this.braking = true;
  170.          }
  171.          else
  172.          {
  173.             this.braking = false;
  174.          }
  175.       }
  176.       if(acceleratorHit and !_root.raceStarted and !_root.garage)
  177.       {
  178.          trace("STARTED AT " + _root.gameTicks);
  179.          _root.startBonus = (10 - _root.gameTicks) * 1000;
  180.          trace("garage is " + _root.garage);
  181.          if(_root.startBonus <= 0 or _root.garage or _root.donutChallenge)
  182.          {
  183.             _root.startBonus = "";
  184.          }
  185.          else
  186.          {
  187.             _root.bonusPoints(_root.startBonus);
  188.          }
  189.          _root.raceStarted = true;
  190.       }
  191.       if(_root.gameTicks > 60)
  192.       {
  193.          _root.startBonus = "";
  194.       }
  195.       var audioStartPointUp = this.currentSpeed / this.MAX_SPEED * 3;
  196.       var audioStartPointDown = 3 - audioStartPointUp;
  197.       if(acceleratorHit)
  198.       {
  199.          trace("trigger");
  200.          _root.engineDown_sound.stop();
  201.          _root.idle_sound.stop();
  202.          _root.engineUp_sound.stop();
  203.          _root.engineUp_sound.start(audioStartPointUp,1);
  204.       }
  205.       if(acceleratorRelease)
  206.       {
  207.          _root.engineUp_sound.stop();
  208.          _root.topSpeed_sound.stop();
  209.          _root.engineDown_sound.stop();
  210.          _root.engineDown_sound.start(audioStartPointDown,1);
  211.       }
  212.       if(this.loseIt)
  213.       {
  214.          _root.engineUp_sound.stop();
  215.          _root.topSpeed_sound.stop();
  216.          _root.engineDown_sound.stop();
  217.          _root.engineDown_sound.start(audioStartPointDown,1);
  218.          this.loseIt = false;
  219.       }
  220.       if(this.onGrass)
  221.       {
  222.          this.wipeOutThreshold = this.GRASS_WIPEOUT_THRESHOLD;
  223.       }
  224.       else
  225.       {
  226.          this.wipeOutThreshold = this.ROAD_WIPEOUT_THRESHOLD;
  227.       }
  228.       switch(this.tyres)
  229.       {
  230.          case 1:
  231.             break;
  232.          case 2:
  233.             this.wipeOutThreshold += 10;
  234.             break;
  235.          case 3:
  236.             this.wipeOutThreshold += 5;
  237.       }
  238.       if(this.onBump)
  239.       {
  240.          this.currentSpeed *= Math.random();
  241.          this.wipeOutThreshold = this.BUMP_WIPEOUT_THRESHOLD;
  242.       }
  243.       if(this.onOil)
  244.       {
  245.          this.lostIt = true;
  246.       }
  247.       if(!this.lostIt)
  248.       {
  249.          if(this.accelerating)
  250.          {
  251.             if(this.currentSpeed < this.MAX_SPEED)
  252.             {
  253.                this.currentSpeed += this.ACCELERATION;
  254.             }
  255.          }
  256.          else if(this.braking)
  257.          {
  258.             if(this.currentSpeed > 0)
  259.             {
  260.                this.currentSpeed -= this.BRAKE_POWER;
  261.                if(this.currentSpeed < 0)
  262.                {
  263.                }
  264.             }
  265.             else
  266.             {
  267.                this.reversing = true;
  268.                this.currentSpeed -= this.REVERSE_ACCELERATION;
  269.             }
  270.          }
  271.          else if(this.currentSpeed > 0 and !this.lostIt)
  272.          {
  273.             this.reversing = false;
  274.             this.currentSpeed -= this.FRICTION;
  275.             if(this.currentSpeed < 0)
  276.             {
  277.                this.currentSpeed = 0;
  278.             }
  279.          }
  280.       }
  281.       if(this.lostIt)
  282.       {
  283.          this.currentSpeed -= this.WIPE_OUT_FRICTION;
  284.       }
  285.       if(this.lostIt)
  286.       {
  287.          if(this.steeringPosition > 0)
  288.          {
  289.             this.turningRight = true;
  290.          }
  291.          else
  292.          {
  293.             this.turningLeft = true;
  294.          }
  295.       }
  296.       if(this.turningLeft)
  297.       {
  298.          if(this.steeringPosition > - this.STEERING_MAX)
  299.          {
  300.             this.steeringPosition -= this.STEERING_RATE_OF_CHANGE;
  301.          }
  302.       }
  303.       else if(this.turningRight)
  304.       {
  305.          if(this.steeringPosition < this.STEERING_MAX)
  306.          {
  307.             this.steeringPosition += this.STEERING_RATE_OF_CHANGE;
  308.          }
  309.       }
  310.       else
  311.       {
  312.          if(this.steeringPosition > 0)
  313.          {
  314.             this.steeringPosition -= this.STEERING_RATE_OF_CHANGE;
  315.          }
  316.          else if(this.steeringPosition < 0)
  317.          {
  318.             this.steeringPosition += this.STEERING_RATE_OF_CHANGE;
  319.          }
  320.          if(Math.abs(this.steeringPosition) < this.STEERING_RATE_OF_CHANGE)
  321.          {
  322.             this.steeringPosition = 0;
  323.          }
  324.       }
  325.       this.heading += this.currentSpeed / this.MAX_SPEED * this.steeringPosition;
  326.       this.heading = Math.round(this.heading);
  327.       this.steeringResponse = this.DRIFT - this.currentSpeed / this.MAX_SPEED * 2 + this.directionLatency / 90 * 4;
  328.       if(!this.lostIt)
  329.       {
  330.          if(this.direction < this.heading)
  331.          {
  332.             this.direction += this.steeringResponse;
  333.             this.directionLatency = this.heading - this.direction;
  334.          }
  335.          else if(this.direction > this.heading)
  336.          {
  337.             this.direction -= this.steeringResponse;
  338.             this.directionLatency = this.direction - this.heading;
  339.          }
  340.       }
  341.       if(this.currentSpeed < 0)
  342.       {
  343.          if(this.currentSpeed < - this.MAX_SPEED_REVERSE)
  344.          {
  345.             this.currentSpeed = - this.MAX_SPEED_REVERSE;
  346.          }
  347.          if(this.currentSpeed < - this.FRICTION and !this.reversing)
  348.          {
  349.             this.currentSpeed = 0;
  350.          }
  351.          else
  352.          {
  353.             this.currentSpeed += this.FRICTION;
  354.          }
  355.       }
  356.       if(this.currentSpeed <= 0)
  357.       {
  358.          this.direction = this.heading;
  359.          this.directionLatency = 0;
  360.          this.lostIt = false;
  361.          this.accelerating = false;
  362.       }
  363.       if(this.directionLatency > this.SKIDMARK_THRESHOLD and !this.lostIt)
  364.       {
  365.          this.currentSkidmarkDepth = this.currentSkidmarkDepth + 1;
  366.          _root.newClipName = "tracks" + this.currentSkidmarkDepth;
  367.          if(_root.leaveSkidMarks)
  368.          {
  369.             if(this.onGrass)
  370.             {
  371.                _root.game_mc.tracks_grass.duplicateMovieClip(_root.newClipName,this.currentSkidmarkDepth);
  372.             }
  373.             else
  374.             {
  375.                _root.game_mc.tracks_road.duplicateMovieClip(_root.newClipName,this.currentSkidmarkDepth);
  376.             }
  377.             var skidAmount = this.directionLatency - this.SKIDMARK_THRESHOLD;
  378.             eval("_root.game_mc." + _root.newClipName)._x = this._x;
  379.             eval("_root.game_mc." + _root.newClipName)._y = this._y;
  380.             if(this.direction < this.heading)
  381.             {
  382.                eval("_root.game_mc." + _root.newClipName)._rotation = this.direction - this.SKIDMARK_ADJUSTMENT;
  383.             }
  384.             else
  385.             {
  386.                eval("_root.game_mc." + _root.newClipName)._rotation = this.direction + this.SKIDMARK_ADJUSTMENT;
  387.             }
  388.             eval("_root.game_mc." + _root.newClipName)._alpha = skidAmount;
  389.             _root.screech_sound.setVolume(skidAmount);
  390.          }
  391.       }
  392.       if(!this.lostIt)
  393.       {
  394.          var speedBleed = this.SPEED_BLEED_STRENGTH * (Math.abs(this.directionLatency) / this.SPEED_BLEED_MAX);
  395.          if(speedBleed > this.SPEED_BLEED_STRENGTH)
  396.          {
  397.             speedBleed = this.SPEED_BLEED_STRENGTH;
  398.          }
  399.          if(speedBleed < 0.1)
  400.          {
  401.             speedBleed = 0;
  402.          }
  403.          this.currentSpeed -= speedBleed;
  404.       }
  405.       var pivotShift = (this.wipeOutThreshold - Math.abs(this.directionLatency)) / this.wipeOutThreshold * 23;
  406.       _root.originalCar.car._y = -39 + pivotShift;
  407.       if(this.directionLatency > this.SKIDMARK_THRESHOLD and !this.lostIt)
  408.       {
  409.          if(!this.drifting)
  410.          {
  411.             _root.game_mc.video_mc.gotoAndPlay("car");
  412.             _root.currentDrift = 0;
  413.             _root.screech_sound.setVolume(0);
  414.             _root.screech_sound.start(0,999);
  415.             this.drifting = true;
  416.             if(this.noDriftTimer > this.MULTIPLIER_TIMEOUT)
  417.             {
  418.                this.driftTimer = 0;
  419.             }
  420.          }
  421.          this.driftTimer = this.driftTimer + 1;
  422.          this.noDriftTimer = 0;
  423.          _root.driftScoreMultiplier = Math.round(this.driftTimer / _root.DRIFT_SCORE_MULTIPLIER_INTERVAL);
  424.          if(_root.driftScoreMultiplier > 1)
  425.          {
  426.             _root.driftScoreMultiplier_str = _root.driftScoreMultiplier + "X";
  427.          }
  428.          else
  429.          {
  430.             _root.driftScoreMultiplier_str = "";
  431.          }
  432.          switch(this.tyres)
  433.          {
  434.             case 1:
  435.                var tyreBonus = 0;
  436.                break;
  437.             case 2:
  438.                var tyreBonus = 10;
  439.                break;
  440.             case 3:
  441.                var tyreBonus = 20;
  442.          }
  443.          _root.currentDrift += Math.round(_root.driftScoreMultiplier * (2 * (this.directionLatency - this.SKIDMARK_THRESHOLD + tyreBonus)));
  444.          if(_root.currentDrift > 0 and !_root.garage)
  445.          {
  446.             _root.currentDrift_str = _root.convertNumberDisplay(_root.currentDrift);
  447.          }
  448.          else
  449.          {
  450.             _root.currentDrift_str = "";
  451.          }
  452.       }
  453.       else
  454.       {
  455.          _root.screech_sound.stop();
  456.          this.noDriftTimer = this.noDriftTimer + 1;
  457.          if(this.drifting)
  458.          {
  459.             _root.game_mc.video_mc.gotoAndPlay("turn");
  460.             trace("drift was " + _root.currentDrift);
  461.             if(_root.donutChallenge)
  462.             {
  463.                _root.score = _root.currentDrift;
  464.             }
  465.             else
  466.             {
  467.                _root.score += _root.currentDrift;
  468.             }
  469.             _root.currentDrift = 0;
  470.             _root.currentDrift_str = "";
  471.             _root.driftScoreMultiplier_str = "";
  472.             if(_root.donutChallenge)
  473.             {
  474.                if(_root.bestDonut < _root.score)
  475.                {
  476.                   _root.bestDonut = _root.score;
  477.                   _root.so.data.bestDonut = _root.score;
  478.                }
  479.                if(_root.game_mc.car.currentSkidmarkDepth > 0)
  480.                {
  481.                   var i = 1;
  482.                   while(i <= _root.game_mc.car.currentSkidmarkDepth)
  483.                   {
  484.                      eval("_root.game_mc.tracks" + i).removeMovieClip();
  485.                      i++;
  486.                   }
  487.                }
  488.             }
  489.          }
  490.          this.drifting = false;
  491.          _root.screech_sound.setVolume(0);
  492.       }
  493.       _root.directionLatency = this.directionLatency;
  494.       if(Math.abs(this.directionLatency) > this.wipeOutThreshold and !this.lostIt)
  495.       {
  496.          this.loseIt = true;
  497.          this.lostIt = true;
  498.       }
  499.       if(this.heading > 360 and this.direction > 360)
  500.       {
  501.          this.heading -= 360;
  502.          this.direction -= 360;
  503.       }
  504.       if(this.heading < 0 and this.direction < 0)
  505.       {
  506.          this.heading += 360;
  507.          this.direction += 360;
  508.       }
  509.    }
  510.    function setCarStats(param_carNumber)
  511.    {
  512.       this.BRAKE_POWER = _root.carData_array[param_carNumber].BRAKE_POWER;
  513.       this.MAX_SPEED = _root.carData_array[param_carNumber].MAX_SPEED;
  514.       this.MAX_SPEED_REVERSE = _root.carData_array[param_carNumber].MAX_SPEED_REVERSE;
  515.       this.ACCELERATION = _root.carData_array[param_carNumber].ACCELERATION;
  516.       this.REVERSE_ACCELERATION = _root.carData_array[param_carNumber].REVERSE_ACCELERATION;
  517.       this.ROAD_WIPEOUT_THRESHOLD = _root.carData_array[param_carNumber].ROAD_WIPEOUT_THRESHOLD;
  518.       this.GRASS_WIPEOUT_THRESHOLD = _root.carData_array[param_carNumber].GRASS_WIPEOUT_THRESHOLD;
  519.       this.tyres = _root.carData_array[this.carNumber].TYRES;
  520.    }
  521. }
  522.