/// <summary> /// 讲生成器的通行数据拷贝到我们的tmap中 /// </summary> public void CopyUnWalkableToGrid(CStarGrid targetGrid) { //不可通行的来源有很多地方 //1 terrain的湖水 targetGrid.CopyUnWalkableFrom(TerrainGrid); //2 单位层的房子 //3 障碍物 targetGrid.CopyUnWalkableFrom(BlockGrid); }
private void CopyWalkableData(int range, CStarGrid grid, Vector2Int center) { int startCol = center.x - range; int startRow = center.y - range; for (int c = 0; c < m_grid.NumCols; c++) { for (int r = 0; r < m_grid.NumRows; r++) { bool w = grid.IsWalkable(startCol + c, startRow + r); m_grid.SetWalkable(c, r, w); } } }
public static void PrintGird(CStarGrid gird) { string str = ""; for (int row = gird.NumRows - 1; row >= 0; row--) { for (int col = 0; col < gird.NumCols; col++) { bool node = gird.IsWalkable(col, row); int v = node ? 1 : 0; str = string.Format("{0},{1}", str, v); } str += "\n"; } Debug.Log(str); }
public ActorGeneraterPlaceholder(CStarGrid walkGrid) { m_numRows = walkGrid.NumRows; m_numCols = walkGrid.NumCols; for (int row = 0; row < m_numRows; ++row) { for (int col = 0; col < m_numCols; ++col) { //这里存储不可通行的位置 bool w = walkGrid.IsWalkable(col, row); if (w) { continue; } int key = GetKey(col, row); m_unitRangeDict[key] = true; m_unitPosDict[key] = true; } } }
/// <summary> /// 根据可通行性显示格子 /// 请自行保证之前是全部隐藏的 /// </summary> public void Show(int range, CStarGrid grid, Vector3 center) { //如果放在hero下面, hero会旋转, 这样我们的格子也会被旋转. //所以需要设置格子的旋转.否则显示的和可通行数据对不上 m_tran.rotation = Quaternion.identity; Vector2Int pos = new Vector2Int((int)center.x, (int)center.z); DectectGridRange(range); CopyWalkableData(range, grid, pos); int gap = m_maxView - range; m_grid.SetStartNode(range, range); for (int c = 0; c < m_grid.NumCols; c++) { for (int r = 0; r < m_grid.NumRows; r++) { int dist = Math.Abs(c - range) + Math.Abs(r - range); if (dist > range) { continue; } m_grid.SetEndNode(c, r); var go = m_dict[(r + gap) * 1000 + (c + gap)]; bool w = m_star.FindPath(m_grid, true); //如果不可通行, 或者通行的路径大于range if (!w || m_star.Path.Count >= range) { go.SetActive(false); } else { go.SetActive(true); } } } }
public static void DrawGrid(CStarGrid gird) { var parent = new GameObject("map cube"); var t = parent.transform; t.localPosition = Vector3.zero; for (int row = gird.NumRows - 1; row >= 0; row--) { for (int col = 0; col < gird.NumCols; col++) { bool node = gird.IsWalkable(col, row); if (!node) { continue; } var go = GameObject.CreatePrimitive(PrimitiveType.Cube); go.transform.localPosition = new Vector3(col, 0, row); go.transform.SetParent(t, true); } } }
public void Generate(MapMeta meta, CStarGrid walkableGrid) { m_grid.Init(meta.Cols, meta.Rows); m_tilesData = new ActorGeneraterPlaceholder(walkableGrid); GenerateActor(meta); }
/// <summary> /// 拷贝单位占领的不可同行区域 /// </summary> public void CopyActorUnWalkableToGrid(CStarGrid targetGrid) { targetGrid.CopyUnWalkableFrom(ActorGrid); }
/// <summary> /// 根据地图配置, 随机放置角色 /// </summary> public void CreateActor(CStarGrid walkableGrid) { m_actorGen = new ActorGenerator(); m_actorGen.Generate(m_mapMeta, walkableGrid); }
public void SetStarGrid(CStarGrid grid) { m_grid = grid; }