Exemplo n.º 1
0
    public TileEntity GetBuildingAtScreenPoint(Vector3 screenPoint)
    {
        int mx, my;

        if (IsoHelper.ScreenPositionToGrid(screenPoint, out mx, out my))
        {
            return(GetAnyBuildingEntity(mx, my));
        }
        return(null);
    }
Exemplo n.º 2
0
 protected override void OnPan(Vector2 screenPosition, Vector3 deltaPosition)
 {
     isPanned = true;
     if (isDragging)
     {
         int mx, my;
         IsoHelper.ScreenPositionToGrid(screenPosition, out mx, out my);//REMARK 忽略边界检查,允许多拽出边界
         DraggingBuildingTo(mx - dragDeltaX, my - dragDeltaY);
     }
     else
     {
         base.OnPan(screenPosition, deltaPosition);
     }
 }
Exemplo n.º 3
0
    private void CheckDragBuilding(Vector2 screenPosition)
    {
        int mx, my;

        IsoHelper.ScreenPositionToGrid(screenPosition, out mx, out my);//REMARK 忽略边界检查
        int dx = mx - currentBuilding.GetTilePos().x;
        int dy = my - currentBuilding.GetTilePos().y;

        if (dx >= 0 && dx < currentBuilding.width && dy >= 0 && dy < currentBuilding.height)
        {
            dragDeltaX = dx;
            dragDeltaY = dy;
            isDragging = true;
        }
    }
Exemplo n.º 4
0
    //suggest a building position
    private TilePoint SuggestPosition(TilePoint?suggestPoint = null)
    {
        Queue <TilePoint> notVisited = new Queue <TilePoint>();

        bool[,] visitedMap = new bool[Constants.WIDTH, Constants.HEIGHT];
        visitedMap.Initialize();
        int centerX, centerY;

        if (suggestPoint == null)
        {
            if (lastPoint != null)
            {
                var viewportPoint =
                    Camera.main.WorldToViewportPoint(new Vector3(lastPoint.Value.x, 0, lastPoint.Value.y));
                Debug.Log(viewportPoint);
                if (viewportPoint.x > 0.2f && viewportPoint.x < 0.8f && viewportPoint.y > 0.2f && viewportPoint.y < 0.8f)
                {
                    suggestPoint = lastPoint.Value;
                }
            }
            if (suggestPoint == null)
            {
                var cameraCenter = Camera.main.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
                IsoHelper.ScreenPositionToGrid(cameraCenter, out centerX, out centerY);
                suggestPoint = new TilePoint(centerX, centerY);
            }
        }
        centerX = suggestPoint.Value.x;
        centerY = suggestPoint.Value.y;
        //REMARK 一个建筑格子占2个小格子
        if (centerX % 2 == 1)
        {
            centerX += 1;
        }
        if (centerY % 2 == 1)
        {
            centerY += 1;
        }
        int STEP = 2;

        visitedMap[centerX, centerY] = true;
        notVisited.Enqueue(new TilePoint(centerX, centerY));
        int x, y;

        while (notVisited.Count > 0)
        {
            var point = notVisited.Dequeue();
            x = point.x;
            y = point.y;
            if (IsoMap.Instance.CanPlaceBuilding(x, y, currentBuilding.width, currentBuilding.height))
            {
                return(new TilePoint(x, y));
            }
            int dx = Mathf.Abs(x - centerX);
            int dy = Mathf.Abs(y - centerY);
            if (dx > currentBuilding.width || dy > currentBuilding.height)//建议的点不能离中心太远
            {
                continue;
            }
            if (x - STEP >= 0 && !visitedMap[x - STEP, y])
            {
                visitedMap[x - STEP, y] = true;
                notVisited.Enqueue(new TilePoint(x - STEP, y));
            }
            if (x + STEP < Constants.WIDTH && !visitedMap[x + STEP, y])
            {
                visitedMap[x + STEP, y] = true;
                notVisited.Enqueue(new TilePoint(x + STEP, y));
            }
            if (y - STEP >= 0 && !visitedMap[x, y - STEP])
            {
                visitedMap[x, y - STEP] = true;
                notVisited.Enqueue(new TilePoint(x, y - STEP));
            }
            if (y + STEP < Constants.HEIGHT && !visitedMap[x, y + STEP])
            {
                visitedMap[x, y + STEP] = true;
                notVisited.Enqueue(new TilePoint(x, y + STEP));
            }
        }
        //no best point,use center of screen
        return(new TilePoint(centerX, centerY));
    }