Advertisement
Czaplicki

XNA Sandard Object Class with Pixel Collision

Oct 25th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.53 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.Graphics;
  7.  
  8. namespace CMFRGReworked
  9. {
  10.     class Object
  11.     {
  12.         public Rectangle position; //standard varibel for 2D Objects
  13.         protected Texture2D sprite; //standard varibel for 2D Objects
  14.         protected SpriteBatch spriteBatch; //standard varibel for 2D Objects
  15.         public Color[] textureData; // arrey of color varibels
  16.  
  17.     // just load in all data needed when a new object is made  
  18.         public Object(Texture2D sprite, SpriteBatch spriteBatch, int x, int y)
  19.         {
  20.             this.sprite = sprite;
  21.             this.spriteBatch = spriteBatch;
  22.             position = new Rectangle(x, y, sprite.Width , sprite.Height );
  23.             textureData = new Color[sprite.Width * sprite.Height];
  24.             sprite.GetData(textureData);          
  25.         }
  26.     // all the suffs needed for pixel collition
  27.         public bool Intersects(Rectangle rectangleIn, Color[] textureDataIn)
  28.         {
  29.             int top = Math.Max(position.Top, rectangleIn.Top);
  30.             int bottom = Math.Min(position.Bottom, rectangleIn.Bottom);
  31.             int left = Math.Max(position.Left, rectangleIn.Left);
  32.             int right = Math.Min(position.Right, rectangleIn.Right);
  33.  
  34.             for (int y = top; y < bottom; y++)
  35.             {
  36.                 for (int x = left; x < right; x++)
  37.                 {
  38.                     Color color1 = textureData[(x - position.Left) + (y - position.Top) * position.Width];
  39.                     Color color2 = textureDataIn[(x - rectangleIn.Left) + (y - rectangleIn.Top) * rectangleIn.Width];
  40.  
  41.                     if (color1.A != 0 && color2.A != 0)
  42.                     {
  43.                         return true;
  44.                     }
  45.                 }
  46.             }
  47.             return false;
  48.         }
  49.     // simple draw method
  50.         public void Draw()
  51.         {
  52.             spriteBatch.Draw(sprite, position, Color.White);
  53.         }
  54.     // method overloading
  55.     // same thing but if u send a color with it will use that color as filter.
  56.         public void Draw(Color color)
  57.         {
  58.             spriteBatch.Draw(sprite, position, color);
  59.         }
  60.  
  61.     }
  62. }
  63. // to use the pixel collition
  64. // ObjectName.Intersects(otherObject.Position, otherObject.TextureData)
  65. // all objects that is going to intersect needs to have this data
  66. // so the smartest wey to use it is to make allt classes douthers classes to this one
  67. // so u allweys have the right data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement