Esempio n. 1
0
    private void DrawLine()
    {
        if (Input.GetMouseButton(0))
        {
            var          mouseVector = (Vector2)cam.ScreenToWorldPoint(Input.mousePosition) - origin;
            RaycastHit2D hit2        = new RaycastHit2D();
            var          hit         = Physics2D.Raycast(origin, mouseVector, Mathf.Infinity, ~(1 << 9));
            if (hit.collider != null)
            {
                firstPoint = hit.point;
                Debug.DrawLine(origin, hit.point, Color.green);
                Debug.DrawRay(origin, hit.point - origin, Color.magenta);
                Debug.DrawRay(hit.point, Vector2.Reflect(hit.point - origin, Vector2.right), Color.magenta);
                ActivateLine();

                lineRenderer.SetPosition(0, origin);
                lineRenderer.SetPosition(1, hit.point);
                if (hit.collider.CompareTag("Wall") && hit != null)
                {
                    if (hit.point.x >= 0)
                    {
                        hit2 = Physics2D.Raycast(hit.point - Vector2.right * 0.01f, Vector2.Reflect(hit.point - origin, Vector2.right), Mathf.Infinity, ~(1 << 9));
                    }
                    else
                    {
                        hit2 = Physics2D.Raycast(hit.point + Vector2.right * 0.01f, Vector2.Reflect(hit.point - origin, Vector2.right), Mathf.Infinity, ~(1 << 9));
                    }

                    if (hit2.collider.CompareTag("Wall") && hit2 != null)
                    {
                        DeactivateLine();
                    }
                    else
                    {
                        lineRenderer.SetPosition(2, hit2.point);
                        secondPoint = hit2.point;
                    }
                }
                else
                {
                    hit2        = hit;
                    secondPoint = Vector2.zero;
                }
                lineRenderer.SetPosition(2, hit2.point);
                if (isActive)
                {
                    Bubble ghostBubble = hit2.collider.gameObject.GetComponent <Bubble>().nearBubbles.ToList().Where(a => a != null).OrderBy(x => Vector2.Distance(hit2.point, x.transform.position)).FirstOrDefault(y => y.isGhost);

                    if (ghostBubble != null)
                    {
                        currentGhostBubble = ghostBubble;
                        shot.GetComponent <Fly>().currentGhostBubble = currentGhostBubble;
                        if (secondPoint == Vector2.zero)
                        {
                            firstPoint  = ghostBubble.transform.position;
                            secondPoint = firstPoint;
                        }
                        else
                        {
                            secondPoint = ghostBubble.transform.position;
                        }
                    }
                }

                if (ghostBubbleEffect == null || (Vector2)ghostBubbleEffect.transform.position != secondPoint)
                {
                    Destroy(ghostBubbleEffect);
                    ghostBubbleEffect = Instantiate(ghostBubblePrefab, secondPoint, Quaternion.identity);
                }
            }
            else
            {
                Destroy(ghostBubbleEffect);
                DeactivateLine();
            }
        }

        if (Input.GetMouseButtonUp(0) && isActive && !isShooting)
        {
            Destroy(ghostBubbleEffect);
            DeactivateLine();
            if (secondPoint == Vector2.zero)
            {
                Debug.Log("onepoint");
                Shoot(firstPoint);
            }
            else
            {
                Shoot(firstPoint, secondPoint);
            }

            isShooting = true;
        }
    }