//This method implements a certain action to communicate internal static void DoAction(byte[] data) { switch (data[0]) { case 0: string s = Encoding.ASCII.GetString(data, 1, data.Length - 1); NetworkedCameraFrame cameraFrame = NetworkedCameraFrame.Deserialize(s); currentGame.UpdateFrame(cameraFrame); break; case 1: reporter.GameOver(); break; case 2: string str = Encoding.ASCII.GetString(data, 1, data.Length - 1); reporter.DisplayStats(GameStats.Deserialize(str)); IsListening = true; break; case 3: reporter.DisplaySecondsTillStart( Convert.ToInt32( Encoding.ASCII.GetString(data, 1, data.Length - 1))); break; } }
public void Render(object sender, NetworkedCameraFrame currentFrame) { Dispatcher.Invoke(() => { if (!gameStarted) { gameStarted = true; DrawCanvas.Children.Remove(grid_secondsLeft); SetupGameWindow(); Render(null, currentFrame); } else { for (int i = 0; i < 4; ++i) { Canvas.SetLeft(planets[i], currentFrame.Backgrounds[3][i].Item1); Canvas.SetTop(planets[i], currentFrame.Backgrounds[3][i].Item2); Canvas.SetLeft(rings[i], currentFrame.Backgrounds[2][i].Item1); Canvas.SetTop(rings[i], currentFrame.Backgrounds[2][i].Item2); Canvas.SetLeft(twins[i], currentFrame.Backgrounds[1][i].Item1); Canvas.SetTop(twins[i], currentFrame.Backgrounds[1][i].Item2); Canvas.SetLeft(stars[i], currentFrame.Backgrounds[0][i].Item1); Canvas.SetTop(stars[i], currentFrame.Backgrounds[0][i].Item2); } int sLeft = 300 - (int)currentFrame.Seconds; txtTimeLeft.Text = (sLeft / 60) + ":" + (sLeft % 60).ToString("D2"); int playerdiff = OtherHumanImages.Count - currentFrame.OtherHumanShips.Count; if (playerdiff > 0) { RemoveGameObjects(OtherHumanImages, playerdiff); RemoveGameObjects(OtherHumanNames, playerdiff); } if (playerdiff < 0) { AddGameObjects(OtherHumanImages, -playerdiff); AddGameObjects(OtherHumanNames, -playerdiff); } for (int i = 0; i < OtherHumanImages.Count; ++i) { OtherHumanImages[i].Source = shipImage; OtherHumanImages[i].Width = 50; Canvas.SetLeft(OtherHumanImages[i], currentFrame.OtherHumanShips[i].Item1); Canvas.SetTop(OtherHumanImages[i], currentFrame.OtherHumanShips[i].Item2); Canvas.SetLeft(OtherHumanNames[i], currentFrame.OtherHumanShips[i].Item1); Canvas.SetTop(OtherHumanNames[i], currentFrame.OtherHumanShips[i].Item2 - 30); OtherHumanNames[i].Content = currentFrame.OtherHumanShips[i].Item3; Canvas.SetZIndex(OtherHumanNames[i], 7); Canvas.SetZIndex(OtherHumanImages[i], 7); } int wellDiff = wellDict.Count - currentFrame.StableWells.Count; if (wellDiff > 0) { RemoveGameObjects(wellDict, wellDiff); } if (wellDiff < 0) { AddGameObjects(wellDict, -wellDiff); } for (int i = 0; i < wellDict.Count; ++i) { int color = currentFrame.StableWells[i].Item3; wellDict[i].Source = wellImages[color]; Canvas.SetLeft(wellDict[i], currentFrame.StableWells[i].Item1); Canvas.SetTop(wellDict[i], currentFrame.StableWells[i].Item2); Canvas.SetZIndex(wellDict[i], 5); } int destableDiff = destableDict.Count - currentFrame.UnstableWells.Count; if (destableDiff > 0) { RemoveGameObjects(destableDict, destableDiff); } if (destableDiff < 0) { AddGameObjects(destableDict, -destableDiff); } for (int i = 0; i < destableDict.Count; ++i) { destableDict[i].Source = destabilizedImage; Canvas.SetLeft(destableDict[i], currentFrame.UnstableWells[i].Item1); Canvas.SetTop(destableDict[i], currentFrame.UnstableWells[i].Item2); Canvas.SetZIndex(destableDict[i], 6); } int orbDiff = orbDict.Count - currentFrame.Orbs.Count; if (orbDiff > 0) { RemoveGameObjects(orbDict, orbDiff); } if (orbDiff < 0) { AddGameObjects(orbDict, -orbDiff); } for (int i = 0; i < orbDict.Count; ++i) { int color = currentFrame.Orbs[i].Item3; orbDict[i].Source = orbImages[color]; Canvas.SetLeft(orbDict[i], currentFrame.Orbs[i].Item1); Canvas.SetTop(orbDict[i], currentFrame.Orbs[i].Item2); Canvas.SetZIndex(orbDict[i], 7); } Canvas.SetLeft(ship, currentFrame.PlayerShip.Item1); Canvas.SetTop(ship, currentFrame.PlayerShip.Item2); Canvas.SetZIndex(ship, 10); txtScore.Text = "Score: " + currentFrame.Points; //to be implemented with AI int shipDiff = AiImages.Count - currentFrame.AIShips.Count; if (shipDiff > 0) { RemoveGameObjects(AiImages, shipDiff); } if (shipDiff < 0) { AddGameObjects(AiImages, -shipDiff); } for (int i = 0; i < AiImages.Count; ++i) { AiImages[i].Source = AiImage; AiImages[i].Width = 50; Canvas.SetLeft(AiImages[i], currentFrame.AIShips[i].Item1); Canvas.SetTop(AiImages[i], currentFrame.AIShips[i].Item2); Canvas.SetZIndex(AiImages[i], 9); } //========================== //HUD //========================== if (!Enumerable.SequenceEqual(currentOrbs, currentFrame.PlayerOrbs)) { UpdateHudOrbs(currentFrame.PlayerOrbs); currentOrbs = currentFrame.PlayerOrbs.ToList(); } UpdateHudPowerups(currentFrame.HasNeutralizePowerup, currentFrame.HasDestabilizePowerup, currentFrame.HasGhostingPowerup); } }); }
//This method emits an event representing a frame update public void UpdateFrame(NetworkedCameraFrame ncf) { GameUpdatedEvent(this, ncf); }
//This method implements the logic to deserialize a camera frame string and return a NetworkedCameraFrame object. public static NetworkedCameraFrame Deserialize(string data) { string[] parts = data.Split(' '); NetworkedCameraFrame newFrame = new NetworkedCameraFrame(); newFrame.ScreenX = Convert.ToDouble(parts[0]); newFrame.ScreenY = Convert.ToDouble(parts[1]); newFrame.PlayerShip = Tuple.Create <double, double>(newFrame.ScreenX, newFrame.ScreenY); double changeX = Convert.ToDouble(parts[2]); double changeY = Convert.ToDouble(parts[3]); var Backgrounds = new List <Tuple <double, double> > [4]; for (int i = 0; i < 4; i++) { BackgroundXY[i, 0] = (BackgroundXY[i, 0] - changeX * (0.04 + 0.09 * i) + Width * (1 + 0.2 * i)) % (Width * (1 + 0.2 * i)); BackgroundXY[i, 1] = (BackgroundXY[i, 1] - changeY * (0.04 + 0.09 * i) + Height * (1 + 0.2 * i)) % (Height * (1 + 0.2 * i)); Backgrounds[i] = new List <Tuple <double, double> >(); for (int j = 0; j < 4; j++) { Backgrounds[i].Add(Tuple.Create(BackgroundXY[i, 0] - Width * (1 + 0.2 * i) * (j / 2), BackgroundXY[i, 1] - Height * (1 + 0.2 * i) * (j % 2))); } } newFrame.Backgrounds = Backgrounds; string[] wells = parts[4].Split('|'); if (wells.Length > 0) { for (int i = 0; i < wells.Length; i++) { string[] wellParts = wells[i].Split(','); if (wellParts.Length == 3) { newFrame.StableWells.Add(Tuple.Create( Convert.ToDouble(wellParts[0]), Convert.ToDouble(wellParts[1]), Convert.ToInt32(wellParts[2]))); } } } string[] unstableWells = parts[5].Split('|'); if (unstableWells.Length > 0) { for (int i = 0; i < unstableWells.Length; i++) { string[] wellParts = unstableWells[i].Split(','); if (wellParts.Length == 2) { newFrame.UnstableWells.Add(Tuple.Create( Convert.ToDouble(wellParts[0]), Convert.ToDouble(wellParts[1]))); } } } string[] orbs = parts[6].Split('|'); if (orbs.Length > 0) { for (int i = 0; i < orbs.Length; i++) { string[] wellParts = orbs[i].Split(','); if (wellParts.Length == 3) { newFrame.Orbs.Add(Tuple.Create( Convert.ToDouble(wellParts[0]), Convert.ToDouble(wellParts[1]), Convert.ToInt32(wellParts[2]))); } } } string[] playerOrbs = parts[7].Split('|'); for (int i = 0; i < playerOrbs.Length; i++) { if (!string.IsNullOrEmpty(playerOrbs[i])) { newFrame.PlayerOrbs.Add(Convert.ToInt32(playerOrbs[i])); } } string[] aiShips = parts[8].Split('|'); for (int i = 0; i < aiShips.Length; i++) { string[] wellParts = aiShips[i].Split(','); if (wellParts.Length == 2) { newFrame.AIShips.Add(Tuple.Create( Convert.ToDouble(wellParts[0]), Convert.ToDouble(wellParts[1]))); } } string[] PlayerShips = parts[9].Split('|'); for (int i = 0; i < PlayerShips.Length; i++) { string[] wellParts = PlayerShips[i].Split(','); if (wellParts.Length == 3) { newFrame.OtherHumanShips.Add(Tuple.Create( Convert.ToDouble(wellParts[0]), Convert.ToDouble(wellParts[1]), wellParts[2])); } } newFrame.Seconds = Convert.ToInt32(parts[10]); newFrame.SecondsLeft = Convert.ToInt32(parts[11]); newFrame.Points = Convert.ToInt32(parts[12]); newFrame.HasGhostingPowerup = (parts[13] == "t"); newFrame.HasNeutralizePowerup = (parts[14] == "t"); newFrame.HasDestabilizePowerup = (parts[15] == "t"); return(newFrame); }