public ShapeTeamContainer(ShapeTeam team) { this.team = team; if (GetTeam() == ShapeTeam.GREEN) basePosition = new Vector2(-1000, 0); else basePosition = new Vector2(1000, 0); }
public ShapeTeamContainer(ShapeTeam team) { this.team = team; if (GetTeam() == ShapeTeam.GREEN) { basePosition = new Vector2(-1000, 0); } else { basePosition = new Vector2(1000, 0); } }
/// <summary> /// Assigns the player to a team and handles setting the appropriate color and so on. /// </summary> /// <param name="team"></param> public void SetTeam(ShapeTeam team) { this.team = team; switch (team) { case ShapeTeam.GREEN: color = Color.ForestGreen; break; case ShapeTeam.RED: color = Color.DarkRed; break; } }
void HandleClientMessages(object peer) { NetIncomingMessage msg; while ((msg = client.ReadMessage()) != null) { switch (msg.MessageType) { case NetIncomingMessageType.Data: switch ((ShapeCustomNetMessageType)msg.ReadByte()) { //Contains the current locations of the players on the server case ShapeCustomNetMessageType.LocationUpdate: int numOfPlayers = msg.ReadInt32(); for (int i = 0; i < numOfPlayers; i++) { int index = msg.ReadInt32(); float powr = msg.ReadFloat(); float time = msg.ReadFloat(); Vector2 pos = msg.ReadVector2(); if (index == player.indexOnServer) { player.power = powr; player.positions.Add(new PositionInTime(time, pos, false)); } else if (playersOnSameServer[index] != null) { playersOnSameServer[index].power = powr; playersOnSameServer[index].positions.Add(new PositionInTime(time, pos, false)); } int numTrail = msg.ReadInt32(); for (int j = 0; j < numTrail; j++) { Vector2 position = msg.ReadVector2(); float size = msg.ReadFloat(); if (index == player.indexOnServer) { if (j < player.trail.Count) { if (player.trail[j] != null) { player.trail[j].position = position; player.trail[j].size = size; player.trail[j].lastUpdatedValues = System.Environment.TickCount - lastReceivedLocationUpdate; } if (numTrail < player.trail.Count) { player.trail.RemoveRange(numTrail - 1, player.trail.Count - numTrail); } } else { Trail t = new Trail(position, size, Color.Beige, spriteBatch.GraphicsDevice, null); t.lastUpdatedValues = System.Environment.TickCount - lastReceivedLocationUpdate; player.trail.Add(t); } } else if (playersOnSameServer[index] != null) { if (j < playersOnSameServer[index].trail.Count) { if (playersOnSameServer[index].trail[j] != null) { playersOnSameServer[index].trail[j].position = position; playersOnSameServer[index].trail[j].size = size; playersOnSameServer[index].trail[j].lastUpdatedValues = System.Environment.TickCount - lastReceivedLocationUpdate; } if (numTrail < playersOnSameServer[index].trail.Count) { playersOnSameServer[index].trail.RemoveRange(numTrail - 1, playersOnSameServer[index].trail.Count - numTrail); } } else { Trail t = new Trail(position, size, Color.Beige, spriteBatch.GraphicsDevice, null); t.lastUpdatedValues = System.Environment.TickCount - lastReceivedLocationUpdate; playersOnSameServer[index].trail.Add(t); } } } } int numRemnants = msg.ReadInt32(); for (int i = 0; i < numRemnants; i++) { Vector2 pos = msg.ReadVector2(); float size = msg.ReadFloat(); if (i < remnants.Count) { remnants[i].position = pos; remnants[i].size = size; remnants[i].lastUpdatedValues = System.Environment.TickCount - lastReceivedLocationUpdate; } else { CollisionRemnant newRemnant = new CollisionRemnant(pos, size, Color.GhostWhite, spriteBatch.GraphicsDevice, null); remnants.Add(newRemnant); } if (numRemnants < remnants.Count) { remnants.RemoveRange(numRemnants - 1, remnants.Count - numRemnants); } } lastReceivedLocationUpdate = System.Environment.TickCount; break; //A new player has joined the server which has to be added to this client case ShapeCustomNetMessageType.NewPlayerJoined: int indexOnServer = msg.ReadInt32(); ShapeTeam team = (ShapeTeam)msg.ReadByte(); int power = msg.ReadInt32(); Player newPlayer = new Player(spriteBatch.GraphicsDevice); newPlayer.indexOnServer = indexOnServer; newPlayer.SetTeam(team); newPlayer.power = power; newPlayer.LoadContent(cManager); playersOnSameServer[indexOnServer] = newPlayer; //MessageBox.Show("New player jioned"); break; case ShapeCustomNetMessageType.SetupSuccessful: //Have our index on server handed to us player.indexOnServer = msg.ReadInt32(); player.SetTeam((ShapeTeam)msg.ReadByte()); int numOfPlayers1 = msg.ReadInt32(); for (int i = 0; i < numOfPlayers1; i++) { Player p = new Player(spriteBatch.GraphicsDevice); int index = msg.ReadInt32(); p.indexOnServer = index; p.SetTeam((ShapeTeam)msg.ReadByte()); int powaar = msg.ReadInt32(); //p.power = powaar; p.LoadContent(cManager); playersOnSameServer[index] = p; } break; case ShapeCustomNetMessageType.SetupFailed: MessageBox.Show(msg.ReadString()); break; } break; case NetIncomingMessageType.DiscoveryResponse: //MessageBox.Show(msg.ReadString() + " | " + msg.ReadIPEndPoint() + " | " + msg.SenderEndPoint); if (client.GetConnection(msg.SenderEndPoint) == null) { ConnectToServer(msg.SenderEndPoint.Address.ToString(), 55678); } break; case NetIncomingMessageType.StatusChanged: switch ((NetConnectionStatus)msg.ReadByte()) { //When connected to the server case NetConnectionStatus.Connected: //1. Handle hailmessage containing server info int maxPlayersOnServer = msg.SenderConnection.RemoteHailMessage.ReadInt32(); playersOnSameServer = new Player[maxPlayersOnServer]; //2. Send client info NetOutgoingMessage outMsg = client.CreateMessage(); outMsg.Write((byte)ShapeCustomNetMessageType.SetupRequest); //outMsg.Write((byte)ShapeTeam.BLUE); outMsg.Write("UserName"); client.SendMessage(outMsg, NetDeliveryMethod.ReliableOrdered); break; //When disconnected from the server case NetConnectionStatus.Disconnected: //Contains a string of the reason for the disconnection string reason = msg.ReadString(); if (string.IsNullOrEmpty(reason)) { MessageBox.Show("Disconnected! Reason unknown."); } else { MessageBox.Show("Disconnected, Reason: " + reason); } HandleDisconnection(); break; } break; default: //Console.WriteLine("Unhandled type: " + msg.MessageType); break; } client.Recycle(msg); } }