Exemple #1
0
    private void moveTo(SquareIndex index)
    {
        if (nodeList.Count == 0)
        {
            lastIndex    = startIndex;
            currentIndex = index;

            nodeList.Add(instantiateMoveNode(moveNodeFinalPrefab, currentIndex, 10));
        }
        else if (!index.Equals(nodeList[0].index))
        {
            int listIndex = calculateListIndex(index);

            if (listIndex > 0)
            {
                removeIndex(listIndex, index);
            }
            else
            {
                addIndex(index);
            }

            lastIndex    = currentIndex;
            currentIndex = index;
        }

        createNodeLinks();
        calculateNumMoves();
    }
Exemple #2
0
    public void clear(Player owner, SquareIndex index)
    {
        this.owner = owner;
        this.index = index;

        toMoveSquareList = new List <SquareIndex>();

        int directionX = UnityEngine.Random.Range(-1, 2);
        int directionY = UnityEngine.Random.Range(-1, 2);

        //Check that the direction is not (0, 0)
        if (directionX == 0 && directionY == 0)
        {
            directionX = 1;
        }

        int distance = UnityEngine.Random.Range(1, 7);

        for (int i = 0; i < distance; i++)
        {
            toMoveSquareList.Add(index + new SquareIndex(directionX, directionY) * i);
        }

        canBeIntercepted = false;

        endAction = endClear;
        StartCoroutine(updateMove());
    }
Exemple #3
0
    private bool isDirectionChange(SquareIndex nextIndex)
    {
        List <SquareIndex> indexList = retrieveIndexList();
        SquareIndex        index     = indexList[0];

        if (isDiagonal(startIndex, index))
        {
            if (nodeList.Count == 1)
            {
                return(false);
            }
            else
            {
                if (isHorizontal(indexList[0], indexList[1]))
                {
                    return(!isHorizontal(indexList[1], nextIndex));
                }
                else
                {
                    return(isHorizontal(indexList[1], nextIndex));
                }
            }
        }
        else
        {
            if (isHorizontal(startIndex, indexList[0]))
            {
                return(!isHorizontal(indexList[0], nextIndex));
            }
            else
            {
                return(isHorizontal(indexList[0], nextIndex));
            }
        }
    }
Exemple #4
0
    private static int calculateNumDiagonalsInPath(SquareIndex startIndex, int a, int b)
    {
        bool isDiagonal;
        int  diagonals = 0;

        SquareIndex currentIndex = new SquareIndex(a, b);
        SquareIndex parentIndex  = parent[currentIndex.x][currentIndex.y];

        if (parentIndex == null)
        {
            return(0);
        }

        while (parentIndex != startIndex)
        {
            isDiagonal = (Mathf.Abs(currentIndex.x - parentIndex.x) == 1 && Mathf.Abs(currentIndex.y - parentIndex.y) == 1);
            if (isDiagonal)
            {
                diagonals++;
            }

            currentIndex = new SquareIndex(parentIndex.x, parentIndex.y);
            parentIndex  = parent[currentIndex.x][currentIndex.y];
        }

        //Check between the start index and the first index
        isDiagonal = (Mathf.Abs(currentIndex.x - startIndex.x) == 1 && Mathf.Abs(currentIndex.y - startIndex.y) == 1);
        if (isDiagonal)
        {
            diagonals++;
        }

        return(diagonals);
    }
Exemple #5
0
        public bool CanMove(Move move)
        {
            bool fCanMove = false;

            SquareIndex selectedSquare = this.EmptySquare.Plus(MoveVector.OrthogonalSteps[(int)move]);
            int         tileId         = this[selectedSquare];

            if (SquareIsOnTheBoard(selectedSquare))
            {
                // Can't move the "empty" tile:
                if (tileId != 0)
                {
                    // Find specified tile:
                    SquareIndex currentSquare = Tiles[tileId];

                    // Calculate required movement from current square
                    // to the empty square:
                    MoveVector movement = SquareIndex.Subtract(EmptySquare, currentSquare);

                    // See if movement is an orthogonal step:
                    foreach (MoveVector orthogonalStep in MoveVector.OrthogonalSteps)
                    {
                        if (movement == orthogonalStep)
                        {
                            fCanMove = true;
                            break;
                        }
                    }
                }
            }

            return(fCanMove);
        }
Exemple #6
0
        /// <summary>
        /// Move the specified tile from its current square to the empty square;
        /// returns true if the tile moves, false if tile can't be moved.
        /// </summary>

        public bool MoveTile(int tileId)
        {
            // Can't move the "empty" tile:
            if (tileId == 0)
            {
                return(false);
            }

            // Find specified tile:
            SquareIndex selectedSquare = Tiles[tileId];

            // Calculate required movement from current square
            // to the empty square:
            MoveVector movement = SquareIndex.Subtract(EmptySquare, selectedSquare);

            // See if movement is an orthogonal step:
            foreach (MoveVector orthogonalStep in MoveVector.OrthogonalSteps)
            {
                if (movement == orthogonalStep)
                {
                    ExchangeTiles(selectedSquare, EmptySquare);
                    return(true);
                }
            }

            // Can't move the tile from the specified square
            // to the empty square in a single orthogonal step:
            return(false);
        }
Exemple #7
0
    //Converts a square index to world coordinates
    public Vector3 squareIndexToWorld(SquareIndex index)
    {
        float posx = origin.x + transform.position.x + (index.x * SIZE_SQUARE);
        float posy = origin.y + transform.position.z + (index.y * SIZE_SQUARE);

        return(new Vector3(posx, 0, posy));
    }
Exemple #8
0
        /// <summary>
        /// Exchange contents of two squares.
        /// </summary>

        private void ExchangeTiles(SquareIndex squareA, SquareIndex squareB)
        {
            int tileId = this[squareA];

            this[squareA] = this[squareB];
            this[squareB] = tileId;
        }
Exemple #9
0
    public SquareIndex inverseIndex(SquareIndex index)
    {
        int x = (SIZE_X - 1) - index.x;
        int y = (SIZE_Y - 1) - index.y;

        return(new SquareIndex(x, y));
    }
Exemple #10
0
    public void setPosition(SquareIndex index)
    {
        _index = index;
        cachedTransform.position = board.squareIndexToWorld(index);

        board.updatePlayerPosition(index, this);
    }
Exemple #11
0
    private void addIndex(SquareIndex index)
    {
        MoveNode endNode = nodeList[0];

        nodeList.Add(instantiateMoveNode(moveNodePrefab, endNode.index, 10));

        endNode.index = index;
        endNode.gameObject.transform.localPosition = board.squareIndexToWorld(index);
    }
Exemple #12
0
    public void init(Board board, Player player)
    {
        this.board  = board;
        this.player = player;

        isActive = true;

        startIndex = player.index;
    }
Exemple #13
0
    public void showRadius(SquareIndex index, int radius, bool squareWithPlayerIn)
    {
        List <SquareIndex> indexList = getMoveToSquareList(index, radius, squareWithPlayerIn);

        for (int i = 0; i < indexList.Count; i++)
        {
            instantiateSquare(indexList[i].x, indexList[i].y);
        }
    }
Exemple #14
0
        /// <summary>
        /// Is specified square on the board.
        /// </summary>

        private bool SquareIsOnTheBoard(SquareIndex selectedSquare)
        {
            if (selectedSquare.Row >= 0 && selectedSquare.Row < Dimension &&
                selectedSquare.Col >= 0 && selectedSquare.Col < Dimension)
            {
                return(true);
            }

            return(false);
        }
Exemple #15
0
    public void save(SquareIndex targetIndex)
    {
        if (index != targetIndex)
        {
            toMoveSquareList = PathFinder.findPath(index, targetIndex, board, false, true, 10);
        }

        endAction = endSave;
        StartCoroutine(updateMove());
    }
Exemple #16
0
 private int calculateListIndex(SquareIndex index)
 {
     for (int i = 0; i < nodeList.Count; i++)
     {
         if (nodeList[i].index.Equals(index))
         {
             return(i);
         }
     }
     return(-1);
 }
Exemple #17
0
    /// <summary>
    /// Instantiates a new move node
    /// </summary>
    /// <returns>The move node.</returns>
    /// <param name="prefab">The prefab for the node (there could be different nodes).</param>
    /// <param name="index">The index for the node.</param>
    /// <param name="numMoves">Number of moves left to show on the node.</param>
    private MoveNode instantiateMoveNode(GameObject prefab, SquareIndex index, int numMoves)
    {
        MoveNode moveNode = (GameObject.Instantiate(prefab) as GameObject).GetComponent <MoveNode>();

        moveNode.init(numMoves, index);

        moveNode.transform.parent        = transform;
        moveNode.transform.localPosition = board.squareIndexToWorld(index);

        return(moveNode);
    }
Exemple #18
0
 public Player getPlayerOnSquare(SquareIndex index)
 {
     if (isOnBounds(index))
     {
         return(_boardData[index.x][index.y].player);
     }
     else
     {
         return(null);
     }
 }
Exemple #19
0
        /// <summary>
        /// Access a board square.
        /// </summary>

        private int this[SquareIndex selectedSquare]
        {
            get
            {
                return(Squares[selectedSquare.Row][selectedSquare.Col]);
            }
            set
            {
                Squares[selectedSquare.Row][selectedSquare.Col] = value;
                Tiles[value] = selectedSquare;
            }
        }
Exemple #20
0
    public IEnumerator updateMove()
    {
        if (toMoveSquareList.Count > 0)
        {
            SquareIndex nextSquareIndex = toMoveSquareList[0];
            Vector2     direction       = nextSquareIndex.V2 - index.V2;

            Vector3 position       = board.squareIndexToWorld(index);
            Vector3 targetPosition = board.squareIndexToWorld(nextSquareIndex);

            float magnitude        = (targetPosition - position).sqrMagnitude;
            float currentMagnitude = (cachedTransform.position - position).sqrMagnitude;

            Vector3 directionV3 = Util.v2ToV3(direction);

            while (currentMagnitude < magnitude)
            {
                cachedTransform.position += directionV3 * moveSpeed * Time.deltaTime;
                currentMagnitude          = (cachedTransform.position - position).sqrMagnitude;

                yield return(null);
            }
            cachedTransform.position = board.squareIndexToWorld(nextSquareIndex);

            _index = nextSquareIndex;
            board.updatePlayerPosition(_index, this);

            toMoveSquareList.RemoveAt(0);

            //If the player has the ball, check for tackles
            if (hasTheBall)
            {
                List <Player> playerList = board.retrieveAdjacentPlayerList(_index, team.opponentTeam.teamData.id);
                if (playerList.Count > 0)
                {
                    checkForTackle(playerList[0]);
                }
                else
                {
                    StartCoroutine(updateMove());
                }
            }
            else
            {
                StartCoroutine(updateMove());
            }
        }
        else
        {
            endAction();
        }
    }
Exemple #21
0
    public void pass(Player owner, SquareIndex index, List <SquareIndex> toMoveSquareList)
    {
        this.owner            = owner;
        this.index            = index;
        this.toMoveSquareList = toMoveSquareList;

        //Add the player level as power
        power            = owner.playerData.level;
        canBeIntercepted = true;

        endAction = endPass;
        StartCoroutine(updateMove());
    }
Exemple #22
0
        /// <summary>
        /// Access board by row and column.
        /// </summary>

        public int this[int row, int col]
        {
            get
            {
                SquareIndex selectedSquare = new SquareIndex(row, col);
                return(this[selectedSquare]);
            }
            set
            {
                SquareIndex selectedSquare = new SquareIndex(row, col);
                this[selectedSquare] = value;
            }
        }
Exemple #23
0
    /// <summary>
    /// Retrieves all the players adjacent to an index
    /// </summary>
    /// <returns>The player list.</returns>
    /// <param name="index">The index whose adjacent players we want to retrieve.</param>
    /// <param name="teamId">The id of the team whose players we want to retrieve.</param>
    public List <Player> retrieveAdjacentPlayerList(SquareIndex index, string teamId)
    {
        List <Player>      playerList = new List <Player>();
        List <SquareIndex> indexList  = getAdjacentTileList(index);

        for (int i = 0; i < indexList.Count; i++)
        {
            Player player = getPlayerOnSquare(indexList[i]);
            if (player != null && !player.isInactive && player.team.teamData.id == teamId)
            {
                playerList.Add(player);
            }
        }

        return(playerList);
    }
Exemple #24
0
        /// <summary>
        /// Serial board accessor: left-to-right, then top-to-bottom
        /// </summary>

        public int this[int index]
        {
            get
            {
                SquareIndex selectedSquare =
                    new SquareIndex(index / Dimension, index % Dimension);

                return(this[selectedSquare]);
            }
            set
            {
                SquareIndex selectedSquare =
                    new SquareIndex(index / Dimension, index % Dimension);

                this[selectedSquare] = value;
            }
        }
Exemple #25
0
    public void intercept(SquareIndex index, bool success)
    {
        if (success)
        {
            toMoveSquareList = new List <SquareIndex>();
            toMoveSquareList.Add(index);

            board.removePlayerFromSquare(index);

            endAction = endIntercept;
            StartCoroutine(updateMove());
        }
        else
        {
            setInactive(true);
        }
    }
Exemple #26
0
    public List <SquareIndex> getAdjacentTileList(SquareIndex index)
    {
        List <SquareIndex> tileList = new List <SquareIndex>();

        for (int i = index.x - 1; i <= index.x + 1; i++)
        {
            for (int j = index.y - 1; j <= index.y + 1; j++)
            {
                SquareIndex newindex = new SquareIndex(i, j);
                if (isOnBounds(newindex) && index != newindex)
                {
                    tileList.Add(newindex);
                }
            }
        }

        return(tileList);
    }
Exemple #27
0
    private void removeIndex(int listIndex, SquareIndex index)
    {
        for (int i = listIndex; i < nodeList.Count; i++)
        {
            GameObject.Destroy(nodeList[i].gameObject);
        }
        for (int i = listIndex; i < nodeList.Count; i++)
        {
            nodeList.RemoveRange(listIndex, nodeList.Count - listIndex);
        }

        MoveNode head = nodeList[0];

        head.index = index;
        head.gameObject.transform.position = board.squareIndexToWorld(head.index);

        SquareIndex prevIndex = nodeList.Count == 1 ? startIndex : nodeList[nodeList.Count - 1].index;
    }
Exemple #28
0
    //Returns a list with all the squares in an area
    public List <SquareIndex> getSquareListInArea(SquareIndex index, int radius)
    {
        List <SquareIndex> indexList = new List <SquareIndex>();

        for (int i = index.x - radius; i < index.x + radius + 1; i++)
        {
            for (int j = index.y - radius; j < index.y + radius + 1; j++)
            {
                if (Mathf.Abs(index.x - i) <= radius && Mathf.Abs(index.y - j) <= radius && Mathf.Abs(index.x - i) + Mathf.Abs(index.y - j) <= radius)
                {
                    SquareIndex currentIndex = new SquareIndex(i, j);
                    bool        onBounds     = isOnBounds(currentIndex);

                    indexList.Add(currentIndex);
                }
            }
        }
        return(indexList);
    }
Exemple #29
0
    private SquareIndex getCorrectIndex()
    {
        SquareIndex index = new SquareIndex(player.team.isP1Team ? (player.index.x - 1) : (player.index.x + 1), player.index.y);

        if (!isIndexCorrect(index))
        {
            index = new SquareIndex(player.index.x, index.y - 1);
        }
        if (!isIndexCorrect(index))
        {
            index = new SquareIndex(player.index.x, index.y + 1);
        }
        if (!isIndexCorrect(index))
        {
            index = new SquareIndex(player.team.isP1Team ? (player.index.x + 1) : (player.index.x - 1), index.y);
        }

        return(index);
    }
Exemple #30
0
        public void Click(SquareIndex index)
        {
            ClickSquare(index.Row, index.Column);

            foreach (int row in Enumerable
                     .Range(0, Constants.GridDimension)
                     .Except(new[] { index.Row }))
            {
                ClickSquare(row, index.Column);
            }

            foreach (int column in Enumerable
                     .Range(0, Constants.GridDimension)
                     .Except(new[] { index.Column }))
            {
                ClickSquare(index.Row, column);
            }

            FindSequences();
        }