Пример #1
0
 /// <summary>
 /// Calculates new moves of an enemy.
 /// If player is close enough the sprite will follow player using AStar algorithm.
 /// </summary>
 /// <param name="gameTime">Instance of GameTime class that is used to calculate time elapsed</param>
 public override void Update(GameTime gameTime)
 {
     if (Delay == 0)
     {
         var oldPosition = Position;
         if (IsPlayerInRange)
         {
             var astar = new AGwiazdka(board);
             var path  = astar.FindPath(board.Fields[this.Position.X, this.Position.Y],
                                        board.Fields[board.Player.Position.X, board.Player.Position.Y]);
             Field lastPoint = null;
             if (path != null)
             {
                 lastPoint = path.LastOrDefault();
             }
             if (lastPoint != null)
             {
                 Position = new Point(lastPoint.X, lastPoint.Y);
                 board.ChangePosition(oldPosition, this);
             }
         }
         else
         {
             timeElapsed += gameTime.ElapsedGameTime.TotalMilliseconds;
             var newPosition = Position;
             if (timeElapsed >= timeBeforeDirectionChange)
             {
                 timeElapsed = 0;
                 ChangeDirection();
             }
             newPosition = new Point(newPosition.X + _direction.X, newPosition.Y + _direction.Y);
             newPosition = board.AdjustToBoardSize(newPosition);
             if (board.IsPositionValidExceptPlayer(newPosition))
             {
                 Position = newPosition;
                 board.ChangePosition(oldPosition, this);
             }
             else
             {
                 ChangeDirection();
             }
         }
     }
     base.Update(gameTime);
     Delay = (Delay + 1) % 20;
 }
 /// <summary>
 /// Calculates moves for monster that will follow user using Astar algorithm
 /// </summary>
 /// <param name="gameTime"></param>
 public override void Update(GameTime gameTime)
 {
     if (Delay == 0)
     {
         var astar = new AGwiazdka(board);
         var path  = astar.FindPath(board.Fields[this.Position.X, this.Position.Y],
                                    board.Fields[board.Player.Position.X, board.Player.Position.Y]);
         Field lastPoint = null;
         if (path != null)
         {
             lastPoint = path.LastOrDefault();
         }
         if (lastPoint != null)
         {
             var oldPosition = Position;
             Position = new Point(lastPoint.X, lastPoint.Y);
             board.ChangePosition(oldPosition, this);
         }
     }
     base.Update(gameTime);
     Delay = (Delay + 1) % 25;
 }