private void StopDragging() { dragging = false; dropShadow.enabled = false; // Find the nearest valid slot InputSlot nearestSlot = null; float shortestDistance = 0; foreach (InputSlot inputSlot in inputSlots) { float distance = CanvasScale.Instance.WorldToCanvas(Vector3.Distance(draggedKey.transform.position, inputSlot.transform.position)); if (distance < snapDistance && (nearestSlot == null || distance < shortestDistance)) { nearestSlot = inputSlot; shortestDistance = distance; } } if (nearestSlot != null) { AudioManager.Instance.Play("key_insert"); // Remove the key that was occupying the slot InputKey replacedKey = nearestSlot.GetInputKey(); if (replacedKey != null) { replacedKey.SetInputSlot(null); // Fire off in a random direction //float randomAngle = Random.Range(0, 360); //replacedKey.SetVelocity(1000 * new Vector3(Mathf.Cos(randomAngle), Mathf.Sin(randomAngle), 0)); // Fire off away from the dragged key replacedKey.SetVelocity(1000 * (replacedKey.transform.position - draggedKey.transform.position).normalized); replacedKey.transform.SetAsLastSibling(); InputMapper.Instance.RemoveMapping(nearestSlot.GetAction()); } nearestSlot.SetInputKey(draggedKey); draggedKey.SetInputSlot(nearestSlot); draggedKey.transform.SetAsFirstSibling(); InputMapper.Instance.AddMapping(nearestSlot.GetAction(), draggedKey.GetKeyCode()); } }
private void StartDragging(InputKey inputKey) { dragging = true; draggedKey = inputKey; dragOffset = draggedKey.transform.position - Input.mousePosition; dropShadow.transform.SetAsLastSibling(); inputKey.transform.SetAsLastSibling(); inputKey.SetVelocity(Vector3.zero); // Check if it's being removed from a slot InputSlot oldInputSlot = draggedKey.GetInputSlot(); if (oldInputSlot != null) { oldInputSlot.SetInputKey(null); draggedKey.SetInputSlot(null); InputMapper.Instance.RemoveMapping(oldInputSlot.GetAction()); } dropShadow.enabled = true; dropShadow.transform.position = draggedKey.transform.position + CanvasScale.Instance.CanvasToWorld(dropShadowOffset); AudioManager.Instance.Play("key_pickup"); }