Exemplo n.º 1
0
        /// <summary>
        /// Initialize this system to a working state.
        /// </summary>
        /// <param name="game">
        /// The <see cref="IGame"/> game, that requested the initialization. That is the game,
        /// the system will be running in.
        /// </param>
        public override void Initialize(IGame game)
        {
            base.Initialize(game);

            // Register the component types, required by this system. Since this
            // system uses a state machine, it also needs to register all types
            // of components, required by the individual states.
            this.Game.ComponentSystem.RegisterComponentType <LevelComponent>();
            this.Game.ComponentSystem.RegisterComponentType <BallComponent>();
            this.Game.ComponentSystem.RegisterComponentType <DestructableComponent>();
            this.Game.ComponentSystem.RegisterComponentType <PlayerComponent>();

            // Register the events, required by this system
            this.Game.EventManager.RegisterListener(BreakoutEvents.GameStarted, this.OnGameStarted);
            this.Game.EventManager.RegisterListener(BreakoutEvents.LoadLevel, this.OnLoadLevel);

            // Set up the state machine, top-down, as described in the model design
            // (Documents/Breakout Game StateMachine.png). This is mostly boilerplate,
            // but it represents the modeled behaviour and can be written down very
            // fast and with a lower error rate than defining the whole state machine
            // in this system. Check out LevelSystemWithoutStateMachine.cs for a
            // comparison.
            this.StateMachine = new StateMachine();

            LevelSpawnState spawnState = new LevelSpawnState(this);

            spawnState.BallsLeftEvent = LevelEvents.Finished;
            spawnState.AddTransition(LevelEvents.Finished, LevelStates.Paused);
            spawnState.GameOverEvent = LevelEvents.NoLivesLeft;
            spawnState.AddTransition(LevelEvents.NoLivesLeft, LevelStates.LevelFailed);
            this.StateMachine.AddState(LevelStates.Spawn, spawnState);

            LevelPlayingState playingState = new LevelPlayingState(this);

            playingState.PauseButtonEvent = LevelEvents.Pause;
            playingState.AddTransition(LevelEvents.Pause, LevelStates.Paused);
            playingState.NoBlocksLeftEvent = LevelEvents.NoBlocksLeft;
            playingState.AddTransition(LevelEvents.NoBlocksLeft, LevelStates.LevelFinished);
            playingState.NoBallsLeftEvent = LevelEvents.NoBallsLeft;
            playingState.AddTransition(LevelEvents.NoBallsLeft, LevelStates.Spawn);
            this.StateMachine.AddState(LevelStates.Playing, playingState);

            LevelPausedState pausedState = new LevelPausedState(this);

            pausedState.PlayButtonEvent = LevelEvents.Finished;
            pausedState.AddTransition(LevelEvents.Finished, LevelStates.Playing);
            this.StateMachine.AddState(LevelStates.Paused, pausedState);

            LevelFinishedState finishedState = new LevelFinishedState(this);

            finishedState.LevelLoadingEvent = LevelEvents.Finished;
            finishedState.AddTransition(LevelEvents.Finished, LevelStates.Loading);
            this.StateMachine.AddState(LevelStates.LevelFinished, finishedState);

            LevelFailedState failedState = new LevelFailedState(this);

            failedState.LevelLoadingEvent = LevelEvents.Finished;
            failedState.AddTransition(LevelEvents.Finished, LevelStates.Loading);
            this.StateMachine.AddState(LevelStates.LevelFailed, failedState);

            LevelLoadingState loadingState = new LevelLoadingState(this);

            loadingState.LevelLoadedEvent = LevelEvents.Finished;
            loadingState.AddTransition(LevelEvents.Finished, LevelStates.Spawn);
            this.StateMachine.AddState(LevelStates.Loading, loadingState);

            if (GameObject.FindObjectOfType <LevelComponent>() == null)
            {
                // This is a bit of a dirty hack, but it works in conjunction with the SystemSceneLoader component.
                this.Game.EventManager.QueueEvent(new HelGamesEvent(BreakoutEvents.LoadLevel, this.StartLevelIndex));
            }
        }
        /// <summary>
        /// Initialize this system to a working state.
        /// </summary>
        /// <param name="game">
        /// The <see cref="IGame"/> game, that requested the initialization. That is the game,
        /// the system will be running in.
        /// </param>
        public override void Initialize(IGame game)
        {
            base.Initialize(game);

            // Register the component types, required by this system. Since this
            // system uses a state machine, it also needs to register all types
            // of components, required by the individual states.
            this.Game.ComponentSystem.RegisterComponentType<LevelComponent>();
            this.Game.ComponentSystem.RegisterComponentType<BallComponent>();
            this.Game.ComponentSystem.RegisterComponentType<DestructableComponent>();
            this.Game.ComponentSystem.RegisterComponentType<PlayerComponent>();

            // Register the events, required by this system
            this.Game.EventManager.RegisterListener(BreakoutEvents.GameStarted, this.OnGameStarted);
            this.Game.EventManager.RegisterListener(BreakoutEvents.LoadLevel, this.OnLoadLevel);

            // Set up the state machine, top-down, as described in the model design
            // (Documents/Breakout Game StateMachine.png). This is mostly boilerplate,
            // but it represents the modeled behaviour and can be written down very
            // fast and with a lower error rate than defining the whole state machine
            // in this system. Check out LevelSystemWithoutStateMachine.cs for a
            // comparison.
            this.StateMachine = new StateMachine();

            LevelSpawnState spawnState = new LevelSpawnState(this);
            spawnState.BallsLeftEvent = LevelEvents.Finished;
            spawnState.AddTransition(LevelEvents.Finished, LevelStates.Paused);
            spawnState.GameOverEvent = LevelEvents.NoLivesLeft;
            spawnState.AddTransition(LevelEvents.NoLivesLeft, LevelStates.LevelFailed);
            this.StateMachine.AddState(LevelStates.Spawn, spawnState);

            LevelPlayingState playingState = new LevelPlayingState(this);
            playingState.PauseButtonEvent = LevelEvents.Pause;
            playingState.AddTransition(LevelEvents.Pause, LevelStates.Paused);
            playingState.NoBlocksLeftEvent = LevelEvents.NoBlocksLeft;
            playingState.AddTransition(LevelEvents.NoBlocksLeft, LevelStates.LevelFinished);
            playingState.NoBallsLeftEvent = LevelEvents.NoBallsLeft;
            playingState.AddTransition(LevelEvents.NoBallsLeft, LevelStates.Spawn);
            this.StateMachine.AddState(LevelStates.Playing, playingState);

            LevelPausedState pausedState = new LevelPausedState(this);
            pausedState.PlayButtonEvent = LevelEvents.Finished;
            pausedState.AddTransition(LevelEvents.Finished, LevelStates.Playing);
            this.StateMachine.AddState(LevelStates.Paused, pausedState);

            LevelFinishedState finishedState = new LevelFinishedState(this);
            finishedState.LevelLoadingEvent = LevelEvents.Finished;
            finishedState.AddTransition(LevelEvents.Finished, LevelStates.Loading);
            this.StateMachine.AddState(LevelStates.LevelFinished, finishedState);

            LevelFailedState failedState = new LevelFailedState(this);
            failedState.LevelLoadingEvent = LevelEvents.Finished;
            failedState.AddTransition(LevelEvents.Finished, LevelStates.Loading);
            this.StateMachine.AddState(LevelStates.LevelFailed, failedState);

            LevelLoadingState loadingState = new LevelLoadingState(this);
            loadingState.LevelLoadedEvent = LevelEvents.Finished;
            loadingState.AddTransition(LevelEvents.Finished, LevelStates.Spawn);
            this.StateMachine.AddState(LevelStates.Loading, loadingState);

            if (GameObject.FindObjectOfType<LevelComponent>() == null)
            {
                // This is a bit of a dirty hack, but it works in conjunction with the SystemSceneLoader component.
                this.Game.EventManager.QueueEvent(new HelGamesEvent(BreakoutEvents.LoadLevel, this.StartLevelIndex));
            }
        }