Exemplo n.º 1
0
    // Move connected pieces to the correct positions
    public void setPositions(JigsawPieceData eventPiece)
    {
        if (isNew)
        {
            Vector2 evt    = eventPiece.gameObject.transform.localPosition;
            Vector2 offset = new Vector2(-eventPiece.getDistanceFromCenter().x, -eventPiece.getDistanceFromCenter().y);
            Vector2 move   = new Vector2(evt.x + offset.x, evt.y + offset.y);

            this.GetComponent <RectTransform> ().localPosition = move;

            isNew = false;
        }

        if (markedForDeath)
        {
            return;
        }

        foreach (GameObject go in connectedPieces)
        {
            RectTransform   rect = go.GetComponent <RectTransform> ();
            JigsawPieceData jpd  = go.GetComponent <JigsawPieceData> ();

            go.transform.SetParent(this.transform);

            jpd.moveToConnectPosition();
        }
    }
    /*
     * Method which will return a collection of adjacent puzzle pieces when one is placed.
     */
    public List <JigsawPieceData> checkAdjacentPiece(Vector2 piece, JigsawPieceData jpd)
    {
        List <JigsawPieceData> adj = new List <JigsawPieceData> ();

        foreach (JigsawPieceData jp in foundPieces)
        {
            if (jp != jpd)
            {
                float x = Mathf.Abs(Mathf.Abs(piece.x) - Mathf.Abs(jp.gameObject.GetComponent <Transform> ().position.x));
                float y = Mathf.Abs(Mathf.Abs(piece.y) - Mathf.Abs(jp.gameObject.GetComponent <Transform> ().position.y));

                if (x <= 140 && y <= 165)
                {
                    adj.Add(jp);
                }
            }
        }

        foreach (JigsawPieceData jp in adj)
        {
            Debug.Log(jp.name);
            Debug.Log(jp.getPieceNumber());
        }

        return(adj);
    }
    void Start()
    {
        jmgr = GameObject.FindObjectOfType <JigsawManager> ();
        jevt = GameObject.FindObjectOfType <JigsawPieceEvents> ();

        jpd = gameObject.GetComponent <JigsawPieceData> ();

        jevt.onPieceDroppedEvent += CheckPieceConnection;
    }
    public void OnEndDrag(PointerEventData eventData)
    {
        //Debug.Log ("OnEndDrag");
        cGroup.blocksRaycasts = true;
        if (eventData.pointerDrag.GetComponent <JigsawPieceData> ())
        {
            JigsawPieceData jpd = eventData.pointerDrag.GetComponent <JigsawPieceData> ();
            //Debug.Log ("Piece let go: " + jpd.getPieceNumber());

            JigsawPieceEvents jpevents = GameObject.FindObjectOfType <JigsawPieceEvents> ();
            jpevents.pieceDroppedEvent(this.transform.position, jpd);
        }
    }
 // Method used to place adjacent pieces together
 public void CheckPieceConnection(Vector2 position, JigsawPieceData jpd)
 {
     if (jpd == this.jpd)
     {
         List <JigsawPieceData> pieces = jmgr.checkAdjacentPiece(position, jpd);
         foreach (JigsawPieceData piece in pieces)
         {
             int check = piece.getPieceNumber();
             foreach (PieceConnections pc in this.jpd.getConnections())
             {
                 if (check == pc.getPieceId())
                 {
                     Debug.Log(this.name + " - Connects to: " + check);
                     jmgr.createContainer(this.transform.localPosition, this.jpd.gameObject, piece.gameObject);
                 }
             }
         }
     }
 }
 public void pieceDroppedEvent(Vector2 pos, JigsawPieceData jpd)
 {
     //Debug.Log ("Piece Dropped Event Fired");
     onPieceDroppedEvent?.Invoke(pos, jpd);
 }