Beispiel #1
0
    private void UpdateBubblePlacement()
    {
        // Handle inputs
        bool primary   = inputManager.PrimaryInputPressed();
        bool secondary = inputManager.SecondaryInputPressed();

        if (primary || secondary)
        {
            RaycastHit hit;
            bool       hitSomething = false;
            Ray        ray          = Camera.main.ScreenPointToRay(Input.mousePosition);

            // Cast a ray into the scene and see if we hit anything.
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.collider != null)
                {
                    hitSomething = true;
                    bubbles.Remove(hit.collider.gameObject);
                    bubblesLarge.Remove(hit.collider.gameObject);
                    Destroy(hit.collider.gameObject);
                }
            }

            // Verify we're in the safezone. If we are and nothing was hit,
            // well, we can go ahead and place a bubble now!
            if (!hitSomething)
            {
                DataPoint pos = inputManager.PrimaryInputPosWorld();

                float left   = safeArea.transform.position.x - safeArea.size.x / 2;
                float right  = safeArea.transform.position.x + safeArea.size.x / 2;
                float top    = safeArea.transform.position.y + safeArea.size.y / 2;
                float bottom = safeArea.transform.position.y - safeArea.size.y / 2;

                if (pos.X > right || pos.X < left || pos.Y > top || pos.Y < bottom)
                {
                    return;
                }

                // Default to regular bubble
                GameObject toSpawn = templateRegular;
                float      radius  = GameCore.bubbleRadiusStandard;

                // Override to large bubble
                if (secondary)
                {
                    toSpawn = templateLarge;
                    radius  = GameCore.bubbleRadiusLarge;
                }

                // Determine scale accordingly
                radius /= toSpawn.transform.localScale.x;

                // Spawn the bubble, and provide the collider.
                GameObject     newBubble = GameObject.Instantiate(toSpawn);
                SphereCollider col       = newBubble.AddComponent <SphereCollider>();
                newBubble.transform.position = pos;
                newBubble.SetActive(true);
                col.radius = radius;

                if (secondary)
                {
                    bubblesLarge.Add(newBubble);
                }
                else if (primary)
                {
                    bubbles.Add(newBubble);
                }
            }
        }
    }