Ejemplo n.º 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;
            }
        }
Ejemplo n.º 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;
            }
        }
Ejemplo n.º 3
0
        private void CheckCollisions(Strand strand)
        {
            for (int i = 0; i < _repellents.Count; i++)
            {
                if (Geometry.GetDistance(strand.Position, _repellents[i].Position) <= 1)
                {
                    this.ProcessCollision(strand, _repellents[i], _repellents, _repellentCollisionAction, () => this.AddRepellent());
                }
            }

            for (int i = 0; i < _attractors.Count; i++)
            {
                if (Geometry.GetDistance(strand.Position, _attractors[i].Position) <= 1)
                {
                    this.ProcessCollision(strand, _attractors[i], _attractors, _attractorCollisionAction, this.CheckRespawn);
                    break;
                }
            }

            if (_solidStrands)
            {
                for (int i = 0; i < _strands.Count; i++)
                {
                    Strand other;

                    other = _strands[i];

                    if (!object.ReferenceEquals(strand, other) && strand.Position == other.Position)
                    {
                        strand.UndoMove();
                        strand.Heading = _movementRandom.NextDouble() >= 0.5 ? Compass.GetNextQuarter(strand.Heading) : Compass.GetPreviousQuarter(strand.Heading);
                        break;
                    }
                }
            }
        }