home *** CD-ROM | disk | FTP | other *** search
- package game.view
- {
- import core.CoreEngine;
- import core.bonuses.AbstractBonus;
- import core.bonuses.BonusAttach;
- import core.bonuses.BonusElastic;
- import core.bonuses.BonusFreeze;
- import core.bonuses.BonusInverse;
- import core.bonuses.BonusPowerup;
- import core.bonuses.BonusPushField;
- import core.objects.BonusBall;
- import flash.display.Sprite;
- import flash.events.KeyboardEvent;
- import flash.events.MouseEvent;
- import flash.net.URLRequest;
- import flash.net.navigateToURL;
- import game.model.AppProxy;
- import game.model.GameProxy;
- import game.model.NotesProxy;
- import game.view.components.BonusIconView;
- import game.view.components.GameView;
- import game.view.components.LevelView;
- import org.puremvc.as3.interfaces.IMediator;
- import org.puremvc.as3.interfaces.INotification;
- import org.puremvc.as3.patterns.mediator.Mediator;
-
- public class GameMediator extends Mediator implements IMediator
- {
-
- public static const NAME:String = "GameMediator";
-
-
- private var _view:GameView;
-
- private var gameProxy:GameProxy;
-
- public function GameMediator(viewComponent:GameView)
- {
- super(NAME,viewComponent);
- init();
- }
-
- override public function handleNotification(note:INotification) : void
- {
- switch(note.getName())
- {
- case GameProxy.UPDATE:
- update(note.getBody() as int);
- break;
- case AppProxy.END_GAME:
- endGame();
- break;
- case GameProxy.BONUS_ACTIVATE:
- bonusActivate(note.getBody() as AbstractBonus);
- }
- }
-
- public function getObjectsContainer() : Sprite
- {
- return _view.objectsContainer;
- }
-
- private function destroyBonusMed(bonus:AbstractBonus) : void
- {
- var bonusName:String = null;
- var bonusIconMed:BonusIconMediator = null;
- if(bonus is BonusFreeze)
- {
- bonusName = BonusBall.FREEZE;
- }
- else if(bonus is BonusPowerup)
- {
- bonusName = BonusBall.POWERUP;
- }
- else if(bonus is BonusPushField)
- {
- bonusName = BonusBall.PUSH_FIELD;
- }
- else if(bonus is BonusAttach)
- {
- bonusName = BonusBall.ATTACH;
- }
- else if(bonus is BonusElastic)
- {
- bonusName = BonusBall.ELASTIC;
- }
- else if(bonus is BonusInverse)
- {
- bonusName = BonusBall.INVERSE;
- }
- facade.removeMediator(bonusName);
- }
-
- private function update(time:int) : void
- {
- var scoreText:String = gameProxy.getScore().toString();
- var zeros:* = "";
- for(var i:uint = 0; i < 6 - scoreText.length; i++)
- {
- zeros += "0";
- }
- scoreText = zeros.concat(scoreText);
- _view.score_tf.text = scoreText;
- }
-
- private function init() : void
- {
- _view = getViewComponent() as GameView;
- _view.score_tf.text = "0";
- gameProxy = facade.retrieveProxy(GameProxy.NAME) as GameProxy;
- var appProxy:AppProxy = facade.retrieveProxy(AppProxy.NAME) as AppProxy;
- if(appProxy.soundEnabled)
- {
- _view.soundIcon.gotoAndStop(1);
- }
- else
- {
- _view.soundIcon.gotoAndStop(2);
- }
- if(appProxy.musicEnabled)
- {
- _view.musicIcon.gotoAndStop(1);
- }
- else
- {
- _view.musicIcon.gotoAndStop(2);
- }
- _view.menuIcon.addEventListener(MouseEvent.CLICK,menuClickHandler);
- _view.soundIcon.addEventListener(MouseEvent.CLICK,soundClickHandler);
- _view.musicIcon.addEventListener(MouseEvent.CLICK,musicClickHandler);
- if(Preloader.SPONSOR_VERSION)
- {
- _view.sponsorLogo_bt.visible = false;
- }
- else
- {
- _view.sponsorLogo_bt.addEventListener(MouseEvent.CLICK,sponsorLogoClickHandler);
- }
- _view.fps.visible = false;
- _view.pause.visible = false;
- _view.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
- }
-
- private function soundClickHandler(e:MouseEvent) : void
- {
- var appProxy:AppProxy = facade.retrieveProxy(AppProxy.NAME) as AppProxy;
- appProxy.toggleSoundEnabled();
- if(appProxy.soundEnabled)
- {
- _view.soundIcon.gotoAndStop(1);
- }
- else
- {
- _view.soundIcon.gotoAndStop(2);
- }
- }
-
- public function getEffectsContainer() : Sprite
- {
- return _view.effectsContainer;
- }
-
- public function initialize() : void
- {
- initBonuses();
- var levelView:LevelView = new LevelView(_view.levelView);
- var levelMed:LevelMediator = new LevelMediator(levelView);
- levelMed.initialize(CoreEngine.getInstance().level);
- facade.registerMediator(levelMed);
- }
-
- private function createBonusMed(bonus:AbstractBonus) : void
- {
- var bonusName:String = null;
- var bonusIcon:Sprite = null;
- var bonusIconMed:BonusIconMediator = null;
- if(bonus is BonusFreeze)
- {
- bonusName = BonusBall.FREEZE;
- bonusIcon = _view.freezeIcon;
- }
- else if(bonus is BonusPowerup)
- {
- bonusName = BonusBall.POWERUP;
- bonusIcon = _view.powerupIcon;
- }
- else if(bonus is BonusPushField)
- {
- bonusName = BonusBall.PUSH_FIELD;
- bonusIcon = _view.pushFieldIcon;
- }
- else if(bonus is BonusAttach)
- {
- bonusName = BonusBall.ATTACH;
- bonusIcon = _view.attachIcon;
- }
- else if(bonus is BonusElastic)
- {
- bonusName = BonusBall.ELASTIC;
- bonusIcon = _view.elasticIcon;
- }
- else if(bonus is BonusInverse)
- {
- bonusName = BonusBall.INVERSE;
- bonusIcon = _view.inverseIcon;
- }
- var bonusIconView:BonusIconView = new BonusIconView(bonusIcon);
- bonusIconMed = new BonusIconMediator(bonusName,bonusIconView);
- bonusIconMed.initialize(bonus);
- facade.registerMediator(bonusIconMed);
- }
-
- public function getCoinsContainer() : Sprite
- {
- return _view.coinsContainer;
- }
-
- private function initBonuses() : void
- {
- var gameProxy:GameProxy = facade.retrieveProxy(GameProxy.NAME) as GameProxy;
- var bonuses:Array = gameProxy.getBonuses();
- for(var i:uint = 0; i < bonuses.length; i++)
- {
- createBonusMed(bonuses[i]);
- }
- }
-
- override public function listNotificationInterests() : Array
- {
- return [GameProxy.UPDATE,AppProxy.END_GAME];
- }
-
- private function menuClickHandler(e:MouseEvent) : void
- {
- var notesProxy:NotesProxy = facade.retrieveProxy(NotesProxy.NAME) as NotesProxy;
- notesProxy.push(AppProxy.QUIT_GAME);
- }
-
- private function musicClickHandler(e:MouseEvent) : void
- {
- var appProxy:AppProxy = facade.retrieveProxy(AppProxy.NAME) as AppProxy;
- appProxy.toggleMusicEnabled();
- if(appProxy.musicEnabled)
- {
- _view.musicIcon.gotoAndStop(1);
- }
- else
- {
- _view.musicIcon.gotoAndStop(2);
- }
- var notesProxy:NotesProxy = facade.retrieveProxy(NotesProxy.NAME) as NotesProxy;
- notesProxy.push(AppProxy.MUSIC_TOGGLE);
- }
-
- private function keyDownHandler(e:KeyboardEvent) : void
- {
- if(e.keyCode == 80)
- {
- gameProxy.togglePause();
- _view.pause.visible = gameProxy.isPaused ? true : false;
- }
- }
-
- public function getPlayerContainer() : Sprite
- {
- return _view.playerContainer;
- }
-
- private function endGame() : void
- {
- _view.stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
- }
-
- private function sponsorLogoClickHandler(e:MouseEvent) : void
- {
- navigateToURL(new URLRequest("http://www.ourworld.com/v11/tracking?source=game&id=fglbeez"),"_blank");
- }
-
- private function bonusActivate(bonus:AbstractBonus) : void
- {
- var bonusName:String = null;
- var bonusIcon:Sprite = null;
- var bonusIconMed:BonusIconMediator = null;
- var bonusIconView:BonusIconView = null;
- if(bonus is BonusFreeze)
- {
- bonusName = BonusBall.FREEZE;
- bonusIcon = _view.freezeIcon;
- }
- else if(bonus is BonusPowerup)
- {
- bonusName = BonusBall.POWERUP;
- bonusIcon = _view.powerupIcon;
- }
- else if(bonus is BonusPushField)
- {
- bonusName = BonusBall.PUSH_FIELD;
- bonusIcon = _view.pushFieldIcon;
- }
- else if(bonus is BonusAttach)
- {
- bonusName = BonusBall.ATTACH;
- bonusIcon = _view.attachIcon;
- }
- if(facade.hasMediator(bonusName))
- {
- bonusIconMed = facade.retrieveMediator(bonusName) as BonusIconMediator;
- bonusIconMed.initialize(bonus);
- }
- else
- {
- bonusIconView = new BonusIconView(bonusIcon);
- bonusIconMed = new BonusIconMediator(bonusName,bonusIconView);
- bonusIconMed.initialize(bonus);
- facade.registerMediator(bonusIconMed);
- }
- }
-
- public function destroy() : void
- {
- var gameProxy:GameProxy = facade.retrieveProxy(GameProxy.NAME) as GameProxy;
- var bonuses:Array = gameProxy.getBonuses();
- for(var i:uint = 0; i < bonuses.length; i++)
- {
- destroyBonusMed(bonuses[i]);
- }
- facade.removeMediator(LevelMediator.NAME);
- _view.parent.removeChild(_view);
- facade.removeMediator(NAME);
- }
- }
- }
-