using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; using System.Threading; using System.IO; using System.Collections.Generic; using System; /* * For any questions or help, contact us: * Zepharis Lubrano, 3861368 * zepharislubrano@hotmail.com * Luciano van der Veekens, 3784827 * luciano_vd_veekens@live.nl */ class DuckHuntFinal : Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; InputHelper inputHelper; public GameWorld gameWorld; Level currentLevel; GameModeHandler gmHandler; IPClass ipClass; Menu menu; HighScore highScore; int levelIndex; int lastScore; Server server; Client client; Thread s, c; int timer, timer2, timer3, timer4, timer5, timer6; string ipaddress; float posxclient, posyclient, posxserver, posyserver; //Server and Client positions from cursor. string Timertext; //Text for drawing the Time on the screen (for client) string ServerHits, ClientHits; //Amount of hits from server and client float startxpos, startypos, duckxvel, duckyvel, duckxpos, duckypos; //Server's duck positions and velocities string coordinates_old, timeleft_old, targetshot_old, count_old, duckposvel_old; // old values that check if a message should be send or not (so no repeats occur) //server side string c_coordinates_old, c_targetshot_old, c_count_old; // old values that check if a message should be send or not (so no repeats occur) //client side static void Main() { DuckHuntFinal game = new DuckHuntFinal(); game.Run(); } public DuckHuntFinal() { Content.RootDirectory = "Content"; graphics = new GraphicsDeviceManager(this); graphics.PreferredBackBufferWidth = 1280; graphics.PreferredBackBufferHeight = 720; inputHelper = new InputHelper(); } protected override void LoadContent() { levelIndex = 0; lastScore = 0; spriteBatch = new SpriteBatch(GraphicsDevice); gameWorld = new GameWorld(Content); gmHandler = new GameModeHandler(0, "gmHandler"); gameWorld.Add(gmHandler); gmHandler.CurrentGameMode = GameModeHandler.GameMode.Menu; currentLevel = new Level(gameWorld, Content, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight, levelIndex); ipClass = new IPClass(gameWorld); highScore = new HighScore(gameWorld, 150, "HighScore"); menu = new Menu(gameWorld, Content, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight); } protected void HandleInput() { inputHelper.Update(); if (inputHelper.KeyPressed(Keys.F11) || inputHelper.IsKeyDown(Keys.LeftAlt) && inputHelper.KeyPressed(Keys.Enter)) { graphics.ToggleFullScreen(); } switch (gmHandler.CurrentGameMode) { case GameModeHandler.GameMode.Menu: menu.HandleInput(inputHelper); break; case GameModeHandler.GameMode.IP: ipClass.HandleInput(inputHelper); break; case GameModeHandler.GameMode.HighScore: highScore.HandleInput(inputHelper); break; case GameModeHandler.GameMode.Single: case GameModeHandler.GameMode.Host: case GameModeHandler.GameMode.Join: currentLevel.HandleInput(inputHelper); break; } if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Single) { if (currentLevel.Lost && inputHelper.MouseRightButtonPressed()) { ScoreGameObject score = gameWorld.Find("score") as ScoreGameObject; LevelGameObject level = gameWorld.Find("level") as LevelGameObject; highScore.EditHighScore(score, level); gmHandler.CurrentGameMode = GameModeHandler.GameMode.HighScore; } } if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.HighScore) { if (highScore.PlayerName.Length == 3) { if (inputHelper.KeyPressed(Keys.Enter) || inputHelper.MouseLeftButtonPressed() && inputHelper.MouseBoundingRectangle.Intersects(highScore.Duck.BoundingRectangle) || inputHelper.MouseLeftButtonPressed() && inputHelper.MouseBoundingRectangle.Intersects(highScore.Back.BoundingRectangle)) { currentLevel.currentState = GameState.Playing; currentLevel.MenuReset(); gmHandler.CurrentGameMode = GameModeHandler.GameMode.Menu; } } } } protected override void Update(GameTime gameTime) { Duck duck = gameWorld.Find("Duck") as Duck; //shows overlay: can't find server if the server can't be found if (server != null) { if (server.CH != null) { if (server.ClientConnected == false) { menu.LostConnection = true; currentLevel.MenuReset(); duck.Randomness(gameWorld); duck.Reset2(gameWorld); currentLevel.CurrentState = GameState.Playing; } else menu.LostConnection = false; } } if (client != null) { //if the server can't be found, reset everything again. if (client.ServerFound == false) { menu.NoServerFound = true; currentLevel.MenuReset(); duck.Randomness(gameWorld); duck.Reset2(gameWorld); currentLevel.CurrentState = GameState.Playing; } else menu.NoServerFound = false; } switch (gmHandler.CurrentGameMode) { case GameModeHandler.GameMode.Single: currentLevel.SingleInstances(); break; case GameModeHandler.GameMode.Host: currentLevel.HostInstances(); break; case GameModeHandler.GameMode.Join: currentLevel.JoinInstances(); break; } HandleInput(); switch (gmHandler.CurrentGameMode) { case GameModeHandler.GameMode.Menu: menu.Update(gameTime); break; case GameModeHandler.GameMode.IP: ipaddress = ipClass.IPAddress; ipClass.Update(gameTime); break; case GameModeHandler.GameMode.HighScore: highScore.Update(gameTime, gameWorld); break; case GameModeHandler.GameMode.Single: case GameModeHandler.GameMode.Host: case GameModeHandler.GameMode.Join: currentLevel.Update(gameTime); break; } //stops client and server (if started) when you return to the menu. if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Menu) { if (server != null) { server.kill(); s.Abort(); s = null; server = null; } if (client != null) { client.kill(); c.Abort(); c = null; client = null; } } if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Host) { SpriteGameObject waiting = gameWorld.Find("waiting") as SpriteGameObject; if (waiting.Visible == true) { TimerGameObject time = gameWorld.Find("Timer") as TimerGameObject; time.Reset(); HitCounter hitcount = gameWorld.Find("HitCounter") as HitCounter; hitcount.Reset(); HitCounter hitcount2 = gameWorld.Find("HitCounter2") as HitCounter; hitcount2.Reset(); } //Maak een server aan if (server == null) { server = new Server(40000, gameWorld); //Zeg dat de Server.run methode zometeen in een aparte Thread moet worden gestart s = new Thread(server.run); //start de thread voor de server s.Start(); } ServerUpdate(); } if (gmHandler.CurrentGameMode == GameModeHandler.GameMode.Join) { //Maak een client aan if (client == null) { client = new Client(40000, ipaddress, gameWorld); //Zeg dat de Client.run methode zometeen in een aparte Thread moet worden gestart c = new Thread(client.run); c.Start(); } ClientUpdate(); } } public void ServerUpdate() { Duck ducky = gameWorld.Find("Duck") as Duck; if (server.ClientSended) { string clientstarted = "CLIENTOK"; ducky.Randomness(gameWorld); server.broadcastMessage(clientstarted); } string duckposvel = "DUCK:" + ducky.Position.X + "," + ducky.Position.Y + ";" + ducky.Velocity.X + "," + ducky.Velocity.Y; if (duckposvel_old != duckposvel) { server.broadcastMessage(duckposvel); duckposvel_old = duckposvel; } string gamestart = "START"; if (currentLevel.HostStarted) server.broadcastMessage(gamestart); string respawn = "RESPAWN"; if (ducky.RespawnDuck) { server.broadcastMessage(respawn); } Player player = gameWorld.Find("Cursor") as Player; string coordinates = "PLAYER:" + player.Position.X + "," + player.Position.Y + ";" + player.PlayerShot.ToString(); if (coordinates_old != coordinates) { server.broadcastMessage(coordinates); coordinates_old = coordinates; } TimerGameObject time = gameWorld.Find("Timer") as TimerGameObject; string timeleft = "TIME." + time.Left; if (timeleft_old != timeleft) { server.broadcastMessage(timeleft); timeleft_old = timeleft; } if (time.GameOver) { timer++; string gameover = "GAMEOVER"; if (timer < 100) { server.broadcastMessage(gameover); } } else timer = 0; Player player2 = gameWorld.Find("Cursor2") as Player; player2.Position = new Vector2(posxclient, posyclient); HitCounter hitCount = gameWorld.Find("HitCounter") as HitCounter; HitCounter hitCount2 = gameWorld.Find("HitCounter2") as HitCounter; string targetshot = "TARGETOK"; string count = "HIT:" + hitCount.HitCount; timer5++; if (timer5 > 200) { server.broadcastMessage(count); timer5 = 0; } if (ducky.TargetHit) { server.broadcastMessage(targetshot); } while (server.Inbox.Count != 0) //zolang er berichten zijn { player2.Visible = true; KeyValuePair msg = server.Inbox.Dequeue(); //opens the message from your mailbox //lines to retrieve the x-pos and y-pos from the message if (msg.Value.Contains("CLIENTOK")) { currentLevel.CurrentState = GameState.Playing; ducky.Reset2(gameWorld); time.Reset(); SpriteGameObject waiting = gameWorld.Find("waiting") as SpriteGameObject; waiting.Visible = false; server.ClientSended = false; } if (msg.Value.Contains("PLAYER")) { int length = msg.Value.IndexOf(',') - 7; int yposfirstindex = msg.Value.IndexOf(',') + 1; int yposlength = msg.Value.IndexOf(';') - yposfirstindex; posxclient = float.Parse(msg.Value.Substring(7, length)); posyclient = float.Parse(msg.Value.Substring(yposfirstindex, yposlength)); } if (msg.Value.Contains("HIT")) { ClientHits = msg.Value.Substring(4, msg.Value.Length - 4); hitCount2.HitCount = int.Parse(ClientHits); } if (msg.Value.Contains("TARGETOK")) { ducky.DuckAlive = false; ducky.DuckMoving = false; ducky.Instance.Pause(); } if (msg.Value.Contains("RESPAWNOK")) { ducky.RespawnDuck = false; } if (msg.Value.Contains("STARTED")) { currentLevel.HostStarted = false; } } } public void ClientUpdate() { timer2++; Player player2 = gameWorld.Find("Cursor2") as Player; string c_coordinates = "PLAYER:" + player2.Position.X + "," + player2.Position.Y + ";" + player2.PlayerShot.ToString(); if (c_coordinates_old != c_coordinates) { client.sendMessage(c_coordinates); c_coordinates_old = c_coordinates; } HitCounter hitCount2 = gameWorld.Find("HitCounter2") as HitCounter; Duck duck = gameWorld.Find("Duck") as Duck; string c_targetshot = "TARGETOK"; string c_count = "HIT:" + hitCount2.HitCount; timer3++; if (timer3 > 200) { client.sendMessage(c_count); timer3 = 0; } if (duck.TargetHit) { client.sendMessage(c_targetshot); } Player player = gameWorld.Find("Cursor") as Player; TimerGameObject time = gameWorld.Find("Timer") as TimerGameObject; HitCounter hitCount = gameWorld.Find("HitCounter") as HitCounter; while (client.Inbox.Count != 0) //zolang er berichten zijn { player2.Visible = true; string msg = client.Inbox.Dequeue(); //haal bericht uit mailbox if (msg.Contains("TARGETOK")) { duck.DuckDeath(); duck.DuckAlive = false; duck.DuckMoving = false; duck.Instance.Pause(); } else { if (msg.Contains("DUCK")) { int xposlength = msg.IndexOf(',') - 5; int yposfirstindex = msg.IndexOf(',') + 1; int yposlength = msg.IndexOf(';') - yposfirstindex; int xvelfirstindex = msg.IndexOf(';') + 1; int xvellength = msg.LastIndexOf(',') - xvelfirstindex; int yvelfirstindex = msg.LastIndexOf(',') + 1; int yvellength = msg.Length - yvelfirstindex; duckxpos = float.Parse(msg.Substring(5, xposlength)); duckypos = float.Parse(msg.Substring(yposfirstindex, yposlength)); duckxvel = float.Parse(msg.Substring(xvelfirstindex, xvellength)); duckyvel = float.Parse(msg.Substring(yvelfirstindex, yvellength)); } if (msg.Contains("HIT")) { ServerHits = msg.Substring(4, msg.Length - 4); hitCount.HitCount = int.Parse(ServerHits); } if (msg.Contains("RESPAWN")) { duck.Reset2(gameWorld); string respawnOK = "RESPAWNOK"; { client.sendMessage(respawnOK); } } if (msg.Contains("TIME")) { if (msg.Contains("00:10")) time.TextColor = Color.Red; if (msg.Contains("00:08")) time.TextColor = Color.Red; if (msg.Contains("00:06")) time.TextColor = Color.Red; if (msg.Contains("00:04")) time.TextColor = Color.Red; if (msg.Contains("00:02")) time.TextColor = Color.Red; if (msg.Contains("00:00")) time.TextColor = Color.Red; if (msg.Contains("00:09")) time.TextColor = Color.Yellow; if (msg.Contains("00:07")) time.TextColor = Color.Yellow; if (msg.Contains("00:05")) time.TextColor = Color.Yellow; if (msg.Contains("00:03")) time.TextColor = Color.Yellow; if (msg.Contains("00:01")) time.TextColor = Color.Yellow; Timertext = msg.Substring(5, msg.Length - 5); } if (msg.Contains("GAMEOVER")) { time.TIMELEFT = TimeSpan.FromMinutes(0.0); } if (msg.Contains("START")) { currentLevel.CurrentState = GameState.Playing; duck.Reset2(gameWorld); time.Reset(); hitCount.HitCount = 0; //resets the hitcounter, so it won't be the same after you rejoin. hitCount2.HitCount = 0; string started = "STARTED"; client.sendMessage(started); } if (msg.Contains("CLIENTOK")) { SpriteGameObject waiting = gameWorld.Find("waiting") as SpriteGameObject; duck.Reset2(gameWorld); client.sendMessage("CLIENTOK"); } if (msg.Contains("PLAYER")) { int length = msg.IndexOf(',') - 7; int yposfirstindex = msg.IndexOf(',') + 1; int yposlength = msg.IndexOf(';') - yposfirstindex; posxserver = float.Parse(msg.Substring(7, length)); posyserver = float.Parse(msg.Substring(yposfirstindex, yposlength)); } } } player.Position = new Vector2(posxserver, posyserver); if (duck.DuckAlive) { duck.Position = new Vector2(duckxpos, duckypos); duck.Velocity = new Vector2(duckxvel, duckyvel); } time.Left = Timertext; } protected override void Draw(GameTime gameTime) { spriteBatch.Begin(); GraphicsDevice.Clear(Color.Black); switch (gmHandler.CurrentGameMode) { case GameModeHandler.GameMode.Menu: menu.Draw(gameTime, spriteBatch); break; case GameModeHandler.GameMode.IP: ipClass.Draw(gameTime, spriteBatch); break; case GameModeHandler.GameMode.HighScore: highScore.Draw(gameTime, spriteBatch); break; case GameModeHandler.GameMode.Host: case GameModeHandler.GameMode.Single: case GameModeHandler.GameMode.Join: currentLevel.Draw(gameTime, spriteBatch); break; } spriteBatch.End(); } }