home *** CD-ROM | disk | FTP | other *** search
- class games.Bonus extends AsBroadcaster
- {
- var bonusMC;
- var game;
- var timer;
- var _addString;
- var _stepdisplay;
- var _bonusValues;
- var _bonusOffInfo;
- var _newBonusValue = 0;
- function Bonus(instance, MC, depth)
- {
- super();
- this.bonusMC = MC;
- if(depth != undefined)
- {
- this.bonusMC.swapDepths(depth);
- }
- this.game = instance;
- this.timer = system.Timer.getInstance(this.bonusMC);
- this._addString = this.game.bonus_addString;
- this._stepdisplay = this.game.bonus_stepdisplay;
- this.bonusMC.bonusfield.bonus = "0";
- this.setBonusValues();
- this.setButtons();
- AsBroadcaster.initialize(this);
- }
- function resetTo(val)
- {
- this._newBonusValue = Number(val);
- this.bonusMC.bonusfield.bonus = val;
- this.updateBonus();
- this.updateButtons();
- }
- function canUse(nr, bonusValue)
- {
- return bonusValue >= this.getBonusValue(nr);
- }
- function setValue(bonusValue)
- {
- var displayedbonus = Number(this.bonusMC.bonusfield.bonus);
- var newdisplay = Number(bonusValue + this._addString);
- if(bonusValue == "")
- {
- this.bonusMC.bonusfield.bonus = "0";
- }
- else if(isNaN(bonusValue) || isNaN(displayedbonus))
- {
- this.bonusMC.bonusfield.bonus = newdisplay;
- this._newBonusValue = newdisplay;
- this.timer.removeListener(this.bonusMC);
- }
- else if(displayedbonus != newdisplay)
- {
- this._newBonusValue = newdisplay;
- this.timer.addListener(this.bonusMC,0,system.Delegate.create(this,this.updateBonus),0);
- }
- this.updateButtons();
- }
- function updateBonus()
- {
- var TF = this.bonusMC.bonusfield;
- var displayedbonus = Number(TF.bonus);
- var addvaltodisplay = (this._newBonusValue - displayedbonus) / this._stepdisplay;
- TF.bonus = displayedbonus + (addvaltodisplay <= 0 ? Math.floor(addvaltodisplay) : Math.ceil(addvaltodisplay));
- if(addvaltodisplay == 0)
- {
- this.timer.removeListener();
- }
- }
- function bonusUsed(nr)
- {
- this.broadcastMessage("useBonus",nr);
- }
- function updateButtons()
- {
- var i = 1;
- while(i <= 10)
- {
- this.setBonusButton(this.bonusMC["p_bonus" + i],this.bonusMC["costinfo" + i],this._bonusValues[i - 1],this._bonusOffInfo[i - 1]);
- i++;
- }
- }
- function setBonusButton(BTN, TF, v, offBTN)
- {
- if(this._newBonusValue >= v)
- {
- BTN.enabled = true;
- BTN._visible = true;
- BTN._alpha = 100;
- TF._alpha = 100;
- offBTN._visible = false;
- }
- else if(offBTN != undefined)
- {
- offBTN._visible = true;
- BTN._visible = false;
- TF._alpha = 50;
- }
- else
- {
- BTN.enabled = false;
- BTN._alpha = 50;
- TF._alpha = 50;
- }
- }
- function setButtons()
- {
- var tMC;
- this._bonusOffInfo = new Array();
- var i = 1;
- while(i <= 10)
- {
- tMC = this.bonusMC["p_bonus" + i];
- new extensions.movieclip.SimpleButton(tMC,system.Delegate.create(this,this.bonusUsed,i));
- tMC = this.bonusMC["bonus" + i + "_off"];
- if(tMC != undefined)
- {
- this._bonusOffInfo[i - 1] = tMC;
- tMC.stop();
- tMC._visible = false;
- }
- this.setBonusButton(tMC,this.bonusMC["costinfo" + i],this._bonusValues[i - 1],this._bonusOffInfo[i - 1]);
- i++;
- }
- }
- function setBonusValues()
- {
- this._bonusValues = new Array();
- if(this.game.bonus_Values != undefined)
- {
- this._bonusValues = this.game.bonus_Values.concat();
- }
- else
- {
- var i = 1;
- while(i <= 10)
- {
- this._bonusValues[i - 1] = this.game["bonus_Value" + i];
- i++;
- }
- }
- var i = 0;
- while(i < 10)
- {
- this.bonusMC["costinfo" + i].text = "cost: " + this._bonusValues[i];
- i++;
- }
- }
- function getBonusValue(nr)
- {
- return this._bonusValues[nr - 1];
- }
- function addStringToValue(str)
- {
- this._addString = str;
- }
- function setStepDisplay(step)
- {
- this._stepdisplay = step;
- }
- }
-