Exemplo n.º 1
0
 public void TriggerClick(MonoField field)
 {
     if (selected != null)
     {
         selected.FieldClickEffect(field, board);
     }
 }
Exemplo n.º 2
0
        protected virtual void InstantiateBoard()
        {
            fields = new MonoField[Rows * Columns];
            GetComponent <AspectRatioFitter>().aspectRatio = Columns / (float)Rows;

            Vector2 boardSize = new Vector2(((RectTransform)transform).rect.width * transform.lossyScale.x, ((RectTransform)transform).rect.height * transform.lossyScale.y);

            for (int y = 0; y < Rows; y++)
            {
                for (int x = 0; x < Columns; x++)
                {
                    // Create the field and give it a descriptive name.
                    MonoField newField = MonoField.Instantiate(configuration.fieldPrefab, fieldTransform);
                    newField.name = "Field(" + x + ", " + y + ")";

                    // Anchoring
                    Vector2 minAnchor         = new Vector2(x / (float)Columns, y / (float)Rows);
                    Vector2 maxAnchor         = new Vector2((x + 1) / (float)Columns, (y + 1) / (float)Rows);
                    var     newFieldTransform = (RectTransform)newField.transform;
                    newFieldTransform.anchorMin = minAnchor;
                    newFieldTransform.anchorMax = maxAnchor;

                    // Setup
                    fields[level.FieldIndex(x, y)] = newField;
                    fields[level.FieldIndex(x, y)].Setup(level.FieldIndex(x, y));

                    // Instantiate board pieces
                    var piece     = level.Fields[level.FieldIndex(x, y)];
                    var monoPiece = AddPiece(piece, fields[level.FieldIndex(x, y)]);
                }
            }
        }
Exemplo n.º 3
0
        public override void FieldClickEffect(MonoField field, DesignerBoard board)
        {
            var piece = board.AddPiece(new Piece {
                Type = Piece.PieceType.Player, MagnetPolarity = polarity
            }, field);

            piece.SetDraggable(false);
        }
Exemplo n.º 4
0
        public override void FieldClickEffect(MonoField field, DesignerBoard board)
        {
            var piece = board.AddPiece(new Piece {
                Type = Piece.PieceType.Magnet, MagnetPolarity = (polarityStrength > 0), MagnetStrength = Mathf.Abs(polarityStrength)
            }, field);

            piece.SetDraggable(false);
        }
Exemplo n.º 5
0
        public override Vector3?Place(Draggable piece, MonoField square)
        {
            void RemoveFromOldField()
            {
                foreach (var field in fields)
                {
                    if (field != null && field.piece == piece)
                    {
                        field.piece = null;
                        level.Fields[field.FieldId].Type = Piece.PieceType.Empty;
                    }
                }
            }

            if (square.piece == null)
            {
                RemoveFromOldField();
                // set piece on new field
                square.piece = piece;
                level.Fields[square.FieldId] = piece.basePiece;
                return(square.transform.position);
            }
            else if (!square.piece.CanDrag)
            {
                // For replacing pieces when placed
                RemoveFromOldField();
                DeletePiece(square);
                square.piece = piece;
                level.Fields[square.FieldId] = piece.basePiece;
                return(square.transform.position);
            }
            // Both pieces must be magnets
            bool stackMagnetCondition = piece.basePiece.Type == Piece.PieceType.Magnet;

            stackMagnetCondition &= square.piece != piece;
            stackMagnetCondition &= square.piece.basePiece.Type == piece.basePiece.Type;

            // Neither magnet can be 0
            stackMagnetCondition &= piece.basePiece.MagnetStrength != 0;
            stackMagnetCondition &= square.piece.basePiece.MagnetStrength != 0;

            // Magnet polarities must be equal
            stackMagnetCondition &= piece.basePiece.MagnetPolarity == square.piece.basePiece.MagnetPolarity;

            if (stackMagnetCondition)
            {
                var newMagnet = new Piece {
                    MagnetStrength = (square.piece.basePiece.MagnetStrength + piece.basePiece.MagnetStrength), MagnetPolarity = piece.basePiece.MagnetPolarity, Type = piece.basePiece.Type
                };
                bool oldDragability = square.piece.CanDrag;
                DeletePiece(square);
                RemoveFromOldField();
                Destroy(piece.gameObject);
                var newPiece = AddPiece(newMagnet, square);
                newPiece.SetDraggable(oldDragability);
            }
            return(null);
        }
Exemplo n.º 6
0
 public void DeletePiece(MonoField field)
 {
     if (field.piece != null)
     {
         Destroy(field.piece.gameObject);
         field.piece = null;
         level.Fields[field.FieldId].Type = Piece.PieceType.Empty;
     }
 }
Exemplo n.º 7
0
 public void OnPointerUp(PointerEventData eventData)
 {
     if (CanDrag)
     {
         dragging = false;
         MonoField cell = Utils.GetComponentFromRaycast <MonoField>(Input.mousePosition);
         if (cell == null)
         {
             transform.position = currentPos;
         }
         else
         {
             var placement = board.Place(this, cell);
             transform.position = placement != null ? (Vector3)placement : (Vector3)currentPos;
         }
     }
 }
Exemplo n.º 8
0
        public override void FieldClickEffect(MonoField field, DesignerBoard board)
        {
            var piece = board.AddPiece(pieceType, field);

            piece.SetDraggable(false);
        }
Exemplo n.º 9
0
 public override void FieldClickEffect(MonoField field, DesignerBoard board)
 {
     board.DeletePiece(field);
 }
Exemplo n.º 10
0
 public abstract void FieldClickEffect(MonoField field, DesignerBoard board);
Exemplo n.º 11
0
 public override void FieldClickEffect(MonoField field, DesignerBoard board)
 {
 }
Exemplo n.º 12
0
        public Draggable AddPiece(Piece piece, MonoField field)
        {
            var       fieldAnchorMin = ((RectTransform)field.transform).anchorMin;
            var       fieldAnchorMax = ((RectTransform)field.transform).anchorMax;
            var       fieldSize      = new Vector2(1, 1);
            Draggable draggablePiece = null;

            if (piece.Type == pType.Wall || piece.Type == pType.Hole || piece.Type == pType.Endpoint)
            {
                Draggable staticPiece = null;
                if (piece.Type == pType.Wall)
                {
                    staticPiece = Instantiate(configuration.wallPrefab, pieceTransform);
                }
                if (piece.Type == pType.Hole)
                {
                    staticPiece = Instantiate(configuration.holePrefab, pieceTransform);
                }
                if (piece.Type == pType.Endpoint)
                {
                    staticPiece = Instantiate(configuration.endpointPrefab, pieceTransform);
                }
                staticPiece.Setup(fieldAnchorMin, fieldAnchorMax, this, piece);
                draggablePiece = staticPiece;
            }
            else if (piece.Type == pType.Magnet)
            {
                var magnet = Instantiate(configuration.magnetPrefab, pieceTransform);
                magnet.Setup(fieldAnchorMin, fieldAnchorMax, this, piece, piece.MagnetStrength, piece.MagnetPolarity);
                draggablePiece = magnet;
            }
            else if (piece.Type == pType.Player)
            {
                var player = Instantiate(configuration.playerPrefab, pieceTransform);
                player.Setup(fieldAnchorMin, fieldAnchorMax, this, piece, piece.MagnetStrength, piece.MagnetPolarity);
                draggablePiece = player;
            }
            else if (piece.Type == pType.Door)
            {
                var door = Instantiate(configuration.doorPrefab, pieceTransform);
                door.Setup(fieldAnchorMin, fieldAnchorMax, this, piece, piece.MagnetStrength, piece.MagnetPolarity);
                draggablePiece = door;
            }
            else if (piece.Type == pType.Lock)
            {
                var lockObj = Instantiate(configuration.lockPrefab, pieceTransform);
                lockObj.Setup(fieldAnchorMin, fieldAnchorMax, this, piece, piece.MagnetStrength, piece.MagnetPolarity);
                draggablePiece = lockObj;
            }
            else
            {
                return(null);
            }
            var position = Place(draggablePiece, field);

            if (position == null)
            {
                Destroy(draggablePiece.gameObject);
                return(null);
            }
            return(draggablePiece);
        }
Exemplo n.º 13
0
 public abstract Vector3?Place(Draggable piece, MonoField square);