Exemplo n.º 1
0
    private void OnElementEdited(GridElementData elementData)
    {
        m_gridElements.m_elements[m_selectedElementIndex] = elementData;
        SaveToPlayerPrefs();

        //TODO: Delete singular element instead of rebuilding full grid
        BuildGridElements();
    }
Exemplo n.º 2
0
    public void SwapElements(IntVector2 aIndex, IntVector2 bIndex)
    {
        GridElementData oldA = gridElements[aIndex.x, aIndex.y];

        gridElements[aIndex.x, aIndex.y] = gridElements[bIndex.x, bIndex.y];
        gridElements[bIndex.x, bIndex.y] = oldA;

        gridElements[aIndex.x, aIndex.y].correctWorldPos = CalculateWorldPos(aIndex);
        gridElements[bIndex.x, bIndex.y].correctWorldPos = CalculateWorldPos(bIndex);
    }
Exemplo n.º 3
0
    public void ResetElementWorldPos(IntVector2 gridIndex)
    {
        GridElementData element = gridElements[gridIndex.y, gridIndex.x];

        if (element.elementTransform != null)
        {
            element.correctWorldPos = CalculateWorldPos(gridIndex);
            element.elementTransform.transform.position = element.correctWorldPos;
            element.elementTransform.transform.parent   = this.transform;
        }
    }
Exemplo n.º 4
0
    private IEnumerator MoveAllElementsTowardsCorrectWorldPositions(float movementSpeedIncrementMultiplier = 1f)
    {
        //print("Starting drop 'animations'.");
        float movementSpeed = 0f;
        float movementSpeedIncrementPerSecond = 25f;

        List <IntVector2> indicesToMove = new List <IntVector2>();

        for (int x = 0; x < gridElements.GetLength(0); x++)
        {
            for (int y = 0; y < gridElements.GetLength(1); y++)
            {
                if (gridElements[x, y].elementTransform.position != gridElements[x, y].correctWorldPos)
                {
                    indicesToMove.Add(new IntVector2(x, y));
                }
            }
        }

        //print("Indices to move count " + indicesToMove.Count);

        while (indicesToMove.Count > 0)
        {
            for (int i = 0; i < indicesToMove.Count; i++)
            {
                GridElementData elementToMove = gridElements[indicesToMove[i].x, indicesToMove[i].y];

                Vector3 directionToCorrectPos = elementToMove.correctWorldPos - elementToMove.elementTransform.position;
                float   distanceToCorrectPos  = directionToCorrectPos.magnitude;

                if (distanceToCorrectPos <= movementSpeed * Time.deltaTime)
                {
                    elementToMove.elementTransform.position = elementToMove.correctWorldPos;
                    gridElements[indicesToMove[i].x, indicesToMove[i].y].justSpawned = false;
                    //print("Element at " + indicesToMove[i] + " has arrived at correct world pos. indicesToMove.Count: "
                    //    + indicesToMove.Count + ", i: " + i);
                    indicesToMove.RemoveAt(i);
                    i--;
                }
                else
                {
                    directionToCorrectPos /= distanceToCorrectPos;
                    elementToMove.elementTransform.position += directionToCorrectPos * movementSpeed * Time.deltaTime;
                }
            }

            movementSpeed += movementSpeedIncrementPerSecond * movementSpeedIncrementMultiplier * Time.deltaTime;

            yield return(new WaitForEndOfFrame());
        }

        //print("Finished drop 'animations'.");
        ElementMovementFinished();
    }
Exemplo n.º 5
0
    private void OnNewElementAdded(GridElementData elementData)
    {
        if (m_gridElements.m_elements == null)
        {
            m_gridElements.m_elements = new List <GridElementData>();
        }
        m_gridElements.m_elements.Add(elementData);
        SaveToPlayerPrefs();

        //TODO: Delete singular element instead of rebuilding full grid
        BuildGridElements();
    }
Exemplo n.º 6
0
 bool CanPlaceInGrid(int w, int h, GridElementData element)
 {
     for (var sy = 0; sy < testB; ++sy)
     {
         for (var sx = 0; sx < testA; ++sx)
         {
             Debug.Log("" + element.pos.x + " " + element.pos.y);
             var x = (int)(element.pos.x + 0.5f) + sy;
             var y = (int)(element.pos.y + 0.5f) + sx;
             if (!IsIn(x, y, w, h) || !grid[x, y].GetComponent <GridElementData>().isEmpty)
             {
                 return(false);
             }
         }
     }
     return(true);
 }
Exemplo n.º 7
0
 public void initGameBoard(int row, int column, GridElement gridElement)
 {
     board[row, column] = new GridElementData(GameManager.NONE, gridElement);
 }
Exemplo n.º 8
0
    private void Update()
    {
        if (Input.touchCount > 0 && grid.GetIsElementMovementDone())
        {
            Touch      currentTouch = Input.GetTouch(0);
            IntVector2 hitGridIndex = new IntVector2(0, 0);

            switch (selectionMode)
            {
            case ESelectionMode.SWAP:
                #region SWAP
                #region Touch input
                switch (currentTouch.phase)
                {
                    #region TouchPhase.Began
                case TouchPhase.Began:
                    hitGridIndex = grid.GetGridIndexFromWorldPosition(Camera.main.ScreenToWorldPoint(currentTouch.position));
                    if (hitGridIndex.x >= 0 && hitGridIndex.y >= 0)
                    {
                        selectedElementIndices = new List <IntVector2>();
                        selectedElementIndices.Add(hitGridIndex);
                        GridElementData elementData = grid.GetGridElementDataFromIndex(selectedElementIndices[0]);
                        selectedElementType = elementData.elementType;

                        if (selectedElementType != null)
                        {
                            selectedElementTransform = elementData.elementTransform;
                            effectManager.SpawnSelectionEffectAtIndex(hitGridIndex);

                            //TODO HERE: Highlight available directions
                        }
                    }

                    lastGridIndexToHoverOver = hitGridIndex;
                    break;
                    #endregion

                    #region TouchPhase.Moved
                case TouchPhase.Moved:
                    if (selectedElementTransform != null)
                    {
                        hitGridIndex = grid.GetGridIndexFromWorldPosition(Camera.main.ScreenToWorldPoint(currentTouch.position));
                        if (lastGridIndexToHoverOver != hitGridIndex)
                        {
                            if (hitGridIndex.x >= 0 && hitGridIndex.y >= 0)
                            {
                                if (selectedElementIndices[0] != hitGridIndex)
                                {
                                    ElementType hitElementType = grid.GetGridElementDataFromIndex(hitGridIndex).elementType;

                                    if (hitElementType != null)
                                    {
                                        effectManager.SpawnSelectionEffectAtIndex(hitGridIndex);
                                    }
                                }
                            }

                            //If the lastGridIndexToHoverOver is a valid index and not the same as the grabbed element's index
                            if ((lastGridIndexToHoverOver.x >= 0 && lastGridIndexToHoverOver.y >= 0) && lastGridIndexToHoverOver != selectedElementIndices[0])
                            {
                                effectManager.ClearSelectionEffectAtIndex(lastGridIndexToHoverOver);
                            }

                            lastGridIndexToHoverOver = hitGridIndex;
                        }
                    }
                    break;
                    #endregion

                    #region TouchPhase.Ended
                case TouchPhase.Ended:
                    if (selectedElementTransform != null)
                    {
                        hitGridIndex = grid.GetGridIndexFromWorldPosition(Camera.main.ScreenToWorldPoint(currentTouch.position));
                        if (hitGridIndex.x >= 0 && hitGridIndex.y >= 0)
                        {
                            if (selectedElementIndices.Contains(hitGridIndex))
                            {
                                grid.MoveElementsToCorrectPositions(swapMovementSpeedIncrementMultiplier);
                            }
                            else
                            {
                                IntVector2 releasePointIndex = hitGridIndex;
                                grid.SwapElements(selectedElementIndices[0], releasePointIndex);
                                IncrementMoves();
                            }
                        }

                        grid.MoveElementsToCorrectPositions(swapMovementSpeedIncrementMultiplier);
                    }

                    selectedElementIndices.Clear();
                    selectedElementTransform = null;
                    effectManager.ClearAllSelectionEffects();
                    break;
                    #endregion

                case TouchPhase.Canceled:
                    selectedElementIndices.Clear();
                    selectedElementTransform = null;
                    effectManager.ClearAllSelectionEffects();
                    break;

                default:
                    break;
                }
                #endregion

                #region Mouse input

                /*
                 #region LeftMouseButton Down
                 * if (Input.GetMouseButtonDown(0) && grid.GetIsElementMovementDone())
                 * {
                 *  hitGridIndex = grid.GetGridIndexFromWorldPosition(Camera.main.ScreenToWorldPoint(Input.mousePosition));
                 *  if (hitGridIndex.x >= 0 && hitGridIndex.y >= 0)
                 *  {
                 *      selectedElementIndices = new List<IntVector2>();
                 *      selectedElementIndices.Add(hitGridIndex);
                 *      GridElementData elementData = grid.GetGridElementDataFromIndex(selectedElementIndices[0]);
                 *      selectedElementType = elementData.elementType;
                 *      //print("selectedElementType: " + selectedElementType);
                 *      if (selectedElementType != null)
                 *      {
                 *          selectedElementTransform = elementData.elementTransform;
                 *          effectManager.SpawnSelectionEffectAtIndex(hitGridIndex);
                 *
                 *          //TODO HERE: Highlight available directions
                 *
                 *          //print("Grabbing tile at index " + selectedElementIndices[0]);
                 *      }
                 *      //else
                 *      //{
                 *      //    print("INVALID SELECTION: Selected element type is null!");
                 *      //}
                 *  }
                 *  //else
                 *  //{
                 *  //    print("INVALID SELECTION: Selected position is outside of the grid.");
                 *  //}
                 *
                 *  lastGridIndexToHoverOver = hitGridIndex;
                 * }
                 #endregion
                 *
                 #region LeftMouseButton Held
                 * if (Input.GetMouseButton(0) && selectedElementIndices.Count > 0)
                 * {
                 *  if (lastMousePos != Input.mousePosition)
                 *  {
                 *      hitGridIndex = grid.GetGridIndexFromWorldPosition(Camera.main.ScreenToWorldPoint(Input.mousePosition));
                 *      if (lastGridIndexToHoverOver != hitGridIndex)
                 *      {
                 *          if (hitGridIndex.x >= 0 && hitGridIndex.y >= 0)
                 *          {
                 *              if (!selectedElementIndices.Contains(hitGridIndex))
                 *              {
                 *                  ElementType hitElementType = grid.GetGridElementDataFromIndex(hitGridIndex).elementType;
                 *
                 *                  if (hitElementType != null)
                 *                  {
                 *                      effectManager.SpawnSelectionEffectAtIndex(hitGridIndex);
                 *                  }
                 *              }
                 *          }
                 *
                 *          if ((lastGridIndexToHoverOver.x >= 0 && lastGridIndexToHoverOver.y >= 0) && lastGridIndexToHoverOver != selectedElementIndices[0])
                 *              effectManager.ClearSelectionEffectAtIndex(lastGridIndexToHoverOver);
                 *          lastGridIndexToHoverOver = hitGridIndex;
                 *      }
                 *
                 *      lastMousePos = Input.mousePosition;
                 *  }
                 * }
                 #endregion
                 *
                 #region LeftMouseButton Up
                 * if (Input.GetMouseButtonUp(0) && selectedElementTransform != null)
                 * {
                 *  hitGridIndex = grid.GetGridIndexFromWorldPosition(Camera.main.ScreenToWorldPoint(Input.mousePosition));
                 *  if (hitGridIndex.x >= 0 && hitGridIndex.y >= 0)
                 *  {
                 *      if (selectedElementIndices.Contains(hitGridIndex))
                 *      {
                 *          //print("Grabbed element released at it's original index, resetting element position.");
                 *
                 *          grid.MoveElementsToCorrectPositions(swapMovementSpeedIncrementMultiplier);
                 *      }
                 *      else
                 *      {
                 *          IntVector2 releasePointIndex = hitGridIndex;
                 *          //print("Released element at index " + releasePointIndex + ", swapping positions");
                 *
                 *          //TODO HERE: Check if hitTile is on a viable lane (e.g. if swap is restricted on the same lanes as the grabbed element)
                 *
                 *          //TODO HERE: Check if valid move (e.g. if swap allowed only when it results in a match; pre-check match)
                 *
                 *          grid.SwapElements(selectedElementIndices[0], releasePointIndex);
                 *
                 *          grid.MoveElementsToCorrectPositions(swapMovementSpeedIncrementMultiplier);
                 *          IncrementMoves();
                 *      }
                 *  }
                 *  else
                 *  {
                 *      //print("Released element outside of the grid, resetting element position.");
                 *
                 *      grid.MoveElementsToCorrectPositions(swapMovementSpeedIncrementMultiplier);
                 *  }
                 *
                 *  selectedElementIndices.Clear();
                 *  selectedElementTransform = null;
                 *  effectManager.ClearAllSelectionEffects();
                 * }
                 #endregion
                 */
                #endregion

                if (selectedElementTransform != null)
                {
                    Vector3 touchPosition = Camera.main.ScreenToWorldPoint(currentTouch.position);
                    touchPosition.z = 0;
                    if (touchPosition.y > elementMaxYPosDuringSwapGrab)
                    {
                        touchPosition.y = elementMaxYPosDuringSwapGrab;
                    }

                    selectedElementTransform.position = touchPosition;
                }
                #endregion
                break;

            case ESelectionMode.CONNECT:
                #region CONNECT
                #region Touch Input
                switch (currentTouch.phase)
                {
                    #region TouchPhase.Began
                case TouchPhase.Began:
                    selectedElementIndices = new List <IntVector2>();

                    hitGridIndex = grid.GetGridIndexFromWorldPosition(Camera.main.ScreenToWorldPoint(currentTouch.position));
                    //If the hitGridIndex is a valid index
                    if (hitGridIndex.x >= 0 && hitGridIndex.y >= 0)
                    {
                        selectedElementIndices.Add(hitGridIndex);
                        selectedElementType = grid.GetGridElementDataFromIndex(hitGridIndex).elementType;

                        if (selectedElementType != null)
                        {
                            //Start selection on the selected element
                            invalidSelection = false;
                            effectManager.StartSelectionLine(hitGridIndex);
                            effectManager.SpawnSelectionEffectAtIndex(hitGridIndex);
                            effectManager.HighlightIndices(grid.FindMatchesForIndex(hitGridIndex));
                        }
                    }
                    break;
                    #endregion

                    #region TouchPhase.Moved
                case TouchPhase.Moved:
                    if (!invalidSelection && selectedElementIndices.Count > 0)
                    {
                        hitGridIndex = grid.GetGridIndexFromWorldPosition(Camera.main.ScreenToWorldPoint(currentTouch.position));
                        //If the hitGridIndex is a valid index
                        if (hitGridIndex.x >= 0 && hitGridIndex.y >= 0)
                        {
                            if (!selectedElementIndices.Contains(hitGridIndex))
                            {
                                ElementType hitElementType = grid.GetGridElementDataFromIndex(hitGridIndex).elementType;

                                if (hitElementType != null)
                                {
                                    //If the newly selected element is not a neighbour of the previously selected one
                                    if (!grid.CheckIfNeighbours(selectedElementIndices[selectedElementIndices.Count - 1], hitGridIndex))
                                    {
                                        InvalidateSelection();
                                    }

                                    bool isOfMatchingType = false;

                                    //Check that the newly selected element is of matching type with the first selected element
                                    for (int i = 0; i < selectedElementType.matchingElements.Length; i++)
                                    {
                                        if (hitElementType == selectedElementType.matchingElements[i])
                                        {
                                            isOfMatchingType = true;
                                        }
                                    }

                                    if (isOfMatchingType)
                                    {
                                        //Add to selection
                                        selectedElementIndices.Add(hitGridIndex);
                                        effectManager.AddPointToSelectionLine(hitGridIndex);
                                        effectManager.SpawnSelectionEffectAtIndex(hitGridIndex);
                                    }
                                    else
                                    {
                                        //Add to selection visually, but invalidate selection
                                        effectManager.AddPointToSelectionLine(hitGridIndex);
                                        effectManager.SpawnSelectionEffectAtIndex(hitGridIndex);
                                        InvalidateSelection();
                                    }
                                }
                            }
                        }
                    }
                    break;
                    #endregion

                    #region TouchPhase.Ended
                case TouchPhase.Ended:
                    if (selectedElementIndices.Count > 0)
                    {
                        if (!invalidSelection)
                        {
                            if (selectedElementIndices.Count >= grid.minViableConnection)
                            {
                                //Increase score
                                effectManager.SpawnPointPopUpsForMatch(selectedElementIndices);
                                scoreShouldBe += selectedElementIndices.Count * selectedElementIndices.Count;

                                IncrementMoves();

                                grid.RemoveElementsAtIndices(selectedElementIndices);
                                grid.FillGrid();
                                grid.MoveElementsToCorrectPositions();
                            }
                        }
                    }

                    ClearSelectionsAndRelatedEffects();
                    break;
                    #endregion

                case TouchPhase.Canceled:
                    ClearSelectionsAndRelatedEffects();
                    break;

                default:
                    break;
                }
                #endregion

                #region Mouse Input

                /*
                 #region LeftMouseButton Down
                 * if (Input.GetMouseButtonDown(0) && grid.GetIsElementMovementDone())
                 * {
                 *  selectedElementIndices = new List<IntVector2>();
                 *
                 *  hitGridIndex = grid.GetGridIndexFromWorldPosition(Camera.main.ScreenToWorldPoint(Input.mousePosition));
                 *  if (hitGridIndex.x >= 0 && hitGridIndex.y >= 0)
                 *  {
                 *      selectedElementIndices.Add(hitGridIndex);
                 *      selectedElementType = grid.GetGridElementDataFromIndex(hitGridIndex).elementType;
                 *
                 *      if (selectedElementType != null)
                 *      {
                 *          invalidSelection = false;
                 *          effectManager.StartSelectionLine(hitGridIndex);
                 *          effectManager.SpawnSelectionEffectAtIndex(hitGridIndex);
                 *
                 *          effectManager.HighlightIndices(grid.FindMatchesForIndex(hitGridIndex));
                 *
                 *      }
                 *  }
                 * }
                 #endregion
                 *
                 #region LeftMouseButton Held
                 * if (!invalidSelection && Input.GetMouseButton(0) && selectedElementIndices.Count > 0)
                 * {
                 *  if (lastMousePos != Input.mousePosition)
                 *  {
                 *      hitGridIndex = grid.GetGridIndexFromWorldPosition(Camera.main.ScreenToWorldPoint(Input.mousePosition));
                 *      if (hitGridIndex.x >= 0 && hitGridIndex.y >= 0)
                 *      {
                 *          if (!selectedElementIndices.Contains(hitGridIndex))
                 *          {
                 *              ElementType hitElementType = grid.GetGridElementDataFromIndex(hitGridIndex).elementType;
                 *
                 *              if (hitElementType != null)
                 *              {
                 *                  if (!grid.CheckIfNeighbours(selectedElementIndices[selectedElementIndices.Count - 1], hitGridIndex))
                 *                  {
                 *                      InvalidateSelection();
                 *                  }
                 *
                 *                  bool isOfMatchingType = false;
                 *
                 *                  for (int i = 0; i < selectedElementType.matchingElements.Length; i++)
                 *                  {
                 *                      if (hitElementType == selectedElementType.matchingElements[i])
                 *                          isOfMatchingType = true;
                 *                  }
                 *
                 *                  if (isOfMatchingType)
                 *                  {
                 *                      selectedElementIndices.Add(hitGridIndex);
                 *                      effectManager.AddPointToSelectionLine(hitGridIndex);
                 *                      effectManager.SpawnSelectionEffectAtIndex(hitGridIndex);
                 *                  }
                 *                  else
                 *                  {
                 *                      effectManager.AddPointToSelectionLine(hitGridIndex);
                 *                      effectManager.SpawnSelectionEffectAtIndex(hitGridIndex);
                 *                      InvalidateSelection();
                 *                  }
                 *              }
                 *          }
                 *      }
                 *
                 *      lastMousePos = Input.mousePosition;
                 *  }
                 * }
                 #endregion
                 *
                 #region LeftMouseButton Up
                 * if (Input.GetMouseButtonUp(0))
                 * {
                 *  if (selectedElementIndices.Count > 0)
                 *  {
                 *      if (!invalidSelection)
                 *      {
                 *          if (selectedElementIndices.Count >= grid.minViableConnection)
                 *          {
                 *              effectManager.SpawnPointPopUpsForMatch(selectedElementIndices);
                 *              scoreShouldBe += selectedElementIndices.Count * selectedElementIndices.Count;
                 *              IncrementMoves();
                 *
                 *              grid.RemoveElementsAtIndices(selectedElementIndices);
                 *              grid.FillGrid();
                 *              grid.MoveElementsToCorrectPositions();
                 *          }
                 *      }
                 *  }
                 *
                 *  selectedElementIndices.Clear();
                 *  selectedElementTransform = null;
                 *  effectManager.ClearSelectionLine();
                 *  effectManager.ClearAllSelectionEffects();
                 *  effectManager.ClearHighlights();
                 * }
                 #endregion
                 */
                #endregion
                #endregion
                break;

            default:
                break;
            }
        }
    }
Exemplo n.º 9
0
    public void FillGrid(bool fullSpawn = false)
    {
        int spawnCount = 0;

        while (CheckForEmptyGridIndices())
        {
            //Find all empty indices
            for (int x = gridElements.GetLength(0) - 1; x >= 0; x--)
            {
                for (int y = gridElements.GetLength(1) - 1; y >= 0; y--)
                {
                    if (gridElements[x, y].elementTransform == null)
                    {
                        //Find the correct index for the element to descend
                        Vector3    correctWorldPos           = CalculateWorldPos(new IntVector2(x, y));
                        Vector3    descendingElementWorldPos = correctWorldPos - fallDirection * hexHeight;
                        IntVector2 descendingElementIndex    = (y - 1 >= 0)
                            ? new IntVector2(x, y - 1)
                            : GetGridIndexFromWorldPosition(descendingElementWorldPos);

                        //print(x + "|" + y + " correctWorldPos: " + correctWorldPos + ", fallDirection: " + fallDirection
                        //    + ", hexHeight: " + hexHeight);
                        //print("descendingElementWorldPos: " + descendingElementWorldPos + ", descendingElementIndex: " + descendingElementIndex);
                        if (descendingElementIndex.x >= 0 && descendingElementIndex.x < gridElements.GetLength(0) &&
                            descendingElementIndex.y >= 0 && descendingElementIndex.y < gridElements.GetLength(1))
                        {
                            //"Drop" elements above the empty indices to fill the empty ones
                            GridElementData gridElement = gridElements[descendingElementIndex.x, descendingElementIndex.y];
                            if (!(fullSpawn && gridElement.elementTransform == null))
                            {
                                gridElements[x, y] = gridElement;
                                gridElements[x, y].correctWorldPos = correctWorldPos;
                                RemoveElementAtIndex(descendingElementIndex, false, false);
                                //print("Dropped an element from " + descendingElementIndex + " to fill an empty index at " + x + "|" + y);
                                continue;
                            }
                        }

                        //Count the number of elements spawned on the same column on this turn
                        int numberOfNewlySpawnedElementsUnderThisOne = 0;
                        for (int y2 = y + 1; y2 < gridElements.GetLength(1); y2++)
                        {
                            GridElementData gridElement = gridElements[x, y2];
                            if (gridElement.elementTransform != null)
                            {
                                if (gridElement.justSpawned)
                                {
                                    numberOfNewlySpawnedElementsUnderThisOne++;
                                }
                            }
                        }

                        //Calculate the proper spawn position
                        Vector3 spawnPosOffset = -fallDirection * hexHeight
                                                 * (numberOfNewlySpawnedElementsUnderThisOne + 1);

                        if (!fullSpawn)
                        {
                            spawnPosOffset *= Mathf.Pow(spawnOffsetPerRow, numberOfNewlySpawnedElementsUnderThisOne + 1);
                        }

                        spawnPosOffset.y         += minNewElementSpawnYPos;
                        descendingElementWorldPos = correctWorldPos + spawnPosOffset;

                        //print("FillGrid: Creating new element at " + x + "|" + y + ", spawnPos: " + descendingElementWorldPos
                        //    + ", numberOfNewlySpawnedElementsUnderThisOne: " + numberOfNewlySpawnedElementsUnderThisOne);

                        CreateNewGridElement(ChooseRandomElementType(), descendingElementWorldPos, new IntVector2(x, y), this.transform);
                        spawnCount++;
                    }
                }
            }
        }
    }