Exemplo n.º 1
0
        /// <summary>
        /// Calculates Avoidance movement for flock agent by moving away from its neighbors' average positions
        /// </summary>
        /// <param name="flockAgent"></param>
        /// <param name="context"></param>
        /// <param name="contextCount"></param>
        /// <param name="flock"></param>
        /// <returns></returns>
        public override Vector2 CalculateMove(FlockAgent flockAgent, Transform[] context, int contextCount, Flock flock)
        {
            if (contextCount == 0)
            {
                return(Vector2.zero);
            }

            _avoidanceMove    = Vector2.zero;
            _neighborsAvoided = 0;

            for (int i = 0; i < contextCount; ++i)
            {
                _agentToNeighbor = (Vector2)context[i].position - flockAgent.Position;

                if (_agentToNeighbor.sqrMagnitude < 4 * flock.AgentsAvoidanceRadiusSquared)
                {
                    _avoidanceMove -= _agentToNeighbor;
                    _neighborsAvoided++;
                }
            }

            if (_neighborsAvoided > 0)
            {
                _avoidanceMove /= _neighborsAvoided;
            }

            return(_avoidanceMove);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Calculates movement for flocking agents.
 /// </summary>
 /// <param name="flockAgent"></param>
 /// <param name="context"></param>
 /// <param name="flock"></param>
 /// <returns></returns>
 public abstract Vector2 CalculateMove(FlockAgent flockAgent, Transform[] context, int contextCount, Flock flock);
Exemplo n.º 3
0
        /// <summary>
        /// Calculates Steered Cohesive movement for flock agent by averaging the distances between it.
        /// and its neighbors.
        /// </summary>
        /// <param name="flockAgent"></param>
        /// <param name="context"></param>
        /// <param name="contextCount"></param>
        /// <param name="flock"></param>
        /// <returns></returns>
        public override Vector2 CalculateMove(FlockAgent flockAgent, Transform[] context, int contextCount, Flock flock)
        {
            _cohesionMove = base.CalculateMove(flockAgent, context, contextCount, flock);
            _cohesionMove = Vector2.SmoothDamp(flockAgent.Up, _cohesionMove, ref _currentVelocity, _smoothTime);

            return(_cohesionMove);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Calculates Alignment movement for flock agent by averaging the forward direction of each neighbor.
        /// is facing.
        /// </summary>
        /// <param name="flockAgent"></param>
        /// <param name="context"></param>
        /// <param name="contextCount"></param>
        /// <param name="flock"></param>
        /// <returns></returns>
        public override Vector2 CalculateMove(FlockAgent flockAgent, Transform[] context, int contextCount, Flock flock)
        {
            if (contextCount == 0)
            {
                return(flockAgent.Up);
            }

            _alignmentMove = Vector2.zero;

            for (int i = 0; i < contextCount; ++i)
            {
                _alignmentMove += (Vector2)context[i].up;
            }

            _alignmentMove /= contextCount;

            return(_alignmentMove);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Calculates Cohesive movement for flock agent by averaging the distances between it
        /// and its neighbors.
        /// </summary>
        /// <param name="flockAgent"></param>
        /// <param name="context"></param>
        /// <param name="contextCount"></param>
        /// <param name="flock"></param>
        /// <returns></returns>
        public override Vector2 CalculateMove(FlockAgent flockAgent, Transform[] context, int contextCount, Flock flock)
        {
            if (contextCount == 0)
            {
                return(Vector2.zero);
            }

            _cohesionMove = Vector2.zero;

            for (int i = 0; i < contextCount; ++i)
            {
                _cohesionMove += (Vector2)context[i].position;
            }

            _cohesionMove /= contextCount;
            _cohesionMove -= flockAgent.Position;

            return(_cohesionMove);
        }