Example #1
0
 void Drop()
 {
     if (CheckIfTrashcan())
     {
         Destroy(currentObject.gameObject);
         currentDragAble = null;
         currentObject   = null;
         objectOffset    = Vector3.zero;
         return;
     }
     if (CheckIfDropAble())
     {
         List <Cell> cells = currentDragAble.GetOverlapCells(true);
         foreach (Cell c in cells)
         {
             c.inUse = true;
         }
         currentDragAble.AddToGrid(placementGrid);
     }
     else
     {
         currentObject.transform.position = currentDragAble.oldPosition;
     }
     currentDragAble.beingMoved = false;
     currentDragAble            = null;
     currentObject = null;
     objectOffset  = Vector2.zero;
 }
Example #2
0
    void Grab()
    {
        Vector2    mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Collider2D col      = ObjectUnderCursor();

        if (col != null && col.transform.CompareTag("DragAbleObject"))
        {
            currentObject   = col.transform.parent;
            objectOffset    = (Vector2)currentObject.position - mousePos;
            currentDragAble = currentObject.GetComponent <DragAbleObject>();

            if (currentObject.GetComponent <DragAbleObject>().IsOnGrid())
            {
                List <Cell> cells = currentDragAble.GetOverlapCells(false);
                foreach (Cell c in cells)
                {
                    c.inUse = false;
                }
                currentObject.GetComponent <DragAbleObject>().RemoveFromGrid();
            }
            else
            {
                currentObject.GetComponent <DragAbleObject>().oldPosition = currentObject.transform.position;
                currentObject.GetComponent <DragAbleObject>().oldRotation = currentObject.transform.rotation;
            }
            currentDragAble            = currentObject.GetComponent <DragAbleObject>();
            currentDragAble.beingMoved = true;
        }
    }
Example #3
0
 public void AddObject(DragAbleObject obj)
 {
     if (!heldObjects.Contains(obj))
     {
         heldObjects.Add(obj);
         obj.transform.parent = this.transform;
     }
 }
Example #4
0
 public void RemoveObject(DragAbleObject obj)
 {
     if (heldObjects.Contains(obj))
     {
         heldObjects.Remove(obj);
         obj.transform.parent = null;
     }
 }
Example #5
0
    public void Place(DragAbleObject obj, LayerMask gridLayer)
    {
        Cell       c = obj.GetCell(0);
        Collider2D col;

        col = Physics2D.OverlapPoint(c.transform.position, gridLayer, -100f, 100f);
        Cell gridCell = null;

        if (col != null && col.transform.CompareTag("PlacementGrid"))
        {
            gridCell = col.GetComponent <Cell>();
        }
        else
        {
            return;
        }
        obj.transform.position  = obj.transform.position + (gridCell.transform.position - c.transform.position);
        obj.transform.position += new Vector3(0, 0, -1);
        AddObject(obj.GetComponent <DragAbleObject>());
    }
Example #6
0
    bool CheckIfDropAble()
    {
        DragAbleObject dragObject = currentObject.GetComponent <DragAbleObject>();

        return(currentObject != null && dragObject.GetOverlapCells(true)?.Count == dragObject.cellAmount);
    }