Example #1
0
        public void TestBiggerMap()
        {
            var map = new WorldMap <Chunk>();

            var chunk1 = CreateChunk(0, 0);
            var chunk2 = CreateChunk(1, 0);
            var chunk3 = CreateChunk(-1, 0);
            //chunk3.SetTile(15, 0, 2);
            var chunk4 = CreateChunk(0, 1);
            var chunk5 = CreateChunk(0, -1);
            var chunk6 = CreateChunk(1, 1);
            var chunk7 = CreateChunk(-1, -1);
            var chunk8 = CreateChunk(-1, 1);
            var chunk9 = CreateChunk(1, -1);

            map.AddChunk(chunk1);
            map.AddChunk(chunk2);
            map.AddChunk(chunk3);
            map.AddChunk(chunk4);
            map.AddChunk(chunk5);
            map.AddChunk(chunk6);
            map.AddChunk(chunk7);
            map.AddChunk(chunk8);
            map.AddChunk(chunk9);

            var passableMapArray = PathfinderHelper.GetPassableByteArray(new Position(0, 0), new Position(-1, 0), map.Chunks);

            var path = WorldMap <Chunk> .FindPath(new Position(0, 0), new Position(-1, 0), map.Chunks);

            Assert.That(path.Count == 2);

            Assert.That(path[0].X == 0 && path[0].Y == 0);
            Assert.That(path[1].X == -1 && path[1].Y == 0);
        }
Example #2
0
        /// <summary>
        /// Parses a byte stream into a Map, reading from as a TILED .tmx in csv format
        /// </summary>
        public static WorldMap <ChunkType> Parse <ChunkType>(Stream mapStream) where ChunkType : Chunk, new()
        {
            WorldMap <ChunkType> map = new WorldMap <ChunkType>();

            using (XmlReader reader = XmlReader.Create(mapStream))
            {
                var doc  = XDocument.Load(reader);
                var xMap = doc.Element("map");

                foreach (var tileset in xMap.Elements("tileset"))
                {
                    var    imgSource   = tileset.Element("image").Attribute("source").Value;
                    var    tilesetName = imgSource.Split('/').Last();
                    byte[] image       = MapLoader.LoadImageData(tilesetName);
                    map.Tilesets.Add(tilesetName, image);
                }

                foreach (var xLayer in xMap.Elements("layer"))
                {
                    var layerType = GetLayer(xLayer.Attribute("id").Value);

                    foreach (var xChunk in xLayer.Element("data").Elements("chunk"))
                    {
                        var csvData = Regex.Replace((string)xChunk.Value, @"\t|\n|\r", "");
                        var chunkX  = Int16.Parse(xChunk.Attribute("x").Value) / Chunk.SIZE;
                        var chunkY  = Int16.Parse(xChunk.Attribute("y").Value) / Chunk.SIZE;

                        if (layerType == LayerType.TILES)
                        {
                            var chunk = new ChunkType()
                            {
                                x = chunkX,
                                y = chunkY
                            };
                            var tileArray = csvData.Split(',');
                            for (int y = 0; y < Chunk.SIZE; y++)
                            {
                                for (int x = 0; x < Chunk.SIZE; x++)
                                {
                                    var tile = Int16.Parse(tileArray[x + y * Chunk.SIZE]);
                                    chunk.SetTile(x, y, tile);
                                }
                            }
                            map.AddChunk(chunk);
                        }
                    }
                }
            }
            return(map);
        }
Example #3
0
        public void TestSomePathfinding()
        {
            var chunk1 = CreateChunk(0, 0);

            chunk1.Tiles[1, 0].TileId = BLOCK;
            chunk1.Tiles[1, 1].TileId = BLOCK;
            chunk1.Tiles[1, 2].TileId = BLOCK;
            _map.AddChunk(chunk1);

            var path = _map.FindPath(new Position(0, 0), new Position(2, 0));

            Assert.That(path.Count == 9);

            Assert.That(path[0].X == 0 && path[0].Y == 0);
            Assert.That(path[1].X == 0 && path[1].Y == 1);
            Assert.That(path[2].X == 0 && path[2].Y == 2);
            Assert.That(path[3].X == 0 && path[3].Y == 3);
            Assert.That(path[4].X == 1 && path[4].Y == 3);
            Assert.That(path[5].X == 2 && path[5].Y == 3);
            Assert.That(path[6].X == 2 && path[6].Y == 2);
            Assert.That(path[7].X == 2 && path[7].Y == 1);
            Assert.That(path[8].X == 2 && path[8].Y == 0);
        }
Example #4
0
        public void Test1()
        {
            var chunk1 = CreateChunk(0, 0);

            chunk1.SetTile(1, 0, BLOCK);
            chunk1.SetTile(1, 1, BLOCK);
            chunk1.SetTile(1, 2, BLOCK);
            _map.AddChunk(chunk1);

            var path = _map.FindPath(new Position(0, 0), new Position(2, 0));

            Assert.That(path.Count == 9);

            Assert.That(path[0].X == 0 && path[0].Y == 0);
            Assert.That(path[1].X == 0 && path[1].Y == 1);
            Assert.That(path[2].X == 0 && path[2].Y == 2);
            Assert.That(path[3].X == 0 && path[3].Y == 3);
            Assert.That(path[4].X == 1 && path[4].Y == 3);
            Assert.That(path[5].X == 2 && path[5].Y == 3);
            Assert.That(path[6].X == 2 && path[6].Y == 2);
            Assert.That(path[7].X == 2 && path[7].Y == 1);
            Assert.That(path[8].X == 2 && path[8].Y == 0);
        }