Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void LOS(bool force)
- {
- if (player.HasMoved() || force)
- {
- ClearRayMap();
- for (int x = player.X() - VIEW_RADIUS; x < scene.GetLength(1); x++)
- {
- for (int y = player.Y() - VIEW_RADIUS; y < scene.GetLength(0); y++)
- {
- if (Distance(player.X(), x, player.Y(), y) <= VIEW_RADIUS)
- {
- try
- {
- if (HasLOS(new Point(x, y)))
- {
- RayMap[y, x] = true;
- HasSeen[y, x] = true;
- }
- else
- {
- RayMap[y, x] = false;
- }
- }
- catch
- {
- //block is off the screen, trycatch needed to prevent error
- }
- }
- }
- }
- }
- }
- bool HasLOS(Point blockpoint)
- {
- bool blocked = true;
- for (float i = 0; i < 1; i += 0.05f)
- {
- yvect = (int)GetVector(new Vector2(blockpoint.X, blockpoint.Y), new Vector2(player.X(), player.Y()), i).Y;
- xvect = (int)GetVector(new Vector2(blockpoint.X, blockpoint.Y), new Vector2(player.X(), player.Y()), i).X;
- if (currentlevel[yvect, xvect] == BLOCKING_OBJECT)
- {
- blocked = false;
- }
- }
- return blocked;
- }
- Vector2 GetVector(Vector2 vect1, Vector2 vect2, float t)
- {
- Vector2 delta = vect2 - vect1;
- float distance = delta.Length();
- if (distance == 0.0f)
- {
- return vect1;
- }
- else
- {
- Vector2 direction = delta / distance;
- return vect1 + direction * (distance * t);
- }
- }
- int Distance(int x1, int x2, int y1, int y2)
- {
- int beforeroot = ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
- if (beforeroot > 0)
- {
- return (int)Math.Sqrt(beforeroot);
- }
- else if (beforeroot < 0)
- {
- return (int)Math.Sqrt(-beforeroot);
- }
- else
- {
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement