Ejemplo n.º 1
0
        public void Update(Rectangle rectangle, AIParameters aiParams)
        {
            base.ResetReaction();

            Vector2 pushDirection = Vector2.Zero;
            float weight = aiParams.PerDangerWeight;

            if (Enemy.ReactionDistance > 0.0f &&
                Enemy.ReactionDistance <= aiParams.SeparationDistance)
            {
                //The otherAnimal is too close so we figure out a pushDirection
                //vector in the opposite direction of the otherAnimal and then weight
                //that reaction based on how close it is vs. our separationDistance

                pushDirection = Enemy.position - Enemy.ReactionLocation;
                Vector2.Normalize(ref pushDirection, out pushDirection);

                //push away
                weight *= (1 -
                    (float)Enemy.ReactionDistance / aiParams.SeparationDistance);

                pushDirection *= weight;

                reacted = true;
                reaction += pushDirection;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// CohesionBehavior.Update infuences the owning Enemy to move towards the
        /// otherAnimal that it sees as long as it isn’t too close, in this case 
        /// that means inside the separationDist in the passed in AIParameters.
        /// </summary>
        /// <param name="otherAnimal">the Enemy to react to</param>
        /// <param name="aiParams">the Behaviors' parameters</param>
        public override void Update(Enemy otherAnimal, AIParameters aiParams)
        {
            base.ResetReaction();

            Vector2 pullDirection = Vector2.Zero;
            float weight = aiParams.PerMemberWeight;

            //if the otherAnimal is too close we dont' want to fly any
            //closer to it
            if (Enemy.ReactionDistance > 0.0f
                && Enemy.ReactionDistance > aiParams.SeparationDistance)
            {
                //We want to make the Enemy move closer the the otherAnimal so we
                //create a pullDirection vector pointing to the otherAnimal bird and
                //weigh it based on how close the otherAnimal is relative to the
                //AIParameters.separationDistance.
                pullDirection = -(Enemy.position - Enemy.ReactionLocation);
                Vector2.Normalize(ref pullDirection, out pullDirection);

                weight *= (float)Math.Pow((double)
                    (Enemy.ReactionDistance - aiParams.SeparationDistance) /
                        (aiParams.DetectionDistance - aiParams.SeparationDistance), 2);

                pullDirection *= weight;

                reacted = true;
                reaction = pullDirection;
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// CohesionBehavior.Update infuences the owning Enemy to move towards the
        /// otherAnimal that it sees as long as it isn’t too close, in this case
        /// that means inside the separationDist in the passed in AIParameters.
        /// </summary>
        /// <param name="otherAnimal">the Enemy to react to</param>
        /// <param name="aiParams">the Behaviors' parameters</param>
        public override void Update(Enemy otherAnimal, AIParameters aiParams)
        {
            base.ResetReaction();

            Vector2 pullDirection = Vector2.Zero;
            float   weight        = aiParams.PerMemberWeight;

            //if the otherAnimal is too close we dont' want to fly any
            //closer to it
            if (Enemy.ReactionDistance > 0.0f &&
                Enemy.ReactionDistance > aiParams.SeparationDistance)
            {
                //We want to make the Enemy move closer the the otherAnimal so we
                //create a pullDirection vector pointing to the otherAnimal bird and
                //weigh it based on how close the otherAnimal is relative to the
                //AIParameters.separationDistance.
                pullDirection = -(Enemy.position - Enemy.ReactionLocation);
                Vector2.Normalize(ref pullDirection, out pullDirection);

                weight *= (float)Math.Pow((double)
                                          (Enemy.ReactionDistance - aiParams.SeparationDistance) /
                                          (aiParams.DetectionDistance - aiParams.SeparationDistance), 2);

                pullDirection *= weight;

                reacted  = true;
                reaction = pullDirection;
            }
        }
Ejemplo n.º 4
0
        public void Update(Rectangle rectangle, AIParameters aiParams)
        {
            base.ResetReaction();

            Vector2 pushDirection = Vector2.Zero;
            float   weight        = aiParams.PerDangerWeight;

            if (Enemy.ReactionDistance > 0.0f &&
                Enemy.ReactionDistance <= aiParams.SeparationDistance)
            {
                //The otherAnimal is too close so we figure out a pushDirection
                //vector in the opposite direction of the otherAnimal and then weight
                //that reaction based on how close it is vs. our separationDistance

                pushDirection = Enemy.position - Enemy.ReactionLocation;
                Vector2.Normalize(ref pushDirection, out pushDirection);

                //push away
                weight *= (1 -
                           (float)Enemy.ReactionDistance / aiParams.SeparationDistance);

                pushDirection *= weight;

                reacted   = true;
                reaction += pushDirection;
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// React to an Enemy based on it's type
        /// </summary>
        /// <param name="animal"></param>
        public void ReactTo(Enemy animal, ref AIParameters AIparams)
        {
            if (animal != null)
            {
                //setting the the reactionLocation and reactionDistance here is
                //an optimization, many of the possible reactions use the distance
                //and location of theAnimal, so we might as well figure them out
                //only once !
                Vector2 otherLocation = animal.position;
                ClosestLocation(ref position, ref otherLocation,
                                out reactionLocation);
                reactionDistance = Vector2.Distance(position, reactionLocation); //+ localBounds.Width/2;

                //we only react if theAnimal is close enough that we can see it
                if (reactionDistance < AIparams.DetectionDistance)
                {
                    Behaviors reactions = behaviors[animal.EnemyType];
                    foreach (Behavior reaction in reactions)
                    {
                        reaction.Update(animal, AIparams);
                        if (reaction.Reacted)
                        {
                            aiNewDir += reaction.Reaction;
                            aiNumSeen++;
                        }
                    }
                }
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// AlignBehavior.Update infuences the owning Enemy to move in same the 
        /// direction as the otherAnimal that it sees.
        /// </summary>
        /// <param name="otherAnimal">the Enemy to react to</param>
        /// <param name="aiParams">the Behaviors' parameters</param>
        public override void Update(Enemy otherAnimal, AIParameters aiParams)
        {
            base.ResetReaction();

            if (otherAnimal != null)
            {
                reacted = true;
                //reaction = otherAnimal.Direction * aiParams.PerMemberWeight;
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// AlignBehavior.Update infuences the owning Enemy to move in same the
        /// direction as the otherAnimal that it sees.
        /// </summary>
        /// <param name="otherAnimal">the Enemy to react to</param>
        /// <param name="aiParams">the Behaviors' parameters</param>
        public override void Update(Enemy otherAnimal, AIParameters aiParams)
        {
            base.ResetReaction();

            if (otherAnimal != null)
            {
                reacted = true;
                //reaction = otherAnimal.Direction * aiParams.PerMemberWeight;
            }
        }
Ejemplo n.º 8
0
        public override void Update(GameTime gameTime, ref AIParameters aiParams)
        {
            //Vector2 previousPosition = position;
            //float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            //if(velocity != Vector2.Zero)
            //    velocity.Normalize();
            //velocity = velocity * MoveSpeed * elapsed;

            velocity = new Vector2((float)Math.Cos((double)direction), (float)Math.Sin((double)direction));
            //velocity.Normalize();
            ////velocity.
            //position = position + velocity;

            //this.HandleCollisions();

            float   elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            Vector2 randomDir   = Vector2.Zero;

            randomDir.X = (float)random.NextDouble() - 0.5f;
            randomDir.Y = (float)random.NextDouble() - 0.5f;
            Vector2.Normalize(ref randomDir, out randomDir);

            if (aiNumSeen > 0)
            {
                aiNewDir = (velocity * aiParams.MoveInOldDirectionInfluence) +
                           (aiNewDir * (aiParams.MoveInFlockDirectionInfluence /
                                        (float)aiNumSeen));
            }
            else
            {
                aiNewDir = velocity * aiParams.MoveInOldDirectionInfluence;
            }

            aiNewDir += (randomDir * aiParams.MoveInRandomDirectionInfluence);
            Vector2.Normalize(ref aiNewDir, out aiNewDir);
            aiNewDir = ChangeDirection(velocity, aiNewDir,
                                       aiParams.MaxTurnRadians * elapsedTime);
            velocity = aiNewDir;
            //velocity.Normalize();
            //velocity *= MoveSpeed * elapsedTime;
            direction = (float)Math.Atan2(aiNewDir.Y, aiNewDir.X);

            position = position + velocity;

            // AI(gameTime);

            this.HandleCollisions();
            //if (new Vector2(direction).LengthSquared() > .01f)
            //{
            //    Vector2 moveAmount = new Vector2(direction) * MoveSpeed * elapsedTime;
            //    location = location + moveAmount;

            //}
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Setup the flock boundaries and generate individual members of the flock
        /// </summary>
        /// <param name="tex"> The texture to be used by the birds</param>
        /// <param name="screenWidth">Width of the screen</param>
        /// <param name="screenHeight">Height of the screen</param>
        /// <param name="hoardParameters">Behavior of the flock</param>
        public Hoard(Game game, Random rnd,
                     AIParameters hoardParameters)
        {
            boundryWidth  = game.map.Width;
            boundryHeight = game.map.Height;

            map = game.map;
            //birdTexture = tex;

            hoard       = new List <Enemy>();
            hoardParams = hoardParameters;

            ResetHoard(game, rnd);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Setup the flock boundaries and generate individual members of the flock
        /// </summary>
        /// <param name="tex"> The texture to be used by the birds</param>
        /// <param name="screenWidth">Width of the screen</param>
        /// <param name="screenHeight">Height of the screen</param>
        /// <param name="hoardParameters">Behavior of the flock</param>
        public Hoard( Game game, Random rnd,
            AIParameters hoardParameters)
        {
            boundryWidth = game.map.Width;
            boundryHeight = game.map.Height;

            map = game.map;
            //birdTexture = tex;

            hoard = new List<Enemy>();
            hoardParams = hoardParameters;

            ResetHoard(game, rnd);
        }
Ejemplo n.º 11
0
        //private int cx;
        //private int cy;
        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
            //graphics.ToggleFullScreen();
            //graphics.IsFullScreen = true;

            graphics.PreferredBackBufferHeight = 720;//1080;// 600;//
            graphics.PreferredBackBufferWidth = 1280;//1920;// 800;//
            graphics.ApplyChanges();
            Content.RootDirectory = "Content";
            hoard = null;

            hoardParams = new AIParameters();
            ResetAIParams();
        }
Ejemplo n.º 12
0
        //private int cx;
        //private int cy;

        public Game()
        {
            graphics = new GraphicsDeviceManager(this);
            //graphics.ToggleFullScreen();
            //graphics.IsFullScreen = true;

            graphics.PreferredBackBufferHeight = 720;  //1080;// 600;//
            graphics.PreferredBackBufferWidth  = 1280; //1920;// 800;//
            graphics.ApplyChanges();
            Content.RootDirectory = "Content";
            hoard = null;

            hoardParams = new AIParameters();
            ResetAIParams();
        }
Ejemplo n.º 13
0
        //React to Tilewe
        public void ReactTo(Rectangle Tile, ref AIParameters AIparams)
        {
            Vector2 otherLocation = new Vector2(Tile.Left, Tile.Top);

            ClosestLocation(ref position, ref otherLocation,
                            out reactionLocation);
            reactionDistance = Vector2.Distance(position, reactionLocation);
            Behaviors reactions = behaviors[EnemyType.Generic];

            foreach (Behavior reaction in reactions)
            {
                ((SeparationBehavior)reaction).Update(Tile, AIparams);
                if (reaction.Reacted)
                {
                    aiNewDir += reaction.Reaction;
                    aiNumSeen++;
                }
            }
        }
Ejemplo n.º 14
0
        public override void Update(Enemy otherAnimal, AIParameters aiParams)
        {
            base.ResetReaction();

            Vector2 dangerDirection = Vector2.Zero;

            //Vector2.Dot will return a negative result in this case if the
            //otherAnimal is behind the Enemy, in that case we don’t have to
            //worry about it beceause we’re already moving away from it.
            if (Vector2.Dot(
                Enemy.position, Enemy.ReactionLocation) >= -(Math.PI / 2))
            {
                //set the Enemy to fleeing so that it flashes red
                Enemy.Fleeing = true;
                reacted = true;

                dangerDirection = Enemy.position - Enemy.ReactionLocation;
                Vector2.Normalize(ref dangerDirection, out dangerDirection);

                reaction = (aiParams.PerDangerWeight * dangerDirection);
            }
        }
Ejemplo n.º 15
0
        public override void Update(Enemy otherAnimal, AIParameters aiParams)
        {
            base.ResetReaction();

            Vector2 dangerDirection = Vector2.Zero;

            //Vector2.Dot will return a negative result in this case if the
            //otherAnimal is behind the Enemy, in that case we don’t have to
            //worry about it beceause we’re already moving away from it.
            if (Vector2.Dot(
                    Enemy.position, Enemy.ReactionLocation) >= -(Math.PI / 2))
            {
                //set the Enemy to fleeing so that it flashes red
                Enemy.Fleeing = true;
                reacted       = true;

                dangerDirection = Enemy.position - Enemy.ReactionLocation;
                Vector2.Normalize(ref dangerDirection, out dangerDirection);

                reaction = (aiParams.PerDangerWeight * dangerDirection);
            }
        }
Ejemplo n.º 16
0
 /// <summary>
 /// Abstract function that the subclass must impliment. Figure out the
 /// Behavior reaction here.
 /// </summary>
 /// <param name="otherAnimal">the Enemy to react to</param>
 /// <param name="aiParams">the Behaviors' parameters</param>
 public abstract void Update(Enemy otherAnimal, AIParameters aiParams);
Ejemplo n.º 17
0
 /// <summary>
 /// Empty update function
 /// </summary>
 /// <param name="gameTime"></param>
 public virtual void Update(GameTime gameTime, ref AIParameters aiParams)
 {
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Abstract function that the subclass must impliment. Figure out the 
 /// Behavior reaction here.
 /// </summary>
 /// <param name="otherAnimal">the Enemy to react to</param>
 /// <param name="aiParams">the Behaviors' parameters</param>
 public abstract void Update(Enemy otherAnimal, AIParameters aiParams);
Ejemplo n.º 19
0
        /// <summary>
        /// React to an Enemy based on it's type
        /// </summary>
        /// <param name="animal"></param>
        public void ReactTo(Enemy animal, ref AIParameters AIparams)
        {
            if (animal != null)
            {
                //setting the the reactionLocation and reactionDistance here is
                //an optimization, many of the possible reactions use the distance
                //and location of theAnimal, so we might as well figure them out
                //only once !
                Vector2 otherLocation = animal.position;
                ClosestLocation(ref position, ref otherLocation,
                    out reactionLocation);
                reactionDistance = Vector2.Distance(position, reactionLocation); //+ localBounds.Width/2;

                //we only react if theAnimal is close enough that we can see it
                if (reactionDistance < AIparams.DetectionDistance)
                {
                    Behaviors reactions = behaviors[animal.EnemyType];
                    foreach (Behavior reaction in reactions)
                    {
                        reaction.Update(animal, AIparams);
                        if (reaction.Reacted)
                        {
                            aiNewDir += reaction.Reaction;
                            aiNumSeen++;
                        }
                    }
                }
            }
        }
Ejemplo n.º 20
0
        public void ReactTo(Player player, ref AIParameters AIparams )
        {
            if (player != null)
            {

            }
        }
Ejemplo n.º 21
0
 //React to Tilewe
 public void ReactTo(Rectangle Tile, ref AIParameters AIparams)
 {
     Vector2 otherLocation = new Vector2(Tile.Left, Tile.Top);
     ClosestLocation(ref position, ref otherLocation,
         out reactionLocation);
     reactionDistance = Vector2.Distance(position, reactionLocation);
     Behaviors reactions = behaviors[EnemyType.Generic];
     foreach (Behavior reaction in reactions)
     {
         ((SeparationBehavior)reaction).Update(Tile, AIparams);
         if (reaction.Reacted)
         {
             aiNewDir += reaction.Reaction;
             aiNumSeen++;
         }
     }
 }
Ejemplo n.º 22
0
 public void ReactTo(Player player, ref AIParameters AIparams)
 {
     if (player != null)
     {
     }
 }
Ejemplo n.º 23
0
        public override void Update(GameTime gameTime, ref AIParameters aiParams)
        {
            //Vector2 previousPosition = position;
            //float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            //if(velocity != Vector2.Zero)
            //    velocity.Normalize();
            //velocity = velocity * MoveSpeed * elapsed;

            velocity = new Vector2((float)Math.Cos((double)direction), (float)Math.Sin((double)direction));
            //velocity.Normalize();
            ////velocity.
            //position = position + velocity;

            //this.HandleCollisions();

            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            Vector2 randomDir = Vector2.Zero;

            randomDir.X = (float)random.NextDouble() - 0.5f;
            randomDir.Y = (float)random.NextDouble() - 0.5f;
            Vector2.Normalize(ref randomDir, out randomDir);

            if (aiNumSeen > 0)
            {
                aiNewDir = (velocity * aiParams.MoveInOldDirectionInfluence) +
                    (aiNewDir * (aiParams.MoveInFlockDirectionInfluence /
                    (float)aiNumSeen));
            }
            else
            {
                aiNewDir = velocity * aiParams.MoveInOldDirectionInfluence;
             }

            aiNewDir += (randomDir * aiParams.MoveInRandomDirectionInfluence);
            Vector2.Normalize(ref aiNewDir, out aiNewDir);
            aiNewDir = ChangeDirection(velocity, aiNewDir,
                aiParams.MaxTurnRadians * elapsedTime);
            velocity = aiNewDir;
            //velocity.Normalize();
            //velocity *= MoveSpeed * elapsedTime;
            direction = (float)Math.Atan2(aiNewDir.Y,aiNewDir.X);

            position = position + velocity;

               // AI(gameTime);

            this.HandleCollisions();
            //if (new Vector2(direction).LengthSquared() > .01f)
            //{
            //    Vector2 moveAmount = new Vector2(direction) * MoveSpeed * elapsedTime;
            //    location = location + moveAmount;

            //}
        }
Ejemplo n.º 24
0
 /// <summary>
 /// Empty update function
 /// </summary>
 /// <param name="gameTime"></param>
 public virtual void Update(GameTime gameTime, ref AIParameters aiParams)
 {
 }