Пример #1
0
        public MatrixPiece PieceInDirection(Vector2Int location, MapDirection direction)
        {
            Vector2Int offset        = direction.Offset();
            var        checkLocation = location + offset;

            return(PieceAt(checkLocation));
        }
Пример #2
0
        public MoveResult MovePiece(MatrixPiece piece, MapDirection direction, float duration)
        {
            if (null == piece)
            {
                return(MoveResult.Fail);
            }

            if (null == piece.owner || !piece.owner.TryGetTarget(out object pieceOwner))
            {
                Debug.Log("Error. Piece owner is missing");
                return(MoveResult.Fail);
            }

            MatrixNode2D node = pieceOwner as MatrixNode2D;

            if (null == node)
            {
                Debug.Log("Error. Need MatrixNode2D for Matrix board");
                return(MoveResult.Fail);
            }

            if (node.IsAnimating)
            {
                return(MoveResult.Animating);
            }

            var oldOrigin = piece.origin;
            var newOrigin = oldOrigin + direction.Offset();

            if (newOrigin == oldOrigin)
            {
                return(MoveResult.Fail);
            }

            var excludeList = new HashSet <MatrixPiece>();

            excludeList.Add(piece);
            if (board.IsPieceBlockedAt(newOrigin, piece, excludeList))
            {
                return(MoveResult.Blocked);
            }

            board.RemovePiece(piece);
            board.PutPiece(piece, newOrigin);

            var endPosition = NodePosition(node);

            if (duration > 0)
            {
                node.moveAnimationCurve             = new Optional <AnimationCurve <Vector2> >(new AnimationCurve <Vector2>());
                node.moveAnimationCurve.value.start = node.transform.localPosition;
                node.moveAnimationCurve.value.end   = endPosition;

                node.moveAnimationTimer.duration = duration;
                node.moveAnimationTimer.Reset();
            }
            else
            {
                node.transform.localPosition = endPosition;
            }

            return(MoveResult.Success);
        }