コード例 #1
0
ファイル: NumberTryer.cs プロジェクト: waiter/MathGameUnity
 private void OnGO(Vector3 pos, int number, bool isCorrect, System.Action onComplete)
 {
     Map.Cell cell = NumberSelector.selectedCell;
     transform.localPosition = pos;
     gameObject.SetActive(true);
     _text.text = number.ToString();
     UAnimation.Make(gameObject)
     .MoveTo(MapRenderer.CalculateCellPosition(cell.row, cell.col))
     .Duration(0.5f)
     .Ease(UAnimation.EaseType.Linear)
     .Go(delegate() {
         if (isCorrect)
         {
             gameObject.SetActive(false);
             onComplete();
         }
         else
         {
             UAnimation.Make(gameObject)
             .MoveTo(pos)
             .Duration(0.5f)
             .Ease(UAnimation.EaseType.Linear)
             .Go(delegate() {
                 gameObject.SetActive(false);
                 onComplete();
             });
         }
     });
 }
コード例 #2
0
    /// <summary>
    /// 使几何算法获取两个格子之间的 H 值
    /// 横纵移动代价为 10 ,对角线移动代价为 14
    /// </summary>
    int GetHByEuclidean(Map.Cell a, Map.Cell b)
    {
        int w = Mathf.Abs(a.x - b.x);
        int h = Mathf.Abs(a.y - b.y);

        return(Mathf.RoundToInt(Mathf.Sqrt(w * w + h * h)) * 10);
    }
コード例 #3
0
    /// <summary>
    /// 使用曼哈顿算法获取两个格子之间的 H 值
    /// 横纵移动代价为 10 ,对角线移动代价为 14
    /// </summary>
    int GetHByManhattan(Map.Cell a, Map.Cell b)
    {
        int w = Mathf.Abs(a.x - b.x);
        int h = Mathf.Abs(a.y - b.y);

        return((w + h) * 10);
    }
コード例 #4
0
    private void OnCellSelected(Map.Cell cell)
    {
        cellHighLight.gameObject.SetActive(true);
        int i = cell.row;
        int j = cell.col;

        Debug.LogError(i + ":" + j);
        cellHighLight.rectTransform.localPosition = CalculateCellPosition(i, j);
    }
コード例 #5
0
ファイル: CellView.cs プロジェクト: waiter/MathGameUnity
 public void Bind(Map.Cell cell)
 {
     cell.onCellUpdate = delegate(Map.Cell obj) {
         number          = cell.userValue;
         isNumberVisible = cell.isShow;
     };
     cell.FireUpdate();
     _cell = cell;
 }
コード例 #6
0
 /// <summary>
 /// 相邻格子的 G 值
 /// 横纵移动代价为 10 ,对角线移动代价为 14
 /// </summary>
 int GetG(Map.Cell a, Map.Cell b)
 {
     if (a.x == b.x || a.y == b.y)
     {
         return(10);
     }
     else
     {
         return(14);
     }
 }
コード例 #7
0
    public static Map.Cell CreateCell(CubicCoordinate coord, Map.Graticule parent)
    {
        (int r, int s, int t)c = coord;
        GameObject obj = GameObject.Instantiate(original: Map.Cell.BaseModel.Value, parent: parent.transform);

        obj.name = "HexCell" + coord;
        Map.Cell     cell     = obj.AddComponentWithInit <Map.Cell>(u => u.CoordVector = new Vector3Int(c.r, c.s, c.t));
        MeshCollider collider = obj.AddComponent <MeshCollider>();

        collider.sharedMesh = null; // apparently this is required by unity.
        collider.sharedMesh = obj.GetComponent <MeshFilter>().sharedMesh;
        return(cell);
    }
コード例 #8
0
    /// <summary>
    /// 使用对角算法获取两个格子之间的 H 值
    /// 横纵移动代价为 10 ,对角线移动代价为 14
    /// </summary>
    int GetHByDiagonal(Map.Cell a, Map.Cell b)
    {
        int w = Mathf.Abs(a.x - b.x);
        int h = Mathf.Abs(a.y - b.y);

        // 判断到底是那个轴相差的距离更远
        if (w > h)
        {
            return(14 * h + 10 * (w - h));
        }
        else
        {
            return(14 * w + 10 * (h - w));
        }
    }
コード例 #9
0
 /// <summary>
 /// 获取两个格子之间的 H 值
 /// </summary>
 int GetH(Map.Cell a, Map.Cell b, HType hType)
 {
     if (hType == HType.Manhattan)
     {
         return(GetHByManhattan(a, b));
     }
     else if (hType == HType.Euclidean)
     {
         return(GetHByEuclidean(a, b));
     }
     else
     {
         return(GetHByDiagonal(a, b));
     }
 }
コード例 #10
0
    /// <summary>
    /// 生成路径
    /// </summary>
    void GeneratePath(Map.Cell startCell, Map.Cell endCell)
    {
        List <Map.Cell> path = new List <Map.Cell>();

        if (endCell != null)
        {
            Map.Cell temp = endCell;
            while (temp != startCell)
            {
                path.Add(temp);
                temp = temp.parent;
            }
            // 反转路径
            path.Reverse();
        }
        // 更新路径
        map.GeneratePath(path);
    }
コード例 #11
0
        public static Map Parse(string description, string boosterPack)
        {
            var tokens     = description.Split("#");
            var allPoints  = new AllPoints();
            var edges      = ParseContours(tokens[0], allPoints, false);
            var startPoint = Point.Parse(tokens[1]);
            var obstacles  = ParseContours(tokens[2], allPoints, true);

            var cells = new Map.Cell[allPoints.MaxX + 1, allPoints.MaxY + 1];

            foreach (var e in edges)
            {
                cells[e.X, e.Y] = Map.Cell.Edge;
            }

            foreach (var o in obstacles)
            {
                cells[o.X, o.Y] = Map.Cell.Obstacle;
            }

            ParseBoosters(tokens[3], cells);

            var initialManipulatorCount = 0;
            var initialCloneCount       = 0;

            foreach (var ch in boosterPack)
            {
                switch (ch)
                {
                case 'B':
                    ++initialManipulatorCount;
                    break;

                case 'C':
                    ++initialCloneCount;
                    break;

                default:
                    break;
                }
            }

            return(new Map(startPoint.X, startPoint.Y, cells, initialManipulatorCount, initialCloneCount));
        }
コード例 #12
0
        private void CreateField(string fileName, Loader loader, Material stub)
        {
            float initialX = map.CellSize * (map.Size.X - 1) * 0.5f;
            float initialZ = map.CellSize * (map.Size.Y - 1) * 0.5f;

            for (int y = 0; y < map.Size.Y; y++)
            {
                for (int x = 0; x < map.Size.X; x++)
                {
                    Vector4           position    = new Vector4(initialX - x * map.CellSize, 0, initialZ - y * map.CellSize, 0);
                    List <MeshObject> meshObjects = loader.LoadMeshesFromObject(fileName, stub);
                    meshObjects.ForEach(mesh => mesh.Position = position);
                    map.MeshObjects.AddRange(meshObjects);
                    map[new Point(x, y)] = new Map.Cell
                    {
                        Position   = position,
                        Unit       = Unit.Empty,
                        UnitObject = null
                    };
                }
            }
            for (int i = 0; i < map.Size.Y; i++)
            {
                map[new Point(map.Size.X, i)] = new Cell
                {
                    Position   = Vector4.One,
                    Unit       = Unit.None,
                    UnitObject = null
                }
            }
            ;
            doorPos = new Vector4(0, 0, -(map.CellSize * (map.Size.X - 1) / 2 + map.CellSize), 0);
            map[new Point(map.Size.X, 0)] = new Cell
            {
                Position   = doorPos,
                Unit       = Unit.Door,
                UnitObject = null
            };
            doorMeshes = loader.LoadMeshesFromObject(fileName, stub);
            doorMeshes.ForEach(mesh => { mesh.Position = doorPos; mesh.IsVisible = false; });
            map.MeshObjects.AddRange(doorMeshes);
        }
コード例 #13
0
    public void OnNumberClick()
    {
        Map.Cell cell    = NumberSelector.selectedCell;
        bool     isVaild = cell.CheckVaild(number);

        if (!isVaild)
        {
            gameObject.SetActive(false);
            NumberTryer.Go(transform.localPosition, number, false, delegate() {
                gameObject.SetActive(true);
            });
        }
        else
        {
            gameObject.SetActive(false);
            NumberTryer.Go(transform.localPosition, number, true, delegate() {
                gameObject.SetActive(true);
                cell.isShow    = true;
                cell.userValue = number;
            });
        }
    }
コード例 #14
0
    /// <summary>
    /// A Star 寻路
    /// </summary>
    IEnumerator FindingPath(Vector3 start, Vector3 end, HType hType)
    {
        Map.Cell startCell = map.GetCell(start);
        Map.Cell endCell   = map.GetCell(end);

        /// <summary>
        /// 所有被考虑来寻找最短路径的格子
        /// </summary>
        List <Map.Cell> openList = new List <Map.Cell>();
        /// <summary>
        /// 不会再被考虑的格子
        /// </summary>
        HashSet <Map.Cell> closeSet = new HashSet <Map.Cell>();

        openList.Add(startCell);

        GeneratePath(startCell, null);
        map.ClearProgress();
        map.UpdateProgress(startCell);

        while (openList.Count > 0)
        {
            Map.Cell curCell = openList[0];

            for (int i = 0; i < openList.Count; i++)
            {
                // 从 openList 中找出 f 最小的格子
                if (openList[i].f < curCell.f)
                {
                    curCell = openList[i];
                }
            }

            openList.Remove(curCell);
            closeSet.Add(curCell);

            // 寻路完成
            if (curCell == endCell)
            {
                GeneratePath(startCell, endCell);
                yield break;
            }

            // 判断周围格子,选择一个最优的格子
            foreach (var cell in map.GetNeighbours(curCell))
            {
                if (cell.isWall || closeSet.Contains(cell))
                {
                    continue;
                }

                int g = curCell.g + GetG(curCell, cell);
                // 如果不在列表中,则加入列表
                if (!openList.Contains(cell))
                {
                    cell.g      = g;
                    cell.h      = GetH(cell, endCell, hType);
                    cell.parent = curCell;
                    openList.Add(cell);

                    map.UpdateProgress(cell);
                    yield return(new WaitForSeconds(0.05f));
                }
                else
                {
                    // 如果已经在列表中,并且 g 更小,则更新 g 和 parent
                    if (g < cell.g)
                    {
                        cell.g      = g;
                        cell.parent = curCell;
                    }
                }
            }
        }
        GeneratePath(startCell, null);
    }