public GridRenderer SetGrid(GridBase gridBase) { _cells = gridBase.Cells; _height = gridBase.Height; _width = gridBase.Width; return(Instance); }
//AI pathing across the grid void MoveToTarget(Vector3 position) { //Determines the end (target) node. Node targetNode = GridBase.GetInstance().GetNodeFromVector3(position); Pathfinding.PathfindMaster.GetInstance().RequestPathfind(currNode, targetNode, PathfindCallback); }
/// <summary> /// 总入口 /// </summary> /// <param name="maps"></param> /// <param name="changeDirectionPer"></param> /// <param name="workableCount"></param> /// <param name="randomTimes"></param> public void OnInit(GridBase[,] maps, int changeDirectionPer, int workableCount, int randomTimes) { int rows = maps.GetLength(0); int column = maps.GetLength(1); int r = rows - 2; int c = column - 2; int startRowInd = Random.Range(2, r); int startColumnInd = Random.Range(2, c); SetStartPoint(startRowInd, startColumnInd); //设置开始点 CreateWalkableRoad(maps, changeDirectionPer, workableCount, randomTimes); for (int i = 0; i < randomTimes - 1;) { startRowInd = Random.Range(2, r); startColumnInd = Random.Range(2, c); if (IsEnd(rows, column, startRowInd, startColumnInd)) continue; SetStartPoint(startRowInd, startColumnInd); if (maps[startRowInd, startColumnInd].IsContainMapType(eMapType.Road)) { i++; CreateWalkableRoad(maps, changeDirectionPer, workableCount, randomTimes); } } CreateWall(maps, changeDirectionPer, workableCount, randomTimes); }
public Pathfinder(Node start, Node target, PathfindMaster.PathfindingJobComplete callback) { startPosition = start; endPosition = target; completeCallback = callback; gridBase = GridBase.GetInstance(); }
public void SetGrid(GridBase <PathNode> grid) { this.grid = grid; UpdateVisual(); grid.OnCellChange += OnCellChange; }
public void Rotate() { transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), 90); if (!GridBase.IsValidMove(transform)) { transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), -90); } }
private void Awake() { instance = this; CreateGrid(); CreateMouseCollision(); offset = nodePrefab.transform.localScale.x / 2; }
void Start() { pm = FindObjectOfType <PieceManager>(); gb = FindObjectOfType <GridBase>(); changeTo = -10; changeToColor = Color.red; }
public static dynamic GetTSObject(GridBase dynObject) { if (dynObject is null) { return(null); } return(dynObject.teklaObject); }
public PathNode(GridBase <PathNode> grid, int x, int y) { this.grid = grid; this.x = x; this.y = y; isWalkable = true; tile = null; }
public void MoveRight() { transform.position += new Vector3(1, 0, 0); if (!GridBase.IsValidMove(transform)) { transform.position -= new Vector3(1, 0, 0); } }
void Start() { gridBase = GridBase.GetInstance(); manager = LevelManager.GetInstance(); ui = InterfaceManager.GetInstance(); PaintAll(); }
void Start() { if (!GridBase.IsValidMove(transform)) { Debug.Log("Game Over!"); gameOver.Trigger(); Destroy(this); } }
public void SetGridNumberBase(GridBase newBase) { if (newBase != GridNumberBase) { GridNumberBase = newBase; RenderGrid(); RefreshShapes(); } }
//2-4个占4个格子的装饰 void addIsland4(GridBase grid, int index, Hashtable map) { List <int> cells = grid.getCells(index, editorCfg.pageSize); int count = NumEx.NextInt(2, 5); for (int i = 0; i <= count; i++) { int id = NumEx.NextBool() ? 2 : 3; occupyCells(grid, cells, id, 1, map); } }
// Start is called before the first frame update void Start() { if (cameraFollow != null) { cameraFollow.Setup(() => cameraPosition, () => orthoSize, true, false); } GridBase grid = new GridBase(); grid.Build(25, 30, 1f, new Vector3(-10, -10)); //uiText1 = GameObject.Find("GuestBug").GetComponent<Text>(); }
public void UpdateGrid(string player_id, GridState state) { bool isMe = (player_id == myID); bool isWatching = GameManager.GetInstance().isWatching; Transform spawnPosition; if (isMe) { spawnPosition = grid1SpawnPos; } else { if (isWatching && grid1SpawnPos.childCount <= grid2SpawnPos.childCount) { spawnPosition = grid1SpawnPos; } else { spawnPosition = grid2SpawnPos; } } GridBase target = null; grids.TryGetValue(player_id, out target); if (target == null) { string player_nick = state.player_nick; target = Instantiate(gridPrefab, spawnPosition.position, Quaternion.identity, spawnPosition); target.gameObject.name = player_nick; if (isMe) { target.transform.tag = Constants.PLACEABLE_TAG; GameManager.GetInstance().myColor = colors[grids.Count]; } SpriteRenderer render = target.GetComponent <SpriteRenderer>(); target.color = colors[grids.Count]; grids[player_id] = target; nickToID[player_nick] = player_id; if (!isMe) { ActiveGrid(player_nick); } } if (isWatching) { grids[player_id].UpdateStateFirstTime(state); } else { grids[player_id].UpdateState(state); } }
public void TakeSnapshot(GridBase <PathNode> grid, PathNode current, List <PathNode> openList, List <PathNode> closedList) { GridSnapshotAction gridSnapshotAction = new GridSnapshotAction(); gridSnapshotAction.AddAction(HideNodeVisuals); for (int x = 0; x < grid.Width; x++) { for (int y = 0; y < grid.Height; y++) { PathNode pathNode = grid.GetCell(x, y); int gCost = pathNode.gCost; int hCost = pathNode.hCost; int fCost = pathNode.fCost; Vector3 gridPosition = new Vector3(pathNode.Position.x, pathNode.Position.y) * grid.CellSize + Vector3.one * grid.CellSize * .5f; bool isCurrent = pathNode == current; bool isInOpenList = openList.Contains(pathNode); bool isInClosedList = closedList.Contains(pathNode); int tmpX = x; int tmpY = y; gridSnapshotAction.AddAction(() => { Transform visualNode = visualNodeArray[tmpX, tmpY]; SetupVisualNode(visualNode, gCost, hCost, fCost); Color backgroundColor = UtilsClass.GetColorFromString("636363"); if (isInClosedList) { backgroundColor = new Color(1, 0, 0); } if (isInOpenList) { backgroundColor = UtilsClass.GetColorFromString("009AFF"); } if (isCurrent) { backgroundColor = new Color(0, 1, 0); } var sprite = visualNode.GetComponentInChildren <SpriteRenderer>(); if (sprite != null) { sprite.color = backgroundColor; } }); } } gridSnapshotActionList.Add(gridSnapshotAction); }
/// <summary> /// Mapcell2s the index of the area.取得大图的index映射到分区网格的index /// </summary> /// <returns>The area index.</returns> /// <param name="grid">Grid.</param> /// <param name="gridArea">Grid area.</param> /// <param name="index">Index.</param> /// <param name="scale">Scale.</param> int mapIndex2AreaIndex(GridBase grid, GridBase gridArea, int index, int scale) { int areaIndex = -1; int col = grid.GetColumn(index); int row = grid.GetRow(index); col = col / scale; row = row / scale; areaIndex = gridArea.GetCellIndex(col, row); return(areaIndex); }
public void UpdateGrid(int id, GridState state) { Transform spawnPosition = id == 0 ? grid1SpawnPos : grid2SpawnPos; if (spawnPosition.childCount == 0) { var gridObj = Instantiate(gridPrefab, spawnPosition.position, Quaternion.identity, spawnPosition); gridObj.tag = id == 0 ? Constants.GRID1_TAG : Constants.GRID2_TAG; GridBase grid = gridObj.GetComponent <GridBase>(); grids[id] = grid; } grids[id].UpdateState(state); }
void occupyCells(GridBase grid, List <int> cells, int id, int size, Hashtable map) { int count = 0; int i = NumEx.NextInt(0, cells.Count); int index = 0; while (true) { if (i >= cells.Count) { i = 0; } index = cells[i]; if (size > 1) { bool canAdd = true; List <int> _cells = grid.getCells(index, size); for (int j = 0; j < _cells.Count; j++) { if (_cells[j] < 0 || map[_cells[j]] != null) { canAdd = false; break; } } if (canAdd) { for (int j = 0; j < _cells.Count; j++) { map[_cells[j]] = 99; //占用 } map[index] = id; break; } } else { if (map[index] == null) { map[index] = id; break; } } i++; count++; if (count >= cells.Count) { break; } } }
public static void DestroyGrid(GridBase gridBase) { if (gridBase == null) { return; } foreach (var cellGroup in gridBase.Cells) { foreach (var cell in cellGroup) { cell.Destroy(); } } }
public void MoveDown() { transform.position += new Vector3(0, -1, 0); if (!GridBase.IsValidMove(transform)) { transform.position -= new Vector3(0, -1, 0); GridBase.AddToGrid(transform); GridBase.CheckForLines(); this.enabled = false; FindObjectOfType <Spawner>().SpawnNewPiece(); //Destroy(this); } prevTime = Time.time; }
public void SpawnRandomEvent() { int id = Random.Range(0, 5); if (id == 0) { nextPieceHider.SetActive(false); GridBase.scoreMultiplier = 1; SetCurrentRandomEvent("Speed change!"); GridBase.fallTime = Random.Range(0.4f, 1f); Debug.Log("Fall speed changed to: " + GridBase.fallTime); } else if (id == 1) { nextPieceHider.SetActive(true); GridBase.scoreMultiplier = 1; SetCurrentRandomEvent("Piece hidden!"); } else if (id == 2) { nextPieceHider.SetActive(false); GridBase.scoreMultiplier = 1; SetCurrentRandomEvent("Lines out!"); for (int i = 2; i >= 0; i--) { GridBase.DeleteLine(i); GridBase.RowDown(i); } } else if (id == 3) { nextPieceHider.SetActive(false); SetCurrentRandomEvent("Double score!"); GridBase.scoreMultiplier = 2; } else { SetCurrentRandomEvent("None"); } }
/// 分区网格的index转成大地图每屏的index List <int> areaIndex2MapPageIndexs(GridBase grid, GridBase gridArea, int areaIndex, int scale) { List <int> ret = new List <int>(); int col = gridArea.GetColumn(areaIndex); int row = gridArea.GetRow(areaIndex); col = col * scale; row = row * scale; for (int i = col + editorCfg.pageSize / 2; i < col + scale - 1; i = i + editorCfg.pageSize) { for (int j = row + editorCfg.pageSize / 2; j < row + scale - 1; j = j + editorCfg.pageSize) { ret.Add(grid.GetCellIndex(i, j)); } } return(ret); }
// Start is called before the first frame update void Start() { gb = FindObjectOfType <GridBase>(); pieces = new List <GameObject>(); float offset = 0; for (int i = 0; i < initialX.Length; i++) { GameObject o = Instantiate(piecePrefab, this.transform); Piece p = o.GetComponent <Piece>(); p.rows = initialX[i]; p.columns = initialY[i]; p.pieceNum = i + 1; o.transform.position = new Vector3(offset + this.transform.position.x, this.transform.position.y, 1); offset += p.columns + 2; pieces.Add(o); } }
public void Setup(GridBase <PathNode> grid) { visualNodeArray = new Transform[grid.Width, grid.Height]; for (int x = 0; x < grid.Width; x++) { for (int y = 0; y < grid.Height; y++) { Vector3 gridPosition = new Vector3(x, y) * grid.CellSize + Vector3.one * grid.CellSize * .5f; if (pathFindingDebugNode != null) { Transform visualNode = CreateVisualNode(gridPosition); visualNodeArray[x, y] = visualNode; visualNodeList.Add(visualNode); } } } HideNodeVisuals(); }
void Update() { if (Input.GetKeyDown(KeyCode.LeftArrow)) { transform.position += new Vector3(-1, 0, 0); if (!GridBase.IsValidMove(transform)) { transform.position -= new Vector3(-1, 0, 0); } } else if (Input.GetKeyDown(KeyCode.RightArrow)) { transform.position += new Vector3(1, 0, 0); if (!GridBase.IsValidMove(transform)) { transform.position -= new Vector3(1, 0, 0); } } else if (Input.GetKeyDown(KeyCode.UpArrow)) { transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), 90); if (!GridBase.IsValidMove(transform)) { transform.RotateAround(transform.TransformPoint(rotationPoint), new Vector3(0, 0, 1), -90); } } if (Time.time - prevTime > (Input.GetKey(KeyCode.DownArrow) ? GridBase.fallTime / 10 : GridBase.fallTime)) { transform.position += new Vector3(0, -1, 0); if (!GridBase.IsValidMove(transform)) { transform.position -= new Vector3(0, -1, 0); GridBase.AddToGrid(transform); GridBase.CheckForLines(); this.enabled = false; FindObjectOfType <Spawner>().SpawnNewPiece(); Destroy(this); } prevTime = Time.time; } }
public PathFinding(int width, int height, float cellSize, Tilemap tilemap) { grid = new GridBase <PathNode>(width, height, cellSize, new Vector3(0, 0, 0), (GridBase <PathNode> g, int x, int y) => new PathNode(g, x, y)); tilemap.CompressBounds(); BoundsInt bounds = tilemap.cellBounds; TileBase[] allTiles = tilemap.GetTilesBlock(bounds); for (int x = 0; x < bounds.size.x; x++) { for (int y = 0; y < bounds.size.y; y++) { TileBase tile = allTiles[x + y * bounds.size.x]; if (tile != null) { grid.GetGridObject(x, y).SetTileBase(tile); } } } }
IEnumerator SlotIntoPlace(Vector2 startingPos, Vector2 endingPos) { float duration = 0.1f; float elapsedTime = 0; while (elapsedTime < duration) { transform.position = Vector2.Lerp(startingPos, endingPos, elapsedTime / duration); elapsedTime += Time.deltaTime; yield return(new WaitForEndOfFrame()); } // SoundManager.GetInstance().MakeTileSound(); transform.position = endingPos; GridBase gridBase = transform.parent.parent.parent.GetComponent <GridBase>(); gridBase.UpdateLastTile(this); int[] cell = gridBase.PosToGrid(endingPos); gridBase.UpdateGridVal(cell[0], cell[1], topVal, midVal, botVal); cell[1] = (8 - cell[1]) / 3; EventManager.GetInstance().PostNotification(EVENT_TYPE.PLACE_PIECE, this, cell); transform.tag = Constants.UNTAGGED; }
public void StartCreateBlocks(GridBase[,] mapData) { int r = mapData.GetLength(0); int c = mapData.GetLength(1); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { GameObject go = null; if (mapData[i, j].IsContainMapType(eMapType.BlockCanPass)) { go = Instantiate(canPassObj) as GameObject; } else if(mapData[i, j].IsContainMapType(eMapType.BlockCannotPass)) { go = Instantiate(cannotPassObj) as GameObject; } if (go == null) continue; go.transform.parent = transform; Vector3 worldPos = GetWorldPos(mapData[i, j].RId, mapData[i, j].CId); //设置层级 int layercount = mapData[i, j].SortOrder; Transform gridTf = MapObj.transform.FindChild(MapObj.GetGridName(i, j, mapData[i, j])); go.transform.parent = gridTf; go.transform.localPosition = gridTf.InverseTransformPoint(worldPos); go.transform.GetComponentInChildren<Renderer>().material.renderQueue = layercount; go.name = i + "_" + j+"_"+ layercount; } } }
/// <summary> /// Initializes standard graphic elements. /// </summary> private void InitializeElements() { this.gridBase = GridBase.None; this.gridFactor = 5; }
/// <summary> /// 格子是否为墙壁 /// </summary> /// <param name="r"></param> /// <param name="c"></param> /// <returns></returns> private bool IsWall(GridBase[,] maps, int rows, int column, int r, int c) { if (r < 1 || c < 1 || r > (rows - 2) || c > (column - 2)) return true; if (maps[r, c].IsContainMapType(eMapType.Road)) return false; if (maps[r - 1, c].IsContainMapType ( eMapType.Road) || maps[r + 1, c].IsContainMapType( eMapType.Road) || maps[r, c - 1].IsContainMapType ( eMapType.Road) || maps[r, c + 1].IsContainMapType( eMapType.Road)) { return true; } return false; }
/// <summary> /// 创建墙壁 /// </summary> /// <param name="maps"></param> /// <param name="changeDirectionPer"></param> /// <param name="workableCount"></param> /// <param name="randomTimes"></param> private void CreateWall(GridBase[,] maps, int changeDirectionPer, int workableCount, int randomTimes) { int rows = maps.GetLength(0); int column = maps.GetLength(1); for (int i = 0; i < rows; i++) { for (int j = 0; j < column; j++) { if (IsWall(maps,rows,column,i, j)) maps[i, j].AddMapType(eMapType.Wall); } } }
/// <summary> /// 创建可行走格子 /// </summary> /// <param name="maps"></param> /// <param name="changeDirectionPer"></param> /// <param name="workableCount"></param> /// <param name="randomTimes"></param> private void CreateWalkableRoad(GridBase[,] maps, int changeDirectionPer, int workableCount, int randomTimes) { int count = 0; int cacheLastDir = -10; int rows = maps.GetLength(0); int column = maps.GetLength(1); do { if (count >= workableCount) break; int changePer = Random.Range(0, 100); eDirectionType dir = eDirectionType.None; if (changePer >= changeDirectionPer && cacheLastDir >= 0) { //保持原来的方向前进 dir = direction[cacheLastDir]; } else { int dirInd = Random.Range(0, direction.Length); if (Mathf.Abs(cacheLastDir - dirInd) == 1) dir = direction[cacheLastDir]; else dir = direction[dirInd]; cacheLastDir = dirInd; } int tempColumn = 0; int tempRow = 0; GetRowAndColumnIndex(dir, ref tempColumn, ref tempRow); if (IsEnd( rows , column,startRowInd + tempRow, startColumnInd + tempColumn)) cacheLastDir = -10; //清除缓存方向,重新随机产生下一个方向 else { count++; startColumnInd += tempColumn; startRowInd += tempRow; maps[startRowInd, startColumnInd].AddMapType(eMapType.Road);//可走的 } } while (true); }
/// <summary>设置格子上的物体的层级</summary> /// <param name="maps"></param> private void CreateSortLayer(GridBase[,] maps) { int r = maps.GetLength(0); int c = maps.GetLength(1); for (int i = 0; i < r; i++) { for (int j = 0; j < c; j++) { int sort = 4 * c - 2 * (j + i); maps[i, j].SortOrder = sort; } } }
// Start is called before the first frame update void Start() { x = 0; y = 0; gb = FindObjectOfType <GridBase>(); }
public string GetGridName(int r,int c, GridBase grid) { return "map_" + r + "_" + c+ "_" + (ushort)grid.GetMapType(); ; }
//创建地图 private void RandomCreateRoad(GridBase[,] maps,int maxLinkCount, int changeDirectionPer, int workableCount) { //创建地图 //随机 地图在同方向上的个数 , 不大于 地图总格子数 int rows = maps.GetLength(0); int columns = maps.GetLength(1); int count = 0; int cacheLastDir = -10; int linkCount = 0; do { if (count >= workableCount) break; int changePer = UnityEngine.Random.Range(0, 100); eDirectionType dir = eDirectionType.None; //随机生成最大连接数 linkCount = UnityEngine.Random.Range(2, maxLinkCount); if (changePer >= changeDirectionPer && cacheLastDir >= 0) { //保持原来的方向前进 dir = direction[cacheLastDir]; } else { int dirInd = UnityEngine.Random.Range(0, direction.Length); if (Mathf.Abs(cacheLastDir - dirInd) == 1) dir = direction[cacheLastDir]; else dir = direction[dirInd]; cacheLastDir = dirInd; } for (int i = 0; i < linkCount; i++) { int tempColumn = 0; int tempRow = 0; GetRowAndColumnIndex(dir, ref tempColumn, ref tempRow); if (IsEnd(rows, columns, startRowInd + tempRow, startColumnInd + tempColumn)) { cacheLastDir = -10; //清除缓存方向,重新随机产生下一个方向 break; } else { count++; startColumnInd += tempColumn; startRowInd += tempRow; maps[startRowInd, startColumnInd].ClearAndAddMaptType( eMapType.Road);//可走的 if(!RoadList.Contains(maps[startRowInd,startColumnInd])) { RoadList.Add(maps[startRowInd, startColumnInd]);//加入列表 } } } } while (true); }
/// <summary> /// 格子是否为墙壁 /// </summary> /// <param name="r"></param> /// <param name="c"></param> /// <returns></returns> private bool IsWall(GridBase[,] maps, int rows, int column, int r, int c) { //if ((r < 1 || c < 1 || r > (rows - 2) || c > (column - 2))) return true; if (maps[r, c].IsContainMapType( eMapType.Road)) return false; //是否处于可行走格子邻边 bool isNearRoad = IsNearRoad(maps,rows, column,r, c); if (isNearRoad) return true; return false; }
/// <summary> /// 是否处于可行走格子邻边 /// </summary> /// <param name="maps"></param> /// <param name="r"></param> /// <param name="c"></param> /// <returns></returns> private bool IsNearRoad(GridBase[,] maps, int rows, int column, int r, int c) { bool isNearRoad = false; bool leftBl = false; bool rightBl = false; bool upBl = false; bool downBl = false; if (r - 1 >= 0) leftBl = maps[r - 1, c].IsContainMapType( eMapType.Road); if (r + 1 <= rows - 1) rightBl = maps[r + 1, c].IsContainMapType( eMapType.Road); if (c - 1 >= 0) upBl = maps[r, c - 1].IsContainMapType( eMapType.Road); if (c + 1 <= column - 1) downBl = maps[r, c + 1].IsContainMapType( eMapType.Road); isNearRoad = leftBl || rightBl || upBl|| downBl; return isNearRoad; }
/// <summary> /// 在矩形地图四条边上生成 开始路点 /// </summary> /// <param name="maps"></param> /// <param name="i"></param> private string GetRoadStartPoint(GridBase[,] maps,int i) { int rows = maps.GetLength(0); int column = maps.GetLength(1); int r = rows - 2; int c = column - 2; int startRowInd = 0;// UnityEngine.Random.Range(2, r); int startColumnInd = 0;// UnityEngine.Random.Range(2, c); string result = ""; switch (i) { case 0://上 startColumnInd = UnityEngine.Random.Range(2, c); startRowInd = r; if (maps[startRowInd, startColumnInd].IsContainMapType(eMapType.None) && maps[startRowInd,startColumnInd - 1].IsContainMapType(eMapType.None) && maps[startRowInd,startColumnInd + 1].IsContainMapType(eMapType.None))//相邻都没有路 { result = startRowInd+"_"+startColumnInd; } break; case 1://左 startColumnInd = 1; startRowInd = UnityEngine.Random.Range(2, r); if (maps[startRowInd, startColumnInd].IsContainMapType( eMapType.None) && maps[startRowInd - 1, startColumnInd].IsContainMapType( eMapType.None) && maps[startRowInd + 1, startColumnInd].IsContainMapType( eMapType.None))//相邻都没有路 { result = startRowInd + "_" + startColumnInd; } break; case 2://下 startColumnInd = UnityEngine.Random.Range(2, c); startRowInd = 1; if (maps[startRowInd, startColumnInd].IsContainMapType( eMapType.None) && maps[startRowInd, startColumnInd - 1].IsContainMapType( eMapType.None) && maps[startRowInd, startColumnInd + 1].IsContainMapType( eMapType.None))//相邻都没有路 { result = startRowInd + "_" + startColumnInd; } break; case 3://右 startColumnInd = c; startRowInd = UnityEngine.Random.Range(2, r); if (maps[startRowInd, startColumnInd].IsContainMapType( eMapType.None) && maps[startRowInd - 1, startColumnInd].IsContainMapType( eMapType.None) && maps[startRowInd + 1, startColumnInd].IsContainMapType( eMapType.None))//相邻都没有路 { result = startRowInd + "_" + startColumnInd; } break; } if (string.IsNullOrEmpty(result) == false) return result; //递归 return GetRoadStartPoint(maps, i); }
/// <summary> /// 每条边上的起点坐标 15_5 格式 /// </summary> /// <param name="randomTimes"></param> /// <returns></returns> private string[] GetRandomRoadPoints(GridBase[,] maps, int randomTimes) { int count = randomTimes / 4; //矩形的每个边的随机道路个数 string[] randomDirectionCouns = new string[randomTimes]; System.Random random = new System.Random(); for (int i = 0; i < randomTimes; i++) { randomDirectionCouns[i] = GetRoadStartPoint(maps, i % count); } return randomDirectionCouns; }
public AStarPath(GridBase[,] maps) { this.maps = maps; }
/// <summary> /// 初始化地图 /// </summary> private void InitMap() { maps = new GridBase[rows, column]; for (int i = 0; i < rows; i++) { for (int j = 0; j < column; j++) { maps[i, j] = new GridBase(i, j); } } }
/// <summary> /// 获取下一次位置相对于当前位置的转向方向 /// </summary> /// <param name="currentGrid"></param> /// <param name="nextGrid"></param> /// <returns></returns> public static eDirectionType GetNextDirection(GridBase currentGrid,GridBase nextGrid) { return GetNextDirection(currentGrid.RId,currentGrid.CId,nextGrid.RId,nextGrid.CId); }
/// <summary> /// 地图生成初始化 /// </summary> /// <param name="maps"></param> /// <param name="changeDirectionPer"></param> /// <param name="workableCount"></param> /// <param name="randomTimes"></param> public void OnInit(GridBase[,] maps, int changeDirectionPer, int workableCount, int randomTimes) { //创建可行走道路 CreateRoad(maps, randomTimes, this.maxLinkCount, changeDirectionPer, workableCount); //创建墙壁 CreateWall(maps, changeDirectionPer, workableCount, randomTimes); //生成格子对应的层级 CreateSortLayer(maps); //生成障碍物 CreateBlocks(); }
/// <summary> /// 生成地图 /// </summary> /// <param name="maps"></param> /// <param name="randomTimes"></param> /// <param name="maxLinkCount"></param> /// <param name="changeDirectionPer"></param> /// <param name="workableCount"></param> private void CreateRoad(GridBase[,] maps, int randomTimes, int maxLinkCount, int changeDirectionPer, int workableCount) { string[] randomStartPointsArr = GetRandomRoadPoints(maps, randomTimes); for (int i = 0; i < randomStartPointsArr.Length; i++) { string startPointStr = randomStartPointsArr[i]; // Debug.Log(startPointStr); int startRowId = int.Parse(startPointStr.Split('_')[0]); int startColumnId = int.Parse(startPointStr.Split('_')[1]); SetStartPoint(startRowId, startColumnId); //设置起点 RandomCreateRoad(maps, maxLinkCount, changeDirectionPer, workableCount); //生成道路 } }