コード例 #1
0
    /// FindClosestLocation (based on resource)
    ///
    /// Finds the closest location via the "FindGameObjectsWithTag" function
    /// location string must match a tag within unity for function to work
    /// and must also match the desired resource type
    GameObject FindClosestResource(string location, GameObject resourceType)
    {
        float      distance    = Mathf.Infinity;
        float      curDistance = 0.0f;
        GameObject closest     = null;

        GameObject[]       list     = GameObject.FindGameObjectsWithTag(location);
        ResourceController resource = null;

        if (list.Length <= 0)
        {
            //print("No " + location);
            return(null);
        }


        // NEED TO COMPARE DISTANCE "Vector3.Distance" and determine closest stockpile
        for (int i = 0; i < list.Length; i++)
        {
            resource    = list[i].GetComponent <ResourceController>();
            curDistance = Vector3.Distance(transform.position, list[i].transform.position);
            if (curDistance < distance && (resourceType == null || resource.GetResourceType().name == resourceType.name) && resource.CurrentOwner() == null)
            {
                // MUST DO SOMETHING HERE TO MAKE IT HAPPEN
                distance = curDistance;
                closest  = list[i];
            }
        }
        return(closest);
    }
コード例 #2
0
    private void WorkerWorkingState()
    {
        ResourceController resource = null;

        if (goal != null)
        {
            resource = goal.GetComponent <ResourceController>();
        }

        if (resource == null)
        {
            print("can't gather not a resource");
            goal      = FindClosestResource("Gather", myInventory.itemType);
            currState = personState.Moving;
            if (goal == null)
            {
                currState = personState.MovingItem;
            }
            else
            {
                agent.destination = goal.transform.position;
            }
        }
        else
        {
            if (myInventory.num == maxInventorySpace)
            {
                goal = null;
                resource.SetOwner(null);
                currState = personState.MovingItem;
            }
            else if (resource.CurrentOwner() == gameObject.name)
            {
                timeWorked += (resource.GetWorkTime() * Time.deltaTime);
                if (timeWorked >= resource.GetWorkTime())
                {
                    if (myInventory.itemType == null)
                    {
                        myInventory.itemType = resource.GetResourceType();
                    }
                    myInventory.num += resource.TakeResource(pickupAmount);
                    timeWorked       = 0.0f;
                }
            }
            else if (resource.CurrentOwner() == null)
            {
                resource.SetOwner(gameObject.name);
            }
            else if ((goal == null || resource.CurrentOwner() != gameObject.name) && myInventory.num < maxInventorySpace)
            {
                goal      = FindClosestResource("Gather", myInventory.itemType);
                currState = personState.Moving;
                if (goal == null)
                {
                    currState = personState.MovingItem;
                }
                else
                {
                    agent.destination = goal.transform.position;
                }
            }
            else
            {
                currState = personState.Idle;
                goal      = null;
                print("something dun messed up");
            }
        }
    }