Exemplo n.º 1
0
 /// <summary>
 /// Handles sprite-to-sprite interaction (collision detection, sprite types, etc.).
 /// </summary>
 /// <param name="character">The character to check against.</param>
 public void CheckSpriteColor(Character character)
 {
     // if the sprite we're checking against is CHARly
     if (character.CurrentSpriteType == SpriteType.CHARly)
     {
         // if this sprite is mad or saved
         if (this.CurrentSpriteType == SpriteType.Mad || this.CurrentSpriteType == SpriteType.Saved)
         {
             // set the sprite type based on the character shape
             if (character.CharShapeStatus == this.CharShapeStatus)
             {
                 this.CurrentSpriteType = SpriteType.Saved;
             }
             else
             {
                 this.CurrentSpriteType = SpriteType.Mad;
             }
         }
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Check for pixel-perfect collisions
        /// </summary>
        /// <param name="rectangle0"></param>
        /// <param name="rectangle1"></param>
        /// <returns></returns>
        private bool PixelPerfect(Character charA, Character charB)
        {
            // Get Color data of each Texture
            Color[] bitsA = new Color[charA.Texture.Width * charA.Texture.Height];
            charA.Texture.GetData(bitsA);
            Color[] bitsB = new Color[charB.Texture.Width * charB.Texture.Height];
            charB.Texture.GetData(bitsB);

            // Calculate the intersecting rectangle
            int x1 = Math.Max(charA.CollisionRectangle.X, charB.CollisionRectangle.X);
            int x2 = Math.Min(charA.CollisionRectangle.X + charA.CollisionRectangle.Width, charB.CollisionRectangle.X + charB.CollisionRectangle.Width);

            int y1 = Math.Max(charA.CollisionRectangle.Y, charB.CollisionRectangle.Y);
            int y2 = Math.Min(charA.CollisionRectangle.Y + charA.CollisionRectangle.Height, charB.CollisionRectangle.Y + charB.CollisionRectangle.Height);

            // Calculate image scaling factors
            int scaleFactorXA = charA.Texture.Height / charA.CollisionRectangle.Width; // FIXME LATER!!!
            int scaleFactorXB = charB.Texture.Height / charB.CollisionRectangle.Width; // FIXME LATER!!!
            int scaleFactorYA = charA.Texture.Height / charA.CollisionRectangle.Height;
            int scaleFactorYB = charB.Texture.Height / charB.CollisionRectangle.Height;

            // For each single pixel in the intersecting rectangle
            for (int y = y1; y < y2; ++y)
            {
                for (int x = x1; x < x2; ++x)
                {
                    // Get the color from each texture
                    Color a = bitsA[scaleFactorXA * (x - charA.CollisionRectangle.X) + scaleFactorYA * (y - charA.CollisionRectangle.Y) * charA.Texture.Width];
                    Color b = bitsB[scaleFactorXB * (x - charB.CollisionRectangle.X) + scaleFactorYB * (y - charB.CollisionRectangle.Y) * charB.Texture.Width];

                    if (a.A != 0 && b.A != 0) // If both colors are not transparent (the alpha channel is not 0), then there is a collision
                    {
                        return true;
                    }
                }
            }
            // If no collision occurred by now, we're clear.
            return false;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Checks to see if 2 sprites have collided
        /// </summary>
        public bool CheckForCollisions(Character characterToCheck)
        {
            // Determine if collision has occurred between 2 sprite rectangles
            if (CollisionRectangle.Intersects(characterToCheck.CollisionRectangle))
            {
                // Check for pixel-perfect collision
                if (PixelPerfect(this, characterToCheck))
                {
                    // Check for collision type (between sprites)
                    if (this.CurrentSpriteType == SpriteType.CHARly ||
                        characterToCheck.CurrentSpriteType == SpriteType.CHARly)
                    {
                        // Check for if the sprite collision involves a "mad" character
                        if (this.CurrentSpriteType == SpriteType.Mad ||
                            characterToCheck.CurrentSpriteType == SpriteType.Mad)
                        {
                            return true;
                        }
                        // Check for if the sprite collision involves a "saved" character
                        if (this.CurrentSpriteType == SpriteType.Saved)
                        {
                            this.IsActive = false;
                        }
                        if (characterToCheck.CurrentSpriteType == SpriteType.Saved)
                        {
                            characterToCheck.IsActive = false;
                        }
                    }
                }
            }

            return false;
        }