Example #1
0
    bool drag           = false;        // Are we currently dragging.

    void Update()
    {
        // Check if we are currently dragging the mouse.
        if (!drag)
        {
            // We only enter drag mode during gameplay and when the player hold the left mouse button.
            if (GameplayManager.Instance.CanPlay() && Input.GetMouseButtonDown(0))
            {
                // *** Add your source code here ***
            }
        }
        else
        {
            // Check if we should stay in drag mode. We will exit if the bee has reached a honeycomb
            // or the end of its path.
            if (currentPath == null || currentPath.EndReached() || currentPath.Waypoints.Count == 0)
            {
                drag = false;
            }
            else
            {
                // Create a 3D ray pointing into the world and test against the play plane, which is
                // set at position with z equals 0.
                Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitInfo;
                playPlane.Raycast(ray, out hitInfo, 1000f);

                // Check if we have dragged far enough from our last waypoint.
                Vector3 currentPosition = hitInfo.point;
                Vector3 lastPosition    = currentPath.GetLastPosition();
                if (Vector3.Distance(currentPosition, lastPosition) > segmentLength)
                {
                    // Keep adding fixed-length segments up to the current position. This way the
                    // dash-line for the path will be consistent instead of looking stretched.
                    float   dragLength  = Vector3.Distance(currentPosition, lastPosition);
                    Vector3 direction   = Vector3.Normalize(currentPosition - lastPosition);
                    float   totalLength = segmentLength;
                    while (totalLength < dragLength)
                    {
                        currentPath.Append(lastPosition + (direction * totalLength));
                        totalLength += segmentLength;
                    }
                }

                // Check if the player has stopped dragging.
                if (Input.GetMouseButtonUp(0))
                {
                    // Try to snap the bee's path to the closest honeycomb.
                    Beehive.Instance.SnapToHoneycomb(currentPath);

                    // Exit drag mode.
                    drag = false;
                }
            }
        }
    }
Example #2
0
    /// <summary>
    /// Call this to snap the end of a flight path to an opened honeycomb. This method would
    /// only perform the snapping if there is an opened honeycomb that is close enough.
    /// </summary>
    /// <param name="path">Flight path for a given bee.</param>
    public void SnapToHoneycomb(FlightPath path)
    {
        // Get the end point of the path.
        Vector2 current = path.GetLastPosition();

        // Go through and find an open honeycomb that is close by the end point.
        foreach (GameObject honeycombObject in openHoneycombs)
        {
            Vector2 testPos = honeycombObject.transform.position;
            if (Vector2.Distance(current, testPos) < honeycombSize * 0.55f)
            {
                Honeycomb honeycomb = honeycombObject.GetComponent <Honeycomb>();

                // Snap the path to the honeycomb.
                path.ConnectToHoneycomb(honeycomb);

                // Register this path with the honeycomb, this is so that we can update
                // the path's color if the honeycomb is closed before the bee reaches it.
                honeycomb.RegisterPath(path);
                break;
            }
        }
    }
    bool drag           = false;        // Are we currently dragging.

    void Update()
    {
        // Check if we are currently dragging the mouse.
        if (!drag)
        {
            // We only enter drag mode during gameplay and when the player hold the left mouse button.
            if (GameplayManager.Instance.CanPlay() && Input.GetMouseButtonDown(0))
            {
                //Create 3D ray pointing into the world at the mouse position
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

                //Check if our ray is hitting anything in the scene
                RaycastHit hitInfo;
                if (Physics.Raycast(ray, out hitInfo, 1000f))
                {
                    //Check if the ray hits a bee. If so, we have 'selected the bee
                    if (hitInfo.transform.CompareTag("Bee"))
                    {
                        //Make sure the bee is not stunned
                        BeeController bee = hitInfo.transform.GetComponent <BeeController>();
                        if (!bee.IsStunned())
                        {
                            //Append waypoints to path for this bee,
                            //starting at current position
                            currentPath = hitInfo.transform.GetComponentInChildren <FlightPath>();
                            currentPath.Clear();
                            currentPath.Append(hitInfo.transform.position);

                            //Enter dragmode, keep appending waypoints to bee's path
                            drag = true;
                        }
                    }
                }
            }
        }
        else
        {
            // Check if we should stay in drag mode. We will exit if the bee has reached a honeycomb
            // or the end of its path.
            if (currentPath == null || currentPath.EndReached() || currentPath.Waypoints.Count == 0)
            {
                drag = false;
            }
            else
            {
                // Create a 3D ray pointing into the world and test against the play plane, which is
                // set at position with z equals 0.
                Ray        ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                RaycastHit hitInfo;
                playPlane.Raycast(ray, out hitInfo, 1000f);

                // Check if we have dragged far enough from our last waypoint.
                Vector3 currentPosition = hitInfo.point;
                Vector3 lastPosition    = currentPath.GetLastPosition();
                if (Vector3.Distance(currentPosition, lastPosition) > segmentLength)
                {
                    // Keep adding fixed-length segments up to the current position. This way the
                    // dash-line for the path will be consistent instead of looking stretched.
                    float   dragLength  = Vector3.Distance(currentPosition, lastPosition);
                    Vector3 direction   = Vector3.Normalize(currentPosition - lastPosition);
                    float   totalLength = segmentLength;
                    while (totalLength < dragLength)
                    {
                        currentPath.Append(lastPosition + (direction * totalLength));
                        totalLength += segmentLength;
                    }
                }

                // Check if the player has stopped dragging.
                if (Input.GetMouseButtonUp(0))
                {
                    // Try to snap the bee's path to the closest honeycomb.
                    Beehive.Instance.SnapToHoneycomb(currentPath);

                    // Exit drag mode.
                    drag = false;
                }
            }
        }
    }