private void OnTriggerEnter2D(Collider2D collision) { // Early exit if the outer shape is already complete. if (shapeSlot.shapeComplete) { return; } GameObject currentStick = collision.gameObject; SpriteRenderer currentStickSprite = currentStick.GetComponent <SpriteRenderer>(); // Exit early if *somehow* an object that is not the object being dragged collides with a stickSlot if (currentStick != m_EventSystem.currentSelectedGameObject) { return; } // If the stick is close enough in the right x,y position if ((Mathf.Abs(currentStick.transform.position.x - gameObject.transform.position.x) < 1) && (Mathf.Abs(currentStick.transform.position.y - gameObject.transform.position.y) < 1)) { // And the stick is rotated along the z axis similarly enough if (Mathf.Abs(currentStick.transform.rotation.z - gameObject.transform.rotation.z) < 10) { // If the slot is empty, simply add stick to slot. if (heldSticks.Count == 0) { shapeSlot.AddToSidesFilled(true); // Block moved outside of condition (below) to remove avoid code redundancy. } else if (heldSticks.Count == 1) // If the color is different than the held stick, mix them. { Color heldStickColor = heldSticks[0].GetComponent <SpriteRenderer>().color; if (currentStickSprite.color != heldStickColor) { currentStick.GetComponent <SpriteRenderer>().color = MixColors(currentStickSprite.color, heldStickColor); currentStickSprite.sortingOrder = 3; shapeSlot.AddToSidesFilled(false); } else // Early exit to avoid stick placement if the colors match. { return; } } else // Avoid stick placement if there are 2 sticks already by returning from the function { return; } Camera.main.GetComponent <DraggableObject2D>().draggingMode = false; currentStick.transform.position = gameObject.transform.position; currentStick.transform.eulerAngles = gameObject.transform.eulerAngles; heldSticks.Add(currentStick); currentStick.GetComponent <Stick>().SetParentSlot(this); topStickColor = currentStick.GetComponent <SpriteRenderer>().color; shapeSlot.CheckShapeCompleteness(); } } }
// Update is called once per frame void Update() { // Press R to restart a level if (Input.GetKeyDown(KeyCode.R)) { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } /*---- CLICK TO DRAG ----*/ // First frame when user clicks left mouse if (Input.GetMouseButtonDown(0)) { // If a ray hit a collider (2D) var hit2d = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero); if (hit2d && (hit2d.collider.gameObject.tag == "Stick")) { currentStick = hit2d.collider.gameObject; // Early exit from dragging if a stick is part of a completed shape. StickSlot stickParentSlot = currentStick.GetComponent <Stick>().GetParentSlot(); if (stickParentSlot != null) { if (stickParentSlot.GetShapeSlot().shapeComplete) { return; } } stickCenter = currentStick.transform.position; touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); offset = touchPosition - stickCenter; currentStickSprite = currentStick.GetComponent <SpriteRenderer>(); draggingMode = true; m_EventSystem.SetSelectedGameObject(currentStick); } } // Every frame when user holds on left mouse if (Input.GetMouseButton(0)) { if (draggingMode) { // if stick is in a slot: remove it from that slot now foreach (GameObject slot in slotGameObjects) { if (slot.GetComponent <StickSlot>().heldSticks.Contains(currentStick)) { slot.GetComponent <StickSlot>().heldSticks.Remove(currentStick); currentStick.GetComponent <Stick>().SetParentSlot(null); Color originalColor = currentStick.GetComponent <Stick>().originalColor; // If the stick being removed has a modified color (i.e. there is another stick under): if (currentStickSprite.color != originalColor) { ShapeSlot stickShape = slot.transform.parent.GetComponent <ShapeSlot>(); stickShape.RemoveFromSidesFilled(false); currentStickSprite.color = originalColor; currentStickSprite.sortingOrder = 2; StickSlot currentStickSlot = slot.GetComponent <StickSlot>(); currentStickSlot.topStickColor = currentStickSlot.heldSticks[0].GetComponent <SpriteRenderer>().color; stickShape.CheckShapeCompleteness(); } else { slot.transform.parent.GetComponent <ShapeSlot>().RemoveFromSidesFilled(true); slot.GetComponent <StickSlot>().topStickColor = Color.white; } // avoid unnecessary computations by exiting loop, since only 1 side can be removed at a time // TODO change into a while loop? break; } } touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); newCenter = touchPosition - offset; currentStick.transform.position = new Vector3(newCenter.x, newCenter.y, newCenter.z); } } if (currentStick && draggingMode && (Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.A))) { if (currentStick == m_EventSystem.currentSelectedGameObject) { currentStick.transform.Rotate(Vector3.forward * velocity * Time.deltaTime); } } if (currentStick && draggingMode && (Input.GetKey(KeyCode.RightArrow) || Input.GetKey(KeyCode.D))) { if (currentStick == m_EventSystem.currentSelectedGameObject) { currentStick.transform.Rotate(Vector3.back * velocity * Time.deltaTime); } } // When mouse is released if (Input.GetMouseButtonUp(0)) { draggingMode = false; currentStick = null; } }