Пример #1
0
        /// <summary>
        /// Assigning the target group and the fringe group
        /// </summary>
        private void UpdateActiveGroups(List <Boid> boidList)
        {
            targetGroup.Clear();
            fringeGroup.Clear();

            if (boidList == null || boidList.Count == 0)
            {
                return;
            }

            //find the nearest boid and form the target group from it and its neighbours
            Boid nearestBoid = BoidUtility.GetNearest(boidList, Position);

            targetGroup.Add(nearestBoid);
            foreach (var boid in nearestBoid.Neighbours)
            {
                targetGroup.Add(boid);
            }

            //form the fringe group from the neighbours of the target group that arent in the target group
            foreach (var boid in targetGroup)
            {
                foreach (var neighbour in boid.Neighbours)
                {
                    if (!targetGroup.Contains(neighbour) && !fringeGroup.Contains(neighbour))
                    {
                        fringeGroup.Add(neighbour);
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Vector towards the nearest target.
        /// This is the actual attack of a particular boid. As we get closer to a boid,
        /// our tendency to attack it increases.
        /// </summary>
        private Vector3 CalculateAttackEagerness(HashSet <Boid> targets, float currentWithdrawalWeight)
        {
            if (targets == null || targets.Count == 0)
            {
                return(Vector3.zero);
            }

            var toNearestBoid = BoidUtility.GetNearest(targets, Position).Position - Position;
            var weight        = 1f - Mathf.Clamp01(toNearestBoid.sqrMagnitude / attackRadiusSqr);      //weight nearer boids most heavily

            return(toNearestBoid.normalized * weight * (1f - currentWithdrawalWeight));
        }
Пример #3
0
        /// <summary>
        /// Vector away from other predators.
        /// This prevents the predator from targeting boids that are also
        /// being targeted by other predators.
        /// </summary>
        private Vector3 CalculateIndependence(List <BoidPredator> predators)
        {
            HashSet <BoidPredator> others = new HashSet <BoidPredator>(predators);

            others.Remove(this);

            if (others.Count > 0)
            {
                var toNearestPred = BoidUtility.GetNearest(others, Position).Position - Position;
                var weight        = 1f - Mathf.Clamp01(toNearestPred.sqrMagnitude / viewRadius);          //weight nearer predators most heavily
                return(-toNearestPred.normalized * weight);
            }

            return(Vector3.zero);
        }
Пример #4
0
        private bool CheckForTargetCapture()
        {
            if (targetGroup == null || targetGroup.Count == 0)
            {
                return(false);
            }

            var nearestBoid = BoidUtility.GetNearest(targetGroup, Position);

            if ((nearestBoid.Position - Position).sqrMagnitude <= captureRadiusSqr)
            {
                nearestBoid.Kill();
                killCount++;
                return(true);
            }

            return(false);
        }