Exemple #1
0
        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));
        }
Exemple #2
0
        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();
        }