public bool ContainsPlayer()
        {
            XDocument regionFile = XDocument.Load(RegionPathGenerator.GetRegionPath(Coords));

            return(regionFile != null &&
                   regionFile.Root.Element("spawns") != null &&
                   regionFile.Root.Element("spawns").Element("player") != null);
        }
        private void LoadActivity()
        {
            XDocument regionFile   = XDocument.Load(RegionPathGenerator.GetRegionPath(Coords));
            XElement  activityData = regionFile.Root.Element("activity");

            if (activityData != null && activityData.Element("type") != null)
            {
                int      mapXCoords     = (Coords.X * FieldsInLine);
                int      mapYCoords     = (Coords.Y * FieldsInLine);
                Vector2i ActivityCoords = new Vector2i(mapXCoords + int.Parse(activityData.Element("x").Value), mapYCoords + int.Parse(activityData.Element("y").Value));
                Activity = activityData.Element("type").Value switch
                {
                    "destroy" => new DestroyAllActivity(ActivityCoords, Enemies),
                    "protect" => new ProtectActivity(ActivityCoords, Enemies, activityData.Element("health") != null ? int.Parse(activityData.Element("health").Value) : -1),
                    "wave" => new WaveActivity(ActivityCoords, Enemies,
                                               new Queue <List <EnemySpawnData> >(from waves in activityData.Element("waves").Descendants("wave")
                                                                                  select new List <EnemySpawnData>(from spawnData in waves.Descendants("enemy")
                                                                                                                   select new EnemySpawnData(
                                                                                                                       new Vector2i(mapXCoords + int.Parse(spawnData.Element("x").Value), mapYCoords + int.Parse(spawnData.Element("y").Value)),
                                                                                                                       spawnData.Element("type").Value,
                                                                                                                       spawnData.Element("aimc").Value,
                                                                                                                       spawnData.Element("path") != null && spawnData.Element("path").Descendants("point") != null
                                        ? new List <Vector2i>(from point in spawnData.Element("path").Descendants("point") select new Vector2i(mapXCoords + int.Parse(point.Element("x").Value), mapYCoords + int.Parse(point.Element("y").Value)))
                                        : null))),
                                               this,
                                               activityData.Element("currentWave") != null ? uint.Parse(activityData.Element("currentWave").Value) : 0),
                    "waveprotect" => new WaveProtectActivity(ActivityCoords, Enemies,
                                                             new Queue <List <EnemySpawnData> >(from waves in activityData.Element("waves").Descendants("wave")
                                                                                                select new List <EnemySpawnData>(from spawnData in waves.Descendants("enemy")
                                                                                                                                 select new EnemySpawnData(
                                                                                                                                     new Vector2i(mapXCoords + int.Parse(spawnData.Element("x").Value), mapYCoords + int.Parse(spawnData.Element("y").Value)),
                                                                                                                                     spawnData.Element("type").Value,
                                                                                                                                     spawnData.Element("aimc").Value,
                                                                                                                                     spawnData.Element("path") != null && spawnData.Element("path").Descendants("point") != null
                                        ? new List <Vector2i>(from point in spawnData.Element("path").Descendants("point") select new Vector2i(mapXCoords + int.Parse(point.Element("x").Value), mapYCoords + int.Parse(point.Element("y").Value)))
                                        : null))),
                                                             this,
                                                             activityData.Element("currentWave") != null ? uint.Parse(activityData.Element("currentWave").Value) : 0,
                                                             activityData.Element("health") != null ? int.Parse(activityData.Element("health").Value) : -1),
                    _ => throw new NotImplementedException()
                };

                Activity.Field            = GetFieldAtMapCoords(Activity.Coords);
                Activity.Field.GameObject = Activity;

                if (Player != null && Activity.ActivityStatus == ActivityStatus.Stopped)
                {
                    Activity.ChangeStatus(ActivityStatus.Started);
                }
            }
        }
        public void Save()
        {
            if (Loaded)
            {
                XmlDocument savefile      = new XmlDocument();
                XmlElement  regionElement = savefile.CreateElement("region");

                regionElement.AppendChild(SerializeFields(savefile));
                regionElement.AppendChild(SerializeSpawns(savefile));
                regionElement.AppendChild(SerializeActivity(savefile));

                savefile.AppendChild(savefile.CreateXmlDeclaration("1.0", "utf-8", null));
                savefile.AppendChild(regionElement);
                savefile.Save(RegionPathGenerator.GetSavedRegionPath(Coords));
            }
        }
예제 #4
0
        private Vector2i SearchForPlayerRegion()
        {
            for (int column = 0; column < MapSize; column++)
            {
                for (int row = 0; row < MapSize; row++)
                {
                    Vector2i regionCoords = new Vector2i(column, row);

                    if (RegionPathGenerator.GetRegionPath(regionCoords) != null && new Region(regionCoords, FieldsInLine, false).ContainsPlayer())
                    {
                        return(regionCoords);
                    }
                }
            }

            return(new Vector2i(-1, -1));
        }
        private void LoadPlayer()
        {
            XDocument regionFile = XDocument.Load(RegionPathGenerator.GetRegionPath(Coords));
            XElement  playerData = regionFile.Root.Element("spawns").Element("player");

            Player        = new Player(new Vector2f(((Coords.X * FieldsInLine) + float.Parse(playerData.Element("x").Value)) * 64.0F, ((Coords.Y * FieldsInLine) + float.Parse(playerData.Element("y").Value)) * 64.0F), new Vector2f(64.0F, 64.0F));
            Player.Health = (playerData.Element("health") is null) ? Player.Health : int.Parse(playerData.Element("health").Value);

            GamestateManager.Instance.Player = Player;

            MessageBus.Instance.PostEvent(MessageType.PlayerHealthChanged, this, new PlayerHealthChangeEventArgs(Player.Health));
            if (Player.Health == 0)
            {
                Player.OnDestroy();
            }

            GetFieldAtMapCoords(Player.Coords).PawnOnField = Player;
        }
        private void LoadFields()
        {
            XDocument regionFile = XDocument.Load(RegionPathGenerator.GetRegionPath(Coords));
            int       x          = 0;
            int       y          = 0;

            Fields = new List <Field>();

            regionFile.Root.Element("fields").Descendants("field").ToList().ForEach((XElement fieldElement) =>
            {
                Fields.Add(new Field(
                               new Vector2i((Coords.X * FieldsInLine) + x, (Coords.Y * FieldsInLine) + y),
                               FieldType.FieldTypes[fieldElement.Element("type").Value].Item1,
                               FieldType.FieldTypes[fieldElement.Element("type").Value].Item2,
                               TextureManager.Instance.GetTexture(TextureType.Field, fieldElement.Element("texture").Value),
                               fieldElement.Element("type").Value,
                               (fieldElement.Element("object") != null && fieldElement.Element("object").Value != null)
                        ? new GameObject(
                                   new Vector2i((Coords.X * FieldsInLine) + x, (Coords.Y * FieldsInLine) + y),
                                   GameObjectType.GameObjectTypes[fieldElement.Element("object").Element("type").Value],
                                   TextureManager.Instance.GetTexture(TextureType.GameObject, fieldElement.Element("object").Element("type").Value),
                                   fieldElement.Element("object").Element("type").Value,
                                   (fieldElement.Element("object").Element("hp") != null && fieldElement.Element("object").Element("hp").Value != null)
                                ? int.Parse(fieldElement.Element("object").Element("hp").Value)
                                : -1)
                        : null));

                if (y == FieldsInLine - 1)
                {
                    y = 0;
                    x++;
                }
                else
                {
                    y++;
                }
            });
        }
        private void LoadEnemies()
        {
            XDocument regionFile = XDocument.Load(RegionPathGenerator.GetRegionPath(Coords));

            if (regionFile.Root.Element("spawns") != null && regionFile.Root.Element("spawns").Descendants("enemy") != null)
            {
                Enemies = new HashSet <Enemy>(from enemy in regionFile.Root.Element("spawns").Descendants("enemy")
                                              select EnemyFactory.CreateEnemy(
                                                  new Vector2i((Coords.X * FieldsInLine) + int.Parse(enemy.Element("x").Value), (Coords.Y * FieldsInLine) + int.Parse(enemy.Element("y").Value)),
                                                  enemy.Element("type").Value,
                                                  enemy.Element("aimc").Value,
                                                  enemy.Element("path") != null && enemy.Element("path").Descendants("point") != null
                            ? new List <Vector2i>(from point in enemy.Element("path").Descendants("point") select new Vector2i((Coords.X * FieldsInLine) + int.Parse(point.Element("x").Value), (Coords.Y * FieldsInLine) + int.Parse(point.Element("y").Value)))
                            : null,
                                                  enemy.Element("health") != null
                            ? int.Parse(enemy.Element("health").Value)
                            : -1,
                                                  this));
            }
            else
            {
                Enemies = new HashSet <Enemy>();
            }
        }
예제 #8
0
        private void LoadNineRegions(Vector2i coords, bool save = true)
        {
            if (coords.IsValid())
            {
                Regions.ToList().ForEach(region =>
                {
                    if (region.Coords.X > coords.X + 1 || region.Coords.X < coords.X - 1 || region.Coords.Y > coords.Y + 1 || region.Coords.Y < coords.Y - 1)
                    {
                        region.Save();
                        region.Dispose();
                        Regions.Remove(region);
                    }
                });

                for (int columnModifier = -1; columnModifier <= 1; columnModifier++)
                {
                    for (int rowModifier = -1; rowModifier <= 1; rowModifier++)
                    {
                        Vector2i newCoords = new Vector2i(coords.X + columnModifier, coords.Y + rowModifier);

                        if (GetRegionFromMapCoords(newCoords) is null && newCoords.X >= 0 && newCoords.X <= MapSize && newCoords.Y >= 0 && newCoords.Y <= MapSize && RegionPathGenerator.GetRegionPath(newCoords) != null)
                        {
                            Regions.Add(new Region(newCoords, FieldsInLine, true));
                        }
                    }
                }
            }

            if (save)
            {
                GamestateManager.Instance.Save();
            }
        }