Esempio n. 1
0
        public GameController(ChangeGame gameIn, GraphicsController graphicsControllerIn, InputController inputControllerIn)
        {
            // Set references.
            game = gameIn;
            graphicsController = graphicsControllerIn;
            inputController    = inputControllerIn;

            // Add reference to graphics controller.
            graphicsController.SetGameControllerReference(this);

            // Define state definitions (with references to this GameController instance's content).
            #region State definitions
            GameState.Title = new GameState("title")
            {
                OnEnterState  = ResetContent,
                OnStateUpdate = UpdateMainMenu
            };
            GameState.MapControls = new GameState("mapcontrols")
            {
                OnEnterState  = StartMappingControls,
                OnStateUpdate = UpdateMapControls
            };
            GameState.Playing = new GameState("playing")
            {
                OnEnterState  = OnStartPlaying,
                OnStateUpdate = UpdatePlay
            };
            GameState.BetweenStages = new GameState("betweenstages")
            {
                OnEnterState  = () => { },
                OnStateUpdate = (GameTime gameTime) =>
                {
                    UpdatePlay(gameTime);
                    UpdateBetweenStages(gameTime);
                }
            };
            GameState.GameOver = new GameState("gameover")
            {
                OnEnterState = () =>
                {
                    gameOverMenu.Drop();

                    bestCoinScore = Math.Max(bestCoinScore, currentCoinScore);

                    laserPlayer.FiringLaser = false;
                    SoundController.StopAllLoops();
                },
                OnStateUpdate = UpdateGameOverMenu
            };
            #endregion

            // Set up controllers.
            coinBackgroundController = new CoinBackgroundController();

            // Set up menus.
            mainMenu          = new MainMenu(game, this);
            gameOverMenu      = new GameOverMenu(this);
            stageCompleteMenu = new StageCompleteMenu();

            // Set up players.
            laserPlayer  = new LaserPlayer(this);
            paddlePlayer = new PaddlePlayer();

            // Define collections.
            enemies     = new List <Enemy>();
            corpses     = new List <EnemyCorpse>();
            notes       = new List <Note>();
            notesOnFire = new List <NoteOnFire>();

            // Define timers.
            piggyBankSpawner          = new Timer(30000);
            piggyBankSpawner.Elapsed += (a, b) => ReadyToSpawnPiggyBank = true;

            vacuumSpawner          = new Timer();
            vacuumSpawner.Elapsed += (a, b) => ReadyToSpawnVacuum = true;

            noteSpawner          = new Timer();
            noteSpawner.Elapsed += (a, b) => ReadyToSpawnNote = true;
        }
Esempio n. 2
0
        public void UpdatePlay(GameTime gameTime)
        {
            #region Create objects
            if (ReadyToSpawnPiggyBank)
            {
                SpawnPiggyBank();

                ReadyToSpawnPiggyBank = false;
            }

            if (ReadyToSpawnVacuum)
            {
                SpawnVacuum();

                ReadyToSpawnVacuum = false;
            }

            if (ReadyToSpawnNote)
            {
                SpawnNote();

                ReadyToSpawnNote = false;
            }
            #endregion

            #region Update objects
            if (CurrentStage.HasFlag(Stage.StageFlags.LaserPlayerEnabled))
            {
                laserPlayer.Update(gameTime);
                laserPlayer.FixedUpdate();
            }
            if (CurrentStage.HasFlag(Stage.StageFlags.PaddlePlayerEnabled))
            {
                paddlePlayer.Update(gameTime);
            }

            coinBackgroundController.Update(gameTime);

            // Update enemies.
            for (int i = 0; i < enemies.Count; i++)
            {
                enemies[i].Update(gameTime);

                if (enemies[i] is VacuumEnemy)
                {
                    var vEnemy = enemies[i] as VacuumEnemy;

                    for (int j = notes.Count - 1; j >= 0; j--)
                    {
                        var note = notes[j];

                        vEnemy.TryCollect(note);
                    }
                }
            }

            for (int i = 0; i < notes.Count; i++)
            {
                notes[i].Update(gameTime);
            }

            foreach (var c in corpses)
            {
                c.Update(gameTime);
            }

            foreach (var c in notesOnFire)
            {
                c.Update(gameTime);
            }

            CurrentStage.Update(gameTime);
            #endregion

            #region Remove objects
            // Remove destroyed enemies.
            for (int i = 0; i < enemies.Count; i++)
            {
                // A "killed" enemy. Show the death sequence.
                if (enemies[i].IsDead)
                {
                    AddCoins(enemies[i].CoinsOnDeath);

                    var newCorpse = new EnemyCorpse(enemies[i]);
                    corpses.Add(newCorpse);

                    enemies[i].OnDeath();

                    DestroyEnemy(enemies[i]);
                }
                else if (enemies[i].ReadyToRemove)
                {
                    if (enemies[i] is VacuumEnemy)
                    {
                        // Only play sound once.
                        if (((VacuumEnemy)enemies[i]).totalNotesHeld > 0)
                        {
                            SoundController.Play(Sound.FireFlare);
                        }

                        for (int j = ((VacuumEnemy)enemies[i]).totalNotesHeld - 1; j >= 0; j--)
                        {
                            var note = ((VacuumEnemy)enemies[i]).NotesHeld[j];

                            notes.Remove(note);
                            notesMissed++;
                        }
                    }

                    DestroyEnemy(enemies[i]);
                }
            }

            for (int i = corpses.Count - 1; i >= 0; i--)
            {
                if (corpses[i].ReadyToRemove)
                {
                    corpses.RemoveAt(i);
                }
            }

            for (int i = notes.Count - 1; i >= 0; i--)
            {
                // Set on fire. No money earnt.
                if (notes[i].IsDead)
                {
                    var newOnFire = new NoteOnFire(notes[i]);
                    notesOnFire.Add(newOnFire);

                    if (notes[i].CaughtByVacuum != null)
                    {
                        notes[i].CaughtByVacuum.RemoveNoteFromVacuum(notes[i]);
                    }

                    SoundController.Play(Sound.FireFlare);

                    notes.RemoveAt(i);

                    notesMissed++;
                }
                // Otherwise, if caught, give money.
                else if (CurrentStage.HasFlag(Stage.StageFlags.PaddlePlayerEnabled) && notes[i].InRangeForCatching && !notes[i].CaughtByPlayer && BoxCollisionTest.IntersectAABB(paddlePlayer.CollisionRect, notes[i].CollisionRect))
                {
                    notes[i].CaughtByPlayer = true;
                }
                // Otherwise, check if ready to remove
                else if (notes[i].ReadyToRemove)
                {
                    if (notes[i].CaughtByPlayer)
                    {
                        AddCoins(Note.noteWorths[notes[i].Type]);
                        SoundController.Play(Sound.Ding);
                    }
                    else
                    {
                        notesMissed++;

                        SoundController.Play(Sound.FireFlare);
                    }

                    notes.RemoveAt(i);
                }
            }

            for (int i = notesOnFire.Count - 1; i >= 0; i--)
            {
                if (notesOnFire[i].ReadyToRemove)
                {
                    notesOnFire.RemoveAt(i);
                }
            }
            #endregion

            // If run out of health, game over.
            if (CurrentStage.HasFlag(Stage.StageFlags.NotesEnabled) && notesMissed >= CurrentStage.MaxNotesMissed)
            {
                SetState(GameState.GameOver);
            }

            // Move on to the next stage.
            if (CurrentState == GameState.Playing && CurrentStage.IsComplete())
            {
                ToNextStage();
            }

            if (Control.Return.IsJustPressed)
            {
                SetState(GameState.GameOver);
            }
        }
Esempio n. 3
0
        // Determines when coins should be spawned, updated, and moved into the background.
        public void Update(GameTime gameTime)
        {
            if (coinsToSpawn > 0)
            {
                SoundController.Play(Sound.CoinsDrop, true);
            }
            else
            {
                SoundController.Stop(Sound.CoinsDrop);
            }

            for (int i = 0; i < COINS_SPAWNED_PER_FRAME; i++)
            {
                if (coinsToSpawn > 0)
                {
                    coinsToSpawn--;

                    var randomNum = GameController.random.Next(0, totalTrendCount);
                    int j         = 0;
                    while (randomNum > 0)
                    {
                        randomNum -= currentLayerCoinTrend[j];
                        j++;
                    }

                    var newCoin = new Coin();
                    newCoin.SetX(j);
                    coins.Add(newCoin);
                }
            }

            // Update and remove destroyed coins.
            for (int i = coins.Count - 1; i >= 0; i--)
            {
                var moveBy = coins[i].fallBy * (float)gameTime.ElapsedGameTime.TotalSeconds;

                for (int m = 0; m < moveBy; m++)
                {
                    coins[i].MoveBy(new Vector2(0, 1));

                    if (coins[i].MoveAndCheckLand(coinDataBuffer))
                    {
                        var pos      = coins[i].CollisionRect.Location;
                        int arrayLoc = pos.Y * ChangeGame.PLAYABLE_AREA_WIDTH + pos.X;

                        coinDataBuffer[arrayLoc] = 1;

                        coins.RemoveAt(i);

                        coinsInLayer++;

                        CurrentCoinBufferUpdated(this, new CoinBufferUpdatedArgs(arrayLoc));
                        break;
                    }
                }

                // Move coin buffers if required.
                if (coinsInLayer >= COINS_PER_LAYER)
                {
                    coinsInLayer = 0;
                    CalculateCoinTrend();

                    CoinBufferCompleted(this, EventArgs.Empty);

                    ClearCoinDataBuffer();
                }
            }
        }