home *** CD-ROM | disk | FTP | other *** search
/ 404 Jogos / CLJG.iso / Diversos / Beez.swf / scripts / game / view / GameMediator.as < prev    next >
Encoding:
Text File  |  2008-09-03  |  10.4 KB  |  333 lines

  1. package game.view
  2. {
  3.    import core.CoreEngine;
  4.    import core.bonuses.AbstractBonus;
  5.    import core.bonuses.BonusAttach;
  6.    import core.bonuses.BonusElastic;
  7.    import core.bonuses.BonusFreeze;
  8.    import core.bonuses.BonusInverse;
  9.    import core.bonuses.BonusPowerup;
  10.    import core.bonuses.BonusPushField;
  11.    import core.objects.BonusBall;
  12.    import flash.display.Sprite;
  13.    import flash.events.KeyboardEvent;
  14.    import flash.events.MouseEvent;
  15.    import flash.net.URLRequest;
  16.    import flash.net.navigateToURL;
  17.    import game.model.AppProxy;
  18.    import game.model.GameProxy;
  19.    import game.model.NotesProxy;
  20.    import game.view.components.BonusIconView;
  21.    import game.view.components.GameView;
  22.    import game.view.components.LevelView;
  23.    import org.puremvc.as3.interfaces.IMediator;
  24.    import org.puremvc.as3.interfaces.INotification;
  25.    import org.puremvc.as3.patterns.mediator.Mediator;
  26.    
  27.    public class GameMediator extends Mediator implements IMediator
  28.    {
  29.       
  30.       public static const NAME:String = "GameMediator";
  31.        
  32.       
  33.       private var _view:GameView;
  34.       
  35.       private var gameProxy:GameProxy;
  36.       
  37.       public function GameMediator(viewComponent:GameView)
  38.       {
  39.          super(NAME,viewComponent);
  40.          init();
  41.       }
  42.       
  43.       override public function handleNotification(note:INotification) : void
  44.       {
  45.          switch(note.getName())
  46.          {
  47.             case GameProxy.UPDATE:
  48.                update(note.getBody() as int);
  49.                break;
  50.             case AppProxy.END_GAME:
  51.                endGame();
  52.                break;
  53.             case GameProxy.BONUS_ACTIVATE:
  54.                bonusActivate(note.getBody() as AbstractBonus);
  55.          }
  56.       }
  57.       
  58.       public function getObjectsContainer() : Sprite
  59.       {
  60.          return _view.objectsContainer;
  61.       }
  62.       
  63.       private function destroyBonusMed(bonus:AbstractBonus) : void
  64.       {
  65.          var bonusName:String = null;
  66.          var bonusIconMed:BonusIconMediator = null;
  67.          if(bonus is BonusFreeze)
  68.          {
  69.             bonusName = BonusBall.FREEZE;
  70.          }
  71.          else if(bonus is BonusPowerup)
  72.          {
  73.             bonusName = BonusBall.POWERUP;
  74.          }
  75.          else if(bonus is BonusPushField)
  76.          {
  77.             bonusName = BonusBall.PUSH_FIELD;
  78.          }
  79.          else if(bonus is BonusAttach)
  80.          {
  81.             bonusName = BonusBall.ATTACH;
  82.          }
  83.          else if(bonus is BonusElastic)
  84.          {
  85.             bonusName = BonusBall.ELASTIC;
  86.          }
  87.          else if(bonus is BonusInverse)
  88.          {
  89.             bonusName = BonusBall.INVERSE;
  90.          }
  91.          facade.removeMediator(bonusName);
  92.       }
  93.       
  94.       private function update(time:int) : void
  95.       {
  96.          var scoreText:String = gameProxy.getScore().toString();
  97.          var zeros:* = "";
  98.          for(var i:uint = 0; i < 6 - scoreText.length; i++)
  99.          {
  100.             zeros += "0";
  101.          }
  102.          scoreText = zeros.concat(scoreText);
  103.          _view.score_tf.text = scoreText;
  104.       }
  105.       
  106.       private function init() : void
  107.       {
  108.          _view = getViewComponent() as GameView;
  109.          _view.score_tf.text = "0";
  110.          gameProxy = facade.retrieveProxy(GameProxy.NAME) as GameProxy;
  111.          var appProxy:AppProxy = facade.retrieveProxy(AppProxy.NAME) as AppProxy;
  112.          if(appProxy.soundEnabled)
  113.          {
  114.             _view.soundIcon.gotoAndStop(1);
  115.          }
  116.          else
  117.          {
  118.             _view.soundIcon.gotoAndStop(2);
  119.          }
  120.          if(appProxy.musicEnabled)
  121.          {
  122.             _view.musicIcon.gotoAndStop(1);
  123.          }
  124.          else
  125.          {
  126.             _view.musicIcon.gotoAndStop(2);
  127.          }
  128.          _view.menuIcon.addEventListener(MouseEvent.CLICK,menuClickHandler);
  129.          _view.soundIcon.addEventListener(MouseEvent.CLICK,soundClickHandler);
  130.          _view.musicIcon.addEventListener(MouseEvent.CLICK,musicClickHandler);
  131.          if(Preloader.SPONSOR_VERSION)
  132.          {
  133.             _view.sponsorLogo_bt.visible = false;
  134.          }
  135.          else
  136.          {
  137.             _view.sponsorLogo_bt.addEventListener(MouseEvent.CLICK,sponsorLogoClickHandler);
  138.          }
  139.          _view.fps.visible = false;
  140.          _view.pause.visible = false;
  141.          _view.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
  142.       }
  143.       
  144.       private function soundClickHandler(e:MouseEvent) : void
  145.       {
  146.          var appProxy:AppProxy = facade.retrieveProxy(AppProxy.NAME) as AppProxy;
  147.          appProxy.toggleSoundEnabled();
  148.          if(appProxy.soundEnabled)
  149.          {
  150.             _view.soundIcon.gotoAndStop(1);
  151.          }
  152.          else
  153.          {
  154.             _view.soundIcon.gotoAndStop(2);
  155.          }
  156.       }
  157.       
  158.       public function getEffectsContainer() : Sprite
  159.       {
  160.          return _view.effectsContainer;
  161.       }
  162.       
  163.       public function initialize() : void
  164.       {
  165.          initBonuses();
  166.          var levelView:LevelView = new LevelView(_view.levelView);
  167.          var levelMed:LevelMediator = new LevelMediator(levelView);
  168.          levelMed.initialize(CoreEngine.getInstance().level);
  169.          facade.registerMediator(levelMed);
  170.       }
  171.       
  172.       private function createBonusMed(bonus:AbstractBonus) : void
  173.       {
  174.          var bonusName:String = null;
  175.          var bonusIcon:Sprite = null;
  176.          var bonusIconMed:BonusIconMediator = null;
  177.          if(bonus is BonusFreeze)
  178.          {
  179.             bonusName = BonusBall.FREEZE;
  180.             bonusIcon = _view.freezeIcon;
  181.          }
  182.          else if(bonus is BonusPowerup)
  183.          {
  184.             bonusName = BonusBall.POWERUP;
  185.             bonusIcon = _view.powerupIcon;
  186.          }
  187.          else if(bonus is BonusPushField)
  188.          {
  189.             bonusName = BonusBall.PUSH_FIELD;
  190.             bonusIcon = _view.pushFieldIcon;
  191.          }
  192.          else if(bonus is BonusAttach)
  193.          {
  194.             bonusName = BonusBall.ATTACH;
  195.             bonusIcon = _view.attachIcon;
  196.          }
  197.          else if(bonus is BonusElastic)
  198.          {
  199.             bonusName = BonusBall.ELASTIC;
  200.             bonusIcon = _view.elasticIcon;
  201.          }
  202.          else if(bonus is BonusInverse)
  203.          {
  204.             bonusName = BonusBall.INVERSE;
  205.             bonusIcon = _view.inverseIcon;
  206.          }
  207.          var bonusIconView:BonusIconView = new BonusIconView(bonusIcon);
  208.          bonusIconMed = new BonusIconMediator(bonusName,bonusIconView);
  209.          bonusIconMed.initialize(bonus);
  210.          facade.registerMediator(bonusIconMed);
  211.       }
  212.       
  213.       public function getCoinsContainer() : Sprite
  214.       {
  215.          return _view.coinsContainer;
  216.       }
  217.       
  218.       private function initBonuses() : void
  219.       {
  220.          var gameProxy:GameProxy = facade.retrieveProxy(GameProxy.NAME) as GameProxy;
  221.          var bonuses:Array = gameProxy.getBonuses();
  222.          for(var i:uint = 0; i < bonuses.length; i++)
  223.          {
  224.             createBonusMed(bonuses[i]);
  225.          }
  226.       }
  227.       
  228.       override public function listNotificationInterests() : Array
  229.       {
  230.          return [GameProxy.UPDATE,AppProxy.END_GAME];
  231.       }
  232.       
  233.       private function menuClickHandler(e:MouseEvent) : void
  234.       {
  235.          var notesProxy:NotesProxy = facade.retrieveProxy(NotesProxy.NAME) as NotesProxy;
  236.          notesProxy.push(AppProxy.QUIT_GAME);
  237.       }
  238.       
  239.       private function musicClickHandler(e:MouseEvent) : void
  240.       {
  241.          var appProxy:AppProxy = facade.retrieveProxy(AppProxy.NAME) as AppProxy;
  242.          appProxy.toggleMusicEnabled();
  243.          if(appProxy.musicEnabled)
  244.          {
  245.             _view.musicIcon.gotoAndStop(1);
  246.          }
  247.          else
  248.          {
  249.             _view.musicIcon.gotoAndStop(2);
  250.          }
  251.          var notesProxy:NotesProxy = facade.retrieveProxy(NotesProxy.NAME) as NotesProxy;
  252.          notesProxy.push(AppProxy.MUSIC_TOGGLE);
  253.       }
  254.       
  255.       private function keyDownHandler(e:KeyboardEvent) : void
  256.       {
  257.          if(e.keyCode == 80)
  258.          {
  259.             gameProxy.togglePause();
  260.             _view.pause.visible = gameProxy.isPaused ? true : false;
  261.          }
  262.       }
  263.       
  264.       public function getPlayerContainer() : Sprite
  265.       {
  266.          return _view.playerContainer;
  267.       }
  268.       
  269.       private function endGame() : void
  270.       {
  271.          _view.stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
  272.       }
  273.       
  274.       private function sponsorLogoClickHandler(e:MouseEvent) : void
  275.       {
  276.          navigateToURL(new URLRequest("http://www.ourworld.com/v11/tracking?source=game&id=fglbeez"),"_blank");
  277.       }
  278.       
  279.       private function bonusActivate(bonus:AbstractBonus) : void
  280.       {
  281.          var bonusName:String = null;
  282.          var bonusIcon:Sprite = null;
  283.          var bonusIconMed:BonusIconMediator = null;
  284.          var bonusIconView:BonusIconView = null;
  285.          if(bonus is BonusFreeze)
  286.          {
  287.             bonusName = BonusBall.FREEZE;
  288.             bonusIcon = _view.freezeIcon;
  289.          }
  290.          else if(bonus is BonusPowerup)
  291.          {
  292.             bonusName = BonusBall.POWERUP;
  293.             bonusIcon = _view.powerupIcon;
  294.          }
  295.          else if(bonus is BonusPushField)
  296.          {
  297.             bonusName = BonusBall.PUSH_FIELD;
  298.             bonusIcon = _view.pushFieldIcon;
  299.          }
  300.          else if(bonus is BonusAttach)
  301.          {
  302.             bonusName = BonusBall.ATTACH;
  303.             bonusIcon = _view.attachIcon;
  304.          }
  305.          if(facade.hasMediator(bonusName))
  306.          {
  307.             bonusIconMed = facade.retrieveMediator(bonusName) as BonusIconMediator;
  308.             bonusIconMed.initialize(bonus);
  309.          }
  310.          else
  311.          {
  312.             bonusIconView = new BonusIconView(bonusIcon);
  313.             bonusIconMed = new BonusIconMediator(bonusName,bonusIconView);
  314.             bonusIconMed.initialize(bonus);
  315.             facade.registerMediator(bonusIconMed);
  316.          }
  317.       }
  318.       
  319.       public function destroy() : void
  320.       {
  321.          var gameProxy:GameProxy = facade.retrieveProxy(GameProxy.NAME) as GameProxy;
  322.          var bonuses:Array = gameProxy.getBonuses();
  323.          for(var i:uint = 0; i < bonuses.length; i++)
  324.          {
  325.             destroyBonusMed(bonuses[i]);
  326.          }
  327.          facade.removeMediator(LevelMediator.NAME);
  328.          _view.parent.removeChild(_view);
  329.          facade.removeMediator(NAME);
  330.       }
  331.    }
  332. }
  333.