Advertisement
Hyluss

[Monogame PingPong] Sprite

Oct 30th, 2018
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5.  
  6.  
  7. namespace PingPong
  8. {
  9.     public class Sprite
  10.     {
  11.         protected Texture2D _texture;
  12.  
  13.         public Vector2 Position;
  14.         public Vector2 Velocity;
  15.         public float Speed;
  16.         public Input Input;
  17.  
  18.         int offset=10;
  19.  
  20.         protected int _textureWidth, _textureHeight;
  21.  
  22.         public bool Shining = false;
  23.  
  24.         public Color Color = Color.White;
  25.  
  26.         public Rectangle Rectangle
  27.         {
  28.             get
  29.             {
  30.                 return new Rectangle((int)Position.X, (int)Position.Y, _textureWidth, _textureHeight);
  31.             }
  32.         }
  33.  
  34.  
  35.         public Sprite(Texture2D texture)
  36.         {
  37.             _texture = texture;
  38.             _textureWidth = texture.Width;
  39.             _textureHeight= texture.Height;
  40.         }
  41.  
  42.         public virtual void Update(GameTime gameTime, List<Sprite> sprites)
  43.         {
  44.            
  45.         }
  46.  
  47.         public virtual void Draw(SpriteBatch spriteBatch)
  48.         {
  49.            
  50.            
  51.             spriteBatch.Draw(_texture, Position, Color);
  52.         }
  53.  
  54.         //kolizja
  55.  
  56.         protected bool IsTouchingLeft(Sprite sprite)
  57.         {
  58.             return this.Rectangle.Right + this.Velocity.X  - offset > sprite.Rectangle.Left &&
  59.               this.Rectangle.Left < sprite.Rectangle.Left &&
  60.               this.Rectangle.Bottom > sprite.Rectangle.Top &&
  61.               this.Rectangle.Top < sprite.Rectangle.Bottom;
  62.         }
  63.  
  64.         protected bool IsTouchingRight(Sprite sprite)
  65.         {
  66.             return this.Rectangle.Left + this.Velocity.X + offset < sprite.Rectangle.Right &&
  67.               this.Rectangle.Right > sprite.Rectangle.Right &&
  68.               this.Rectangle.Bottom > sprite.Rectangle.Top &&
  69.               this.Rectangle.Top < sprite.Rectangle.Bottom;
  70.         }
  71.  
  72.         protected bool IsTouchingTop(Sprite sprite)
  73.         {
  74.             return this.Rectangle.Bottom + this.Velocity.Y - offset > sprite.Rectangle.Top &&
  75.               this.Rectangle.Top < sprite.Rectangle.Top &&
  76.               this.Rectangle.Right > sprite.Rectangle.Left &&
  77.               this.Rectangle.Left < sprite.Rectangle.Right;
  78.         }
  79.  
  80.         protected bool IsTouchingBottom(Sprite sprite)
  81.         {
  82.             return this.Rectangle.Top + this.Velocity.Y + offset < sprite.Rectangle.Bottom &&
  83.               this.Rectangle.Bottom > sprite.Rectangle.Bottom &&
  84.               this.Rectangle.Right > sprite.Rectangle.Left &&
  85.               this.Rectangle.Left < sprite.Rectangle.Right;
  86.         }
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement