Advertisement
Guest User

Untitled

a guest
Feb 29th, 2012
1,239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 KB | None | 0 0
  1.         void LOS(bool force)
  2.         {
  3.                 if (player.HasMoved() || force)
  4.                 {
  5.                     ClearRayMap();
  6.                     for (int x = player.X() - VIEW_RADIUS; x < scene.GetLength(1); x++)
  7.                     {
  8.                         for (int y = player.Y() - VIEW_RADIUS; y < scene.GetLength(0); y++)
  9.                         {
  10.                             if (Distance(player.X(), x, player.Y(), y) <= VIEW_RADIUS)
  11.                             {
  12.                                 try
  13.                                 {
  14.                                     if (HasLOS(new Point(x, y)))
  15.                                     {
  16.                                         RayMap[y, x] = true;
  17.                                         HasSeen[y, x] = true;
  18.                                     }
  19.                                     else
  20.                                     {
  21.                                         RayMap[y, x] = false;
  22.                                     }
  23.                                 }
  24.                                 catch
  25.                                 {
  26.                                     //block is off the screen, trycatch needed to prevent error
  27.                                 }
  28.                             }
  29.  
  30.                         }
  31.                     }
  32.                 }
  33.         }
  34.  
  35.         bool HasLOS(Point blockpoint)
  36.         {
  37.             bool blocked = true;
  38.             for (float i = 0; i < 1; i += 0.05f)
  39.             {
  40.                 yvect = (int)GetVector(new Vector2(blockpoint.X, blockpoint.Y), new Vector2(player.X(), player.Y()), i).Y;
  41.                 xvect = (int)GetVector(new Vector2(blockpoint.X, blockpoint.Y), new Vector2(player.X(), player.Y()), i).X;
  42.                 if (currentlevel[yvect, xvect] == BLOCKING_OBJECT)
  43.                 {
  44.                     blocked = false;
  45.                 }
  46.             }
  47.             return blocked;
  48.         }
  49.  
  50.         Vector2 GetVector(Vector2 vect1, Vector2 vect2, float t)
  51.         {
  52.             Vector2 delta = vect2 - vect1;
  53.             float distance = delta.Length();
  54.             if (distance == 0.0f)
  55.             {
  56.                 return vect1;
  57.             }
  58.             else
  59.             {
  60.                 Vector2 direction = delta / distance;
  61.                 return vect1 + direction * (distance * t);
  62.             }
  63.         }
  64.  
  65. int Distance(int x1, int x2, int y1, int y2)
  66.         {
  67.             int beforeroot = ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
  68.             if (beforeroot > 0)
  69.             {
  70.                 return (int)Math.Sqrt(beforeroot);
  71.             }
  72.             else if (beforeroot < 0)
  73.             {
  74.                 return (int)Math.Sqrt(-beforeroot);
  75.             }
  76.             else
  77.             {
  78.                 return 0;
  79.             }
  80.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement