Esempio n. 1
0
    private bool TryPutDown()
    {
        List <PlacementPad> possiblePlacements = new List <PlacementPad>();

        bool foundPlacement = false;

        foreach (Collider2D hit in GetCollidersFacing())
        {
            PlacementPad placeable = hit.GetComponent <PlacementPad>();
            if (placeable != null)
            {
                foundPlacement = true;
                if (placeable.CanPlace(heldObject))
                {
                    possiblePlacements.Add(placeable);
                }
            }
        }

        if (possiblePlacements.Count > 0)
        {
            PlacementPad closest         = null;
            float        closestDistance = Mathf.Infinity;
            foreach (PlacementPad pad in possiblePlacements)
            {
                float distance = Vector3.Distance(pad.transform.position, holdSpot.transform.position);
                if (distance < closestDistance)
                {
                    closestDistance = distance;
                    closest         = pad;
                }
            }

            closest.Place(heldObject);
            OnSetDown.Invoke();
            heldObject.isCarried = false;
            heldObject           = null;
        }
        else
        {
            if (!foundPlacement)
            {
                return(true);

                heldObject.Throw(transform.up);
                heldObject.isCarried = false;
                heldObject           = null;
            }
        }

        return(true);
    }