Beispiel #1
0
        public bool PutPiece(MatrixPiece piece, Vector2Int origin)
        {
            if (IsPieceBlockedAt(origin, piece))
            {
                return(false);
            }
            if (piece.board != null)
            {
                return(false);
            }

            piece.board  = new WeakReference <MatrixBoard>(this);
            piece.origin = origin;

            for (int x = 0; x < piece.Width; x++)
            {
                for (int y = 0; y < piece.Height; y++)
                {
                    var pieceLocation     = new Vector2Int(x, y);
                    var isOccupiedByPiece = piece.CellAt(pieceLocation);
                    if (!isOccupiedByPiece)
                    {
                        continue;
                    }

                    var cell = CellAt(origin + pieceLocation);
                    cell.piece         = piece;
                    cell.pieceLocation = pieceLocation;
                }
            }

            return(true);
        }
        public void TestMatrixPiece_ClearAndFill()
        {
            var sut = new MatrixPiece(new Vector2Int(1, 1));

            Assert.AreEqual(false, sut.CellAt(new Vector2Int(0, 0)));

            sut.Fill();
            Assert.AreEqual(true, sut.CellAt(new Vector2Int(0, 0)));

            sut.Clear();
            Assert.AreEqual(false, sut.CellAt(new Vector2Int(0, 0)));
        }
Beispiel #3
0
        protected override void Awake()
        {
            base.Awake();

            if (null == piece)
            {
                piece = new MatrixPiece(new Vector2Int(1, 1));
                if (!piece.BuildFromShape(pieceShape))
                {
                    // Default piece: a 1x1
                    piece.Fill();
                }
            }

            piece.owner = new WeakReference <object>(this);

            // Matrix nodes must be kinematic only
            rigidbody.isKinematic = true;
        }
        public void TestMatrixPiece_BuildFromShape()
        {
            var sut = new MatrixPiece(new Vector2Int(1, 1));

            Assert.AreEqual(false, sut.CellAt(new Vector2Int(0, 0)));

            var pieceShape = new List <string>();

            pieceShape.Add(" *");
            pieceShape.Add("*");

            sut.BuildFromShape(pieceShape);

            Assert.AreEqual(2, sut.Width);
            Assert.AreEqual(2, sut.Height);

            Assert.AreEqual(false, sut.CellAt(new Vector2Int(0, 0)));
            Assert.AreEqual(true, sut.CellAt(new Vector2Int(1, 0)));
            Assert.AreEqual(true, sut.CellAt(new Vector2Int(0, 1)));
            Assert.AreEqual(false, sut.CellAt(new Vector2Int(1, 1)));
        }
Beispiel #5
0
        public bool IsPieceBlockedAt(Vector2Int origin, MatrixPiece piece, HashSet <MatrixPiece> excludePieces)
        {
            for (int x = 0; x < piece.Width; x++)
            {
                for (int y = 0; y < piece.Height; y++)
                {
                    var pieceLocation     = new Vector2Int(x, y);
                    var isOccupiedByPiece = piece.CellAt(pieceLocation);
                    if (!isOccupiedByPiece)
                    {
                        continue;
                    }

                    var location = origin + pieceLocation;

                    if (IsCellBlockedAt(location))
                    {
                        // If specified, ignore some pieces for check
                        if (excludePieces.Count > 0)
                        {
                            var cell = CellAt(location);
                            if (null != cell)
                            {
                                if (excludePieces.Contains(cell.piece))
                                {
                                    continue;
                                }
                            }
                        }

                        return(true);
                    }
                }
            }

            return(false);
        }
Beispiel #6
0
        public void RemovePiece(MatrixPiece piece)
        {
            if (null == piece)
            {
                return;
            }
            if (piece.board.TryGetTarget(out MatrixBoard ownerBoard))
            {
                if (ownerBoard != this)
                {
                    Debug.Log("Error. Can't remove a piece from a different board");
                    return;
                }
            }
            piece.board = null;

            for (int x = 0; x < piece.Width; x++)
            {
                for (int y = 0; y < piece.Height; y++)
                {
                    var pieceLocation     = new Vector2Int(x, y);
                    var isOccupiedByPiece = piece.CellAt(pieceLocation);
                    if (!isOccupiedByPiece)
                    {
                        continue;
                    }

                    var cell = CellAt(piece.origin + pieceLocation);
                    if (cell.piece != piece)
                    {
                        Debug.Log("Error. Cell/piece mismatch");
                        continue;
                    }
                    cell.piece = null;
                }
            }
        }
Beispiel #7
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);
        }
        public void TestMatrixBoard()
        {
            var sut    = new MatrixBoard(new Vector2Int(3, 3));
            var lPiece = new MatrixPiece(new Vector2Int(3, 2));

            lPiece.SetCell(new Vector2Int(0, 0), true);
            lPiece.SetCell(new Vector2Int(1, 0), true);
            lPiece.SetCell(new Vector2Int(2, 0), true);
            lPiece.SetCell(new Vector2Int(2, 1), true);

            var squarePiece = new MatrixPiece(new Vector2Int(2, 2));

            squarePiece.Fill();
            Assert.AreEqual(true, squarePiece.CellAt(new Vector2Int(0, 0)));
            Assert.AreEqual(true, squarePiece.CellAt(new Vector2Int(1, 0)));
            Assert.AreEqual(true, squarePiece.CellAt(new Vector2Int(0, 1)));
            Assert.AreEqual(true, squarePiece.CellAt(new Vector2Int(1, 1)));

            Assert.AreEqual(true, sut.PutPiece(lPiece, new Vector2Int(0, 0)));
            Assert.AreEqual(false, sut.PutPiece(squarePiece, new Vector2Int(0, 0)));
            Assert.AreEqual(false, sut.PutPiece(squarePiece, new Vector2Int(1, 0)));
            Assert.AreEqual(false, sut.PutPiece(squarePiece, new Vector2Int(1, 1)));
            Assert.AreEqual(true, sut.PutPiece(squarePiece, new Vector2Int(0, 1)));

            Assert.AreEqual(lPiece, sut.PieceAt(new Vector2Int(0, 0)));
            Assert.AreEqual(lPiece, sut.PieceAt(new Vector2Int(1, 0)));
            Assert.AreEqual(lPiece, sut.PieceAt(new Vector2Int(2, 0)));
            Assert.AreEqual(lPiece, sut.PieceAt(new Vector2Int(2, 1)));

            Assert.AreEqual(squarePiece, sut.PieceAt(new Vector2Int(0, 1)));
            Assert.AreEqual(squarePiece, sut.PieceAt(new Vector2Int(1, 1)));
            Assert.AreEqual(squarePiece, sut.PieceAt(new Vector2Int(0, 2)));
            Assert.AreEqual(squarePiece, sut.PieceAt(new Vector2Int(1, 2)));

            Assert.AreEqual(new Vector2Int(0, 1), squarePiece.origin);
            Assert.AreEqual(new Vector2Int(0, 0), lPiece.origin);

            Assert.AreEqual(new Vector2Int(0, 0), sut.CellAt(new Vector2Int(0, 0)).pieceLocation);
            Assert.AreEqual(new Vector2Int(1, 0), sut.CellAt(new Vector2Int(1, 0)).pieceLocation);
            Assert.AreEqual(new Vector2Int(2, 0), sut.CellAt(new Vector2Int(2, 0)).pieceLocation);

            Assert.AreEqual(new Vector2Int(0, 0), sut.CellAt(new Vector2Int(0, 1)).pieceLocation);
            Assert.AreEqual(new Vector2Int(1, 0), sut.CellAt(new Vector2Int(1, 1)).pieceLocation);

            Assert.IsTrue(sut.IsCellBlockedAt(new Vector2Int(0, 0)));
            Assert.IsFalse(sut.IsCellBlockedAt(new Vector2Int(2, 2)));

            Assert.IsTrue(sut.IsPieceBlockedAt(new Vector2Int(0, 1), squarePiece));

            var excludeList = new HashSet <MatrixPiece>();

            excludeList.Add(squarePiece);
            Assert.IsFalse(sut.IsPieceBlockedAt(new Vector2Int(0, 1), squarePiece, excludeList));
            Assert.IsTrue(sut.IsPieceBlockedAt(new Vector2Int(1, 1), squarePiece, excludeList));

            sut.RemovePiece(squarePiece);
            Assert.IsFalse(sut.IsPieceBlockedAt(new Vector2Int(0, 1), squarePiece));

            Assert.AreEqual(null, sut.CellAt(new Vector2Int(0, 1)).piece);
            Assert.AreEqual(null, sut.CellAt(new Vector2Int(1, 1)).piece);
            Assert.AreEqual(null, sut.CellAt(new Vector2Int(0, 2)).piece);
            Assert.AreEqual(null, sut.CellAt(new Vector2Int(1, 2)).piece);
        }
Beispiel #9
0
 public bool IsPieceBlockedAt(Vector2Int origin, MatrixPiece piece)
 {
     return(IsPieceBlockedAt(origin, piece, new HashSet <MatrixPiece>()));
 }