public void LoadGame_ValidInput_ValidClass() { Game g = new Game(0, false, null, null, 1); Game.map = new Map(0); g.currentWave = 5; g.waveProgress = 3; g.score = 700; Game.money = 200; g.isWaveOver = false; g.spawner = new Spawner(null); Infantry i1 = Infantry.MakeInfantry("b"); i1.posX = 2; i1.posY = 4; i1.health = 13; i1.pathProgress = 3; Vehicle v1 = Vehicle.MakeVehicle("b"); v1.posX = 6; v1.posY = 2; v1.health = 10; v1.pathProgress = 2; Spawner.enemies.Add(i1); Spawner.enemies.Add(v1); g.currentEnemies.Add(i1); g.currentEnemies.Add(v1); Mortar m1 = Mortar.MakeMortar(2, 3, 0); Stun s1 = Stun.MakeStun(7, 5, 0); Tesla t1 = Tesla.MakeTesla(3, 5, 0); g.currentTurrets = new List <Turret>(); g.currentTurrets.Add(m1); g.currentTurrets.Add(s1); g.currentTurrets.Add(t1); Game loadedGame = Game.LoadGame("SavedGame.txt", null, null); // all should be working Assert.AreEqual(g.currentEnemies.Count, loadedGame.currentEnemies.Count); Assert.AreEqual(8, Game.lives); Assert.AreEqual(g.currentWave, loadedGame.currentWave); Assert.AreEqual(200, Game.money); Assert.AreEqual(g.score, loadedGame.score); Assert.AreEqual(g.waveProgress, loadedGame.waveProgress); Assert.AreEqual(g.difficulty, loadedGame.difficulty); Assert.AreEqual(g.isWaveOver, loadedGame.isWaveOver); Assert.AreEqual(g.currentTurrets.Count, loadedGame.currentTurrets.Count); }
// takes the x and y coordinates of its gui counterpart as well as // the gui's list index and returns a default tesla object. public static Tesla MakeTesla(double x, double y, int index) { Tesla t = new Tesla(); t.xPos = x; t.yPos = y; t.imageID = 5; t.imageIndex = index; t.fireRate = 5; t.cost = 175; t.upCost = Convert.ToInt32(t.cost / 2); t.damage = 1; t.range = 100; t.type = "tesla"; return(t); }
public void SaveGame_ValidInput_ValidString() { Game g = new Game(0, false, null, null, 1); g.currentWave = 5; g.waveProgress = 3; Game.money = 200; g.score = 700; g.isWaveOver = false; Game.lives = 8; Game.map = new Map(0); Mortar m1 = Mortar.MakeMortar(2, 3, 0); Stun s1 = Stun.MakeStun(7, 5, 0); Tesla t1 = Tesla.MakeTesla(3, 5, 0); g.currentTurrets = new List <Turret>(); g.currentTurrets.Add(m1); g.currentTurrets.Add(s1); g.currentTurrets.Add(t1); g.spawner = new Spawner(null); Infantry i1 = Infantry.MakeInfantry("b"); i1.posX = 2; i1.posY = 4; i1.health = 13; i1.pathProgress = 3; Vehicle v1 = Vehicle.MakeVehicle("b"); v1.posX = 6; v1.posY = 2; v1.health = 10; v1.pathProgress = 2; g.currentEnemies.Add(i1); g.currentEnemies.Add(v1); Spawner.enemies.Add(i1); Spawner.enemies.Add(v1); g.SaveGame("SavedGame1.txt"); StreamReader reader = new StreamReader("SavedGame1.txt"); string SerializedGame = reader.ReadToEnd(); Assert.AreEqual(SerializedGame, "NG\r\n0,5,3,700,200,20,false,1\r\nENEMIES\r\nbinfantry,2,4,13,3\r\nbvehicle,6,2,10,2\r\nENDENEMIES\r\nTURRETS\r\nmortar,2,3\r\nstun,7,5\r\ntesla,3,5\r\nENDTURRETS\r\nEND\r\n"); reader.Close(); }
// loads a game that is saved in the file named "filename" and returns that game public static Game LoadGame(string fileName, Action <Enemy> add, Action <Enemy, bool> remove) { using (StreamReader reader = new StreamReader(fileName)) { string begin = reader.ReadLine(); // read the first line and check for a New Game or NG Game newGame = new Game(0, true, add, remove, 0); if (begin == "NG") { string[] gameInfo = reader.ReadLine().Split(','); // grab the game state information map = new Map(Convert.ToInt32(gameInfo[0])); // create a new map based on the mapid newGame.currentWave = Convert.ToInt32(gameInfo[1]); newGame.waveProgress = Convert.ToInt32(gameInfo[2]); newGame.score = Convert.ToInt32(gameInfo[3]); Game.money = Convert.ToInt32(gameInfo[4]); newGame.difficulty = Convert.ToInt32(gameInfo[5]); // need to change/fix this so that it is a difficulty instead of a int if (gameInfo[6] == "false") { newGame.isWaveOver = false; } else { newGame.isWaveOver = true; } Game.lives = Convert.ToInt32(gameInfo[7]); if (gameInfo[8] == "false") { cheatMode = false; } // cheatmode is static btw Spawner.count = new int[2] { Convert.ToInt32(gameInfo[9]), Convert.ToInt32(gameInfo[10]) }; Spawner.types = new string[2] { gameInfo[11], gameInfo[12] }; while (true) { string section = reader.ReadLine(); // read the section header if (section.Trim() == "END") { break; } // if its END we're done if (section.Trim() == "ENEMIES") { while (true) { string line = reader.ReadLine(); // read a line if (line.Trim() == "ENDENEMIES") { break; } // see if we're at the end yet break if we are string[] ele = line.Split(','); switch (ele[0]) // grab the type and call methods based on it { case "gboss": Boss bG = Boss.MakeBoss("g"); bG = bG.Deserialize(line) as Boss; Spawner.enemies.Add(bG); break; case "aboss": Boss bA = Boss.MakeBoss("a"); bA = bA.Deserialize(line) as Boss; Spawner.enemies.Add(bA); break; case "baircraft": Aircraft a = Aircraft.MakeAircraft("b"); a = a.Deserialize(line) as Aircraft; Spawner.enemies.Add(a); break; case "aaircraft": Aircraft aA = Aircraft.MakeAircraft("a"); aA = aA.Deserialize(line) as Aircraft; Spawner.enemies.Add(aA); break; case "binfantry": Infantry i = Infantry.MakeInfantry("b"); i = i.Deserialize(line) as Infantry; Spawner.enemies.Add(i); break; case "ainfantry": Infantry iA = Infantry.MakeInfantry("a"); iA = iA.Deserialize(line) as Infantry; Spawner.enemies.Add(iA); break; case "bvehicle": Vehicle v = Vehicle.MakeVehicle("b"); v = v.Deserialize(line) as Vehicle; Spawner.enemies.Add(v); break; case "avehicle": Vehicle vA = Vehicle.MakeVehicle("a"); vA = vA.Deserialize(line) as Vehicle; Spawner.enemies.Add(vA); break; default: Debug.WriteLine("The enemy type was not correct."); break; } } } else if (section.Trim() == "TURRETS") { while (true) { string lineT = reader.ReadLine(); // read a line if (lineT.Trim() == "ENDTURRETS") { break; } // see if we're at the end yet break if we are string[] eleT = lineT.Split(','); switch (eleT[0]) // grab the type and call methods based on it { case "flak": Flak f = Flak.MakeFlak(0, 0, 0); f.Deserialize(lineT); newGame.currentTurrets.Add(f); break; case "laser": Laser l = Laser.MakeLaser(0, 0, 0); l.Deserialize(lineT); newGame.currentTurrets.Add(l); break; case "machinegun": MachineGun m = MachineGun.MakeMachineGun(0, 0, 0); m.Deserialize(lineT); newGame.currentTurrets.Add(m); break; case "mortar": Mortar mo = Mortar.MakeMortar(0, 0, 0); mo.Deserialize(lineT); newGame.currentTurrets.Add(mo); break; case "stun": Stun s = Stun.MakeStun(0, 0, 0); s.Deserialize(lineT); newGame.currentTurrets.Add(s); break; case "tesla": Tesla t = Tesla.MakeTesla(0, 0, 0); t.Deserialize(lineT); newGame.currentTurrets.Add(t); break; default: Debug.WriteLine("The turret type was not correct."); break; } } } } } for (int i = 0; i < Spawner.enemies.Count; ++i) { newGame.addEnemy(Spawner.enemies[i]); } for (int i = 0; i < newGame.currentTurrets.Count; ++i) { for (int i2 = 0; i2 < newGame.currentTurrets[i].upgradeLvl - 1; ++i2) { newGame.currentTurrets[i2].Upgrade(); } } return(newGame); } }
// event handler for when the user places a tower. // check for if the user is over a valid location then creates and places the selected turret at the position of the mouse but snaped to a postion on a grid of block of sise 50x50 // updates the view with the new turret\ // also removes the selected turret items from the screen if neccassarys // returns nothing private void MapImage_MouseDown(object sender, MouseButtonEventArgs e) { //places selected tower on map mousePos = e.GetPosition(GameWindowCanvas); if (mousePos.X > 1000) { return; } if (isPlacing) { imagetowerplace.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/empty.png")); // adds tower image to map Image image = new Image(); isPlacing = false; image.RenderTransformOrigin = new Point(0.5, 0.5); image.Width = 50; image.Height = 50; double posX = SnapToGridX(mousePos.X); double posY = SnapToGridY(mousePos.Y); image.Margin = new Thickness(posX, posY, 0, 0); foreach (Image tI in turrets) { if (tI.Margin == image.Margin) { imagetowerplace.Margin = new Thickness(-50, -50, 0, 0); return; } } for (int i = 0; i < Map.coords.Count - 2; i++) { var pt1 = Map.coords[i]; var pt2 = Map.coords[i + 1]; if (!IsOnPath(image.Margin, pt1, pt2)) { imagetowerplace.Margin = new Thickness(-50, -50, 0, 0); return; } } image.MouseDown += SelectTurret; int index = turrets.Count; if (machinegunplace) { Game.money -= 50; image.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/machine gun tower.png")); MachineGun g = MachineGun.MakeMachineGun(posX, posY, index); game.currentTurrets.Add(g); } else if (flakplace) { Game.money -= 75; image.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/flak tower.PNG")); Flak g = Flak.MakeFlak(posX, posY, index); game.currentTurrets.Add(g); } else if (mortarplace) { Game.money -= 200; image.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/mortar tower.PNG")); Mortar g = Mortar.MakeMortar(posX, posY, index); game.currentTurrets.Add(g); } else if (teslaplace) { Game.money -= 175; image.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/tesla tower.PNG")); Tesla g = Tesla.MakeTesla(posX, posY, index); game.currentTurrets.Add(g); } else if (laserplace) { Game.money -= 125; image.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/laser tower.PNG")); Laser g = Laser.MakeLaser(posX, posY, index); game.currentTurrets.Add(g); } else { Game.money -= 200; image.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/stun tower.PNG")); Stun g = Stun.MakeStun(posX, posY, index); game.currentTurrets.Add(g); } turrets.Add(image); GameWindowCanvas.Children.Add(image); txtMoney.Text = "$" + Game.money; imagetowerplace.Margin = new Thickness(0, 0, 0, 0); } else { selectedTurret = null; b_upgrade_border.Visibility = Visibility.Hidden; btn_Upgrade.Visibility = Visibility.Hidden; lb_cost_to_upgrade.Visibility = Visibility.Hidden; lb_current_Dps.Visibility = Visibility.Hidden; lb_selectedType.Visibility = Visibility.Hidden; lb_upgraded_dps.Visibility = Visibility.Hidden; lb_turret_lvl.Visibility = Visibility.Hidden; } if (GameWindowCanvas.Children.Contains(selectedRing)) { GameWindowCanvas.Children.Remove(selectedRing); } }