Exemplo n.º 1
0
 private IEnumerable<Fruit> GetListOfFruit(World world, ref Tile[,] tiles)
 {
     var result = new List<Fruit>();
     foreach (WorldArea wa in this._worldAreas)
         {
         foreach (FruitDefinition fd in wa.FruitDefinitions.Values)
             {
             for (int i = 0; i < fd.FruitQuantity; )
                 {
                 var tilePos = new TilePos(wa.Area.X + Rnd.Next(wa.Area.Width), wa.Area.Y + Rnd.Next(wa.Area.Height));
                 Tile t = tiles[tilePos.X, tilePos.Y];
                 if (!t.IsFree)
                     continue;
                 tiles[tilePos.X, tilePos.Y].SetOccupationByFruit();
                 Vector2 position = tilePos.ToPosition();
                 var f = new Fruit(world, position, fd.FruitType);
                 result.Add(f);
                 i++;
                 }
             }
         }
     return result;
 }
Exemplo n.º 2
0
        private static IEnumerable<TileException> SetTileOccupation(ref Tile[,] tiles, IEnumerable<StaticItem> gameObjects, bool isMoving)
        {
            Action<Tile> action;
            if (isMoving)
                action = t => t.SetOccupationByMovingMonster();
            else
                action = t => t.SetOccupationByStaticItem();

            var result = new List<TileException>();
            foreach (var item in gameObjects)
                {
                TilePos tp = item.TilePosition;

                try
                    {
                    action(tiles[tp.X, tp.Y]);
                    }
                catch (TileException te)
                    {
                    var nte = new TileException(te, string.Format("{0} at tile {1},{2} for {3} occupation", item.GetType().Name, tp.X, tp.Y, isMoving ? "moving" : "static"));
                    result.Add(nte);
                    }
                }
            return result;
        }
Exemplo n.º 3
0
        private IEnumerable<Wall> GetLayout(World world, out Tile[,] tiles)
        {
            var worldDef = (XmlElement)this._xmlDoc.SelectSingleNode("World");
            if (worldDef == null)
                throw new InvalidOperationException("No World element found.");
            int width = int.Parse(worldDef.GetAttribute("Width"));
            int height = int.Parse(worldDef.GetAttribute("Height"));
            var layout = (XmlElement) this._xmlDoc.SelectSingleNode("World/Layout");
            if (layout == null)
                throw new InvalidOperationException("No Layout element found.");
            tiles = new Tile[width, height];

            var lines = layout.InnerText.Trim().Replace("\r\n", "\t").Split('\t');
            if (lines.GetLength(0) != height)
                throw new InvalidOperationException();

            for (int y = 0; y < height; y++)
                {
                lines[y] = lines[y].Trim();
                if (lines[y].Length != width)
                    throw new InvalidOperationException();
                }

            var result = new List<Wall>();
            foreach (WorldArea wa in this._worldAreas)
                {
                if (wa.TileDefinitions.Count == 0)
                    continue;

                string defaultFloorName = wa.TileDefinitions.Values.Single(td => td.TileTypeByMap == TileTypeByMap.Floor).TextureName;
                foreach (TilePos p in wa.Area.PointsInside())
                    {
                    char symbol = lines[p.Y][p.X];
                    TileDefinition td;
                    if (!wa.TileDefinitions.TryGetValue(symbol, out td))
                        {
                        string text = string.Format("Don't know what symbol {0} indicates in world area {1}", symbol, wa.Id);
                        throw new InvalidOperationException(text);
                        }
                    Texture2D floor;
                    switch (td.TileTypeByMap)
                        {
                        case TileTypeByMap.Wall:
                            floor = world.LoadTexture("Tiles/" + defaultFloorName);
                            tiles[p.X, p.Y] = new Tile(floor, TileTypeByMap.Wall);
                            var wall = new Wall(world, p.ToPosition(), "Tiles/" + td.TextureName);
                            result.Add(wall);
                            break;
                        case TileTypeByMap.Floor:
                            floor = world.LoadTexture("Tiles/" + td.TextureName);
                            tiles[p.X, p.Y] = new Tile(floor, TileTypeByMap.Floor);
                            break;
                        case TileTypeByMap.PotentiallyOccupied:
                            floor = world.LoadTexture("Tiles/" + defaultFloorName);
                            tiles[p.X, p.Y] = new Tile(floor, symbol);
                            break;
                        default:
                            throw new InvalidOperationException();
                        }
                    }
                }

            return result;
        }
Exemplo n.º 4
0
 private static void ReviewPotentiallyOccupiedTiles(ref Tile[,] tiles, ICollection<TileException> exceptions)
 {
     var cy = tiles.GetLength(1);
     var cx = tiles.GetLength(0);
     for (int y = 0; y < cy; y++)
         {
         for (int x = 0; x < cx; x++)
             {
             Tile t = tiles[x, y];
             try
                 {
                 t.CheckIfIncorrectlyPotentiallyOccupied();
                 }
             catch (TileException te)
                 {
                 var nte = new TileException(te, string.Format("Tile at {0},{1}", x, y));
                 exceptions.Add(nte);
                 }
             }
         }
 }