Exemple #1
0
 bool KillEnemyReflex(GameTime gameTime)
 {
     if (gameTime.TotalGameTime - lastKillEnemyReflexTime > KillEnemyReflexTimeout && tools.Any(x => (x is Gun && x.Usable)))
     {
         lastKillEnemyReflexTime = gameTime.TotalGameTime;
         Human      seenEnemy         = null;
         float      enemyShotDistance = tools.Max(x => (x is Gun && x.Usable ? ((Gun)x).Type.Range : 0f));
         Quadrangle viewCone          = GetViewCone(enemyShotDistance);
         if (lastSeenEnemy != null && viewCone.IsInCollisionWith(lastSeenEnemy))
         {
             //Quadrangle clearViewQuad = new Quadrangle(lastSeenEnemy.PositionInQuarter.XZToVector2(), lastSeenEnemy.PositionInQuarter.XZToVector2(), Pivot.PositionInQuarter, Pivot.PositionInQuarter);
             //IEnumerable<Quadrangle> inView = from obj in Position.Quarter.SpaceGrid.GetAllCollisions(clearViewQuad) where obj != this && obj != lastSeenEnemy select obj;
             //if (inView.Any())
             //{
             //lastSeenEnemy = null;
             //}
             //else
             //{
             seenEnemy = lastSeenEnemy;
             //}
         }
         else
         {
             lastSeenEnemy = null;
         }
         if (gameTime.TotalGameTime - lastTimeSawEnemy >= CheckEnemiesInViewConeTimeout && seenEnemy == null)
         {
             IEnumerable <Human> seenEnemies = from obj in Position.Quarter.SpaceGrid.GetAllCollisions(viewCone)
                                               where obj is Human && obj != this && enemies.Contains((Human)obj)
                                               select obj as Human;
             if (seenEnemies.Any())
             {
                 seenEnemy = seenEnemies.First();
             }
             lastTimeSawEnemy = gameTime.TotalGameTime;
         }
         if (seenEnemy != null)
         {
             lastSeenEnemy     = seenEnemy;
             selectedToolIndex = tools.FindIndex(x => (x is Gun && ((Gun)x).Type.Range == enemyShotDistance));
             float direction = (seenEnemy.PositionInQuarter.XZToVector2() - Position.PositionInQuarter).GetAngle() + 1 * MathHelper.PiOver2;
             if (!IsAzimuthTooFarFrom(direction, gameTime.ElapsedGameTime.TotalSeconds * RotateAngle))
             {
                 DoToolAction(gameTime);
             }
             else
             {
                 GoThisWay(seenEnemy.Position, (float)gameTime.ElapsedGameTime.TotalSeconds);
             }
             return(true);
         }
     }
     return(false);
 }
Exemple #2
0
        /// <summary>
        /// Gets all the fields where the specified object should belong to.
        /// </summary>
        /// <param name="obj">The object</param>
        /// <returns>Set of fields</returns>
        public IEnumerable <GridField> GetFieldsByObject(Quadrangle obj)
        {
            Vector2[] corners = new Vector2[] {
                obj.UpperLeftCorner,
                obj.UpperRightCorner,
                obj.LowerRightCorner,
                obj.LowerLeftCorner
            };

            float minX = corners.Min(c => c.X),
                  maxX = corners.Max(c => c.X),
                  minY = corners.Min(c => c.Y),
                  maxY = corners.Max(c => c.Y);

            int wX           = (int)(minX / fieldWidth), //auto floor
                wY           = (int)(minY / fieldHeight);
            Rectangle window = new Rectangle(wX, wY,
                                             (int)(maxX / fieldWidth) - wX + 1,
                                             (int)(maxY / fieldHeight) - wY + 1
                                             );

            List <GridField> result = new List <GridField>();

            for (int y = 0; y < window.Height; y++)
            {
                int actY = y + window.Y;
                for (int x = 0; x < window.Width; x++)
                {
                    int        actX     = x + window.X;
                    Quadrangle fieldOne = new Quadrangle(
                        new Vector2(actX * fieldWidth, actY * fieldHeight),
                        new Vector2(actX * fieldWidth + fieldWidth, actY * fieldHeight),
                        new Vector2(actX * fieldWidth, actY * fieldHeight + fieldHeight),
                        new Vector2(actX * fieldWidth + fieldWidth, actY * fieldHeight + fieldHeight)
                        );

                    if (fieldOne.IsInCollisionWith(obj) && actX < width && actY < height && actX >= 0 && actY >= 0)
                    {
                        result.Add(GetField(actX, actY));
                    }
                }
            }
            if (result.Count == 0)
            {
                result.Add(outside);
            }
            return(result);
        }
Exemple #3
0
 /// <summary>
 /// Calculates all callision of the specified objects inside this field.
 /// </summary>
 /// <param name="obj">The tested object</param>
 /// <returns>Set of colliding objects from this field</returns>
 public IEnumerable <Quadrangle> GetCollisions(Quadrangle obj)
 {
     isInCollisionProcessing = true;
     foreach (Quadrangle quad in objects)
     {
         if (obj.IsInCollisionWith(quad) && obj != quad)
         {
             yield return(quad);
         }
     }
     isInCollisionProcessing = false;
     foreach (Quadrangle rem in objectsForRemove)
     {
         RemoveObject(rem);
     }
     objectsForRemove.Clear();
 }
Exemple #4
0
 /// <summary>
 /// Calculates all callision of the specified objects inside this field.
 /// </summary>
 /// <param name="obj">The tested object</param>
 /// <returns>Set of colliding objects from this field</returns>
 public IEnumerable<Quadrangle> GetCollisions(Quadrangle obj)
 {
     isInCollisionProcessing = true;
     foreach (Quadrangle quad in objects)
     {
         if (obj.IsInCollisionWith(quad) && obj != quad)
         {
             yield return quad;
         }
     }
     isInCollisionProcessing = false;
     foreach (Quadrangle rem in objectsForRemove)
     {
         RemoveObject(rem);
     }
     objectsForRemove.Clear();
 }