Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.44 KB | None | 0 0
  1. #region using
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using Microsoft.Xna.Framework;
  6. using Microsoft.Xna.Framework.Audio;
  7. using Microsoft.Xna.Framework.Content;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11. using V2 = Microsoft.Xna.Framework.Vector2;
  12. using MH = Microsoft.Xna.Framework.MathHelper;
  13. using V3 = Microsoft.Xna.Framework.Vector3;
  14. using MX = Microsoft.Xna.Framework.Matrix;
  15. #endregion
  16.  
  17. namespace aidrivers
  18. {
  19.     class Map : Drawable
  20.     {
  21.         #region GroundType
  22.         public enum GroundType
  23.         {
  24.             Road,
  25.             Wall,
  26.             Water
  27.         }
  28.         #endregion
  29.  
  30.         private GroundType[,] grounds;
  31.         public GroundType this[float x, float y]
  32.         {
  33.             get
  34.             {
  35.                 x /= Scale.X;
  36.                 y /= Scale.X;
  37.                 if (x < 0 || x >= grounds.GetLength(0) || y < 0 || y >= grounds.GetLength(1))
  38.                     return GroundType.Wall;
  39.                 return grounds[(int)(x), (int)(y)];
  40.             }
  41.         }
  42.         public V2 calc_ray_to_wall(V2 drc, V2 position)
  43.         {
  44.             V2 ray = V2.Zero;
  45.  
  46.             int nx = (int)(position.X + ray.X);
  47.             int ny = (int)(position.Y + ray.Y);
  48.  
  49.             while (this[nx, ny] != GroundType.Wall)
  50.             {
  51.                 ray += drc;
  52.                 nx = (int)(position.X + ray.X);
  53.                 ny = (int)(position.Y + ray.Y);
  54.             }
  55.             return ray+position;
  56.         }
  57.         public float? find_road_x(float y)
  58.         {
  59.             int yi = (int)(y / Scale.Y);
  60.             for (int x = 0; x < grounds.GetLength(1); x++)
  61.             {
  62.                 if (grounds[x, yi] == GroundType.Road)
  63.                 {
  64.                     return x * Scale.X;
  65.                 }
  66.             }
  67.             return null;
  68.         }
  69.         public bool isWallClose(V2 position, V2 lookTo,out V2 meetPoint)
  70.         {
  71.             meetPoint = position + lookTo;
  72.             float maxlength = lookTo.Length();
  73.             lookTo.Normalize();
  74.             V2 rayPos = position;
  75.             while (this[rayPos.X, rayPos.Y] != GroundType.Wall)
  76.             {
  77.                 if ((rayPos - position).Length() > maxlength)
  78.                     return false;
  79.                 rayPos += lookTo;
  80.             }
  81.             meetPoint = rayPos;
  82.             return true;
  83.         }
  84.         #region CTORS
  85.         public Map(string path)
  86.             : this(S.cm.Load<Texture2D>(path))
  87.         {
  88.  
  89.         }
  90.         public Map(Texture2D tex)
  91.             : base(V2.Zero, V2.Zero, tex, true)
  92.         {
  93.             grounds = new GroundType[tex.Width, tex.Height];
  94.  
  95.             Color[] texColor = new Color[tex.Width * tex.Height];
  96.             tex.GetData<Color>(texColor);
  97.  
  98.             #region FILL GROUND ARRAY BY TEX
  99.             for (int x = 0; x < tex.Width; x++)
  100.             {
  101.                 for (int y = 0; y < tex.Height; y++)
  102.                 {
  103.                     grounds[x, y] = GroundType.Wall; //default is wall
  104.  
  105.                     if (texColor[x + y * tex.Width] == GroundColors.Road)
  106.                         grounds[x, y] = GroundType.Road;
  107.                     if (texColor[x + y * tex.Width] == GroundColors.Water)
  108.                         grounds[x, y] = GroundType.Water;
  109.                 }
  110.             }
  111.             #endregion
  112.  
  113.         }
  114.         #endregion
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement