private void ProcessData(byte[] data) { readStream.SetLength(0); readStream.Position = 0;//reset data readStream.Write(data, 0, data.Length);//read data readStream.Position = 0; Protocol p; while (readStream.Position != data.Length) { try { p = (Protocol)reader.ReadByte(); /*if (GameScreen.isExiting) { System.Diagnostics.Debug.WriteLine }*/ if (p == Protocol.Initialize) { id = reader.ReadByte(); //if (id == 0) // terrain.CreateRandomTerrain(new Vector2(0, 0)); } else if (p == Protocol.Connected) { lock (client.GetStream()) { terrainMaster = true; foreach (byte otherID in playerMap.Keys) { if (otherID < this.id) { terrainMaster = false; break; } } //pindex++; System.Diagnostics.Debug.WriteLine(pindex); byte id = reader.ReadByte(); string ip = reader.ReadString(); ScreenManager.CharacterColor = Color.White; CompositeCharacter newPlayer = new CompositeCharacter(world, new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2.0f, ScreenManager.GraphicsDevice.Viewport.Height / 2.0f), Content.Load<Texture2D>("bean_ss1"), new Vector2(35.0f, 50.0f), ScreenManager.CharacterColor); newPlayer.body.IgnoreGravity = true; newPlayer.wheel.IgnoreGravity = true; newPlayer.body.CollidesWith = Category.Cat24; newPlayer.wheel.CollidesWith = Category.Cat24; playerMap.Add(id, newPlayer); writeStream.Position = 0; writer.Write((byte)Protocol.AddCharacter); writer.Write((byte)playerMap.Count); SendData(GetDataFromMemoryStream(writeStream)); try { if (terrainMaster) { writeStream.Position = 0; writer.Write((byte)Protocol.CreateTerrain); writer.Write(terrain.seed1); writer.Write(terrain.seed2); writer.Write(terrain.seed3); SendData(GetDataFromMemoryStream(writeStream)); } } catch (Exception e) { } } } else if (p == Protocol.Disconnected) { //pindex--; //System.Diagnostics.Debug.WriteLine(pindex); byte id = reader.ReadByte(); string ip = reader.ReadString(); playerMap[id].Dispose(); playerMap.Remove(id); } else if (p == Protocol.Movement) { float px = reader.ReadSingle(); float py = reader.ReadSingle(); bool dead = reader.ReadBoolean(); Character.CharDirection dir = (Character.CharDirection)reader.ReadByte(); Character.CharState state = (Character.CharState)reader.ReadByte(); byte id = reader.ReadByte(); string ip = reader.ReadString(); playerMap[id].Dead = dead; playerMap[id].Direction = dir; playerMap[id].State = state; playerMap[id].Position = new Vector2(px, py); } else if (p == Protocol.AddCharacter) { byte numPlayers = reader.ReadByte(); byte id = reader.ReadByte(); string ip = reader.ReadString(); CompositeCharacter newPlayer; if (numPlayers != playerMap.Count) { newPlayer = new CompositeCharacter(world, new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2.0f, ScreenManager.GraphicsDevice.Viewport.Height / 2.0f), Content.Load<Texture2D>("bean_ss1"), new Vector2(35.0f, 50.0f), ScreenManager.CharacterColor); newPlayer.body.IgnoreGravity = true; newPlayer.wheel.IgnoreGravity = true; newPlayer.body.CollidesWith = Category.Cat24; newPlayer.wheel.CollidesWith = Category.Cat24; playerMap.Add(id, newPlayer); } } else if (p == Protocol.CreateTerrain) { if (!terrain.Created) { double s1 = reader.ReadDouble(); double s2 = reader.ReadDouble(); double s3 = reader.ReadDouble(); byte id = reader.ReadByte(); string ip = reader.ReadString(); terrain.CreateTerrain(new Vector2(0, 0), s1, s2, s3); } } else if (p == Protocol.FireProjectile) { byte color = reader.ReadByte(); byte pId = reader.ReadByte(); float cx = reader.ReadSingle(); float cy = reader.ReadSingle(); float px = reader.ReadSingle(); float py = reader.ReadSingle(); byte id = reader.ReadByte(); string ip = reader.ReadString(); Projectile projectile; switch (color) { case 0: projectile = new YellowProjectile(world, new Vector2(px, py), projectileTexYellow, new Vector2(10f, 10f), new Vector2(cx, cy), playerMap[id]); break; case 1: projectile = new RedProjectile(world, new Vector2(px, py), projectileTexRed, new Vector2(10f, 10f), new Vector2(cx, cy), playerMap[id]); break; case 2: projectile = new BlueProjectile(world, new Vector2(px, py), projectileTexBlue, new Vector2(10f, 10f), new Vector2(cx, cy), playerMap[id]); break; default: projectile = new Projectile(world, new Vector2(px, py), projectileTexYellow, new Vector2(10f, 10f), new Vector2(cx, cy), playerMap[id], null); break; } projectile.id = ((id+1) * 100) + pId; projectile.body.CollidesWith = Category.None; projectiles.Add(projectile); } else if (p == Protocol.ModifyTerrain) { byte pId = reader.ReadByte(); float px = reader.ReadSingle(); float py = reader.ReadSingle(); byte id = reader.ReadByte(); string ip = reader.ReadString(); for (int i = 0; i < projectiles.Count; i++ ) { if (projectiles[i].id == ((id + 1) * 100) + pId) { CheckCollision(projectiles[i]); projectiles.RemoveAt(i); } } } oldP = p; } catch (Exception e) { } } }
private void SendModifyTerrainMessage(Projectile p) { writeStream.Position = 0; writer.Write((byte)Protocol.ModifyTerrain); writer.Write((byte)p.id); writer.Write(p.Position.X); writer.Write(p.Position.Y); lock (client.GetStream()) { SendData(GetDataFromMemoryStream(writeStream)); } CheckCollision(p); projectiles.Remove(p); }
private void CheckCollision(Projectile p) { DrawCircleOnMap(p.body.Position, p.level, 1); LaunchPlayer(player, p.Position, ConvertUnits.ToDisplayUnits(p.level), p.damage); terrain.RegenerateTerrain(); explosions.Add(new Explosion(explosionTex, p.level, p.Position, p.color)); }