public void UpdateForTower(GameTime gameTime, BaseTower tower) { RoF.Update(gameTime); if (RoF.IsDone) { RoF.Reset(); ITargetable target = tower.GetTarget(Range); if (target != null) { if (ShootFX != -1) { AudioMgrPooled.Instance.PlaySound(ShootFX); } Level.Instance.Projectiles.Add(new BaseProjectile(CenterLocation + ProjectileSpawnOffset, ProjectileType, true, target, Damage, DamageType, Splash, SplashDmgPerc, ImpactFX)); } } }
public void Update(GameTime gameTime) { #region First things to do MouseWorldLocThisCycle = InputMgr.Instance.Mouse.Location + Player.Camera.Location; // This line comes first in the Update(). BaseRunner.RunnerNearestToFinish = null; ControlMgr.Instance.Update(gameTime); #endregion if (State != eState.Paused) { #region Collision BroadPhase.Instance.ClearEntities(); foreach (BaseRunner r in ActiveRunners) { BroadPhase.Instance.AddEntity(r); } foreach (BaseDefender d in SpawnedAliveDefenders) { BroadPhase.Instance.AddEntity(d); } #endregion // Environements foreach (Environment e in Environments) { e.Update(gameTime); } // Update runners foreach (BaseRunner r in ActiveRunners) { r.Update(gameTime); } // Update towers (and defenders) foreach (BaseTower tower in Towers) { tower.Update(gameTime); } // Visuals for (int i = 0; i < Visuals.Count; i++) { Visuals[i].Update(gameTime); if (Visuals[i].IsDisposed) { Visuals.RemoveAt(i); i--; } } // Update global rallypoint animation. BaseTower.RallyPointAni.Update(gameTime); // Update projectiles foreach (BaseProjectile p in Projectiles) { p.Update(gameTime); } Projectiles.RemoveAll(p => p.IsDisposed); // Update player Player.Update(gameTime); #region Update waves if (State != eState.PreSpawn) { for (int i = 0; i < Waves.Count; i++) { Waves[i].Update(gameTime); } WaveStartDelayTimer.Update(gameTime); if (WaveStartDelayTimer.IsDone) // The WaveStartDelayTimer is reset in the SpawnNextWave() procedure. { SpawnNextWave(); } } #endregion #region MiniMap bool MouseIsOverMiniMap = false; if (!MiniMap.IsHidden && Collision.PointIsInRect(InputMgr.Instance.Mouse.Location, MiniMap.DrawRect)) { MouseIsOverMiniMap = true; if (InputMgr.Instance.Mouse.LeftButtonIsDown) { Vector2 MiniMapToWorldLoc = MiniMap.LocationToWorld(InputMgr.Instance.Mouse.Location); Player.Camera.Location = MiniMapToWorldLoc - new Vector2(Engine.Instance.Width / 2, Engine.Instance.Height / 2); Player.AdjustCameraToLvlBoundary(); } } #endregion if (!MouseIsOverMiniMap && Player.State != QTD.Player.eState.Building) { #region Get tower beneath mouse if (TowerBeneathMouse != null && (SelectedTower == null || (SelectedTower != TowerBeneathMouse))) { TowerBeneathMouse.DrawColor = Color.White; } TowerBeneathMouse = null; foreach (BaseTower tower in Towers) { if (Collision.PointIsInRect(MouseWorldLocThisCycle, tower.AABB)) { if ((TowerBeneathMouse == null || TowerBeneathMouse.AABB.Bottom > tower.AABB.Bottom) && (tower != SelectedTower)) // When there was already a tower beneath the mouse then take the one that has the lowest Y-value for the AABB.Bottom. { TowerBeneathMouse = tower; TowerBeneathMouse.DrawColor = Color.Gray; } } } #endregion #region Get Clicked Tower if (InputMgr.Instance.Mouse.LeftButtonIsPressed) { if (SelectedTower != null) { SelectedTower.DrawColor = Color.White; SelectedTower.HasFocus = false; } SelectedTower = TowerBeneathMouse; if (SelectedTower != null) { SelectedTower.DrawColor = Color.Wheat; SelectedTower.HasFocus = true; AudioMgrPooled.Instance.PlaySound(AudioConstants.MouseClick); } #region SellBtn SellBtn.IsEntirelyDisabled = SelectedTower == null; #endregion } #endregion #region SetRallyPoint if applicable if (SelectedTower != null && InputMgr.Instance.Mouse.RightButtonIsPressed) { SelectedTower.SetRallyPoint(MouseWorldLocThisCycle); } #endregion } // Allow to cancel a category / tower placement if (InputMgr.Instance.Keyboard.IsPressed(Keys.Escape)) { if (Player.State == QTD.Player.eState.Building) { Player.Gold += Player.BuildThisTower.Cost; Player.State = QTD.Player.eState.Normal; } ReturnToCats(); } #region BuildGrid & Building a new tower if (Player.State == QTD.Player.eState.Building) { BuildGrid.Update(MouseWorldLocThisCycle, Player.BuildThisTower.BuildSize); if (!PlaceTowerWaitOneCycle) { if (BuildGrid.CanBuild && InputMgr.Instance.Mouse.LeftButtonIsPressed) { Player.State = QTD.Player.eState.Normal; Player.Gold -= Player.BuildThisTower.Cost; BaseTower t = new BaseTower(); t.Initialize(Player.BuildThisTower.ID); t.SetLocation(BuildGrid.BuildRect.Location.ToVector2()); AddTower(t); } } else { PlaceTowerWaitOneCycle = false; } } #endregion #region Percentage Bar int runnerPercToFinish = 0; if (BaseRunner.RunnerNearestToFinish != null) { RunnerProgressBar.Percentage = BaseRunner.RunnerNearestToFinish.PercentageToFinish; runnerPercToFinish = (int)BaseRunner.RunnerNearestToFinish.PercentageToFinish; } else { RunnerProgressBar.Percentage = 0; } RunnerProgress.Remove(0, RunnerProgress.Length); RunnerProgress.Append(runnerPercToFinish); RunnerProgress.Append(PERC_STR); #endregion WaveSpawnBar.Percentage = WaveStartDelayTimer.PercentageComplete; #region huts HeadUpTextPool.CleanUp(); for (int i = 0; i < HeadUpTexts.Count; i++) { HeadUpTexts[i].Update(gameTime); if (HeadUpTexts[i].IsDisposed) { HeadUpTexts.RemoveAt(i); i--; } } #endregion #region Button info panel #warning todo: optimize this shit. cannot loop each update through all buttons... InfoPanel.ClearText(); foreach (Button btn in CategoryButtons) { if (!btn.IsEntirelyDisabled) { if (btn.HasFocus) { InfoPanel.SetText(((TowerCategoryStruct)btn.Tag).Info); } } } foreach (Button btn in TowerBuildButtons) { if (!btn.IsEntirelyDisabled) { if (btn.HasFocus) { InfoPanel.SetText(((TowerStruct)btn.Tag).Info); } } } #endregion WarningMessages.Instance.Update(gameTime); // Game over check if (!Player.IsAlive) { #warning todo: gameover here } } }
public void RemoveTower(BaseTower t) { Towers.Remove(t); BroadPhase.Instance.RemoveEntity(t); }
public void AddTower(BaseTower t) { Towers.Add(t); BroadPhase.Instance.AddEntity(t); }
public void Load(string xmlName) { // Resets SpawnedDeadDefenders = new List <BaseDefender>(MAX_ACTIVE_DEFENDERS); SpawnedAliveDefenders = new List <BaseDefender>(MAX_ACTIVE_DEFENDERS); Towers = new List <BaseTower>(MAX_TOWERS); TowerBeneathMouse = SelectedTower = null; NextWaveButton.IsEnabled = true; Player.ResetForNewLevel(10); WaveProgressBar.Percentage = 0; State = eState.PreSpawn; Environments = new List <Environment>(); XDocument doc = XDocument.Load("Data/Levels/" + xmlName + ".xml"); #region Level Info XElement levelInfoNode = doc.Root.Element("LevelInfo"); if (levelInfoNode == null) { throw new NullReferenceException("LevelInfo node not found. " + xmlName); } // Level display name LevelDisplayName = new StringBuilder(levelInfoNode.Element("Name").Value); // Start Gold Player.Gold = int.Parse(levelInfoNode.Element("StartGold").Value); // Level Size LevelSize = Common.Str2Point(levelInfoNode.Element("LevelSize").Value); if (LevelSize.X < 1280 || LevelSize.Y < 800) { throw new Exception("Level size must be at least 1280x800. Otherwise there would be a black border around the level plus the camera can not stay in such a small boundary."); } // Broadphase BroadPhase.Instance = new BroadPhase(int.Parse(levelInfoNode.Element("CollisionGridSize").Value), LevelSize); BroadPhase.Instance.Init(); #endregion #region Load Waypoints XElement wpMainNode = doc.Root.Element("Waypoints"); if (wpMainNode == null) { throw new NullReferenceException("Waypoints node missing. " + xmlName); } WayPoint.Spread = int.Parse(wpMainNode.Attribute("spread").Value); WayPoint.StartPoints = new List <WayPoint>(); foreach (XElement wp in wpMainNode.Elements()) { List <int> nextWpIds = new List <int>(); XElement nextWPsNode = wp.Element("NextWaypoints"); if (nextWPsNode != null) { foreach (XElement nextwpID in nextWPsNode.Elements()) { nextWpIds.Add(int.Parse(nextwpID.Value)); } } // Start & Finish bool isStart = false; if (wp.Attribute("isStart") != null) { isStart = bool.Parse(wp.Attribute("isStart").Value); } bool isFinish = false; if (wp.Attribute("isFinish") != null) { isFinish = bool.Parse(wp.Attribute("isFinish").Value); } WayPoints.Add(new WayPoint(int.Parse(wp.Attribute("id").Value), Common.Str2Vector(wp.Attribute("location").Value), isStart, isFinish, nextWpIds.ToArray())); } WayPoints.ForEach(w => w.Initialize()); WayPoint.CalculateTotalRoutelength(); #endregion // Waves after waypoints. CurrentWaveNr = 0; #region Waves Waves = new List <Wave>(); XElement wavesmainNode = doc.Root.Element("Waves"); if (wavesmainNode == null) { throw new NullReferenceException("Node Waves missing." + xmlName); } foreach (XElement waveNode in wavesmainNode.Elements()) { Wave newWave = new Wave(int.Parse(waveNode.Attribute("nr").Value), int.Parse(waveNode.Attribute("spawnDelay").Value)); newWave.TimeUntilNextWaveInMS = int.Parse(waveNode.Attribute("timeUntilNextWave").Value); foreach (XElement startWPNode in waveNode.Elements()) { List <BaseRunner> runners = new List <BaseRunner>(); int startWPID = int.Parse(startWPNode.Attribute("id").Value); foreach (XElement runnerNode in startWPNode.Elements()) { int runnerID = int.Parse(runnerNode.Attribute("id").Value); int amount = int.Parse(runnerNode.Attribute("amount").Value); for (int i = 0; i < amount; i++) { BaseRunner newRunner = new BaseRunner(); newRunner.Initialize(runnerID); newRunner.SetLocation(WayPoints.Find(w => w.ID == startWPID)); runners.Add(newRunner); } } newWave.WaveRunners.Add(new WaveSpawnHelper(startWPID, runners.ToArray())); } Waves.Add(newWave); } TotalWaves = Waves.Count; #endregion #region BuildGrid BuildGrid = new BuildGrid(); XElement unBuildablesNode = doc.Root.Element("UnBuildables"); if (unBuildablesNode != null) { BuildGrid.SetUnBuildables(unBuildablesNode.Value); } #endregion #region BackGround (before minimap region) BGMgr = new BGMgr(); XElement bgMainNode = doc.Root.Element("BackGround"); if (bgMainNode != null) { BGMgr.Load(bgMainNode); } #endregion #region MiniMap MiniMap = new MiniMap(new Vector2(0, Engine.Instance.Height - MiniMap.Size.Y), LevelSize); MiniMap.PreRender(); #endregion #region Initial towers XElement initialTowersMainNode = doc.Root.Element("InitialTowers"); if (initialTowersMainNode != null) { foreach (XElement initialTowerNode in initialTowersMainNode.Elements()) { BaseTower bt = new BaseTower(); bt.Initialize(int.Parse(initialTowerNode.Element("ID").Value)); bt.SetLocation(Common.Str2Vector(initialTowerNode.Element("GridIdx").Value) * BuildGrid.GRID_SIZE); AddTower(bt); } } #endregion #region Buildable Towers BuildableTowers = new List <int>(); XElement buildableTowersMainNode = doc.Root.Element("BuildableTowers"); if (BuildableTowers != null) { if (buildableTowersMainNode.Attribute("type").Value == "restricted") { // Add all towers for (int i = 0; i < DataStructs.Towers.Count; i++) { BuildableTowers.Add(DataStructs.Towers[i].ID); } // Remove restricted towers foreach (XElement restrictedTowerNode in buildableTowersMainNode.Elements()) { BuildableTowers.Remove(int.Parse(restrictedTowerNode.Value)); } } else { // Add allowed towers foreach (XElement restrictedTowerNode in buildableTowersMainNode.Elements()) { BuildableTowers.Add(int.Parse(restrictedTowerNode.Value)); } } } else { // Add all towers for (int i = 0; i < DataStructs.Towers.Count; i++) { BuildableTowers.Add(DataStructs.Towers[i].ID); } } #endregion #region Perma Disable CategoryButtons when they have no towers at all (place this code after region: Buildable Towers) foreach (Button btn in CategoryButtons) { TowerCategoryStruct tcs = (TowerCategoryStruct)btn.Tag; bool hasNoTowers = true; for (int i = 0; i < tcs.TowersInThisCat.Count; i++) { if (BuildableTowers.Contains(tcs.TowersInThisCat[i].ID)) { hasNoTowers = false; break; } } btn.Tag2 = hasNoTowers; btn.IsEntirelyDisabled = hasNoTowers; } #endregion #region Environments XElement environmentMainNode = doc.Root.Element("Environments"); if (environmentMainNode != null) { foreach (XElement environmentNode in environmentMainNode.Elements()) { Environments.Add(new Environment((eAnimation)Enum.Parse(typeof(eAnimation), environmentNode.Element("Type").Value), Common.Str2Vector(environmentNode.Element("Location").Value))); } } #endregion // GC GC.Collect(); }
void SellBtn_Click(Button button) { SelectedTower.Sell(); SellBtn.IsEntirelyDisabled = true; SelectedTower = null; }