Пример #1
0
    /// <summary>
    /// Create a square of all tiles in a selected area.
    /// Do something with the tiles in set selected area.
    /// </summary>
    private void UpdateDragMovement()
    {
        // Prevent stuff from happening when mouse is over UI elements
        if (EventSystem.current.IsPointerOverGameObject())
        {
            return;
        }

        #region Start drag movement
        // Check if left mouse button was pressed.
        // Prevent stuff from happening when mouse is over UI elements.
        if (Input.GetMouseButtonDown(0))
        {
            // Save starting tile
            startDragTilePosition = currentFrameMousePosition;
        }

        int start_X = Mathf.FloorToInt(startDragTilePosition.x + 0.5f);
        int end_X   = Mathf.FloorToInt(currentFrameMousePosition.x + 0.5f);
        int start_Y = Mathf.FloorToInt(startDragTilePosition.y + 0.5f);
        int end_Y   = Mathf.FloorToInt(currentFrameMousePosition.y + 0.5f);

        // Swap intergers if players drags from top to bottom or right to left
        if (end_X < start_X)
        {
            int temp = end_X;
            end_X   = start_X;
            start_X = temp;
        }
        if (end_Y < start_Y)
        {
            int temp = end_Y;
            end_Y   = start_Y;
            start_Y = temp;
        }

        // While there are pooled object in the list, remove them
        while (dragPreviewGameObjects.Count > 0)
        {
            GameObject gameObject = dragPreviewGameObjects[0];
            dragPreviewGameObjects.RemoveAt(0);
            objectPooler.DespawnFromPool(gameObject);
        }

        // Display a preview of all selected tiles
        if (Input.GetMouseButton(0))
        {
            // Loop through all selected tiles and change them
            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)
                    {
                        GameObject gameObject = objectPooler.SpawnFromPool("preview_Cursor", new Vector2(x, y), Quaternion.identity);
                        dragPreviewGameObjects.Add(gameObject);
                    }
                }
            }
        }
        #endregion

        #region End drag movement
        // Check if left mouse button was released
        if (Input.GetMouseButtonUp(0))
        {
            // Loop through all selected tiles and change them
            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.ExecuteBuild(tile);
                    }
                }
            }

            #region Old bit of code
            // if (tileHoverOver != null) // If tile isn't null, flip the tile type
            //{
            //    if (tileHoverOver.Type == Tile.TileType.Empty)
            //        tileHoverOver.Type = Tile.TileType.Floor;
            //    else if (tileHoverOver.Type == Tile.TileType.Floor)
            //        tileHoverOver.Type = Tile.TileType.Empty;
            //}
            #endregion
        }
        #endregion
    }