Exemplo n.º 1
0
        public override void Do(Entity entity)
        {
            double squaredDistance = Utilities.SquaredDistance(entity.Position, fleeObject.Position);

            if (squaredDistance < entity.VisionRange * entity.VisionRange)
            {
                double x = entity.Position.X + entity.Position.X - fleeObject.Position.X;
                double y = entity.Position.Y + entity.Position.Y - fleeObject.Position.Y;

                activeAction = new GotoAction(x, y, entity.Size);
                activeAction.Do(entity);
            }
            else
            {
                entity.ApplyNeedDeltas(UtilityDeltas);
                End();
            }


            if (activeAction != null && activeAction.State == ActionState.Failure) // If the entity has hit a wall while running away, head to a random point.
            {
                OrderedPair <int> randomPoint = Utilities.GetDirectionalPoint(entity.CollisionDirection, entity.Position);
                activeAction = new GotoAction(randomPoint.X, randomPoint.Y, entity.Size);
            }
        }
Exemplo n.º 2
0
        public override void Do(Entity entity)
        {
            moveTime++;
            moveAction.Do(entity); // GotoAction checks collision with destination and hitting walls.
            bool result = moveAction.IsActive;

            if (moveTime == changeCount) // Change directions after a set time.
            {
                moveTime = 0;
                double d = Utilities.Rng.NextDouble();
                if (d < changeProbability)
                {
                    // Applies goto to set direction to (x, y). Goto applies Move(). False means the entity hit a wall and needs to head to another square.
                    destination = Utilities.GetRandomPoint();
                    moveAction  = new GotoAction(destination.X, destination.Y, entitySize);
                }
                else if (d > 0.75)
                {
                    entity.Stop();
                }
            }
            // The entity has hit a wall or reached the destination.
            else if (!result && entity.CollisionDirection != Direction.None)
            {
                destination = Utilities.GetDirectionalPoint(entity.CollisionDirection, entity.Position);
                moveAction  = new GotoAction(destination.X, destination.Y, entitySize);
            }

            //entity.ApplyNeedDeltas(UtilityDeltas);
            // Advertise needs deltas to entities but don't actually give them the reward. This is for a default action.
        }