// PUBLIC MODIFIERS new public void Start() { base.Start(); // hide snap objects ShowSnapLocations(false); // snap to the initial location if one is set if (current_snap_loc != null) { target_pos = current_snap_loc.transform.position; current_snap_loc.SetOccupied(true); } // find/set whether dropable if (snap_locations.Count > 0) { dropable = true; } }
/// <summary> /// Causes the object to position itself at the closest snap location /// Returns whether the item was actually dropped (could be not dropable) /// /// requires: - if dropable, snap_locations is not empty /// - snap_locations contains non null elements /// </summary> public virtual bool Drop() { if (!dropable) { return(false); } // find the closest snap location float min_dist = float.MaxValue; SnapLocation closest_snap_loc = current_snap_loc; foreach (SnapLocation loc in snap_locations) { if (loc.IsOccupied()) { continue; // not an option } float dist = Vector2.Distance(transform.position, loc.transform.position); if (dist < min_dist) { min_dist = dist; closest_snap_loc = loc; } } // return if no available snap locations if (closest_snap_loc == null || closest_snap_loc.IsOccupied()) { return(false); } // snap to the chosen snap location target_pos = closest_snap_loc.transform.position; current_snap_loc = closest_snap_loc; current_snap_loc.SetOccupied(true); OnSnapLocationChosen(closest_snap_loc); // hide snap objects ShowSnapLocations(false); // update state grabbed = false; dropping = true; // update graphics ChangeGraphicsByState(); // stop drawing on top of other items current_graphics_obj.GetComponent <SpriteRenderer>().sortingLayerName = NormalSortingLayer; return(true); }