home *** CD-ROM | disk | FTP | other *** search
/ 600 Games / 600games.iso / Acao / germ_roundup.swf / scripts / __Packages / BuildingHandler.as < prev    next >
Encoding:
Text File  |  2007-03-20  |  9.3 KB  |  281 lines

  1. class BuildingHandler
  2. {
  3.    var oHandler;
  4.    var tileBottomPlacement;
  5.    var oGerms;
  6.    var tileMidPlacement;
  7.    var tileTopPlacement;
  8.    var aBuild;
  9.    static var tileWidth;
  10.    static var home;
  11.    static var tileWidthBot;
  12.    static var leftEdge;
  13.    static var topEdge;
  14.    static var tileHeightBot;
  15.    static var tileHeightMid;
  16.    static var tileHeightTop;
  17.    static var floor;
  18.    static var ceiling;
  19.    static var homeLimit;
  20.    static var tileWidthMid;
  21.    static var tileWidthTop;
  22.    var bAdd = false;
  23.    var nGermChance = 5;
  24.    var powerChance = 10;
  25.    var nTypes = 7;
  26.    var nTotalGerms = 0;
  27.    function BuildingHandler(loc)
  28.    {
  29.       var _loc1_ = this;
  30.       _loc1_.location = loc;
  31.       _loc1_.oHandler = new smashing.ClipHandler(_loc1_.location);
  32.       _loc1_.aPower = ["BonusInvun","BonusTime","BonusSpeed"];
  33.    }
  34.    function update(nElapsed)
  35.    {
  36.       var _loc2_ = this.oHandler.clips;
  37.       var _loc3_ = _loc2_.length;
  38.       var _loc1_ = 0;
  39.       while(_loc1_ < _loc3_)
  40.       {
  41.          _loc2_[_loc1_].render();
  42.          _loc1_ = _loc1_ + 1;
  43.       }
  44.       if(this.bAdd == true)
  45.       {
  46.          this.addBuilding();
  47.       }
  48.    }
  49.    function render()
  50.    {
  51.       var _loc3_ = this;
  52.       var pos = smashing.Viewport.x + smashing.Viewport.halfWidth;
  53.       var backPos = pos - smashing.Viewport.width;
  54.       var index = Math.floor(pos / BuildingHandler.tileWidth) + 1;
  55.       var backIndex = Math.floor(backPos / BuildingHandler.tileWidth);
  56.       if(index > _loc3_.aBuild.length)
  57.       {
  58.          _loc3_.addBuilding(_loc3_.oWorld.level);
  59.       }
  60.       var _loc2_ = undefined;
  61.       var _loc1_ = backIndex;
  62.       while(_loc1_ < index)
  63.       {
  64.          _loc2_ = _loc3_.aBuild[_loc1_];
  65.          if(_loc2_.bOnStage == false)
  66.          {
  67.             _loc3_.buildOnStage(_loc2_);
  68.          }
  69.          _loc1_ = _loc1_ + 1;
  70.       }
  71.       var clips = _loc3_.oHandler.clips;
  72.       var l = clips.length;
  73.       _loc1_ = 0;
  74.       while(_loc1_ < l)
  75.       {
  76.          clips[_loc1_].render();
  77.          _loc1_ = _loc1_ + 1;
  78.       }
  79.    }
  80.    function reset()
  81.    {
  82.       var _loc1_ = this;
  83.       _loc1_.oHandler.removeAllClips();
  84.       BuildingHandler.home = null;
  85.       _loc1_.aBuild = [];
  86.       _loc1_.init();
  87.    }
  88.    function init()
  89.    {
  90.       var _loc1_ = this;
  91.       BuildingHandler.tileWidth = BuildingHandler.tileWidthBot;
  92.       BuildingHandler.leftEdge = - (smashing.Viewport.halfWidth + BuildingHandler.tileWidth);
  93.       BuildingHandler.topEdge = - smashing.Viewport.halfHeight;
  94.       _loc1_.tileBottomPlacement = smashing.Viewport.height - BuildingHandler.tileHeightBot;
  95.       _loc1_.tileMidPlacement = _loc1_.tileBottomPlacement - BuildingHandler.tileHeightMid;
  96.       _loc1_.tileTopPlacement = _loc1_.tileMidPlacement - BuildingHandler.tileHeightTop;
  97.       BuildingHandler.floor = _loc1_.tileBottomPlacement + BuildingHandler.tileHeightBot - 15;
  98.       BuildingHandler.ceiling = smashing.Viewport.height - BuildingHandler.tileHeightBot - BuildingHandler.tileHeightMid - BuildingHandler.tileHeightTop;
  99.       _loc1_.addBuilding(_loc1_.oWorld.level,false);
  100.       _loc1_.addBuilding(_loc1_.oWorld.level,false);
  101.       _loc1_.addBuilding(_loc1_.oWorld.level);
  102.       _loc1_.addBuilding(_loc1_.oWorld.level);
  103.       _loc1_.render();
  104.       _loc1_.nArrayLimit = Math.ceil(_loc1_.oWorld.nDist / BuildingHandler.tileWidth);
  105.    }
  106.    function removeBuilding(o, remove)
  107.    {
  108.       var _loc1_ = o;
  109.       if(_loc1_.mcGerm != null)
  110.       {
  111.          _loc1_.mcGerm.handler.removeClip(_loc1_.mcGerm);
  112.       }
  113.       _loc1_.oData.bOnStage = false;
  114.       this.oHandler.removeClip(_loc1_);
  115.    }
  116.    function addBuilding(nLevel, bObst)
  117.    {
  118.       var _loc2_ = this;
  119.       var _loc3_ = bObst;
  120.       if(BuildingHandler.home == null)
  121.       {
  122.          var _loc1_ = new BData();
  123.          _loc1_.nType = Math.ceil(Math.random() * _loc2_.nTypes);
  124.          _loc1_.nLevel = (nLevel - 1) % 3;
  125.          if(_loc3_ == undefined)
  126.          {
  127.             _loc3_ = smashing.Misc.rndBool();
  128.             if(_loc3_ == false)
  129.             {
  130.                _loc1_.bObstBot = false;
  131.                _loc1_.sPowerupBot = _loc2_.shouldAddPowerup();
  132.             }
  133.             else
  134.             {
  135.                _loc1_.bObstBot = true;
  136.                _loc2_.nTotalGerms += Number(_loc1_.bGermBot = _loc2_.shouldAddGerm());
  137.             }
  138.          }
  139.          else
  140.          {
  141.             _loc1_.sPowerupBot = null;
  142.             _loc1_.bObstBot = false;
  143.          }
  144.          if(smashing.Misc.rndBool())
  145.          {
  146.             _loc1_.bObstMid = true;
  147.             _loc2_.nTotalGerms += Number(_loc1_.bGermMid = _loc2_.shouldAddGerm());
  148.          }
  149.          else
  150.          {
  151.             _loc1_.bObstMid = false;
  152.             _loc1_.sPowerupMid = _loc2_.shouldAddPowerup();
  153.          }
  154.          if(smashing.Misc.rndBool())
  155.          {
  156.             _loc1_.bObstTop = true;
  157.             _loc2_.nTotalGerms += Number(_loc1_.bGermTop = _loc2_.shouldAddGerm());
  158.          }
  159.          else
  160.          {
  161.             _loc1_.bObstTop = false;
  162.             _loc1_.sPowerupTop = _loc2_.shouldAddPowerup();
  163.          }
  164.          _loc1_.x = _loc2_.aBuild.length * BuildingHandler.tileWidth;
  165.          if(_loc2_.nArrayLimit - _loc2_.aBuild.length < 10 - _loc2_.nTotalGerms)
  166.          {
  167.             switch(Math.floor(Math.random() * 3))
  168.             {
  169.                case 0:
  170.                   _loc1_.bGermBot = true;
  171.                   break;
  172.                case 1:
  173.                   _loc1_.bGermMid = true;
  174.                   break;
  175.                case 2:
  176.                   _loc1_.bGermTop = true;
  177.             }
  178.          }
  179.          _loc2_.aBuild.push(_loc1_);
  180.       }
  181.    }
  182.    function buildOnStage(oBuild)
  183.    {
  184.       var _loc1_ = oBuild;
  185.       _loc1_.bOnStage = true;
  186.       if(_loc1_.bHome == false)
  187.       {
  188.          var _loc3_ = undefined;
  189.          var _loc2_ = undefined;
  190.          var build = this.oHandler.addClip("mcBuildingBottom" + _loc1_.nLevel);
  191.          build.x = _loc1_.x;
  192.          build.y = this.tileBottomPlacement;
  193.          build.init(this,_loc1_.bObstBot,_loc1_.nType,_loc1_,0);
  194.          if(_loc1_.sPowerupBot != null)
  195.          {
  196.             _loc3_ = this.oGerms.addGerm(build.x + build.mcBonusLoc._x,build.y + build.mcBonusLoc._y,_loc1_.sPowerupBot);
  197.             _loc3_.oData = _loc1_;
  198.             _loc3_.nSegment = 0;
  199.             _loc3_.mcBuild = build;
  200.             build.mcGerm = _loc3_;
  201.          }
  202.          else if(_loc1_.bGermBot == true)
  203.          {
  204.             _loc2_ = this.oGerms.addGerm(build.x + build.gfx.mcGermPlacer._x,build.y + build.gfx.mcGermPlacer._y,"mcGerm");
  205.             _loc2_.oData = _loc1_;
  206.             _loc2_.nSegment = 0;
  207.             _loc2_.mcBuild = build;
  208.             build.mcGerm = _loc2_;
  209.          }
  210.          build = this.oHandler.addClip("mcBuildingMiddle" + _loc1_.nLevel);
  211.          build.x = _loc1_.x;
  212.          build.y = this.tileMidPlacement;
  213.          build.init(this,_loc1_.bObstMid,_loc1_.nType,_loc1_,0);
  214.          if(_loc1_.sPowerupMid != null)
  215.          {
  216.             _loc3_ = this.oGerms.addGerm(build.x + build.mcBonusLoc._x,build.y + build.mcBonusLoc._y,_loc1_.sPowerupMid);
  217.             _loc3_.oData = _loc1_;
  218.             _loc3_.nSegment = 1;
  219.             _loc3_.mcBuild = build;
  220.             build.mcGerm = _loc3_;
  221.          }
  222.          else if(_loc1_.bGermMid == true)
  223.          {
  224.             _loc2_ = this.oGerms.addGerm(build.x + build.gfx.mcGermPlacer._x,build.y + build.gfx.mcGermPlacer._y,"mcGerm");
  225.             _loc2_.oData = _loc1_;
  226.             _loc2_.nSegment = 1;
  227.             _loc2_.mcBuild = build;
  228.             build.mcGerm = _loc2_;
  229.          }
  230.          build = this.oHandler.addClip("mcBuildingTop" + _loc1_.nLevel);
  231.          build.x = _loc1_.x;
  232.          build.y = this.tileTopPlacement;
  233.          build.init(this,_loc1_.bObstTop,_loc1_.nType,_loc1_,0);
  234.          if(_loc1_.sPowerupTop != null)
  235.          {
  236.             _loc3_ = this.oGerms.addGerm(build.x + build.mcBonusLoc._x,build.y + build.mcBonusLoc._y,_loc1_.sPowerupTop);
  237.             _loc3_.oData = _loc1_;
  238.             _loc3_.nSegment = 2;
  239.             _loc3_.mcBuild = build;
  240.             build.mcGerm = _loc3_;
  241.          }
  242.          else if(_loc1_.bGermTop == true)
  243.          {
  244.             _loc2_ = this.oGerms.addGerm(build.x + build.gfx.mcGermPlacer._x,build.y + build.gfx.mcGermPlacer._y,"mcGerm");
  245.             _loc2_.oData = _loc1_;
  246.             _loc2_.nSegment = 2;
  247.             _loc2_.mcBuild = build;
  248.             build.mcGerm = _loc2_;
  249.          }
  250.       }
  251.       else
  252.       {
  253.          var build = this.oHandler.addClip("home");
  254.          build.x = _loc1_.x;
  255.          build.y = BuildingHandler.floor - build._height - 50;
  256.          BuildingHandler.homeLimit = build.x + BuildingHandler.tileWidth - smashing.Viewport.halfWidth;
  257.          BuildingHandler.home = build;
  258.       }
  259.    }
  260.    function addHome()
  261.    {
  262.       var _loc1_ = new BData();
  263.       _loc1_.bHome = true;
  264.       _loc1_.x = this.aBuild.length * BuildingHandler.tileWidth;
  265.       this.aBuild.push(_loc1_);
  266.    }
  267.    function shouldAddGerm()
  268.    {
  269.       return 0 == Math.floor(Math.random() * this.nGermChance);
  270.    }
  271.    function shouldAddPowerup()
  272.    {
  273.       var _loc1_ = this;
  274.       if(0 == Math.floor(Math.random() * _loc1_.powerChance))
  275.       {
  276.          return _loc1_.aPower[Math.floor(Math.random() * _loc1_.aPower.length)];
  277.       }
  278.       return null;
  279.    }
  280. }
  281.