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

  1. class Game extends MovieClip
  2. {
  3.    var sectors;
  4.    var objects;
  5.    var backgrounds;
  6.    var effectsObjects;
  7.    var updateObjects;
  8.    var camera;
  9.    var display;
  10.    var outsideFogColor;
  11.    var deadPreview;
  12.    var fogColor;
  13.    var width;
  14.    var height;
  15.    var halfWidth;
  16.    var halfHeight;
  17.    var levelSpline;
  18.    var currentVisibleMarker;
  19.    var fogDistanceNear = 200;
  20.    var fogDistanceFar = 3000;
  21.    var fogAmount = 0;
  22.    var nearestSector = 0;
  23.    var objectCount = 0;
  24.    function Game()
  25.    {
  26.       super();
  27.       this.sectors = new Array();
  28.       this.objects = new Array();
  29.       this.backgrounds = new Array();
  30.       this.effectsObjects = new Array();
  31.       this.updateObjects = new Array();
  32.       this.camera = new GameCamera();
  33.       this.addObject(this.camera);
  34.       this.camera.update();
  35.       this.camera.moveTo(0,0,1);
  36.       this.display = this.createEmptyMovieClip("display",100);
  37.       this.setSize(this._width,this._height);
  38.       this._xscale = 100;
  39.       this._yscale = 100;
  40.       this.setFog(new RGB(62,153,255),200,4500,0);
  41.       this.outsideFogColor = new RGB(215,215,235);
  42.       this.deadPreview.swapDepths(255);
  43.       this.deadPreview.removeMovieClip();
  44.    }
  45.    function addBackground(bg)
  46.    {
  47.       bg.target = this.display.attachMovie(bg.linkageID,"BG" + this.backgrounds.length,bg.depth);
  48.       this.backgrounds.push(bg);
  49.       this.backgrounds.sortOn(bg.depth,Array.NUMERIC);
  50.    }
  51.    function setFog(color, near, far, amount)
  52.    {
  53.       this.fogColor = color;
  54.       this.fogDistanceNear = near;
  55.       this.fogDistanceFar = far;
  56.       this.fogAmount = Math.min(1,Math.max(Number(amount),0));
  57.    }
  58.    function setSize(width, height)
  59.    {
  60.       this.width = width;
  61.       this.height = height;
  62.       this.halfWidth = width * 0.5;
  63.       this.halfHeight = height * 0.5;
  64.       this.display._x = width * 0.5;
  65.       this.display._y = height * 0.5;
  66.    }
  67.    function registerForUpdates(object)
  68.    {
  69.       if(object.___GU)
  70.       {
  71.          return undefined;
  72.       }
  73.       object.___GU = true;
  74.       this.updateObjects.push(object);
  75.    }
  76.    function removeFromUpdates(object)
  77.    {
  78.       var _loc2_ = this.updateObjects.length - 1;
  79.       while(_loc2_ > -1)
  80.       {
  81.          if(this.updateObjects[_loc2_] == object)
  82.          {
  83.             this.updateObjects.splice(_loc2_,1);
  84.          }
  85.          _loc2_ = _loc2_ - 1;
  86.       }
  87.    }
  88.    function registerForEffects(object)
  89.    {
  90.       if(object.___EF)
  91.       {
  92.          return undefined;
  93.       }
  94.       object.___EF = true;
  95.       this.effectsObjects.push(object);
  96.    }
  97.    function addObject(object, x, y, z, marker)
  98.    {
  99.       this.objects.push(object);
  100.       object.uniqueID = this.objectCount++;
  101.       object.world = this;
  102.       object.moveTo(x,y,z);
  103.       if(!object.trackInSectorGrid)
  104.       {
  105.          return undefined;
  106.       }
  107.       if(!marker)
  108.       {
  109.          marker = this.findMarker(z);
  110.       }
  111.       this.sectors[marker].addObject(object);
  112.       object.sector = this.sectors[marker];
  113.       object.onAddToWorld();
  114.       if(object.inScene)
  115.       {
  116.          object.addToScene();
  117.       }
  118.    }
  119.    function removeObject(object)
  120.    {
  121.       var _loc2_ = this.objects.length;
  122.       while(_loc2_ > -1)
  123.       {
  124.          if(this.objects[_loc2_] == object)
  125.          {
  126.             this.objects.splice(_loc2_,1);
  127.          }
  128.          _loc2_ = _loc2_ - 1;
  129.       }
  130.       object.removeFromScene();
  131.       object.sector.removeObject(object);
  132.    }
  133.    function findMarker(z)
  134.    {
  135.       var _loc2_ = 0;
  136.       var _loc4_ = this.sectors.length - 1;
  137.       var _loc3_ = Math.round((_loc4_ - _loc2_) * 0.5);
  138.       var _loc6_ = 0;
  139.       if(z <= this.sectors[_loc2_].z)
  140.       {
  141.          return 0;
  142.       }
  143.       if(z >= this.sectors[_loc4_].z)
  144.       {
  145.          return _loc4_;
  146.       }
  147.       while(true)
  148.       {
  149.          if(z >= this.sectors[_loc2_].z && z < this.sectors[_loc3_].z)
  150.          {
  151.             _loc4_ = _loc3_;
  152.             _loc3_ = _loc2_ + Math.round((_loc4_ - _loc2_) * 0.5);
  153.          }
  154.          else
  155.          {
  156.             _loc2_ = _loc3_;
  157.             _loc3_ = _loc2_ + Math.round((_loc4_ - _loc2_) * 0.5);
  158.          }
  159.          if(_loc2_ == _loc3_ || _loc4_ == _loc3_)
  160.          {
  161.             return _loc4_;
  162.          }
  163.       }
  164.    }
  165.    function finalizeLevel()
  166.    {
  167.    }
  168.    function setLevelSpline(spline)
  169.    {
  170.       this.levelSpline = spline;
  171.    }
  172.    function update(elapsed)
  173.    {
  174.       var _loc2_ = this.updateObjects.length - 1;
  175.       while(_loc2_ > -1)
  176.       {
  177.          this.updateObjects[_loc2_].onUpdate(elapsed);
  178.          _loc2_ = _loc2_ - 1;
  179.       }
  180.    }
  181.    function processLevel(maxSpan, range, flip)
  182.    {
  183.       if(!range)
  184.       {
  185.          range = maxSpan * 0.2;
  186.       }
  187.       var _loc18_ = 5;
  188.       var _loc21_ = 0.01;
  189.       this.levelSpline.flipped = flip;
  190.       var _loc19_ = this.levelSpline.length;
  191.       var _loc7_ = undefined;
  192.       var _loc2_ = undefined;
  193.       var _loc3_ = undefined;
  194.       var _loc22_ = undefined;
  195.       var _loc23_ = undefined;
  196.       var _loc10_ = new Vector();
  197.       var _loc14_ = 0;
  198.       var _loc9_ = 0;
  199.       while(_loc9_ < _loc19_)
  200.       {
  201.          _loc3_ = this.levelSpline.getPoint(_loc9_);
  202.          if(_loc2_)
  203.          {
  204.             var _loc12_ = 0;
  205.             _loc10_.loc(_loc3_.x - _loc2_.x,_loc3_.y - _loc2_.y,_loc3_.z - _loc2_.z);
  206.             var _loc6_ = _loc10_.magnitude;
  207.             var _loc5_ = _loc21_;
  208.             while(_loc12_ < _loc18_)
  209.             {
  210.                if(Math.abs(_loc6_ - maxSpan) < range)
  211.                {
  212.                   break;
  213.                }
  214.                if(_loc14_ != Math.floor(_loc9_))
  215.                {
  216.                   _loc5_ = _loc9_ - (_loc9_ = Math.floor(_loc9_));
  217.                   _loc3_ = this.levelSpline.getPoint(_loc9_);
  218.                   break;
  219.                }
  220.                var _loc16_ = maxSpan / _loc6_;
  221.                _loc9_ -= _loc5_;
  222.                _loc9_ += _loc5_ = _loc5_ * maxSpan / _loc6_;
  223.                if(_loc14_ == Math.floor(_loc9_))
  224.                {
  225.                   _loc3_ = this.levelSpline.getPoint(_loc9_);
  226.                   _loc10_.loc(_loc3_.x - _loc2_.x,_loc3_.y - _loc2_.y,_loc3_.z - _loc2_.z);
  227.                   _loc6_ = _loc10_.magnitude;
  228.                   _loc12_ += 1;
  229.                }
  230.             }
  231.          }
  232.          _loc22_ = this.levelSpline.getRadius(_loc9_);
  233.          _loc7_ = _loc2_;
  234.          _loc2_ = this.levelSpline.getCourseSegment(this,_loc9_);
  235.          _loc2_.index = this.sectors.length;
  236.          _loc2_.color = this.levelSpline.getRGB(_loc9_);
  237.          var _loc15_ = this.levelSpline.getAssetName(_loc9_);
  238.          if(_loc15_ != null)
  239.          {
  240.             _loc2_.assetName = _loc15_;
  241.          }
  242.          if((_loc2_.useTint = this.levelSpline[Math.floor(_loc9_)].useTint) == null)
  243.          {
  244.             delete _loc2_.useTint;
  245.          }
  246.          _loc2_.worldIndex = _loc9_;
  247.          _loc2_.span = new Vector(_loc3_.x - _loc7_.x,_loc3_.y - _loc7_.y,_loc3_.z - _loc7_.z);
  248.          _loc2_.length = _loc2_.span.length;
  249.          _loc2_.direction = _loc2_.span.getNormalized();
  250.          _loc2_.arcClosed = this.levelSpline[Math.floor(_loc9_)].arcClosed;
  251.          _loc2_.arcClosedFace = !_loc7_.arcClosed && _loc2_.arcClosed;
  252.          _loc7_.next = _loc2_;
  253.          _loc2_.previous = _loc7_;
  254.          this.sectors.push(_loc2_);
  255.          _loc14_ = Math.floor(_loc9_);
  256.          _loc9_ += _loc21_;
  257.       }
  258.       _loc2_ = this.sectors[0];
  259.       _loc9_ = 0;
  260.       while(_loc9_ < _loc19_)
  261.       {
  262.          var _loc17_ = this.levelSpline[Math.floor(_loc9_)];
  263.          var _loc8_ = _loc17_.objects;
  264.          if(_loc8_.length != 0)
  265.          {
  266.             _loc8_.sortOnNumeric("index");
  267.             var _loc13_ = _loc8_.length;
  268.             while((_loc13_ = _loc13_ - 1) > -1)
  269.             {
  270.                var _loc4_ = _loc8_[_loc13_];
  271.                this.addObjectRelativeToSpline(_loc4_.object,_loc9_ + _loc4_.index,_loc4_.angle,_loc4_.elevation,_loc4_.useRotation,_loc2_);
  272.                _loc2_ = _loc4_.object.sector;
  273.             }
  274.          }
  275.          _loc9_ = _loc9_ + 1;
  276.       }
  277.       this.currentVisibleMarker = this.sectors[0];
  278.    }
  279.    function addObjectRelativeToSpline(object, index, angle, elevation, useRotation, sector)
  280.    {
  281.       var _loc3_ = this.levelSpline.getPoint(index);
  282.       var _loc8_ = this.levelSpline.getRadius(index);
  283.       if(!sector)
  284.       {
  285.          sector = this.sectors[0];
  286.       }
  287.       else
  288.       {
  289.          while(sector && _loc3_.z < sector.z)
  290.          {
  291.             sector = sector.previous;
  292.          }
  293.       }
  294.       while(sector && _loc3_.z > sector.z)
  295.       {
  296.          sector = sector.next;
  297.       }
  298.       var _loc6_ = Math.sin(Number(angle + 90) / 180 * 3.141592653589793) * sector.yScale;
  299.       var _loc5_ = Math.cos(Number(angle + 90) / 180 * 3.141592653589793) * sector.xScale;
  300.       if(this.levelSpline.flipped)
  301.       {
  302.          _loc5_ *= -1;
  303.          angle = Math.atan2(_loc6_,_loc5_) * 180 / 3.141592653589793 - 90;
  304.       }
  305.       if(useRotation)
  306.       {
  307.          object.setAssetRotation(angle);
  308.          object.up = new Point(- _loc5_,- _loc6_);
  309.       }
  310.       else
  311.       {
  312.          object.up = new Point(0,-1);
  313.       }
  314.       object.up.normalize();
  315.       object.right = object.up.cross();
  316.       this.addObject(object,_loc3_.x + _loc5_ * (_loc8_ - object.radius - elevation - object.generatorDisplacement),_loc3_.y + _loc6_ * (_loc8_ - object.radius - elevation - object.generatorDisplacement),_loc3_.z,sector.index);
  317.    }
  318.    function render()
  319.    {
  320.       while(this.currentVisibleMarker.previous && this.currentVisibleMarker.previous.z - this.camera.z > this.camera.hither)
  321.       {
  322.          this.currentVisibleMarker = this.currentVisibleMarker.previous;
  323.       }
  324.       if(this.currentVisibleMarker.z - this.camera.z < this.camera.hither)
  325.       {
  326.          this.currentVisibleMarker.removeFromScene();
  327.          while(this.currentVisibleMarker = this.currentVisibleMarker.next)
  328.          {
  329.             if(this.currentVisibleMarker.z - this.camera.z > this.camera.hither)
  330.             {
  331.                break;
  332.             }
  333.             this.currentVisibleMarker.removeFromScene();
  334.          }
  335.       }
  336.       var _loc5_ = undefined;
  337.       var _loc8_ = undefined;
  338.       var _loc7_ = undefined;
  339.       var _loc2_ = undefined;
  340.       var _loc4_ = this.currentVisibleMarker;
  341.       var _loc15_ = new RGB();
  342.       var _loc11_ = 16777215;
  343.       while(_loc4_)
  344.       {
  345.          _loc2_ = _loc4_.z - this.camera.z;
  346.          if(_loc2_ > this.camera.yon)
  347.          {
  348.             _loc4_.removeFromScene();
  349.             if(!_loc4_ = _loc4_.next.target)
  350.             {
  351.                break;
  352.             }
  353.          }
  354.          else
  355.          {
  356.             var _loc13_ = Math.min(1,Math.max(0,(_loc2_ - this.fogDistanceNear) / this.fogDistanceFar)) * this.fogAmount;
  357.             _loc8_ = _loc4_.x - this.camera.x;
  358.             _loc7_ = _loc4_.y - this.camera.y;
  359.             var _loc12_ = _loc5_;
  360.             _loc5_ = this.camera.depthFocus / _loc2_;
  361.             var _loc14_ = this.camera.depthFocus / (_loc4_.midPoint.z - this.camera.z);
  362.             _loc4_.setDisplay(_loc8_,_loc7_,_loc5_,_loc12_,_loc13_);
  363.             if(!_loc4_.arcClosed)
  364.             {
  365.                _loc11_ = Math.min(_loc11_,_loc2_);
  366.             }
  367.             if(_loc4_.hasObjects)
  368.             {
  369.                var _loc3_ = undefined;
  370.                var _loc9_ = _loc4_.objects;
  371.                var _loc10_ = _loc9_.length - 1;
  372.                while(_loc10_ > -1)
  373.                {
  374.                   _loc3_ = _loc9_[_loc10_];
  375.                   _loc2_ = _loc3_.z - this.camera.z;
  376.                   if(_loc3_.target._visible = _loc2_ > this.camera.hither && _loc2_ < _loc3_.cullDistance)
  377.                   {
  378.                      _loc8_ = _loc3_.x - this.camera.x;
  379.                      _loc7_ = _loc3_.y - this.camera.y;
  380.                      _loc5_ = this.camera.depthFocus / _loc2_;
  381.                      _loc3_.setDisplay(_loc8_,_loc7_,_loc2_,_loc5_);
  382.                   }
  383.                   _loc10_ = _loc10_ - 1;
  384.                }
  385.             }
  386.             _loc4_ = _loc4_.next;
  387.          }
  388.       }
  389.       _loc10_ = this.backgrounds.length - 1;
  390.       while(_loc10_ > -1)
  391.       {
  392.          var _loc6_ = this.backgrounds[_loc10_];
  393.          if(_loc6_.target._visible = _loc11_ < 3000)
  394.          {
  395.             _loc5_ = this.camera.depthFocus / _loc6_.depth;
  396.             _loc6_.target._x = Math.floor((- this.camera.x) * _loc5_);
  397.             _loc6_.target._y = Math.floor((- this.camera.y) * _loc5_);
  398.             _loc6_.target.swapDepths(65535 - Math.floor(_loc6_.depth + this.camera.z) * 100 + 2);
  399.          }
  400.          _loc10_ = _loc10_ - 1;
  401.       }
  402.       _loc10_ = this.effectsObjects.length - 1;
  403.       while(_loc10_ > -1)
  404.       {
  405.          this.effectsObjects[_loc10_].onEffects();
  406.          _loc10_ = _loc10_ - 1;
  407.       }
  408.       updateAfterEvent();
  409.    }
  410. }
  411.