home *** CD-ROM | disk | FTP | other *** search
/ Champak 48 / cdrom_image.iso / Games / bobsleddin.swf / scripts / __Packages / Sled.as < prev    next >
Encoding:
Text File  |  2007-09-28  |  11.4 KB  |  338 lines

  1. class Sled extends GenericObject
  2. {
  3.    var velocity;
  4.    var surfacePlane;
  5.    var up;
  6.    var shadow;
  7.    var target;
  8.    var world;
  9.    var vrSound;
  10.    var vrOpen;
  11.    var sledSound;
  12.    var onMessage;
  13.    var specialItem;
  14.    var oldX;
  15.    var oldY;
  16.    var oldZ;
  17.    var shadowX;
  18.    var shadowY;
  19.    var shadowLeftX;
  20.    var shadowLeftY;
  21.    var shadowRightX;
  22.    var shadowRightY;
  23.    var currentTurnFrame;
  24.    var linkageID = "Sled";
  25.    var shadowLinkageID = "SledShadow";
  26.    var turnMultiplier = 4;
  27.    var depthGroup = 2;
  28.    var collisionGroup = BB.ALL;
  29.    var radius = 25;
  30.    var isOnGround = false;
  31.    var maxDamage = 150;
  32.    var damage = 0;
  33.    var score = 0;
  34.    var letters = 0;
  35.    var lives = 0;
  36.    var raceTime = 0;
  37.    var timeRemaining = 0;
  38.    var elevated = false;
  39.    var inMotion = true;
  40.    var maxSpeed = 4000;
  41.    var messagesDisabled = false;
  42.    var keepTrackOfTime = true;
  43.    var currentSpeed = 0;
  44.    var targetFrame = 4;
  45.    var centerFrame = 4;
  46.    var turnDegreePerFrame = 15;
  47.    var maxDisplayTurn = 45;
  48.    function Sled(linkageID)
  49.    {
  50.       super();
  51.       this.velocity = new Vector(0,300,300);
  52.       this.surfacePlane = new Point(1,0);
  53.       this.up = new Point(0,1);
  54.       if(linkageID != null)
  55.       {
  56.          this.linkageID = linkageID;
  57.       }
  58.    }
  59.    function onAddToScene()
  60.    {
  61.       this.shadow = this.world.display.attachMovie(this.shadowLinkageID,"BS",this.target.getDepth() - 1);
  62.       this.vrSound = new Sound(this.target);
  63.       this.vrOpen = new Sound(this.target);
  64.       this.vrOpen.attachSound("VRDeploy");
  65.       this.sledSound = new Sound(this.target.createEmptyMovieClip("SledSound",10));
  66.       this.sledSound.attachSound("SledSound");
  67.       this.sledSound.start(0,16777215);
  68.       this.sledSound.setVolume(0);
  69.       this.world.registerForEffects(this);
  70.    }
  71.    function onRemoveFromScene()
  72.    {
  73.       this.shadow.swapDepths(0);
  74.       this.shadow.removeMovieClip();
  75.       stopAllSounds();
  76.       this.vrSound.stop();
  77.       this.vrOpen.stop();
  78.       this.sledSound.stop();
  79.    }
  80.    function repairDamage(value)
  81.    {
  82.       this.damage = Math.max(this.damage - value,0);
  83.    }
  84.    function takeDamage(ammount)
  85.    {
  86.       this.damage += ammount;
  87.       if(this.damage >= this.maxDamage)
  88.       {
  89.          this.onMessage("damage");
  90.       }
  91.    }
  92.    function collectLetter(value, letterNumber)
  93.    {
  94.       this.addPoints(value);
  95.       this.letters |= 1 << letterNumber;
  96.    }
  97.    function addPoints(value)
  98.    {
  99.       this.score += value;
  100.    }
  101.    function setSpecialItem(newSpecialItem)
  102.    {
  103.       if(this.specialItem)
  104.       {
  105.          this.vrSound.stop();
  106.          this.specialItem = null;
  107.       }
  108.       this.vrOpen.start();
  109.       this.specialItem = newSpecialItem;
  110.    }
  111.    function removeSpecialItem()
  112.    {
  113.       this.specialItem = null;
  114.       this.hideSpecialItems();
  115.    }
  116.    function hideSpecialItems()
  117.    {
  118.       var _loc2_ = this.target.sled;
  119.       _loc2_.magnetLeft._visible = false;
  120.       _loc2_.magnetRight._visible = false;
  121.       _loc2_.lasersLeft._visible = false;
  122.       _loc2_.lasersRight._visible = false;
  123.    }
  124.    function onCollision(object)
  125.    {
  126.       var _loc3_ = object.collisionGroup;
  127.       if(_loc3_ & BB.NODE)
  128.       {
  129.          object.onCross(this);
  130.       }
  131.       else if(_loc3_ & BB.COLLECTABLE)
  132.       {
  133.          object.setCollected(this);
  134.       }
  135.       else
  136.       {
  137.          object.onCollision(this);
  138.       }
  139.    }
  140.    function checkCollisions(from, to)
  141.    {
  142.       var _loc4_ = [];
  143.       var _loc2_ = undefined;
  144.       while(from && from != to.next)
  145.       {
  146.          _loc4_ = _loc4_.concat(from.objects);
  147.          from = from.next;
  148.       }
  149.       var _loc11_ = this.x - this.oldX;
  150.       var _loc10_ = this.y - this.oldY;
  151.       var _loc9_ = this.z - this.oldZ;
  152.       var _loc8_ = undefined;
  153.       var _loc3_ = _loc4_.length - 1;
  154.       while(_loc3_ > -1)
  155.       {
  156.          _loc2_ = _loc4_[_loc3_];
  157.          if(!(_loc2_ == this || !_loc2_.collisionEnabled))
  158.          {
  159.             if(this.collisionGroup & _loc2_.collisionGroup && _loc2_.z >= this.oldZ && _loc2_.z <= this.z)
  160.             {
  161.                _loc8_ = (_loc2_.z - this.oldZ) / _loc9_;
  162.                var _loc7_ = _loc2_.x - (this.oldX + _loc11_ * _loc8_);
  163.                var _loc6_ = _loc2_.y - (this.oldY + _loc10_ * _loc8_);
  164.                if(Math.sqrt(_loc7_ * _loc7_ + _loc6_ * _loc6_) < _loc4_[_loc3_].radius + this.radius)
  165.                {
  166.                   this.onCollision(_loc4_[_loc3_]);
  167.                }
  168.             }
  169.          }
  170.          _loc3_ = _loc3_ - 1;
  171.       }
  172.    }
  173.    function updateTime(elapsed)
  174.    {
  175.       if(!this.keepTrackOfTime)
  176.       {
  177.          return undefined;
  178.       }
  179.       this.raceTime += elapsed;
  180.       this.timeRemaining -= elapsed;
  181.       if(this.timeRemaining < 0)
  182.       {
  183.          this.onMessage("timeout");
  184.       }
  185.    }
  186.    function update(elapsed, xShift, yShift, zShift)
  187.    {
  188.       this.updatePosition(elapsed,xShift,yShift,zShift);
  189.       this.updateTime(elapsed);
  190.       if(this.specialItem)
  191.       {
  192.          this.specialItem.update(this,elapsed);
  193.       }
  194.       this.sledSound.setVolume(Math.min(this.currentSpeed / 4000 * 100,35));
  195.    }
  196.    function finishRace()
  197.    {
  198.       this.keepTrackOfTime = false;
  199.       this.inMotion = false;
  200.       this.onMessage("finish");
  201.    }
  202.    function setElevated(elevated)
  203.    {
  204.       this.elevated = elevated;
  205.       if(elevated)
  206.       {
  207.          this.radius *= 2;
  208.          this.target.sled.blades._y += 25;
  209.       }
  210.       else
  211.       {
  212.          this.target.sled.blades._y -= 25;
  213.          delete this.radius;
  214.       }
  215.    }
  216.    function updatePosition(elapsed, xShift, yShift, zShift)
  217.    {
  218.       var _loc23_ = elapsed;
  219.       var _loc18_ = this.sector;
  220.       if(this.inMotion)
  221.       {
  222.          this.currentSpeed += ((0.5 * zShift + 1) * 650 + 10 * this.sector.direction.y - this.currentSpeed) * elapsed;
  223.       }
  224.       else
  225.       {
  226.          this.currentSpeed += (- this.currentSpeed) * elapsed;
  227.       }
  228.       this.currentSpeed = Math.min(this.maxSpeed,this.currentSpeed);
  229.       this.velocity.z += (this.currentSpeed * this.sector.direction.z - this.velocity.z) * elapsed;
  230.       this.oldZ = this.z;
  231.       this.oldY = this.y;
  232.       this.oldX = this.x;
  233.       this.z += this.velocity.z * elapsed;
  234.       var _loc22_ = this.z - this.oldZ;
  235.       if(this.isOnGround)
  236.       {
  237.          this.velocity.x += (this.currentSpeed * this.sector.direction.x + this.surfacePlane.x * xShift * this.velocity.z * 8 - this.velocity.x) * elapsed;
  238.          this.velocity.y += (this.currentSpeed * this.sector.direction.y + this.surfacePlane.y * xShift * this.velocity.z * 8 + 800 * this.sector.yScale - this.velocity.y) * elapsed;
  239.       }
  240.       else
  241.       {
  242.          this.velocity.y += 2400 * elapsed;
  243.       }
  244.       var _loc10_ = this.x + this.velocity.x * elapsed;
  245.       var _loc11_ = this.y + this.velocity.y * elapsed;
  246.       _loc18_ = this.sector;
  247.       if(this.sector)
  248.       {
  249.          this.checkSectorLocation();
  250.       }
  251.       this.checkCollisions(_loc18_,this.sector);
  252.       var _loc7_ = (this.z - this.sector.previous.z) / (this.sector.z - this.sector.previous.z);
  253.       var _loc6_ = this.sector.previous.radius + (this.sector.radius - this.sector.previous.radius) * _loc7_ - this.radius;
  254.       var _loc8_ = this.sector.previous.xScale + (this.sector.xScale - this.sector.previous.xScale) * _loc7_;
  255.       var _loc12_ = this.sector.previous.yScale + (this.sector.yScale - this.sector.previous.yScale) * _loc7_;
  256.       var _loc3_ = this.sector.previous.x + (this.sector.x - this.sector.previous.x) * _loc7_;
  257.       var _loc5_ = this.sector.previous.y + (this.sector.y - this.sector.previous.y) * _loc7_;
  258.       var _loc16_ = new Point((_loc10_ - _loc3_) / _loc8_,(_loc11_ - _loc5_) / _loc12_);
  259.       var _loc19_ = _loc16_.magnitude;
  260.       this.up = _loc16_.getNormalized();
  261.       this.surfacePlane = this.up.cross();
  262.       if((!this.sector.arcClosed || !this.sector.previous.arcClosed) && _loc11_ < _loc5_)
  263.       {
  264.          if(!this.isOnGround)
  265.          {
  266.          }
  267.          this.velocity.x = this.sector.direction.x * this.velocity.magnitude;
  268.          _loc10_ = Math.min(Math.max(_loc10_,_loc3_ - _loc6_ * _loc8_),_loc3_ + _loc6_ * _loc8_);
  269.          this.isOnGround = false;
  270.       }
  271.       else if(this.isOnGround = _loc19_ > _loc6_)
  272.       {
  273.          _loc10_ = _loc3_ + this.up.x * _loc6_ * _loc8_;
  274.          _loc11_ = _loc5_ + this.up.y * _loc6_ * _loc12_;
  275.          this.target._rotation = Math.atan2(this.surfacePlane.y / _loc8_,this.surfacePlane.x / _loc12_) * 180 / 3.14;
  276.       }
  277.       this.x = _loc10_;
  278.       this.y = _loc11_;
  279.       this.updateDepth();
  280.       var _loc13_ = Math.pow(_loc6_ + this.radius,2);
  281.       var _loc9_ = this.x - _loc3_;
  282.       var _loc17_ = Math.sqrt(_loc13_ - _loc9_ * _loc9_);
  283.       var _loc15_ = _loc9_ - this.radius;
  284.       var _loc21_ = Math.sqrt(_loc13_ - _loc15_ * _loc15_);
  285.       var _loc14_ = _loc9_ + this.radius;
  286.       var _loc20_ = Math.sqrt(_loc13_ - _loc14_ * _loc14_);
  287.       var _loc4_ = this.sector.yScale;
  288.       this.shadowX = _loc3_ + _loc9_;
  289.       this.shadowY = _loc5_ + _loc17_ * _loc4_ + (1 - _loc4_) * this.radius;
  290.       this.shadowLeftX = _loc3_ + _loc15_;
  291.       this.shadowLeftY = _loc5_ + _loc21_ * _loc4_ + (1 - _loc4_) * this.radius;
  292.       this.shadowRightX = _loc3_ + _loc14_;
  293.       this.shadowRightY = _loc5_ + _loc20_ * _loc4_ + (1 - _loc4_) * this.radius;
  294.       return undefined;
  295.    }
  296.    function onDisplay(offsetX, offsetY, offsetZ, depthRatio)
  297.    {
  298.       this.shadow.swapDepths(this.target.getDepth() - 1);
  299.       this.shadow.clear();
  300.       this.shadow.beginFill(this.world.fogColor.valueOf());
  301.       var _loc3_ = this.world.camera.depthFocus / (offsetZ + 10);
  302.       var _loc6_ = this.world.camera.depthFocus / (offsetZ + 5);
  303.       var _loc5_ = this.world.camera.x;
  304.       var _loc4_ = this.world.camera.y;
  305.       var _loc10_ = this.shadowX - _loc5_;
  306.       var _loc8_ = this.shadowY - _loc4_;
  307.       var _loc9_ = this.shadowLeftX - _loc5_;
  308.       var _loc7_ = this.shadowLeftY - _loc4_;
  309.       var _loc12_ = this.shadowRightX - _loc5_;
  310.       var _loc11_ = this.shadowRightY - _loc4_;
  311.       var _loc17_ = (this.shadowX - _loc5_) * depthRatio;
  312.       var _loc16_ = (this.shadowY - _loc4_) * depthRatio;
  313.       this.shadow.moveTo(_loc10_ * depthRatio,_loc8_ * depthRatio);
  314.       this.shadow.curveTo(_loc9_ * depthRatio,_loc7_ * depthRatio,_loc9_ * _loc6_,_loc7_ * _loc6_);
  315.       this.shadow.curveTo(_loc9_ * _loc3_,_loc7_ * _loc3_,_loc10_ * _loc3_,_loc8_ * _loc3_);
  316.       this.shadow.curveTo(_loc12_ * _loc3_,_loc11_ * _loc3_,_loc12_ * _loc6_,_loc11_ * _loc6_);
  317.       this.shadow.curveTo(_loc12_ * depthRatio,_loc11_ * depthRatio,_loc10_ * depthRatio,_loc8_ * depthRatio);
  318.       var _loc13_ = this.velocity.x * this.surfacePlane.x + this.velocity.y * this.surfacePlane.y;
  319.       var _loc15_ = Math.max(- this.maxDisplayTurn,Math.min(this.maxDisplayTurn,Math.atan2(this.velocity.z,_loc13_ * 0.4) * 180 / 3.141592653589793 - 90));
  320.       if(this.currentTurnFrame == (this.currentTurnFrame = this.centerFrame - Math.round(_loc15_ / this.turnDegreePerFrame)))
  321.       {
  322.          return undefined;
  323.       }
  324.       this.target.sled.gotoAndStop(this.currentTurnFrame);
  325.       this.target.sled.blades.gotoAndStop(this.currentTurnFrame);
  326.       this.hideSpecialItems();
  327.       this.specialItem.applyDisplay(this,offsetX,offsetY,offsetZ,depthRatio);
  328.    }
  329.    function onEffects()
  330.    {
  331.       this.specialItem.renderEffects(this);
  332.    }
  333.    function toString()
  334.    {
  335.       return "Sled";
  336.    }
  337. }
  338.