Advertisement
Alan468

Tile v2

Oct 16th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.67 KB | None | 0 0
  1.  public class Tile
  2.     {
  3.         public Texture2D Sprite { get; set; }
  4.         public Rectangle Position { get; set; }
  5.         public double Rotation { get; set; }
  6.         public Color TintColor { get; set; }
  7.         public Rectangle? Source { get; set; }
  8.         public float Scale { get; set; }
  9.  
  10.         public int Width { get { return Position.Width; } }
  11.         public int Height { get { return Position.Height; } }
  12.  
  13.         public bool IsVisible { get; set; }
  14.  
  15.         public Tile(Texture2D sprite, Rectangle position, int rotation, Rectangle? source = null)
  16.         {
  17.             Sprite = sprite;
  18.             Position = position;
  19.             Rotation = rotation;
  20.             Source = source;
  21.             TintColor = Color.White;
  22.             IsVisible = true;
  23.             Scale = 1;
  24.         }
  25.        
  26.         public void Rotate(int degrees)
  27.         {
  28.             Rotation += (Math.PI / 180) * degrees;
  29.            
  30.             if (Rotation >= 360)
  31.                 Rotation -= 360;
  32.             else if (Rotation < 0)
  33.                 Rotation += 360;
  34.         }
  35.  
  36.         public bool IsOn(Point mousePosition)
  37.         {
  38.             var position = Position;
  39.             position.X -= Position.Width / 2;
  40.             position.Y -= Position.Height / 2;
  41.  
  42.             return position.Contains(mousePosition);
  43.         }
  44.  
  45.         public void Draw(SpriteBatch spriteBatch, bool center = true)
  46.         {
  47.             if (IsVisible)
  48.             {
  49.                 var source = Source.HasValue ? Source : new Rectangle(0, 0, Sprite.Width, Sprite.Height);
  50.  
  51.                 Vector2 origin = new Vector2(0, 0);
  52.                 if (center)
  53.                     origin = Source.HasValue ? new Vector2(Source.Value.Width / 2, Source.Value.Height / 2) : new Vector2(Sprite.Width / 2, Sprite.Height / 2);
  54.  
  55.  
  56.                 var position = new Rectangle(Position.X, Position.Y, (int)(Position.Width * Scale), (int)(Position.Height * Scale));
  57.  
  58.                 spriteBatch.Draw(Sprite, position, source, TintColor, (float)Rotation, origin, SpriteEffects.None, 0);
  59.             }
  60.         }
  61.  
  62.         public void Resize(int width, int height)
  63.         {
  64.             Position = new Rectangle(Position.X, Position.Y, width, height);
  65.         }
  66.  
  67.         public void SetPosition(int x, int y)
  68.         {
  69.             Position = new Rectangle(x, y, Position.Width, Position.Height);
  70.         }
  71.  
  72.         public void SetScale(float scale)
  73.         {
  74.             Scale = scale;
  75.         }
  76.  
  77.         public void SetColor(Color color)
  78.         {
  79.             TintColor = color;
  80.         }
  81.  
  82.         public void SetVisibility(bool visible)
  83.         {
  84.             IsVisible = visible;
  85.         }
  86.  
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement