Advertisement
Alan468

Tile v2

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