Пример #1
0
        public void Start()
        {
            gameCell.value = Utils.LoadFromJsonFile <GameModel>(saveFileName, () => null, false);

            // game starts paused
            paused.value = true;

            // This is reactive binding, so menu will be active when game paused,
            // This approach allows to write cleaner, easier to change code
            menu.SetActive(paused);
            menu.newGame.Subscribe(() =>
            {
                gameCell.value = GameModel.New(menu.rocketConfigSelected);
                paused.value   = false;
                ResetTitleToDefault();
                view.AdjustCamera(game.planets.Count - 1);
            });
            // Also a reactive binding shortcut
            menu.resume.SetActive(gameCell.IsNot(null));
            menu.resume.Subscribe(() => paused.value = false);
            menu.exit.Subscribe(Application.Quit);

            // Here is the reactive library operators example
            // game model is changing over time, and collections in game model are changing over time
            // Join() allows to collapse any complex dependant changes into simple IReactiveCollection or ICell
            // Its very cool because view actually do not care when and how game model is changed, it just shows dynamic collection of items
            // MapWithDefaultIfNull(...) allows to choose empty collection when game model is null
            var planets = gameCell.MapWithDefaultIfNull(m => m.planets, StaticCollection <Planet> .Empty()).Join();
            var rockets = gameCell.MapWithDefaultIfNull(m => m.rockets, StaticCollection <RocketInstance> .Empty()).Join();

            view.Show(planets, rockets, gameCell.Map(g => g.playerPlanetId));

            view.SetHudCanvasVisible(paused.Not());
            ResetTitleToDefault();
        }