private void DrawGameOverScreen() { GraphicsDevice.Clear(Color.Navy); spriteBatch.Begin(); string gameover = "Game over!\n"; foreach (NetworkGamer gamer in networkSession.AllGamers) { UserControlledSprite sprite = (UserControlledSprite)gamer.Tag; if (!sprite.isChasing) { gameover += "Score: " + sprite.score.ToString(); } } gameover += "\nPress Enter to Switch players and Play Again"; gameover += "\nPress Escape to exit to Game Lobby"; spriteBatch.DrawString(scoreFont, gameover, new Vector2((Window.ClientBounds.Width / 2) - (scoreFont.MeasureString(gameover).X / 2), (Window.ClientBounds.Height / 2) - (scoreFont.MeasureString(gameover).Y / 2)), Color.WhiteSmoke); spriteBatch.End(); }
protected void AddBomb(Vector2 position) { UserControlledSprite newBomb = new UserControlledSprite(Content.Load <Texture2D>(@"Images/bomb"), position, new Point(75, 75), 10, new Point(0, 0), new Point(6, 8), Vector2.Zero, false); bombList.Add(newBomb); bombCoolDown = 5000; }
protected void EffectExpired(GameTime gameTime) { bombEffectCoolDown -= gameTime.ElapsedGameTime.Milliseconds; if (bombEffectCoolDown <= 0) { NetworkGamer chaser = GetChaser(); UserControlledSprite realchaser = (UserControlledSprite)chaser.Tag; realchaser.speed = realchaser.originalSpeed; } }
protected void HitBomb(int index) { bombList.Remove(bombList[index]); bombEffectCoolDown = 5000; NetworkGamer chaser = GetChaser(); UserControlledSprite realchaser = (UserControlledSprite)chaser.Tag; realchaser.speed *= .5f; }
protected void Update_InGame(GameTime gameTime) { //will update your own player, must also send updates to other player too UpdateLocalPlayer(gameTime); //will update the other player ProcessIncomingData(gameTime); //you only want the host checking for collisions if (networkSession.IsHost) { //why check for collisions when you dont have >1 gamer :O if (networkSession.AllGamers.Count == 2) { UserControlledSprite sprite1 = (UserControlledSprite)networkSession.AllGamers[0].Tag; UserControlledSprite sprite2 = (UserControlledSprite)networkSession.AllGamers[1].Tag; //collision if (sprite1.collisionRect.Intersects(sprite2.collisionRect)) { //dont set enum GameState, just EndGame() since the method already does the enum stuff //you want to make sure the other player knows its game over though writer.Write((int)MessageType.EndGame); //reliable because you need it to go through networkSession.LocalGamers[0].SendData(writer, SendDataOptions.Reliable); EndGame(); } NetworkGamer chaser = GetChaser(); UserControlledSprite realchaser = (UserControlledSprite)chaser.Tag; for (int i = 0; i < bombList.Count; i++) { UserControlledSprite bomb = bombList[i]; if (realchaser.collisionRect.Intersects(bomb.collisionRect)) { HitBomb(i); writer.Write((int)MessageType.HitBomb); writer.Write(i); networkSession.LocalGamers[0].SendData(writer, SendDataOptions.InOrder); } } } } EffectExpired(gameTime); }
protected void UpdateLocalPlayer(GameTime gameTime) { //you know this will def be the local gamer as there is only 1 allowed LocalNetworkGamer localGamer = networkSession.LocalGamers[0]; //gotta get the sprite to modify it UserControlledSprite localSprite = (UserControlledSprite)localGamer.Tag; //now actually update sprite regualrly //moving is true because this is just you controlling it localSprite.Update(gameTime, Window.ClientBounds, true); if (!localSprite.isChasing) { localSprite.score += gameTime.ElapsedGameTime.Milliseconds; } writer.Write((int)MessageType.UpdatePlayerPos); writer.Write(localSprite.Position); if (!localSprite.isChasing) { writer.Write(localSprite.score); } //simple sprites may not send data, you must use the localnetworkgamer object //the options is InOrder because its ok if the packets dont get there, but they at least must be in order localGamer.SendData(writer, SendDataOptions.ReliableInOrder); //if your local sprite is a chaser if (localSprite.isChasing) { //if you're able to drop a bomb if (bombCoolDown <= 0) { //and if you press space if (Keyboard.GetState().IsKeyDown(Keys.Space)) { AddBomb(localSprite.Position); writer.Write((int)MessageType.AddBomb); writer.Write(localSprite.Position); localGamer.SendData(writer, SendDataOptions.ReliableInOrder); } } } }
protected void UpdateOtherPlayer(GameTime gameTime) { NetworkGamer otherPlayer = GetOtherPlayer(); UserControlledSprite otherSprite = (UserControlledSprite)otherPlayer.Tag; //you will always need the packet reader to read a vector or int to update a remote player from another comp Vector2 otherPosition = reader.ReadVector2(); otherSprite.Position = otherPosition; if (!otherSprite.isChasing) { int score = reader.ReadInt32(); otherSprite.score = score; } //set moving to false because you dont want to move it, you already set position, only animate frame otherSprite.Update(gameTime, Window.ClientBounds, false); }
private void DrawInGameScreen(GameTime gameTime) { GraphicsDevice.Clear(Color.White); spriteBatch.Begin(); foreach (NetworkGamer gamer in networkSession.AllGamers) { UserControlledSprite sprite = (UserControlledSprite)gamer.Tag; sprite.Draw(gameTime, spriteBatch); if (!sprite.isChasing) { string text = "Score: " + sprite.score.ToString(); spriteBatch.DrawString(scoreFont, text, new Vector2(10, 10), Color.SaddleBrown); } } foreach (UserControlledSprite bomb in bombList) { bomb.Draw(gameTime, spriteBatch); } spriteBatch.End(); }