void newTBBtn_Click(Button button) { TowerStruct ts = (TowerStruct)button.Tag; ReturnToCats(); if (ts.Cost <= Player.Gold) { if (Player.IsSupplyPossible(ts.SupplyCost, 0)) { Player.Gold -= ts.Cost; Player.BuildThisTower = ts; Player.State = QTD.Player.eState.Building; PlaceTowerWaitOneCycle = true; } else { WarningMessages.Instance.AddWarning(1); AudioMgrPooled.Instance.PlaySound(AudioConstants.Error); } } else { WarningMessages.Instance.AddWarning(0); AudioMgrPooled.Instance.PlaySound(AudioConstants.Error); } }
/// <summary> /// Click event for tower category buttons /// </summary> /// <param name="button"></param> void newTCBtn_Click(Button button) { TowerCategoryStruct tcs = (TowerCategoryStruct)button.Tag; foreach (Button btn in CategoryButtons) { btn.IsEntirelyDisabled = true; } foreach (Button btn in TowerBuildButtons) { TowerStruct ts = (TowerStruct)btn.Tag; if (tcs.TowersInThisCat.Contains(ts) && BuildableTowers.Contains(ts.ID)) { btn.IsEntirelyDisabled = false; if (ts.Requirements.Count > 0) { // Check if the player meets the tower requirements (excluding gold and supply) bool requirementsAreMet = false; for (int i = 0; i < ts.Requirements.Count; i++) // OR-loop { bool andReqMet = true; for (int j = 0; j < ts.Requirements[i].Count; j++) // AND-loop { if (Towers.Find(t => t.ID == ts.Requirements[i][j]) == null) { andReqMet = false; } } if (andReqMet) { requirementsAreMet = true; break; } } btn.IsEnabled = requirementsAreMet; } } } }
public void Initialize(int id) { #warning the struct below is copied and thus is allocates memory. should be done differently like find the index with linq instead and access it by index. TowerStruct ts = DataStructs.Towers.Find(t => t.ID == id); ID = id; Name = new StringBuilder(ts.Name, ts.Name.Length); Desc = new StringBuilder(ts.Desc, ts.Desc.Length); TotalValue = Cost = ts.Cost; BuildSize = ts.BuildSize; BuildTimeInMS = ts.BuildTimeInMS; MaxRallyPointRange = ts.MaxRallyPointRange; RallyPointPersuitRange = ts.RallyPointPersuitRange; OccupiedGridIndices = new List <Point>(ts.BuildSize.X * ts.BuildSize.Y); OccupyRect = Rectangle.Empty; if (ts.SupplyCost > 0) { SupplyCost = ts.SupplyCost; SupplyGive = 0; Level.Instance.Player.Supply += ts.SupplyCost; } else { SupplyCost = 0; SupplyGive = ts.SupplyCost * -1; Level.Instance.Player.MaxSupply += SupplyGive; } m_HasFocus = false; #region Income if (ts.IncomeTickDelayInMS != 0) { IncomeTickDelayTimer = new SimpleTimer(ts.IncomeTickDelayInMS); } else { IncomeTickDelayTimer = null; } IncomePerTick = ts.IncomePerTick; IncomePerWave = ts.IncomePerWave; #endregion Animation = AnimationFactory.Create(ts.AnimationType, out RelativeAABB); Animation.DrawColor = ts.DrawColor; DrawColor = Color.White; #region Weapons Weapons = new List <BaseWeapon>(); if (ts.Weapons.Count > 0) { foreach (WeaponStruct ws in ts.Weapons) { Weapons.Add(new BaseWeapon(m_CenterLoc, ws, this)); } } #endregion Vector2 btnOffset = Vector2.Zero; #region Spawns if (SpawnUpgradeButtons != null) { foreach (Button sbtn in SpawnUpgradeButtons) { sbtn.Click -= new Button.OnClick(spawnUpgBtn_Click); } } SpawnUpgradeButtons = new List <Button>(); if (ts.Defenders.Count == 0) { Defenders = new List <BaseDefender>(0); } else { Defenders = new List <BaseDefender>(MAX_DEFENDERS); foreach (DefenderSpawnStruct dss in ts.Defenders) { int maxDefenders = dss.Max; int currentInitialCnt = 0; for (int i = 0; i < dss.Max; i++) { BaseDefender bd = new BaseDefender(); bd.Initialize(dss.ID); bd.SetRallyPoint(RallyPoint, MaxRallyPointRange, RallyPointPersuitRange); Defenders.Add(bd); if (currentInitialCnt < dss.InitialAmount) { bd.Spawn(); currentInitialCnt++; } } if (dss.InitialAmount < dss.Max) { Button spawnUpgBtn = new Button(SpawnUpgradeBtnsLoc + btnOffset, "GUI/btnEmpty", "GUI/btnEmptyHover", null) { OverlayTexture = Common.str2Tex("Icons/soldier1"), Tag = dss }; spawnUpgBtn.Click += new Button.OnClick(spawnUpgBtn_Click); btnOffset.Y += 50; SpawnUpgradeButtons.Add(spawnUpgBtn); } } } #endregion #region Tower Upgrades if (TowerUpgradeButtons != null) { foreach (Button tbtn in TowerUpgradeButtons) { tbtn.Click -= new Button.OnClick(TowerUpgBtn_Click); } } TowerUpgradeButtons = new List <Button>(); foreach (TowerUpgrade twrUpg in ts.Upgrades) { Button newTowerUpgBtn = new Button(TowerUpgradeBtnsLoc + btnOffset, "GUI/btnTowerUpgEmpty", "GUI/btnTowerUpgEmptyHover", null) { OverlayTexture = twrUpg.Icon, Tag = twrUpg }; btnOffset.Y += 50; newTowerUpgBtn.Click += new Button.OnClick(TowerUpgBtn_Click); TowerUpgradeButtons.Add(newTowerUpgBtn); } #endregion }
public static void LoadTowers() { Towers.Clear(); string[] towerXmls = Directory.GetFiles("Data/Towers", "*.xml"); foreach (string path in towerXmls) { XDocument doc = XDocument.Load(path); #region Required XElement RequiredNode = doc.Root.Element("Required"); if (RequiredNode == null) { throw new Exception("Required node missing. " + path); } TowerStruct ts = new TowerStruct(int.Parse(RequiredNode.Element("ID").Value)); XElement required = RequiredNode.Element("Name"); if (required != null) { ts.Name = required.Value; } else { throw new Exception("Name missing." + path); } required = null; required = RequiredNode.Element("Description"); if (required != null) { ts.Desc = required.Value; } else { throw new Exception("Description missing." + path); } required = null; required = RequiredNode.Element("Cost"); if (required != null) { ts.Cost = int.Parse(required.Value); } else { throw new Exception("Cost missing." + path); } required = null; required = RequiredNode.Element("Icon"); if (required != null) { ts.Icon = Common.str2Tex("Icons/" + required.Value); } else { throw new Exception("Icon missing." + path); } required = null; required = RequiredNode.Element("Animation"); if (required != null) { ts.AnimationType = (eAnimation)Enum.Parse(typeof(eAnimation), required.Value); } else { throw new Exception("Animation missing." + path); } required = null; #endregion #region Basic XElement basicNode = doc.Root.Element("Basic"); if (basicNode != null) { XElement basic; basic = basicNode.Element("BuildSize"); if (basic != null) { ts.BuildSize = Common.Str2Point(basic.Value); } basic = null; basic = basicNode.Element("BuildTime"); if (basic != null) { ts.BuildTimeInMS = int.Parse(basic.Value); } basic = null; basic = basicNode.Element("Supply"); if (basic != null) { ts.SupplyCost = int.Parse(basic.Value); } basic = null; basic = basicNode.Element("UpgradeTime"); if (basic != null) { ts.UpgTimeInMS = int.Parse(basic.Value); } basic = null; } #endregion #region Advanced XElement advancedNode = doc.Root.Element("Advanced"); if (advancedNode != null) { XElement advanced; advanced = advancedNode.Element("IncomePerTick"); if (advanced != null) { ts.IncomePerTick = int.Parse(advanced.Value); } advanced = null; advanced = advancedNode.Element("IncomeTickDelay"); if (advanced != null) { ts.IncomeTickDelayInMS = int.Parse(advanced.Value); } advanced = null; advanced = advancedNode.Element("IncomePerWave"); if (advanced != null) { ts.IncomePerWave = int.Parse(advanced.Value); } advanced = null; } #endregion #region Weapons XElement wpnNode = doc.Root.Element("Weapons"); if (wpnNode != null) { foreach (XElement wpn in wpnNode.Elements()) { XElement wpnRange = wpn.Element("Range"); if (wpnRange == null) { throw new Exception("Weapon has no range. " + path); } XElement wpnRoF = wpn.Element("RoF"); if (wpnRoF == null) { throw new Exception("Weapon has no RoF. " + path); } XElement wpnDmg = wpn.Element("Damage"); if (wpnDmg == null) { throw new Exception("Weapon has no Damage. " + path); } XElement wpnProjectileType = wpn.Element("Projectile"); if (wpnProjectileType == null) { throw new Exception("Weapon has no Projectile. " + path); } XElement wpnSplash = wpn.Element("Splash"); int wpnSplashValue; if (wpnSplash == null) { wpnSplashValue = 0; } else { wpnSplashValue = int.Parse(wpnSplash.Value); } XElement wpnSplashDmgPercentage = wpn.Element("SplashDmgPercentage"); int wpnSplashDmgPercentageValue; if (wpnSplashDmgPercentage == null) { wpnSplashDmgPercentageValue = 0; } else { wpnSplashDmgPercentageValue = int.Parse(wpnSplashDmgPercentage.Value); } XElement wpnAttackableTargets = wpn.Element("AttackableTargets"); eAttackableTargets wpnAttackableTargetsValue; if (wpnAttackableTargets == null) { wpnAttackableTargetsValue = eAttackableTargets.Ground; } else { wpnAttackableTargetsValue = (eAttackableTargets)Enum.Parse(typeof(eAttackableTargets), required.Value); } WeaponStruct ws = new WeaponStruct(int.Parse(wpnRange.Value), int.Parse(wpnRoF.Value), int.Parse(wpnDmg.Value), (eProjectile)Enum.Parse(typeof(eProjectile), wpnProjectileType.Value), wpnSplashValue, wpnSplashDmgPercentageValue, wpnAttackableTargetsValue); XElement wpnProjectileSpawnOffset = wpn.Element("ProjectileSpawnOffset"); if (wpnProjectileSpawnOffset != null) { ws.ProjectileSpawnOffset = Common.Str2Vector(wpnProjectileSpawnOffset.Value); } XElement wpnCritical; wpnCritical = wpn.Element("CritRate"); if (wpnCritical != null) { ws.CritChance = int.Parse(wpnCritical.Value); } XElement wpnCritMultiplier; wpnCritMultiplier = wpn.Element("CritMultiplier"); if (wpnCritMultiplier != null) { ws.CritMultiplier = int.Parse(wpnCritMultiplier.Value); } XElement shootFXNode = wpn.Element("ShootFX"); if (shootFXNode != null) { ws.ShootFX = int.Parse(shootFXNode.Value); } XElement impactFXNode = wpn.Element("ImpactFX"); if (impactFXNode != null) { ws.ImpactFX = int.Parse(impactFXNode.Value); } #region Modifiers XElement modifierNode = wpn.Element("Stunchance"); if (modifierNode != null) { ws.WeaponModifiers.StunChance = int.Parse(modifierNode.Value); } modifierNode = null; modifierNode = wpn.Element("Slows"); if (modifierNode != null) { ws.WeaponModifiers.Slows = bool.Parse(modifierNode.Value); } modifierNode = null; modifierNode = wpn.Element("RootChance"); if (modifierNode != null) { ws.WeaponModifiers.RootChance = int.Parse(modifierNode.Value); } modifierNode = null; modifierNode = wpn.Element("ArmorReductionValue"); if (modifierNode != null) { ws.WeaponModifiers.ArmorReductionValue = int.Parse(modifierNode.Value); } modifierNode = null; #endregion ts.Weapons.Add(ws); } } #endregion #region Spawn XElement spawnMainNode = doc.Root.Element("Spawns"); if (spawnMainNode != null) { ts.MaxRallyPointRange = int.Parse(spawnMainNode.Attribute("maxRallyPointRange").Value); ts.RallyPointPersuitRange = int.Parse(spawnMainNode.Attribute("persuitRange").Value); foreach (XElement spawnNode in spawnMainNode.Elements()) { ts.Spawns = true; int defID = int.Parse(spawnNode.Element("ID").Value); DefenderSpawnStruct ds = new DefenderSpawnStruct(defID); XElement initialAmountNode = spawnNode.Element("InitialAmount"); if (initialAmountNode != null) { ds.InitialAmount = int.Parse(initialAmountNode.Value); } XElement spawnUpgCost = spawnNode.Element("UpgCntCost"); if (spawnUpgCost != null) { ds.UpgCntCost = int.Parse(spawnUpgCost.Value); } XElement maxNode = spawnNode.Element("Max"); if (maxNode != null) { ds.Max = int.Parse(maxNode.Value); } ts.Defenders.Add(ds); } } #endregion #region Upgrades (to another tower) XElement towerUpgradeMainNode = doc.Root.Element("Upgrades"); if (towerUpgradeMainNode != null) { foreach (XElement towerUpgNode in towerUpgradeMainNode.Elements()) { ts.Upgrades.Add(new TowerUpgrade(int.Parse(towerUpgNode.Element("NewTowerID").Value), int.Parse(towerUpgNode.Element("Cost").Value), ts.Icon ) ); } } #endregion #region Categories XElement catMainNode = doc.Root.Element("Categories"); if (catMainNode != null) { foreach (XElement catIDNode in catMainNode.Elements()) { ts.Categories.Add(int.Parse(catIDNode.Value)); } } #endregion #region info XElement infoMainNode = doc.Root.Element("Info"); if (infoMainNode != null) { foreach (XElement infoNode in infoMainNode.Elements()) { ts.Info.Add(new StringBuilder(infoNode.Value)); } } #endregion #region Requirements XElement requiredMainNode = doc.Root.Element("Requirements"); if (requiredMainNode != null) { foreach (XElement requiredOrNode in requiredMainNode.Elements()) { List <int> requirementsAND = new List <int>(); foreach (XElement requiredAndNode in requiredOrNode.Elements()) { requirementsAND.Add(int.Parse(requiredAndNode.Value)); } ts.Requirements.Add(requirementsAND); } } #endregion Towers.Add(ts); } }