Esempio n. 1
0
    void UpdateDragging()
    {
        if (EventSystem.current.IsPointerOverGameObject())
        {
            return;
        }

        //Clean old drag previews
        for (int i = 0; i < _dragPreviewGameObjects.Count; i++)
        {
            SimplePool.Despawn(_dragPreviewGameObjects[i]);
        }
        _dragPreviewGameObjects.Clear();

        if (_currentMode != MouseMode.BUILD)
        {
            return;
        }

        //Start mouse drag
        if (Input.GetMouseButtonDown(0))
        {
            _dragStartPosition = _currFramePosition;
            _isDragging        = true;
        }
        else if (!_isDragging)
        {
            _dragStartPosition = _currFramePosition;
        }

        if (Input.GetMouseButtonUp(1) || Input.GetKeyUp(KeyCode.Escape))
        {
            _isDragging = false;
        }

        if (!_buildModeController.IsObjectDraggable())
        {
            _dragStartPosition = _currFramePosition;
        }

        int start_x = Mathf.RoundToInt(_dragStartPosition.x);
        int end_x   = Mathf.RoundToInt(_currFramePosition.x);

        if (end_x < start_x)
        {
            int tmp = end_x;
            end_x   = start_x;
            start_x = tmp;
        }

        int start_y = Mathf.RoundToInt(_dragStartPosition.y);
        int end_y   = Mathf.RoundToInt(_currFramePosition.y);

        if (end_y < start_y)
        {
            int tmp = end_y;
            end_y   = start_y;
            start_y = tmp;
        }

        /*while (_dragPreviewGameObjects.Count > 0)
         * {
         *  GameObject go = _dragPreviewGameObjects[0];
         *  _dragPreviewGameObjects.RemoveAt(0);
         *  SimplePool.Despawn(go);
         * }*/

        /*if (_isDragging)
         * {*/
        for (int x = start_x; x <= end_x; x++)
        {
            for (int y = start_y; y <= end_y; y++)
            {
                Tile t = WorldController.Instance.World.GetTileAt(x, y);
                if (t != null)
                {
                    if (_buildModeController.BuildModeIsObject)
                    {
                        ShowInstalledObjectSpriteAtTile(_buildModeController.BuildModeObjectType, t);
                    }
                    else
                    {
                        GameObject go = SimplePool.Spawn(_circleCursorPrefab, new Vector3(x, y, 0), Quaternion.identity);
                        go.transform.SetParent(_cursorGameObjectsContainer, true);
                        _dragPreviewGameObjects.Add(go);
                    }
                }
            }
        }
        /*}*/

        //End mouse drag
        if (_isDragging && Input.GetMouseButtonUp(0))
        {
            _isDragging = false;
            for (int x = start_x; x <= end_x; x++)
            {
                for (int y = start_y; y <= end_y; y++)
                {
                    Tile tile = WorldController.Instance.World.GetTileAt(x, y);

                    if (tile != null)
                    {
                        _buildModeController.Build(tile);
                    }
                }
            }
        }
    }
Esempio n. 2
0
    void UpdateTileDragging()
    {
        // If mouse is over a user element, do nothing!
        if (EventSystem.current.IsPointerOverGameObject())
        {
            return;
        }

        // Start left button drag
        if (Input.GetMouseButtonDown(0))
        {
            dragStartPos = currentFramePos;
        }

        int startX = Mathf.FloorToInt(dragStartPos.x);
        int endX   = Mathf.FloorToInt(currentFramePos.x);

        // Flip coords if they are wrong way round to avoid negatives in loop.
        if (startX > endX)
        {
            int temp = endX;
            endX   = startX;
            startX = temp;
        }

        int startY = Mathf.FloorToInt(dragStartPos.y);
        int endY   = Mathf.FloorToInt(currentFramePos.y);

        // Flip coords if they are wrong way round to avoid negatives in loop.
        if (startY > endY)
        {
            int temp = endY;
            endY   = startY;
            startY = temp;
        }

        dragPreviewObjects.ForEach(obj => SimplePool.Despawn(obj));
        dragPreviewObjects.Clear();


        // While mouse button is held down, display preview of the drag area.
        if (Input.GetMouseButton(0))
        {
            for (int x = startX; x <= endX; x++)
            {
                for (int y = startY; y <= endY; y++)
                {
                    if (x >= 0 && y >= 0)
                    {
                        // Checks tile is in range.
                        Tile tile = WorldController.instance.world.GetTileAt(x, y);
                        if (tile != null)
                        {
                            GameObject cursorOverlayGameObject =
                                SimplePool.Spawn(cursorPrefab, new Vector3(x, y, 0), Quaternion.identity);
                            dragPreviewObjects.Add(cursorOverlayGameObject);
                        }
                    }
                }
            }
        }

        // End left button drag
        if (Input.GetMouseButtonUp(0))
        {
            // Loop through all the tiles in the selection and change their type.
            for (int x = startX; x <= endX; x++)
            {
                for (int y = startY; y <= endY; y++)
                {
                    // Checks tile is in range.
                    if (x >= 0 && y >= 0)
                    {
                        Tile tile = WorldController.instance.world.GetTileAt(x, y);
                        if (tile != null)
                        {
                            buildModeController.Build(tile);
                        }
                    }
                }
            }
        }
    }