// takes the x and y coordinates of its gui counterpart as well as // the gui's list index and returns a default machinegun object. public static MachineGun MakeMachineGun(double x, double y, int index) { MachineGun m = new MachineGun(); m.imageIndex = index; m.imageID = 0; m.fireRate = 10; m.xPos = x; m.yPos = y; m.cost = 50; m.upCost = Convert.ToInt32(m.cost / 2); m.damage = 2; m.range = 125; m.type = "machinegun"; return(m); }
// 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); } }