Exemple #1
0
    private IEnumerator CollectNode(ResourceNode node)
    {
        // Find closest point to node.
        Vector3 nodePos = node.ObstacleCollider.ClosestPoint(transform.position);

        // Move to selected node. Wait to collect until arrived.
        yield return(StartCoroutine(MoveToPoint(nodePos)));

        // Collect node until full or until node is depleted
        while (carryingAmount < carryingCapacity && node != null && !node.IsDepledted)
        {
            Debug.Log("Mining");
            // TODO: Perhaps play some kind of animation here.
            node.CollectNode();
            carryingAmount++;

            // Wait until ready to collect again
            yield return(new WaitForSeconds(collectionRate));
        }

        // Drop off resources at closest drop off point if needed
        if (carryingAmount == carryingCapacity)
        {
            // Go to drop off point
            // Wait until we're finished dropping off
            yield return(StartCoroutine(DropOff(SearchDropOff())));
        }

        // Return to node position. Find new node if current node is depleted.
        yield return(StartCoroutine(MoveToPoint(nodePos)));

        // Find new node is current node is gone/depleted
        if (node == null || node.IsDepledted)
        {
            ResourceNode newNode = SearchNode();

            // If no new node in collection radius, drop off resource, return, and quit working.
            if (newNode == null && carryingAmount > 0)
            {
                // Drop off
                yield return(StartCoroutine(DropOff(SearchDropOff())));

                // Return
                yield return(StartCoroutine(MoveToPoint(nodePos)));

                // Quit
                yield break;
            }
            // Repeat process on new node.
            else
            {
                Collect(newNode);
            }
        }
        // Repeat process on current node
        else
        {
            Collect(node);
        }

        // Done working
        yield break;
    }