home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Aventura / VenusMission.swf / scripts / __Packages / games / Bonus.as next >
Encoding:
Text File  |  2008-09-23  |  4.3 KB  |  163 lines

  1. class games.Bonus extends AsBroadcaster
  2. {
  3.    var bonusMC;
  4.    var game;
  5.    var timer;
  6.    var _addString;
  7.    var _stepdisplay;
  8.    var _bonusValues;
  9.    var _bonusOffInfo;
  10.    var _newBonusValue = 0;
  11.    function Bonus(instance, MC, depth)
  12.    {
  13.       super();
  14.       this.bonusMC = MC;
  15.       if(depth != undefined)
  16.       {
  17.          this.bonusMC.swapDepths(depth);
  18.       }
  19.       this.game = instance;
  20.       this.timer = system.Timer.getInstance(this.bonusMC);
  21.       this._addString = this.game.bonus_addString;
  22.       this._stepdisplay = this.game.bonus_stepdisplay;
  23.       this.bonusMC.bonusfield.bonus = "0";
  24.       this.setBonusValues();
  25.       this.setButtons();
  26.       AsBroadcaster.initialize(this);
  27.    }
  28.    function resetTo(val)
  29.    {
  30.       this._newBonusValue = Number(val);
  31.       this.bonusMC.bonusfield.bonus = val;
  32.       this.updateBonus();
  33.       this.updateButtons();
  34.    }
  35.    function canUse(nr, bonusValue)
  36.    {
  37.       return bonusValue >= this.getBonusValue(nr);
  38.    }
  39.    function setValue(bonusValue)
  40.    {
  41.       var displayedbonus = Number(this.bonusMC.bonusfield.bonus);
  42.       var newdisplay = Number(bonusValue + this._addString);
  43.       if(bonusValue == "")
  44.       {
  45.          this.bonusMC.bonusfield.bonus = "0";
  46.       }
  47.       else if(isNaN(bonusValue) || isNaN(displayedbonus))
  48.       {
  49.          this.bonusMC.bonusfield.bonus = newdisplay;
  50.          this._newBonusValue = newdisplay;
  51.          this.timer.removeListener(this.bonusMC);
  52.       }
  53.       else if(displayedbonus != newdisplay)
  54.       {
  55.          this._newBonusValue = newdisplay;
  56.          this.timer.addListener(this.bonusMC,0,system.Delegate.create(this,this.updateBonus),0);
  57.       }
  58.       this.updateButtons();
  59.    }
  60.    function updateBonus()
  61.    {
  62.       var TF = this.bonusMC.bonusfield;
  63.       var displayedbonus = Number(TF.bonus);
  64.       var addvaltodisplay = (this._newBonusValue - displayedbonus) / this._stepdisplay;
  65.       TF.bonus = displayedbonus + (addvaltodisplay <= 0 ? Math.floor(addvaltodisplay) : Math.ceil(addvaltodisplay));
  66.       if(addvaltodisplay == 0)
  67.       {
  68.          this.timer.removeListener();
  69.       }
  70.    }
  71.    function bonusUsed(nr)
  72.    {
  73.       this.broadcastMessage("useBonus",nr);
  74.    }
  75.    function updateButtons()
  76.    {
  77.       var i = 1;
  78.       while(i <= 10)
  79.       {
  80.          this.setBonusButton(this.bonusMC["p_bonus" + i],this.bonusMC["costinfo" + i],this._bonusValues[i - 1],this._bonusOffInfo[i - 1]);
  81.          i++;
  82.       }
  83.    }
  84.    function setBonusButton(BTN, TF, v, offBTN)
  85.    {
  86.       if(this._newBonusValue >= v)
  87.       {
  88.          BTN.enabled = true;
  89.          BTN._visible = true;
  90.          BTN._alpha = 100;
  91.          TF._alpha = 100;
  92.          offBTN._visible = false;
  93.       }
  94.       else if(offBTN != undefined)
  95.       {
  96.          offBTN._visible = true;
  97.          BTN._visible = false;
  98.          TF._alpha = 50;
  99.       }
  100.       else
  101.       {
  102.          BTN.enabled = false;
  103.          BTN._alpha = 50;
  104.          TF._alpha = 50;
  105.       }
  106.    }
  107.    function setButtons()
  108.    {
  109.       var tMC;
  110.       this._bonusOffInfo = new Array();
  111.       var i = 1;
  112.       while(i <= 10)
  113.       {
  114.          tMC = this.bonusMC["p_bonus" + i];
  115.          new extensions.movieclip.SimpleButton(tMC,system.Delegate.create(this,this.bonusUsed,i));
  116.          tMC = this.bonusMC["bonus" + i + "_off"];
  117.          if(tMC != undefined)
  118.          {
  119.             this._bonusOffInfo[i - 1] = tMC;
  120.             tMC.stop();
  121.             tMC._visible = false;
  122.          }
  123.          this.setBonusButton(tMC,this.bonusMC["costinfo" + i],this._bonusValues[i - 1],this._bonusOffInfo[i - 1]);
  124.          i++;
  125.       }
  126.    }
  127.    function setBonusValues()
  128.    {
  129.       this._bonusValues = new Array();
  130.       if(this.game.bonus_Values != undefined)
  131.       {
  132.          this._bonusValues = this.game.bonus_Values.concat();
  133.       }
  134.       else
  135.       {
  136.          var i = 1;
  137.          while(i <= 10)
  138.          {
  139.             this._bonusValues[i - 1] = this.game["bonus_Value" + i];
  140.             i++;
  141.          }
  142.       }
  143.       var i = 0;
  144.       while(i < 10)
  145.       {
  146.          this.bonusMC["costinfo" + i].text = "cost: " + this._bonusValues[i];
  147.          i++;
  148.       }
  149.    }
  150.    function getBonusValue(nr)
  151.    {
  152.       return this._bonusValues[nr - 1];
  153.    }
  154.    function addStringToValue(str)
  155.    {
  156.       this._addString = str;
  157.    }
  158.    function setStepDisplay(step)
  159.    {
  160.       this._stepdisplay = step;
  161.    }
  162. }
  163.