/**
     * Updates the movement direction (see NavAgentHelper) according to the
     * pheromones located near the agent.
     *
     * Combat/Harvest pheromones are only taken into account, if the task of the agent
     * is combat and harvest respectively.
     *
     * Also: Combat/Harvest pheromones are ignored in certain states. Repellant pheromones
     * are taken into account in every state.
     */
    void Update()
    {
        _currentSmellAngleCos = Mathf.Cos(_agentConfiguration.smellAngle);

        UpdatePheromoneSensor();

        Vector3 attractionDirection = new Vector3();

        if (_states.IsIdling())
        {
            _previousState = EAntStates.Idle;
        }

        // Only take harvest/combat pheromones into account if the agent is idling or following pheromones.
        if (_states.IsIdling() || _states.IsFollowingPheromone())
        {
            if (_states.GetTask() == EAntTasks.HarvestFood && _previousState != EAntStates.ReturnToBase)
            {
                attractionDirection = AggregateMovementDirection(EPheromoneTypes.Food);
            }
            else if (_states.GetTask() == EAntTasks.Attack)
            {
                attractionDirection = AggregateMovementDirection(EPheromoneTypes.Attack);
            }
        }

        // Always take repellant pheromones into account.
        Vector3 repulsionDirection = AggregateMovementDirection(EPheromoneTypes.Repellant);

        // If pheromones influence movement direction, set this direction and switch
        // to pheromone following state if necessary.
        if (attractionDirection.sqrMagnitude > 0f || repulsionDirection.sqrMagnitude > 0f)
        {
            _navHelper.SetDirection(0.01f * attractionDirection + 0.99f * repulsionDirection);

            if (!_states.IsFollowingPheromone())
            {
                // Save the current state so we can restore status quo lateron.
                _previousState       = _states.currentState;
                _states.currentState = EAntStates.FollowPheromone;
            }
        }
        // Switch back to previous state, if no longer following pheromones.
        else if (_states.IsFollowingPheromone())
        {
            _states.currentState = _previousState;
        }

//		Debug.DrawLine (transform.position, transform.position + Quaternion.Euler(0f, GlobalAntConfiguration.smellAngle * Mathf.Rad2Deg, 0f) * transform.forward * _currentSmellDistance);
//		Debug.DrawLine (transform.position, transform.position + Quaternion.Euler(0f, -GlobalAntConfiguration.smellAngle * Mathf.Rad2Deg, 0f) * transform.forward * _currentSmellDistance);
    }
Ejemplo n.º 2
0
    void Update()
    {
        idleLocationChangeTimer += Time.deltaTime;

        if (idleLocationChangeTimer >= idleLocationChangeInterval)
        {
            // Determine a random angle.
            float randomAngle = Random.Range(-idleRandomAngle, idleRandomAngle);

            // Rotate current direction by this angle.
            Vector3 randomDirection = Quaternion.Euler(0, randomAngle, 0) * transform.forward;
            navHelper.SetDirection(randomDirection);

            idleLocationChangeTimer = 0.0f;
        }
    }
Ejemplo n.º 3
0
    /**
     * Begin to harvest the resource.
     *
     * This method is called by the AgentResourceSensor when it detects a
     * resource.
     */
    public void StartHarvesting(Collider other)
    {
        if (_states.GetTask() != EAntTasks.HarvestFood)
        {
            return;
        }

        ResourceSource resourceSource = other.GetComponent <ResourceSource> ();

        if (resourceSource)
        {
            _navHelper.SetDirection(other.transform.position - transform.position);
            _currentSource          = resourceSource;
            _inventory.resourceType = resourceSource.resourceType;
            _harvestTimer           = 0.0f;
            _states.currentState    = EAntStates.Harvesting;
        }
    }
Ejemplo n.º 4
0
    /**
     * Combating agents circle around their enemies. This method recalculates
     * a random point inside this circle to which the agent moves.
     */
    void DoCombatCircling()
    {
        GameObject target = _attack.GetCurrentTarget();

        // Sanity check. THis is acutally not reqiured, since DoCombatCircling() is only
        // invoked when the agent is _inCombat. The integrity of this state is ensured by
        // the Update() method.
        if (!target)
        {
            return;
        }

        // Orbit around enemy to continue attack and spread pheromones
        Vector2 randomCircle = Random.insideUnitCircle;
        Vector3 randomPoint  = new Vector3(randomCircle.x, 0, randomCircle.y);

        randomPoint = target.transform.position + randomPoint.normalized * _randomRadius;

        _navHelper.SetDirection(randomPoint - transform.position);
    }
Ejemplo n.º 5
0
    /**
     * Sets the movement direction of the agent to the center of the base.
     */
    void FixedUpdate()
    {
        Vector3 homeDirection = agentBase.transform.position - transform.position;

        navHelper.SetDirection(0.9f * navHelper.GetDirection() + 0.01f * homeDirection);
    }