Example #1
0
        public Texture2D MirrorTexture(ISprite sprite, GraphicsDeviceManager graphics, Texture2D texture)
        {
            Texture2D mirroredProjectile = new Texture2D(graphics.GraphicsDevice, sprite.GetSpriteWidth(), sprite.GetSpriteHeight());

            Color[] projectileTextureHelper = new Color[sprite.GetSpriteWidth() * sprite.GetSpriteHeight()];
            Color[] newTextureData          = new Color[sprite.GetSpriteWidth() * sprite.GetSpriteHeight()];
            texture.GetData <Color>(
                0, sprite.GetSourceRect(), projectileTextureHelper, 0, sprite.GetSpriteWidth() * sprite.GetSpriteHeight());

            for (int x = 0; x < sprite.GetSpriteWidth(); x++)
            {
                for (int y = 0; y < sprite.GetSpriteHeight(); y++)
                {
                    newTextureData[y * sprite.GetSpriteWidth() + x]           = projectileTextureHelper[(y + 1) * sprite.GetSpriteWidth() - 1 - x];
                    newTextureData[(y + 1) * sprite.GetSpriteWidth() - 1 - x] = projectileTextureHelper[y * sprite.GetSpriteWidth() + x];
                }
            }

            mirroredProjectile.SetData <Color>(0, new Rectangle(0, 0, sprite.GetSpriteWidth(), sprite.GetSpriteHeight()), newTextureData, 0, sprite.GetSpriteWidth() * sprite.GetSpriteHeight());
            return(mirroredProjectile);
        }
Example #2
0
        public static CollisionState PerPixel(ISprite sprite1, ISprite sprite2, Rectangle rect, GraphicsDeviceManager graphics)
        {
            // sets the coordinates relative to (0,0) being the top left corner of this.
            Texture2D projTexture       = sprite2.GetTexture();
            Texture2D hitBoxTexture     = sprite1.GetHitbox();
            Texture2D vulnerableTexture = sprite1.GetVulnerable();

            Color[]        hitBoxPixels;
            Color[]        vulnerablePixels;
            Color[]        projectilePixels;
            CollisionState results  = CollisionState.None;
            Rectangle      objRect  = rect;
            Rectangle      projRect = new Rectangle(0, 0, rect.Width, rect.Height);

            //initial tests to see if the box is even applicable to the object texure being checked
            if (rect.X + rect.Width <= 0 || rect.Y + rect.Height <= 0)
            {
                return(CollisionState.None);
            }
            if (rect.X >= sprite1.GetSpriteWidth() || rect.Y >= sprite1.GetSpriteHeight())
            {
                return(CollisionState.None);
            }

            if (rect.X < 0)
            {
                objRect.X       = 0;
                objRect.Width  += rect.X;
                projRect.X     -= rect.X;
                projRect.Width += rect.X;
            }

            if (rect.Y < 0)
            {
                objRect.Height += rect.Y;
                objRect.Y       = 0;
                projRect.Y     -= rect.Y;
                projRect.Height = objRect.Height;
            }

            for (int i = 0; i <= objRect.Width; i++)
            {
                if (objRect.X + i == sprite1.GetSpriteWidth())
                {
                    objRect.Width  = i;
                    projRect.Width = objRect.Width;
                    break;
                }
            }
            for (int i = 0; i <= objRect.Height; i++)
            {
                if (objRect.Y + i == sprite1.GetSpriteHeight())
                {
                    objRect.Height  = i;
                    projRect.Height = objRect.Height;
                    break;
                }
            }

            if (objRect.Width == 0 || objRect.Height == 0)
            {
                return(0);
            }

            hitBoxPixels     = new Color[objRect.Width * objRect.Height];
            vulnerablePixels = new Color[objRect.Width * objRect.Height];
            projectilePixels = new Color[objRect.Width * objRect.Height];

            if (sprite2.GetMirrorX())
            {
                projTexture = sprite2.MirrorTexture(sprite2, graphics, sprite2.GetTexture());
            }

            if (sprite1.GetMirrorX())
            {
                hitBoxTexture     = sprite1.MirrorTexture(sprite1, graphics, sprite1.GetHitbox());
                vulnerableTexture = sprite1.MirrorTexture(sprite1, graphics, sprite1.GetVulnerable());
            }


            projTexture.GetData <Color>(
                0, projRect, projectilePixels, 0, objRect.Width * objRect.Height
                );

            hitBoxTexture.GetData <Color>(
                0, objRect, hitBoxPixels, 0, objRect.Width * objRect.Height
                );

            vulnerableTexture.GetData <Color>(
                0, objRect, vulnerablePixels, 0, objRect.Width * objRect.Height
                );

            for (int y = 0; y < objRect.Height; y++)
            {
                for (int x = 0; x < objRect.Width; x++)
                {
                    Color colorA = hitBoxPixels[y * objRect.Width + x];
                    Color colorB = projectilePixels[y * objRect.Width + x];
                    Color colorC = vulnerablePixels[y * objRect.Width + x];
                    if (colorA.A != 0 && colorB.A != 0)
                    {
                        if (results == CollisionState.None)
                        {
                            results = CollisionState.Standard;
                        }
                    }
                    if (colorC.A != 0 && colorB.A != 0)
                    {
                        results = CollisionState.Hurtbox;
                        break;
                    }
                }
            }
            return(results);
        }
Example #3
0
        public static CollisionState PerPixelSprite(ISprite sprite1, ISprite sprite2, GraphicsDeviceManager graphics)
        {
            int xOffset      = 0;
            int yOffset      = 0;
            int widthOffset  = 0;
            int heightOffset = 0;

            if (sprite2 is Spaceman)
            {
                xOffset      = 2;
                yOffset      = 1;
                widthOffset  = -4;
                heightOffset = -1;
            }
            Rectangle rect = new Rectangle((int)(sprite2.GetDestRect().X - sprite1.GetDestRect().X + xOffset), (int)(sprite2.GetDestRect().Y - sprite1.GetDestRect().Y + yOffset), sprite2.GetSpriteWidth() + widthOffset, sprite2.GetSpriteHeight() + heightOffset);

            return(PerPixel(sprite1, sprite2, rect, graphics));
        }
Example #4
0
        // for rectangular collisions between two sprites.
        public static bool RectCollisionDetect(ISprite sprite1, ISprite sprite2)
        {
            Color[] pixels;

            int spriteWidth  = sprite2.GetSpriteWidth() + (sprite2 is Spaceman? -4 : 0); // because the hitbox on the spaceman should be slightly smaller than it is.
            int spriteHeight = sprite2.GetSpriteHeight() + (sprite2 is Spaceman ? -1 : 0);

            Rectangle rect    = new Rectangle((int)(sprite2.GetDestRect().X - sprite1.GetDestRect().X + (sprite2 is Spaceman ? 2 : 0)), (int)(sprite2.GetDestRect().Y - sprite1.GetDestRect().Y + (sprite2 is Spaceman ? 1 : 0)), spriteWidth, spriteHeight); // this rectangle represents the space the sprite takes up relative to "this"'s top left corner
            Rectangle newRect = rect;                                                                                                                                                                                                                         // newRect is the actual rectangle to check

            //initial tests to see if the box is even applicable to the object texure being checked
            if (rect.X + rect.Width <= 0 || rect.Y + rect.Height <= 0)
            {
                return(false);
            }
            if (rect.X >= sprite1.GetSpriteWidth() || rect.Y >= sprite1.GetSpriteHeight())
            {
                return(false);
            }

            // Removes the space on the rectangle that is outside of the bounds of "this"'s texture
            if (rect.X < 0)
            {
                newRect.X      = 0;
                newRect.Width += rect.X;
            }
            if (rect.Y < 0)
            {
                newRect.Y       = 0;
                newRect.Height += rect.Y;
            }

            if (newRect.X + newRect.Width > sprite1.GetSpriteWidth())
            {
                for (int i = 0; i <= sprite1.GetSpriteWidth(); i++)
                {
                    if (newRect.X + i == sprite1.GetSpriteWidth())
                    {
                        newRect.Width = i;
                        break;
                    }
                }
            }
            if (newRect.Y + newRect.Height > sprite1.GetSpriteHeight())
            {
                for (int i = 0; i <= sprite1.GetSpriteHeight(); i++)
                {
                    if (newRect.Y + i == sprite1.GetSpriteHeight())
                    {
                        newRect.Height = i;
                        break;
                    }
                }
            }

            if (newRect.Width == 0 || newRect.Height == 0)
            {
                return(false);
            }

            pixels = new Color[newRect.Width * newRect.Height];

            sprite1.GetTexture().GetData <Color>(
                0, newRect, pixels, 0, newRect.Width * newRect.Height
                );

            for (int y = 0; y < newRect.Height; y++)
            {
                for (int x = 0; x < newRect.Width; x++)
                {
                    Color colorA = pixels[y * newRect.Width + x];
                    if (colorA.A != 0)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Example #5
0
 public static bool CheckMapCollision(int xOffset, int yOffset, ISprite sprite, Map map)
 {
     return(MapCollisionDetect(sprite.GetSpriteWidth(), sprite.GetSpriteHeight(), OffsetRect(sprite.GetDestRect(), xOffset, yOffset).ToRectangle(), map));
 }