Exemplo n.º 1
0
        public void Update(GameTime gameTime)
        {
            if (IsSpawning && !IsDoneSpawning)
            {
                RunnerSpawnDelayer.Update(gameTime);
                if (RunnerSpawnDelayer.IsDone)
                {
                    for (int i = 0; i < WaveRunners.Count; i++)
                    {
                        BaseRunner runner = WaveRunners[i].Runners.Last();
                        Level.Instance.ActiveRunners.Add(runner);
                        WaveRunners[i].Runners.Remove(runner);
                        runner.SetLocation(WaveRunners[i].startWP);

                        if (WaveRunners[i].Runners.Count == 0)
                        {
                            WaveRunners.RemoveAt(i);
                            i--;
                        }
                    }

                    if (WaveRunners.Count == 0)
                    {
                        IsDoneSpawning = true;
                    }
                    else
                    {
                        RunnerSpawnDelayer.Reset();
                    }
                }
            }
        }
Exemplo n.º 2
0
        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();
        }