Example #1
0
        private void Flee(Strand strand, Chemoeffector noxious)
        {
            double distance;

            distance = Geometry.GetDistance(strand.Position, noxious.Position);

            if (distance <= 1)
            {
                this.ProcessCollision(strand, noxious, _repellents, _repellentCollisionAction, () => this.AddRepellent());
            }
            else
            {
                if (distance < strand.PreviousSensor)
                {
                    this.Tumble(strand);
                    this.CheckCollisions(strand);
                }
                else
                {
                    double newDistance;
                    Point  heading;
                    heading = strand.Heading;
                    this.Tumble(strand);
                    strand.Move();
                    newDistance = Geometry.GetDistance(strand.Position, noxious.Position);
                    if (newDistance < distance)
                    {
                        strand.Heading = heading;
                    }
                    strand.UndoMove();
                }

                strand.PreviousSensor = distance;
            }
        }
Example #2
0
        private void Approach(Strand strand, Chemoeffector food)
        {
            double distance;

            distance = Geometry.GetDistance(strand.Position, food.Position);

            if (distance <= 1)
            {
                this.ProcessCollision(strand, food, _attractors, _attractorCollisionAction, this.CheckRespawn);

                strand.PreviousSensor = 0;
            }
            else
            {
                if (distance > strand.PreviousSensor)
                {
                    this.Tumble(strand);
                    this.CheckCollisions(strand);
                }
                else
                {
                    double newDistance;
                    Point  heading;
                    heading = strand.Heading;
                    this.Tumble(strand);
                    strand.Move();
                    newDistance = Geometry.GetDistance(strand.Position, food.Position);
                    if (newDistance >= distance)
                    {
                        strand.Heading = heading;
                    }
                    strand.UndoMove();
                }

                strand.PreviousSensor = distance;
            }
        }
Example #3
0
        private void ProcessCollision(Strand strand, Chemoeffector chemoeffector, ChemoeffectorCollection container, CollisionAction action, Action createNew)
        {
            if (action == CollisionAction.ReduceSelf)
            {
                chemoeffector.Strength--;
                strand.Strength++;
            }
            else if (action == CollisionAction.ReduceOther)
            {
                strand.Strength--;
            }

            if (chemoeffector.Strength <= 0 || action == CollisionAction.DestroySelf)
            {
                container.Remove(chemoeffector);

                createNew();
            }

            if (strand.Strength <= 0 || action == CollisionAction.DestroyOther)
            {
                _strands.Remove(strand);
            }
        }