Esempio n. 1
0
 /// <summary>
 /// Links a <see cref="GameBoard"/> <see cref="Cell"/> to a visual component (ie, <see cref="CellViewModel"/>)
 /// </summary>
 /// <param name="cellView"></param>
 /// <param name="cell"></param>
 void initializeCellViewWithCell(CellViewModel cellView, Cell cell)
 {
     cellView.transform.localPosition = cellPositionToViewportPosition(cell.Position);
     cellView.gameObject.name         = $"C_{cell.Position.X}_{cell.Position.Y}";
     cellView.Cell = cell;
     cellView.Initialize();
 }
Esempio n. 2
0
        private void jump(CellViewModel fromCellVM, CellViewModel toCellVM)
        {
            var fromCell = fromCellVM.Cell;
            var toCell   = toCellVM.Cell;

            inputManager.DisableInput = true;

            audioManager.PlayMove();

            // Animate selected peg to 'to' cell
            // TODO: Make this not a callback but either a coroutine or async function
            fromCellVM.JumpPegTo(toCellVM, () =>
            {
                // Update the game board with the Jump
                var jump = GameBoard.ExecuteJump(fromCell, toCell, ElapsedTime);

                // Return the 'from' cell's peg to its own cell
                fromCellVM.ResetPeg();

                // Add move to history
                MoveHistory.Moves.Add(jump);

                // Update cell valid move states after this move
                GameBoard.UpdateAvailableMoves();

                inputManager.DisableInput = false;
                clearSelectedCell();

                // Trigger win/loss if this jump is the last possible.
                checkForWinLose();
            });
        }
Esempio n. 3
0
        void InputManager_OnObjectHighlightChanged(bool isHighlighted, GameObject obj)
        {
            if (!isActiveAndEnabled)
            {
                return;
            }

            CellViewModel cellVM = obj.GetComponent <CellViewModel>();

            // If no current selection, highlight cells with pegs that can move
            if (SelectedCell == null)
            {
                cellVM.IsHighlighted = isHighlighted && cellVM.Cell.CanPegMove;
            }
            // With a current selection, highlight cell where the selected peg can move to
            else
            {
                // OPTIMIZE: Cache this since it only needs to be calculated once after the cell is selected.
                cellVM.IsHighlighted = isHighlighted && GameBoard.IsValidMove(SelectedCell.Cell, cellVM.Cell);
            }

            // Audio feedback of highlight
            if (cellVM.IsHighlighted)
            {
                audioManager.PlayHighlight();
            }
        }
Esempio n. 4
0
        void InputManager_OnSelectObject(GameObject obj)
        {
            if (obj == null)
            {
                return;
            }

            CellViewModel vm = obj.GetComponent <CellViewModel>();

            // If this is an expander cell, convert this cell to a real cell and add expanders around it.
            if (vm.IsExpanderCell)
            {
                vm.IsExpanderCell = false;
                AddExpanderCellsAround(vm);
            }
        }
Esempio n. 5
0
        public List <CellViewModel> AddExpanderCellsAround(CellViewModel edgeCellVM)
        {
            var possibleExpanderPositions = gameBoard.GetPossibleNeighbourPositions(edgeCellVM.Cell.Position);
            var newExpanders = possibleExpanderPositions.Except(gameBoard.HexCells.Select(x => x.Position));

            List <CellViewModel> newCells = new List <CellViewModel>();

            foreach (var item in newExpanders)
            {
                var newCell = AddCell(item);
                newCell.IsExpanderCell = true;
                newCells.Add(newCell);
            }

            return(newCells);
        }
Esempio n. 6
0
        public void JumpPegTo(CellViewModel selectedCell, Action callback)
        {
            var targetCell          = selectedCell.Cell;
            var targetPeg           = selectedCell.Peg;
            var targetWorldPosition = targetPeg.transform.position;

            // trigger animation. There are better ways to animate, but this is good enough for now.
            var t = Peg.transform;

            runningTween?.Kill(true);
            runningTween = DOTween.Sequence()
                           // move 'from' peg over top of the 'to' peg
                           .Append(t.DOMove(targetWorldPosition, PegMoveAnimationDuration))
                           // shrink the 'from' peg
                           .Append(t.DOScale(targetPeg.transform.localScale, PegPullAnimationDuration))
                           .AppendCallback(() => callback?.Invoke())
                           .OnComplete(clearRunningTween);
        }
Esempio n. 7
0
        void InputManager_OnSelectObject(GameObject obj)
        {
            if (!isActiveAndEnabled)
            {
                return;
            }

            CellViewModel cellVM = obj == null ? null : obj.GetComponent <CellViewModel>();

            // cellVM will be null to signal something other than a GameObject was clicked
            if (cellVM == null)
            {
                clearSelectedCell();
            }
            else if (SelectedCell == null)
            {
                // If no current selection, select the activated object if it has valid moves
                if (cellVM.Cell.CanPegMove)
                {
                    selectCell(cellVM);
                }
                else
                {
                    clearSelectedCell();
                }
            }
            else
            {
                // If there is a current selection then move the peg to the activated cell (if possible)
                if (GameBoard.IsValidMove(SelectedCell.Cell, cellVM.Cell))
                {
                    jump(SelectedCell, cellVM);
                }
                else
                {
                    clearSelectedCell();
                }
            }
        }
Esempio n. 8
0
        private void selectCell(CellViewModel cellVM)
        {
            // Deselect old cell first
            if (SelectedCell != null && SelectedCell != cellVM)
            {
                SelectedCell.DeselectCell();
            }
            // Don't reselect the same cell. Treat it as a deselect instead and bail out.
            if (SelectedCell == cellVM)
            {
                clearSelectedCell();
                return;
            }

            // Note the select and signal the cell's viewmodel that it make the cell/peg look selected.
            SelectedCell = cellVM;
            var holdPosition = PegHoldPosition.position;

            cellVM.SelectCell(holdPosition);

            // Audio feedback of select
            audioManager.PlaySelect();
        }