private void Start() { inputKeys = new List <InputKey>(keyParent.GetComponentsInChildren <InputKey>()); inputSlots = new List <InputSlot>(fragmentParent.GetComponentsInChildren <InputSlot>()); inputFragments = new List <InputFragment>(fragmentParent.GetComponentsInChildren <InputFragment>()); dropShadow.enabled = false; // Insert each key into the nearest slot foreach (InputKey inputKey in inputKeys) { float shortestDistance = 0; InputSlot nearestSlot = null; foreach (InputSlot inputSlot in inputSlots) { float distance = Vector3.Distance(inputKey.transform.position, inputSlot.transform.position); if (nearestSlot == null || distance < shortestDistance) { nearestSlot = inputSlot; shortestDistance = distance; } } nearestSlot.SetInputKey(inputKey); inputKey.SetInputSlot(nearestSlot); inputKey.transform.position = nearestSlot.transform.position; InputMapper.Instance.AddMapping(nearestSlot.GetAction(), inputKey.GetKeyCode()); } }
// Do an animation where they break one by one //private IEnumerator BreakCoroutine() //{ // while (keysToBreak.Count > 0 || fragmentsToBreak.Count > 0) // { // bool itemBroken = false; // if (keysToBreak.Count > 0 && Random.value < 0.5) // { // InputKey inputKey = keysToBreak[Random.Range(0, keysToBreak.Count)]; // keysToBreak.Remove(inputKey); // BreakKey(inputKey); // itemBroken = true; // } // else // { // InputFragment inputFragment = fragmentsToBreak[Random.Range(0, fragmentsToBreak.Count)]; // // Only break a fragment once any keys in it have been broken // bool fragmentContainsKeys = false; // foreach (InputSlot inputSlot in inputFragment.GetComponentsInChildren<InputSlot>()) // { // if (inputSlot.GetInputKey() != null) // { // fragmentContainsKeys = true; // break; // } // } // if (!fragmentContainsKeys) // { // fragmentsToBreak.Remove(inputFragment); // BreakFragment(inputFragment); // itemBroken = true; // } // } // if (itemBroken) { yield return new WaitForSeconds(0.2f); } // } //} private void BreakKey(InputKey inputKey) { if (dragging && draggedKey == inputKey) { StopDragging(); } Vector3 positionVector = inputKey.transform.position - keyParent.transform.position; positionVector = CanvasScale.Instance.WorldToCanvas(positionVector); positionVector.x *= 2; positionVector.y *= 4; inputKey.Break( positionVector + (Vector3)Random.insideUnitCircle * 500f, Random.Range(-360, 360)); InputSlot inputSlot = inputKey.GetInputSlot(); if (inputSlot != null) { inputSlot.SetInputKey(null); InputMapper.Instance.RemoveMapping(inputSlot.GetAction()); } inputKey.SetInputSlot(null); inputKeys.Remove(inputKey); }
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"); }