Esempio n. 1
0
        private void loadTiles()
        {
            for (int tileY = 0; tileY < tileCountY; ++tileY)
            {
                for (int tileX = 0; tileX < tileCountX; ++tileX)
                {
                    AStarData astarData = astarDatas.getAstarData(tileY, tileX);

                    for (int cellY = 0; cellY < cellCountYPerTile; ++cellY)
                    {
                        for (int cellX = 0; cellX < cellCountXPerTile; ++cellX)
                        {
                            int obstacle = astarData.getObstacle(cellX, cellY);
                            if (obstacle == 1)
                            {
                                continue;
                            }

                            int x = tileX * cellCountXPerTile + cellX;
                            int y = tileY * cellCountYPerTile + cellY;

                            /*UnityEngine.GameObject go = new UnityEngine.GameObject();
                             * AStarHelper ash = go.AddComponent<AStarHelper>();
                             * ash.Length = 1;
                             * go.transform.position = new UnityEngine.Vector3(x,0,-y);*/
                            int cellId = makeCellId(x, y);
                            cells.Add(cellId, new AStarCell(x, y));
                        }
                    }
                }
            }
        }
	    public static AStarData Parse(String[] str)
        {
            //String fileName = System.IO.Path.GetFileName(file);
            //StreamReader sr = new StreamReader(file, Encoding.Default);
		    try
            {			
			    String[] headerInfo = str[0].Split(SPLIT_SUFFIX.ToCharArray());
			    if(headerInfo.Length != 3)
                {
				    //throw new ApplicationException("astar文件头数据不合法,文件:" + fileName);
			    }
			
			    int widthInCells = int.Parse(headerInfo[0]);
			    int heightInCells = int.Parse(headerInfo[1]);
			    float cellSize = float.Parse(headerInfo[2]);

				AStarData astarData = new AStarData(widthInCells, heightInCells, cellSize);

				astarData.obstacleInfo = new int[heightInCells, widthInCells];
				astarData.heightInfo = new float[heightInCells, widthInCells];
			
			    for(int h = 0; h < heightInCells; ++h)
                {
                    String[] lineData = str[h+1].Split(SPLIT_SUFFIX.ToCharArray());
				    if(lineData.Length < widthInCells * 2)
                    {
                        //throw new ApplicationException("astar寻路文件错误:读取文件错误。文件:" + fileName + ",行:" + (h + 2));
				    }
				
				    for(int w = 0; w < widthInCells; ++w)
                    {
						astarData.obstacleInfo[h, w] = int.Parse(lineData[w * 2]);
						astarData.heightInfo[h, w] = float.Parse(lineData[w * 2 + 1]);
				    }
			    }

				return astarData;
		    }
            catch (IOException e)
            {
                //throw new ApplicationException("astar寻路文件错误:读取文件错误。文件:" + fileName);
		    }
            finally
            {
                //sr.Close();
			}
			return null;
	    }
Esempio n. 3
0
        public void initstring(int tileRol, int tileCol, string str)
        {
            int       tileId = makeTileId(tileRol, tileCol);
            AStarData data;

            if (astarDatas.TryGetValue(tileId, out data))
            {
                return;
            }
            string[] strs = str.Split(new char[] { '\n', '\r' });

            AStarData astarData = AStarData.Parse(strs);

            astarDatas.Add(tileId, astarData);

            if (_nullTile == null)
            {
                _nullTile = new AStarDataNull(astarData.getWidthInCells(), astarData.getHeightInCells(), astarData.getCellSize());
            }
        }
Esempio n. 4
0
        private void checkTiles()
        {
            for (int h = 0; h < tileCountY; ++h)
            {
                for (int w = 0; w < tileCountX; ++w)
                {
                    AStarData astarData = astarDatas.getAstarData(h, w);;
                    if (astarData == null)
                    {
                        throw new ApplicationException("tile invalid:" + h + "," + w);
                    }

                    if (astarData.getWidthInCells() != cellCountXPerTile ||
                        astarData.getHeightInCells() != cellCountYPerTile)
                    {
                        throw new ApplicationException("tile size invalid:" + h + "," + w);
                    }
                }
            }
        }
Esempio n. 5
0
        public AStarMultiTileMap(AStarDataMgr datas, int tileCountX, int tileCountY)
        {
            this.astarDatas = datas;
            this.tileCountX = tileCountX;
            this.tileCountY = tileCountY;

            AStarData firstData = astarDatas.getAstarData(0, 0);

            this.cellCountXPerTile = firstData.getWidthInCells();
            this.cellCountYPerTile = firstData.getHeightInCells();
            this.cellSize          = firstData.getCellSize();
            this.errorTolerate     = cellSize * ERROR_TOLERATE_RATIO;

            this.widthInCells  = tileCountX * cellCountXPerTile;
            this.heightInCells = tileCountY * cellCountYPerTile;

            checkTiles();

            loadTiles();
        }