// 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); } }