private ChessCharacter FindAdjacentTarget(int findRange) { _cCharacter.ClearPathFindQueue(); GameManager.gameInstance.ResetTilePath(_cCharacter.name); ChessTile startTile; startTile = GameManager.gameInstance.tilemap.GetTile <ChessTile>(_cCharacter.GetTilePosition()); startTile.SetPrevPathTileNodeMap(_cCharacter.name, startTile); _cCharacter.PushPathFindTile(startTile); ChessTile nextQueueTile = _cCharacter.PopPathFindTile(); while (nextQueueTile != null) //###남은 갯수 체크가 아닌 PopPathFindTile 반환 값으로 판별하도록 개선 가능 { ChessTile currentTile = nextQueueTile; //가장 인근의 캐릭터 반환 if (currentTile.gameObject != null && currentTile.GetDistanceWeight() != 0) { ChessCharacter targetCharacter = currentTile.gameObject.GetComponent <ChessCharacter>(); if (_cCharacter.GetCharacterType() != targetCharacter.GetCharacterType() && targetCharacter.GetCharacterType() != ChessCharacter.eCharacterType.WAIT) // 적일 때만 탐색 성공 시킨다. { return(targetCharacter); } } else if (currentTile.GetDistanceWeight() == findRange + 1) { return(null); } else { for (int i = 0; i < (int)ChessCharacter.Direction.MAXSIZE; i++) { ChessCharacter.Direction direction = (ChessCharacter.Direction)i; Vector3Int nextTilePos = currentTile.GetTilePosition() + _cCharacter.GetDirectionTileNext(direction); ChessTile nextTile = GameManager.gameInstance.tilemap.GetTile <ChessTile>(nextTilePos); if (_cCharacter.IsInWall(nextTilePos) && //맵 안에 있을 때 분기 nextTile.GetPrevPathTileNodeMap(_cCharacter.name) == null) // 이미 이전 타일 세팅 안되있을 때 분기 { nextTile.SetPrevPathTileNodeMap(_cCharacter.name, currentTile); nextTile.SetDistanceWeight(currentTile.GetDistanceWeight() + 1); _cCharacter.PushPathFindTile(nextTile); } } } nextQueueTile = _cCharacter.PopPathFindTile(); } return(null); }
private void FindPath(Vector3Int targetTilePosition) { _cCharacter.ClearPathFindQueue(); _cCharacter.ClearPathStack(); GameManager.gameInstance.ResetTilePath(_cCharacter.name); ChessTile startTile; startTile = GameManager.gameInstance.tilemap.GetTile <ChessTile>(_cCharacter.GetTilePosition()); startTile.SetPrevPathTileNodeMap(_cCharacter.name, startTile); _cCharacter.PushPathFindTile(startTile); ChessTile nextQueueTile = _cCharacter.PopPathFindTile(); while (nextQueueTile != null) { ChessTile currentTile = nextQueueTile; //목표 타일에 도달하면 반환 if (currentTile.GetTilePosition() == targetTilePosition) { ChessTile pathTile = currentTile; while (pathTile.GetPrevPathTileNodeMap(_cCharacter.name) != null && pathTile.GetPrevPathTileNodeMap(_cCharacter.name) != pathTile) { _cCharacter.PushPathStackTile(pathTile); pathTile = pathTile.GetPrevPathTileNodeMap(_cCharacter.name); targetTile = pathTile; } Debug.Log("@@@@@path finish1!!!"); return; } else { for (int i = 0; i < (int)ChessCharacter.Direction.MAXSIZE; i++) { ChessCharacter.Direction direction = (ChessCharacter.Direction)i; Vector3Int nextTilePos = currentTile.GetTilePosition() + _cCharacter.GetDirectionTileNext(direction); ChessTile nextTile = GameManager.gameInstance.tilemap.GetTile <ChessTile>(nextTilePos); if (_cCharacter.IsInWall(nextTilePos) && nextTile.GetPrevPathTileNodeMap(_cCharacter.name) == null) { nextTile.SetPrevPathTileNodeMap(_cCharacter.name, currentTile); //Debug.Log("nextTile : " + nextTile.position + "direction : " + direction); _cCharacter.PushPathFindTile(nextTile); } } } nextQueueTile = _cCharacter.PopPathFindTile(); } }