private async void toolStripDropDownButton1_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) { OnlineState state = (OnlineState)Convert.ToInt32(e.ClickedItem.Tag); lbStatus.Text = state.ToString(); tmrTriggerRefresh.Start(); switch (state) { case OnlineState.Online: await DMLClient.Client.SetStatusAsync(UserStatus.Online); break; case OnlineState.Idle: await DMLClient.Client.SetStatusAsync(UserStatus.Idle); break; case OnlineState.DoNotDisturb: await DMLClient.Client.SetStatusAsync(UserStatus.DoNotDisturb); break; case OnlineState.Invisible: await DMLClient.Client.SetStatusAsync(UserStatus.Invisible); break; } }
protected override void Update(GameTime gameTime) { Global.gameTime = gameTime; //assiging the global gametime with the main game instance gametime to use in other classes switch (currentGameState) //switching between the game states { case GameState.MainMenu: if (!menuSongPlaying) //making sure that the menu song is only played once { MediaPlayer.Play(menuSong); menuSongPlaying = true; MediaPlayer.IsRepeating = true; } //getting the mouse coordinates mouse = Mouse.GetState(); mouseRec.X = mouse.X; mouseRec.Y = mouse.Y; //switching states according to the button the was pressed if (pvpButton.isClicked) { currentGameState = GameState.Playing1v1; } if (onlineButton.isClicked) { currentGameState = GameState.PlayingOnline; } if (aiButton.isClicked) { currentGameState = GameState.PlayingVsAI; } if (instructionsButton.isClicked) { currentGameState = GameState.Instructions; } break; case GameState.Playing1v1: //when enter is played, play the chant and allow the players to move if (Keyboard.GetState().IsKeyDown(Keys.Enter) && !player1.isInputAllowed && !player2.isInputAllowed) { MediaPlayer.IsRepeating = false; player1.isInputAllowed = true; player2.isInputAllowed = true; gameStarted = true; didChantPlay = true; MediaPlayer.Play(fight); } //when the chant is played, use the timer to wait until its over and then play the battle song if (didChantPlay) { battleSongTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; if (battleSongTimer > fightChantRate) { didChantPlay = false; // we turn this to false because we dont want to enter this again, even though the chant already played battleSongTimer = 0; MediaPlayer.Play(battleSong); MediaPlayer.IsRepeating = true; } } //if the players bump into each other we only allow them to move away from each other, unless one of them is above the other if (player1.Position.X - player2.Position.X <= minDistance && player1.Position.X - player2.Position.X > 0 && Math.Abs(player1.Position.Y - player2.Position.Y) < 125) { player1.isMovementAllowedRight = true; player1.isMovementAllowedLeft = false; player2.isMovementAllowedRight = false; player2.isMovementAllowedLeft = true; } //if the players bump into each other we only allow them to move away from each other, unless one of them is above the other else if (player2.Position.X - player1.Position.X <= minDistance && player2.Position.X - player1.Position.X > 0 && Math.Abs(player1.Position.Y - player2.Position.Y) < 125) { player2.isMovementAllowedRight = true; player2.isMovementAllowedLeft = false; player1.isMovementAllowedRight = false; player1.isMovementAllowedLeft = true; } //if the players have not reached the min distance from each other we allow them to move freely else { player1.isMovementAllowedRight = true; player1.isMovementAllowedLeft = true; player2.isMovementAllowedRight = true; player2.isMovementAllowedLeft = true; } //if a player lands on another player this part of the code makes sure to push them apart (each one gets pushed 50 pixels to the back) if (Math.Abs(player1.Position.X - player2.Position.X) < minDistance - 10 && Math.Abs(player1.Position.Y - player2.Position.Y) < 125) { if (player1.Position.X < player2.Position.X) { player1.Position -= new Vector2(50, 0); player2.Position += new Vector2(50, 0); } else { player1.Position += new Vector2(50, 0); player2.Position -= new Vector2(50, 0); } } //aligning the player indicator with the player position p1BannerPos = new Vector2(player1.Position.X, player1.Position.Y - 140); p2BannerPos = new Vector2(player2.Position.X, player2.Position.Y - 140); p1Indicator.Position = p1BannerPos; p2Indicator.Position = p2BannerPos; //changing the directions to which the characters face according to the location of their enemy if (player1.Position.X < player2.Position.X) { player2.flipCharachter("left"); player1.flipCharachter("right"); } else { player1.flipCharachter("left"); player2.flipCharachter("right"); } //if a player has an internal hit registered if (player1.HitCount == p1HitCount + 1) { if (Math.Abs(player1.Position.X - player2.Position.X) < 100 && !player2.isDucking && Math.Abs(player1.Position.Y - player2.Position.Y) < 50) // if the enemy is close enough an is not ducking { p1HitCount++; // change the external hit count p2HealthBar.rectangle.Width -= 250 / maxHitCount; // make the enemy's health bar get smaller accordingly to the max hit count } else // if the enemy is too far away or is ducking { player1.cancelHit(); //cancel the internal hit for the player } } //same process but for the other player if (player2.HitCount == p2HitCount + 1) { if (Math.Abs(player1.Position.X - player2.Position.X) < 100 && !player1.isDucking && Math.Abs(player1.Position.Y - player2.Position.Y) < 50) { p2HitCount++; p1HealthBar.rectangle.Width -= 250 / maxHitCount; } else { player2.cancelHit(); } } //if a player had reached the max hit count if (player1.HitCount == maxHitCount && player1.isInputAllowed && player2.isInputAllowed) { player2.loseSequence(); //make the enemy get into a losing sequence player1.winSequence(); //make the player go into a winning sequence //disallow inputs to keep the characters in place player1.isInputAllowed = false; player2.isInputAllowed = false; //make the health bar of the losing player disappear //this is needed because sometimes the width of the health bar divided by the max hit count does not produce a full number therefore the health bar might not go all the way to 0 when the max hit count is achieved p2HealthBar.rectangle.Width = 0; gameOver = true; //mark the game being over MediaPlayer.Play(winSong); //play the winning song } //same process for the other player if (player2.HitCount == maxHitCount && player1.isInputAllowed && player2.isInputAllowed) { player1.loseSequence(); player2.winSequence(); player1.isInputAllowed = false; player2.isInputAllowed = false; p1HealthBar.rectangle.Width = 0; gameOver = true; MediaPlayer.Play(winSong); } //if in the middle of the game the back to menu button is clicked if (midGameExit.isClicked) { gameOver = false; //mark that the game is in play LoadContent(); //reset all variables currentGameState = GameState.MainMenu; //go back to menu } //if the game is over if (gameOver) { if (resetGame.isClicked) //when the reset button is clicked { gameOver = false; //mark that the game is in play LoadContent(); //reset all variables currentGameState = GameState.MainMenu; //go back to menu } } break; case GameState.PlayingOnline: switch (onlineState) { case OnlineState.AskingRole: if (midGameExit.isClicked) { LoadContent(); currentGameState = GameState.MainMenu; } if (Keyboard.GetState().IsKeyDown(Keys.H)) { onlineGame = new HostOnlineGame(int.Parse(File.ReadAllText("port.txt"))); onlineGame.OnConnection += new OnConnectionHandler(onlineGame_OnConnection); onlineGame.Init(); onlineState = OnlineState.Connecting; } else if (Keyboard.GetState().IsKeyDown(Keys.J)) { onlineGame = new JoinOnlineGame(File.ReadAllText("ip.txt"), int.Parse(File.ReadAllText("port.txt"))); onlineGame.OnConnection += new OnConnectionHandler(onlineGame_OnConnection); onlineGame.Init(); onlineState = OnlineState.Connecting; } break; case OnlineState.Connecting: break; case OnlineState.Playing: onlineGame.hostChar.update(); onlineGame.joinChar.update(); //from here until the break of the online game state the process is the same as in playing1v1 if (onlineGame.hostChar.Position.X - onlineGame.joinChar.Position.X <= minDistance && onlineGame.hostChar.Position.X - onlineGame.joinChar.Position.X > 0 && Math.Abs(onlineGame.hostChar.Position.Y - onlineGame.joinChar.Position.Y) < 125) { onlineGame.hostChar.isMovementAllowedRight = true; onlineGame.hostChar.isMovementAllowedLeft = false; onlineGame.joinChar.isMovementAllowedRight = false; onlineGame.joinChar.isMovementAllowedLeft = true; } else if (onlineGame.joinChar.Position.X - onlineGame.hostChar.Position.X <= minDistance && onlineGame.joinChar.Position.X - onlineGame.hostChar.Position.X > 0 && Math.Abs(onlineGame.hostChar.Position.Y - onlineGame.joinChar.Position.Y) < 125) { onlineGame.joinChar.isMovementAllowedRight = true; onlineGame.joinChar.isMovementAllowedLeft = false; onlineGame.hostChar.isMovementAllowedRight = false; onlineGame.hostChar.isMovementAllowedLeft = true; } else { onlineGame.hostChar.isMovementAllowedRight = true; onlineGame.hostChar.isMovementAllowedLeft = true; onlineGame.joinChar.isMovementAllowedRight = true; onlineGame.joinChar.isMovementAllowedLeft = true; } if (Math.Abs(onlineGame.hostChar.Position.X - onlineGame.joinChar.Position.X) < minDistance - 10 && Math.Abs(onlineGame.hostChar.Position.Y - onlineGame.joinChar.Position.Y) < 125) { if (onlineGame.hostChar.Position.X < onlineGame.joinChar.Position.X) { onlineGame.hostChar.Position -= new Vector2(50, 0); onlineGame.joinChar.Position += new Vector2(50, 0); } else { onlineGame.hostChar.Position += new Vector2(50, 0); onlineGame.joinChar.Position -= new Vector2(50, 0); } } p1BannerPos = new Vector2(onlineGame.hostChar.Position.X, onlineGame.hostChar.Position.Y - 140); p2BannerPos = new Vector2(onlineGame.joinChar.Position.X, onlineGame.joinChar.Position.Y - 140); p1Indicator.Position = p1BannerPos; p2Indicator.Position = p2BannerPos; if (onlineGame.hostChar.Position.X < onlineGame.joinChar.Position.X) { onlineGame.joinChar.flipCharachter("left"); onlineGame.hostChar.flipCharachter("right"); } else { onlineGame.hostChar.flipCharachter("left"); onlineGame.joinChar.flipCharachter("right"); } if (onlineGame.hostChar.HitCount == p1HitCount + 1) { if (Math.Abs(onlineGame.hostChar.Position.X - onlineGame.joinChar.Position.X) < 100 && !onlineGame.joinChar.isDucking && Math.Abs(onlineGame.hostChar.Position.Y - onlineGame.joinChar.Position.Y) < 50) { p1HitCount++; p2HealthBar.rectangle.Width -= 250 / maxHitCount; } else { onlineGame.hostChar.cancelHit(); } } if (onlineGame.joinChar.HitCount == p2HitCount + 1) { if (Math.Abs(onlineGame.hostChar.Position.X - onlineGame.joinChar.Position.X) < 100 && !onlineGame.hostChar.isDucking && Math.Abs(onlineGame.hostChar.Position.Y - onlineGame.joinChar.Position.Y) < 50) { p2HitCount++; p1HealthBar.rectangle.Width -= 250 / maxHitCount; } else { onlineGame.joinChar.cancelHit(); } } if (onlineGame.hostChar.HitCount == maxHitCount && onlineGame.hostChar.isInputAllowed && onlineGame.joinChar.isInputAllowed) { onlineGame.joinChar.loseSequence(); onlineGame.hostChar.winSequence(); onlineGame.hostChar.isInputAllowed = false; onlineGame.joinChar.isInputAllowed = false; p2HealthBar.rectangle.Width = 0; gameOver = true; MediaPlayer.Play(winSong); } if (onlineGame.joinChar.HitCount == maxHitCount && onlineGame.hostChar.isInputAllowed && onlineGame.joinChar.isInputAllowed) { onlineGame.hostChar.loseSequence(); onlineGame.joinChar.winSequence(); onlineGame.hostChar.isInputAllowed = false; onlineGame.joinChar.isInputAllowed = false; p1HealthBar.rectangle.Width = 0; gameOver = true; MediaPlayer.Play(winSong); } if (midGameExit.isClicked) { gameOver = false; LoadContent(); currentGameState = GameState.MainMenu; } if (gameOver) { if (resetGame.isClicked) { gameOver = false; LoadContent(); currentGameState = GameState.MainMenu; } } break; } this.Window.Title = onlineState.ToString(); break; case GameState.PlayingVsAI: //from here until the break it is the same process as in playing1v1 but instead of using player2 for the enemy of player1 we use the ai object if (Keyboard.GetState().IsKeyDown(Keys.Enter) && !player1.isInputAllowed && !ai.isInputAllowed) { MediaPlayer.IsRepeating = false; player1.isInputAllowed = true; ai.isInputAllowed = true; gameStarted = true; didChantPlay = true; MediaPlayer.Play(fight); } if (didChantPlay) { battleSongTimer += (float)gameTime.ElapsedGameTime.TotalSeconds; if (battleSongTimer > fightChantRate) { didChantPlay = false; battleSongTimer = 0; MediaPlayer.Play(battleSong); MediaPlayer.IsRepeating = true; } } if (player1.Position.X - ai.Position.X <= minDistance && player1.Position.X - ai.Position.X > 0 && Math.Abs(player1.Position.Y - ai.Position.Y) < 125) { player1.isMovementAllowedRight = true; player1.isMovementAllowedLeft = false; ai.isMovementAllowedRight = false; ai.isMovementAllowedLeft = true; } else if (ai.Position.X - player1.Position.X <= minDistance && ai.Position.X - player1.Position.X > 0 && Math.Abs(player1.Position.Y - ai.Position.Y) < 125) { ai.isMovementAllowedRight = true; ai.isMovementAllowedLeft = false; player1.isMovementAllowedRight = false; player1.isMovementAllowedLeft = true; } else { player1.isMovementAllowedRight = true; player1.isMovementAllowedLeft = true; ai.isMovementAllowedRight = true; ai.isMovementAllowedLeft = true; } if (Math.Abs(player1.Position.X - ai.Position.X) < minDistance - 10 && Math.Abs(player1.Position.Y - ai.Position.Y) < 125) { if (player1.Position.X < ai.Position.X) { player1.Position -= new Vector2(50, 0); ai.Position += new Vector2(50, 0); } else { player1.Position += new Vector2(50, 0); ai.Position -= new Vector2(50, 0); } } p1BannerPos = new Vector2(player1.Position.X, player1.Position.Y - 140); aiBannerPos = new Vector2(ai.Position.X, ai.Position.Y - 140); p1Indicator.Position = p1BannerPos; p2Indicator.Position = aiBannerPos; if (player1.Position.X < ai.Position.X) { ai.flipCharachter("left"); player1.flipCharachter("right"); } else { player1.flipCharachter("left"); ai.flipCharachter("right"); } if (player1.HitCount == p1HitCount + 1) { if (Math.Abs(player1.Position.X - ai.Position.X) < 100 && !ai.isDucking && Math.Abs(player1.Position.Y - ai.Position.Y) < 50) { p1HitCount++; p2HealthBar.rectangle.Width -= 250 / maxHitCount; } else { player1.cancelHit(); } } if (ai.HitCount == p2HitCount + 1) { if (Math.Abs(player1.Position.X - ai.Position.X) < 100 && !player1.isDucking && Math.Abs(player1.Position.Y - ai.Position.Y) < 50) { p2HitCount++; p1HealthBar.rectangle.Width -= 250 / maxHitCount; } else { ai.cancelHit(); } } if (player1.HitCount == maxHitCount && player1.isInputAllowed && ai.isInputAllowed) { ai.loseSequence(); player1.winSequence(); player1.isInputAllowed = false; ai.isInputAllowed = false; p2HealthBar.rectangle.Width = 0; gameOver = true; MediaPlayer.Play(winSong); } if (ai.HitCount == maxHitCount && player1.isInputAllowed && ai.isInputAllowed) { player1.loseSequence(); ai.winSequence(); player1.isInputAllowed = false; ai.isInputAllowed = false; p1HealthBar.rectangle.Width = 0; gameOver = true; MediaPlayer.Play(winSong); } if (midGameExit.isClicked) { LoadContent(); currentGameState = GameState.MainMenu; } if (gameOver) { if (resetGame.isClicked) { gameOver = false; LoadContent(); currentGameState = GameState.MainMenu; } } break; case GameState.Instructions: if (menuButton.isClicked) { currentGameState = GameState.MainMenu; } break; } if (event_update != null) //if the update event is not empty { event_update(); //call all of the functions that registered to the update event } base.Update(gameTime); }