void MoveToNextPoint_Native() { if (IsMoving) { return; } if (!NavLibrary.GetPoint(++curStep, ref curRow, ref curCol)) { IsMoving = false; objSign.SetActive(false); return; } Vector3 position = new Vector3(curCol, 0, curRow); if (position == transform.position) { MoveToNextPoint_Native(); return; } Tweener tweener = transform.DOMove(position, fMoveCost); tweener.SetEase(Ease.InOutQuad); tweener.onComplete = () => { IsMoving = false; MoveToNextPoint_Native(); }; IsMoving = true; }
private void Start() { Instance = this; if (IsNative) { InitNative(); } else { InitLocal(); } GameObject objPrefab = Resources.Load <GameObject>("Prefabs/Brick"); GameObject objBrick = Instantiate(objPrefab, transform); objBrick.name = "Ground"; objBrick.transform.GetChild(0).gameObject.layer = LayerMask.NameToLayer("Brick"); objBrick.transform.localScale = new Vector3(MaxCol, 1, MaxRow); objBrick.transform.position = new Vector3(MaxCol / 2 - 0.5f, 0, MaxRow / 2 - 0.5f); objBrick.GetComponentInChildren <Renderer>().material.mainTextureScale = new Vector2(MaxCol, MaxRow); GameObject objWallsRoot = new GameObject("WallsRoot"); objWallsRoot.transform.SetParent(transform); for (int i = 0, lenRow = Mathf.CeilToInt(MaxRow / RectMeshCreater.MaxRow); i < lenRow; ++i) { for (int j = 0, lenCol = Mathf.CeilToInt(MaxCol / RectMeshCreater.MaxCol); j < lenCol; ++j) { RectMeshCreater.GenerateParams param = new RectMeshCreater.GenerateParams() { row = RectMeshCreater.MaxRow, col = RectMeshCreater.MaxCol, startRow = i * RectMeshCreater.MaxRow, startCol = j * RectMeshCreater.MaxCol, materialPath = "Assets/Resources/Materials/WallMat.mat", objName = "Wall_" + i + "_" + j, parent = objWallsRoot.transform, cbIsGenerateCell = (cellRow, cellCol) => { if (IsNative) { return(NavLibrary.GetPointStatus(cellRow, cellCol) == 0); } NavPoint point = GetPoint(cellRow, cellCol); return(point && point.type < 1); } }; RectMeshCreater.GenerateRectObj(new Vector3(j * RectMeshCreater.MaxCol, 0.01f, i * RectMeshCreater.MaxRow), param); } } GameObject objPlayer = Instantiate(Resources.Load <GameObject>("Prefabs/Myself")); objPlayer.name = "Myself"; objPlayer.transform.position = Vector3.zero; }
void ShowRoad_Native() { if (mfPath && mfPath.gameObject.activeSelf) { mfPath.gameObject.SetActive(false); return; } if (bPathGenerated && mfPath) { mfPath.gameObject.SetActive(true); return; } dicNavWay.Clear(); int row = 0, col = 0, step = -1; int minRow = int.MaxValue, minCol = int.MaxValue, maxRow = int.MinValue, maxCol = int.MinValue; while (NavLibrary.GetPoint(++step, ref row, ref col)) { if (row < minRow) { minRow = row; } if (col < minCol) { minCol = col; } if (row > maxRow) { maxRow = row; } if (col > maxCol) { maxCol = col; } dicNavWay[NavLibrary.GetPointID(row, col)] = true; } RectMeshCreater.GenerateParams param = new RectMeshCreater.GenerateParams() { row = maxRow - minRow + 1, col = maxCol - minCol + 1, startRow = minRow, startCol = minCol, materialPath = "Assets/Resources/Materials/PathMat.mat", objName = "Path", mfOperate = mfPath, parent = MapManager.Instance.transform, cbIsGenerateCell = (cellRow, cellCol) => { return(dicNavWay.ContainsKey(NavLibrary.GetPointID(cellRow, cellCol))); } }; mfPath = RectMeshCreater.GenerateRectObj(new Vector3(minCol, 0.02f, minRow), param); mfPath.gameObject.SetActive(true); bPathGenerated = true; }
void GenerateCoin() { fTimeCounter = 0; do { row = Random.Range(0, MapManager.MaxRow + 1); col = Random.Range(0, MapManager.MaxCol + 1); } while (!MapManager.Instance.IsPointClear(row, col)); NavLibrary.SetPointStatus(row, col, MapManager.Instance.nBigCoinValue); MapManager.Instance.OnBigCoinRefresh.Invoke(new MapPoint(row, col)); }
void SetTarget_Native(int row, int col) { moveRode = null; bPathGenerated = false; if (mfPath) { mfPath.gameObject.SetActive(false); } objSign.SetActive(true); objSign.transform.position = new Vector3(col, 0, row); System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch(); stopWatch.Start(); int roadLength = NavLibrary.Navigation(curRow, curCol, row, col, navType); stopWatch.Stop(); MainView.Instance.RefreshInfo(row, col, stopWatch.Elapsed.TotalMilliseconds); Debug.Log("Cost Time: " + stopWatch.Elapsed.TotalMilliseconds); Debug.Log("Road Length:" + roadLength); curStep = -1; if (!IsMoving) { MoveToNextPoint_Native(); } }
MapPoint FindNextPoint() { List <MapPoint> listNearstPoints = new List <MapPoint>(); int max = Mathf.Max(Mathf.Max(MapManager.MaxRow - row, row), Mathf.Max(MapManager.MaxCol - col, col)); for (int i = 0; i < max; i++) { for (int j = col - i, length = col + i; j < length; ++j) { if (NavLibrary.GetPointStatus(row - i, j) > 0) { listNearstPoints.Add(new MapPoint(row - i, j, GetDistance(row - 1, j))); } if (NavLibrary.GetPointStatus(row + i, j) > 0) { listNearstPoints.Add(new MapPoint(row + i, j, GetDistance(row + 1, j))); } } for (int j = row - i + 1, length = row + i - 1; j < length; ++j) { if (NavLibrary.GetPointStatus(j, col - i) > 0) { listNearstPoints.Add(new MapPoint(j, col - i, GetDistance(j, col - i))); } if (NavLibrary.GetPointStatus(j, col + i) > 0) { listNearstPoints.Add(new MapPoint(j, col + i, GetDistance(j, col + i))); } } if (listNearstPoints.Count > 7) { break; } } for (int i = 0, length = MapManager.Instance.listBigCoins.Count; i < length; ++i) { BigCoin coin = MapManager.Instance.listBigCoins[i]; if (coin.Alive) { listNearstPoints.Add(new MapPoint(coin.row, coin.col, GetDistance(coin.row, coin.col))); } } listNearstPoints.Sort((a, b) => { float aScore = NavLibrary.GetPointStatus(a.row, b.col), bScore = NavLibrary.GetPointStatus(a.row, b.col); aScore -= a.value / MapManager.Instance.fDistanceWeightLv; bScore -= b.value / MapManager.Instance.fDistanceWeightLv; for (int i = 0, length = MapManager.Instance.listEaters.Count; i < length; ++i) { CoinEater eater = MapManager.Instance.listEaters[i]; if (eater == this) { continue; } if (eater.TarRow == a.row && eater.TarCol == a.col && eater.GetDistance(a.row, a.col) < a.value) { aScore -= 100; } if (eater.TarRow == b.row && eater.TarCol == b.col && eater.GetDistance(b.row, b.col) < b.value) { bScore -= 100; } } int disToCenter = Mathf.Abs(MapManager.MaxRow / 2 - a.row) + Mathf.Abs(MapManager.MaxCol / 2 - a.col); aScore -= disToCenter / MapManager.MaxRow; disToCenter = Mathf.Abs(MapManager.MaxRow / 2 - b.row) + Mathf.Abs(MapManager.MaxCol / 2 - b.col); bScore -= disToCenter / MapManager.MaxRow; return(aScore > bScore ? 1 : -1); }); return(listNearstPoints.Count > 0 ? listNearstPoints[0] : new MapPoint(-1, -1)); }
public int GetPointValue(int row, int col) { return(NavLibrary.GetPointStatus(row, col)); }
void RefreshMap() { if (nEaterNum != listEaters.Count) { if (listEaters.Count > nBigCoinNum) { for (int i = nBigCoinNum; i < listEaters.Count; ++i) { Destroy(listEaters[i].gameObject); listEaters.RemoveAt(i); } } else { for (int i = listBigCoins.Count; i < nBigCoinNum; ++i) { BigCoin coin = new BigCoin(); coin.Init(); listBigCoins.Add(coin); } } } if (nBigCoinNum != listBigCoins.Count) { if (listBigCoins.Count > nBigCoinNum) { for (int i = nBigCoinNum; i < listBigCoins.Count; ++i) { if (listBigCoins[i].Alive) { NavLibrary.SetPointStatus(listBigCoins[i].row, listBigCoins[i].col, 1); } listBigCoins.RemoveAt(i); } } else { GameObject objPrefab = Resources.Load <GameObject>("Prefabs/CoinEater"); for (int i = listBigCoins.Count; i < nBigCoinNum; ++i) { GameObject objEater = Instantiate(objPrefab, transform); int row, col; do { row = Random.Range(0, MaxRow + 1); col = Random.Range(0, MaxCol + 1); } while (!IsPointClear(row, col)); CoinEater eater = objPrefab.GetComponent <CoinEater>(); eater.Init(row, col); listEaters.Add(eater); } } } for (int i = 0; i < MaxRow; ++i) { for (int j = 0; j < MaxCol; ++j) { if (GetPointValue(i, j) != nBigCoinValue) { NavLibrary.SetPointStatus(i, j, GeneratePointStatus(i, j)); } } } OnMapRefresh.Invoke(); }
void InitNative() { NavLibrary.InitMap(MaxRow, MaxCol, GeneratePointStatus); }