using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Input; /* * For any questions or help, contact us: * Zepharis Lubrano, 3861368 * zepharislubrano@hotmail.com * Luciano van der Veekens, 3784827 * luciano_vd_veekens@live.nl */ //Found this very useful for making our code easier to read (from Cowboy Henk, though unnecessary code has been removed) enum GameState { Playing, PlayerLost, PlayerDied, PlayerWon }; class Level { GameWorld gameWorld; public GameState currentState; public bool IsMouseVisible { get; set; } //bool for setting the visibility of the mousecursor Player player2, Player; ScoreGameObject scoreGameObject; TimerGameObject timerGameObject; LevelGameObject levelGameObject; SpriteGameObject waiting; HitCounter hitCount, hitCount2; SmallDucks smallDucks; Bullets bullets; bool won, lost; bool hostStarted; //To know if the host started the game already. float quitpositionx; public Level(GameWorld gw, ContentManager Content, int screenWidth, int screenHeight, int LevelIndex) { this.IsMouseVisible = false; gameWorld = gw; Duck Duck = new Duck(gameWorld, 1, "Duck"); QuitDuck Duck2 = new QuitDuck(gameWorld, 4, "Duck2"); Player = new Player(gameWorld, "Sprites/Cursor", 100, "Cursor"); SpriteGameObject Background = new SpriteGameObject(gameWorld, "Sprites/Background", 0, "Background"); SpriteGameObject Background2 = new SpriteGameObject(gameWorld, "Sprites/Background2", 2, "Background2"); SpriteGameObject quit = new SpriteGameObject(gameWorld, "Sprites/Quit", 4, "quit"); SoundEffect backgroundmusic = gameWorld.ResourceManager.getSound("Sounds/Backgroundmusic"); SoundEffectInstance instance = backgroundmusic.CreateInstance(); //needed to create an instance, otherwise the music wouldnt loop. instance.IsLooped = true; //setting the bool value to true, so it will loop instance.Play(); //playing the looped instance gameWorld.Add(Background2); gameWorld.Add(Duck); gameWorld.Add(quit); gameWorld.Add(Duck2); gameWorld.Add(Background); gameWorld.Add(Player); //overlays SpriteGameObject playerWon = new SpriteGameObject(gameWorld, "Overlays/you_win", 99, "you_win"); SpriteGameObject playerLost = new SpriteGameObject(gameWorld, "Overlays/you_lose", 99, "you_lose"); playerWon.Position = new Vector2(screenWidth, screenHeight) / 2 - playerWon.Center; playerLost.Position = new Vector2(screenWidth, screenHeight) / 2 - playerLost.Center; playerWon.Visible = false; playerLost.Visible = false; quit.Position = new Vector2(1200, 720 - quit.Center.Y) - quit.Center; quitpositionx = quit.Position.X + quit.Center.X; Duck2.Position = new Vector2(quitpositionx, quit.Position.Y); gameWorld.Add(playerWon); gameWorld.Add(playerLost); Player.DoUpdate = true; } public void HandleInput(InputHelper inputHelper) { GameModeHandler gmHandler = gameWorld.Find("gmHandler") as GameModeHandler; SpriteGameObject quit = gameWorld.Find("quit") as SpriteGameObject; Duck duck = gameWorld.Find("Duck") as Duck; QuitDuck duck2 = gameWorld.Find("Duck2") as QuitDuck; if (currentState == GameState.Playing) { gameWorld.HandleInput(inputHelper); if (inputHelper.MouseLeftButtonPressed() && inputHelper.MouseBoundingRectangle.Intersects(quit.BoundingRectangle) && duck.Shooting || inputHelper.MouseBoundingRectangle.Intersects(duck2.BoundingRectangle) && inputHelper.MouseLeftButtonPressed() && duck.Shooting) { if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Single) { smallDucks.DuckHits = 0; smallDucks.ResetColors(); smallDucks.Filled = false; } if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Host || gmHandler.CurrentGameMode == GameModeHandler.GameMode.Join) { hitCount.HitCount = 0; //resets the hitcounter, so it won't be the same when you're playing another game. hitCount2.HitCount = 0; } MenuReset(); duck.Randomness(gameWorld); duck.Reset2(gameWorld); Player.Reset(); currentState = GameState.Playing; gmHandler.CurrentGameMode = GameModeHandler.GameMode.Menu; } } if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Single) { SmallDucks smallDucks = gameWorld.Find("SmallDucks") as SmallDucks; if (Lost && inputHelper.MouseRightButtonPressed()) { currentState = GameState.Playing; smallDucks.DuckHits = 0; smallDucks.ResetColors(); smallDucks.Filled = false; duck.Randomness(gameWorld); duck.Reset2(gameWorld); Player.Reset(); } if (Won && inputHelper.MouseRightButtonPressed()) { currentState = GameState.Playing; smallDucks.DuckHits = 0; smallDucks.ResetColors(); smallDucks.Filled = false; duck.Randomness(gameWorld); duck.Reset2(gameWorld); LevelGameObject levelGameObject = gameWorld.Find("level") as LevelGameObject; levelGameObject.Level++; Player.Reset(); } if (Won && inputHelper.KeyPressed(Keys.Escape) || Lost && inputHelper.KeyPressed(Keys.Escape)) { MenuReset(); duck.Randomness(gameWorld); duck.Reset2(gameWorld); Player.Reset(); currentState = GameState.Playing; gmHandler.CurrentGameMode = GameModeHandler.GameMode.Menu; } } if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Host) { if ((Won || Lost) && inputHelper.MouseRightButtonPressed()) { hostStarted = true; currentState = GameState.Playing; duck.Randomness(gameWorld); duck.Reset2(gameWorld); timerGameObject.Reset(); hitCount.HitCount = 0; //resets the hitcounter, so it won't be the same when you're playing another game. hitCount2.HitCount = 0; } if ((Won || Lost) && inputHelper.KeyPressed(Keys.Escape)) { MenuReset(); duck.Randomness(gameWorld); duck.Reset2(gameWorld); hitCount.HitCount = 0; //resets the hitcounter, so it won't be the same after you rejoin. hitCount2.HitCount = 0; currentState = GameState.Playing; gmHandler.CurrentGameMode = GameModeHandler.GameMode.Menu; } } if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Join) { if ((Won || Lost) && inputHelper.KeyPressed(Keys.Escape)) { MenuReset(); duck.Randomness(gameWorld); duck.Reset2(gameWorld); currentState = GameState.Playing; gmHandler.CurrentGameMode = GameModeHandler.GameMode.Menu; } } } public void Update(GameTime gameTime) { GameModeHandler gmHandler = gameWorld.Find("gmHandler") as GameModeHandler; if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Single) SingleInstances(); if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Host) HostInstances(); if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Join) JoinInstances(); SpriteGameObject playerWon = gameWorld.Find("you_win") as SpriteGameObject; SpriteGameObject playerLost = gameWorld.Find("you_lose") as SpriteGameObject; Duck duck = gameWorld.Find("Duck") as Duck; if (currentState == GameState.Playing && gmHandler.CurrentGameMode == GameModeHandler.GameMode.Single) { if (smallDucks.DuckHits >= 5 && smallDucks.Filled) Won = true; else Won = false; if (smallDucks.DuckHits < 5 && smallDucks.Filled) Lost = true; else Lost = false; if (this.Won) { currentState = GameState.PlayerWon; playerWon.Visible = true; } else playerWon.Visible = false; if (this.Lost) { currentState = GameState.PlayerLost; playerLost.Visible = true; } else playerLost.Visible = false; } else { duck.FlyingDuck.Pause(); } if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Host) { if (timerGameObject.GameOver) { if (hitCount.HitCount >= hitCount2.HitCount) { Won = true; } if (hitCount.HitCount < hitCount2.HitCount) { Lost = true; } } else { Won = false; Lost = false; } } if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Join) { if (timerGameObject.GameOver) { if (hitCount2.HitCount >= hitCount.HitCount) { Won = true; } if (hitCount2.HitCount < hitCount.HitCount) { Lost = true; } } else { Won = false; Lost = false; } } if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Host || gmHandler.CurrentGameMode == GameModeHandler.GameMode.Join) { if (this.Won) { currentState = GameState.PlayerWon; playerWon.Visible = true; } else playerWon.Visible = false; if (this.Lost) { currentState = GameState.PlayerLost; playerLost.Visible = true; } else playerLost.Visible = false; } gameWorld.Update(gameTime); } public void Draw(GameTime gameTime, SpriteBatch spriteBatch) { gameWorld.Draw(gameTime, spriteBatch); } public GameState CurrentState { get { return currentState; } set { currentState = value; } } //All the extra instances needed in Single Player-mode public void SingleInstances() { SpriteGameObject waiting = gameWorld.Find("waiting") as SpriteGameObject; if (waiting != null) waiting.Visible = false; if (scoreGameObject == null) { scoreGameObject = new ScoreGameObject(gameWorld, 101, "score"); gameWorld.Add(scoreGameObject); } if (levelGameObject == null) { levelGameObject = new LevelGameObject(gameWorld, 101, "level"); gameWorld.Add(levelGameObject); } if (smallDucks == null) { smallDucks = new SmallDucks(gameWorld, 3, "SmallDucks"); gameWorld.Add(smallDucks); } if (bullets == null) { bullets = new Bullets(gameWorld, 3, "Bullets"); gameWorld.Add(bullets); } } //All the extra instances needed in Host Game-mode public void HostInstances() { if (waiting == null) { waiting = new SpriteGameObject(gameWorld, "Overlays/WaitingForClient", 99, "waiting"); waiting.Position = new Vector2(640, 360) - waiting.Center; gameWorld.Add(waiting); } if (player2 == null) { player2 = new Player(gameWorld, "Sprites/Cursor2", 100, "Cursor2"); player2.Visible = false; player2.DoUpdate = false; Player.DoUpdate = true; gameWorld.Add(player2); } if (timerGameObject == null) { timerGameObject = new TimerGameObject(gameWorld, 101, "Timer"); gameWorld.Add(timerGameObject); } if (hitCount == null) { hitCount = new HitCounter(gameWorld, 101, "HitCounter"); hitCount.TextColor = Color.Red; hitCount.Position = new Vector2(240, 190); hitCount.Name = "Player 1: "; hitCount.DoUpdate = true; gameWorld.Add(hitCount); } if (hitCount2 == null) { hitCount2 = new HitCounter(gameWorld, 101, "HitCounter2"); hitCount2.TextColor = Color.Blue; hitCount2.Position = new Vector2(240, 300); hitCount2.Name = "Player 2: "; hitCount2.DoUpdate = false; gameWorld.Add(hitCount2); } } //All the extra instances needed in Join Game-mode public void JoinInstances() { SpriteGameObject waiting = gameWorld.Find("waiting") as SpriteGameObject; if (waiting != null) waiting.Visible = false; if (player2 == null) { player2 = new Player(gameWorld, "Sprites/Cursor2", 100, "Cursor2"); player2.Visible = false; player2.DoUpdate = true; Player.DoUpdate = false; gameWorld.Add(player2); } if (timerGameObject == null) { timerGameObject = new TimerGameObject(gameWorld, 101, "Timer"); gameWorld.Add(timerGameObject); } if (hitCount == null) { hitCount = new HitCounter(gameWorld, 101, "HitCounter"); hitCount.Name = "Player 1: "; hitCount.TextColor = Color.Red; hitCount.Position = new Vector2(240, 190); hitCount.DoUpdate = false; gameWorld.Add(hitCount); } if (hitCount2 == null) { hitCount2 = new HitCounter(gameWorld, 101, "HitCounter2"); hitCount2.TextColor = Color.Blue; hitCount2.Position = new Vector2(240, 300); hitCount2.Name = "Player 2: "; hitCount2.DoUpdate = true; gameWorld.Add(hitCount2); } } //Whenever you go back to the menu, call this method. This everything will get unloaded when it's not needed. public void MenuReset() { SpriteGameObject waiting = gameWorld.Find("waiting") as SpriteGameObject; if (waiting != null) waiting.Visible = true; Player.DoUpdate = true; Won = false; Lost = false; SpriteGameObject playerWon = gameWorld.Find("you_win") as SpriteGameObject; SpriteGameObject playerLost = gameWorld.Find("you_lose") as SpriteGameObject; playerWon.Visible = false; playerLost.Visible = false; if (scoreGameObject != null) { gameWorld.Remove(scoreGameObject); scoreGameObject = null; } if (levelGameObject != null) { gameWorld.Remove(levelGameObject); levelGameObject = null; } if (smallDucks != null) { gameWorld.Remove(smallDucks); smallDucks = null; } if (bullets != null) { gameWorld.Remove(bullets); bullets = null; } if (player2 != null) { gameWorld.Remove(player2); player2 = null; } if (timerGameObject != null) { gameWorld.Remove(timerGameObject); timerGameObject = null; } if (hitCount != null) { gameWorld.Remove(hitCount); hitCount = null; } if (hitCount2 != null) { gameWorld.Remove(hitCount2); hitCount2 = null; } } public bool Won { get { return won; } set { won = value; } } public bool Lost { get { return lost; } set { lost = value; } } public bool HostStarted { get { return hostStarted; } set { hostStarted = value; } } }