Exemple #1
0
    /// <summary>
    /// Returns the cell positions overlapped by the box defined by max and min.
    /// </summary>
    /// <param name="min">Bottom left corner in world space.</param>
    /// <param name="max">Top right corner in world space.</param>
    /// <returns>List of cell positions.</returns>
    /// <exception cref="ArgumentException"></exception>
    public List <Vector2Int> GetCellsOverlappingArea(Vector2 min, Vector2 max)
    {
        if (Vector2.Min(min, max) != min)
        {
            throw new ArgumentException("min is not strictly less than max");
        }

        List <Vector2Int> cells = new List <Vector2Int>();

        var minCell = _grid.WorldToCell(min);
        var maxCell = _grid.WorldToCell(max);

        for (int x = minCell.x; x <= maxCell.x; x++)
        {
            for (int y = minCell.y; y <= maxCell.y; y++)
            {
                cells.Add(new Vector2Int(x, y));
            }
        }

        return(cells);
    }
Exemple #2
0
        void Update()
        {
            // 不懂为什么,将Enable关闭了之后,会出现鼠标经过两次的问题
            if (!isExecute)
            {
                return;
            }

            var        worldPointV3 = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            Vector3Int cellPos      = grid.WorldToCell(worldPointV3);

            // 非UI层。
            if (!EventSystem.current.IsPointerOverGameObject())
            {
                var hit2D = Physics2D.Raycast(worldPointV3, Vector2.zero, 10);
                if (hit2D.transform != null)
                {
                    bool           gridPlayerLayer = hit2D.transform.gameObject.layer == LayerMask.NameToLayer("GridPlayer");
                    ActivitiesUnit unit            = hit2D.transform.GetComponent <ActivitiesUnit>();
                    if (gridPlayerLayer)
                    {
                        // Touch Unit
                        if (cacheHitRaycastUnit != null && cacheHitRaycastUnit != unit)
                        {
                            Debug.Log("????");
                            //CommanderRangeUnit(null);
                        }

                        if (cacheHitRaycastUnit == null || cacheHitRaycastUnit != unit)
                        {
                            CommanderRangeUnit(unit);
                        }
                    }
                    else
                    {
                        // Exit Unit
                        if (cacheHitRaycastUnit != null)
                        {
                            Debug.Log("??");

                            CommanderRangeUnit(null);
                        }
                    }

                    if (Input.GetMouseButtonDown(0))
                    {
                        ExecuteClickStepEvent(unit, cellPos, gridPlayerLayer);
                    }
                }
                else
                {
                    // 通知现在没点到士兵
                    if (cacheHitRaycastUnit != null)
                    {
                        Debug.Log("??");
                        CommanderRangeUnit(null);
                    }
                }
            }

            if (Input.GetMouseButtonDown(1))
            {
                CancelStepEvent();
            }
        }