void Start() { _previousState = _states.currentState; _currentSmellDistance = 0f; UpdatePheromoneSensor(); // Repeadetly prune destroyed pheromones. InvokeRepeating("PruneDeletedPheromones", 20.0f, 10.0f); }
// Use this for initialization void Start() { currentState = EAntStates.Idle; // Enable / disable components as necessary randomWalk.enabled = true; harvesting.enabled = false; pheromonePlacement.enabled = false; pheromonePerception.enabled = true; returnToBase.enabled = false; combat.enabled = false; }
/** * 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); }
/** * Translate given state into a color code. */ public static Color ColorEncode(EAntStates state) { switch (state) { case EAntStates.Idle: return(Color.white); case EAntStates.Harvesting: return(Color.blue); case EAntStates.ReturnToBase: return(new Color(184, 110, 0)); case EAntStates.FollowPheromone: return(Color.green); case EAntStates.Combat: return(Color.red); default: return(Color.magenta); } }
/** * Translate a given state to a human readable name. */ public static string StateToString(EAntStates state) { switch (state) { case EAntStates.Idle: return("Random walk"); case EAntStates.Harvesting: return("Harvesting"); case EAntStates.ReturnToBase: return("Back to Base"); case EAntStates.FollowPheromone: return("Reacting to Pheromones"); case EAntStates.Combat: return("In combat"); default: return("n/a"); } }