Exemple #1
0
        /// <summary>
        /// checks for collision against all other tiles
        /// </summary>
        /// <param name="checkingTile">the tile making this check</param>
        /// <param name="checkPos">the position to check at</param>
        /// <param name="includeTypes">the valid types of tiles to check</param>
        /// <returns>if there is a collision at the given position</returns>
        public bool CheckTileCollision(GameTile checkingTile, Vector2 checkPos, params Type[] includeTypes)
        {
            Rectangle checkingRect = new Rectangle((int)checkingTile.Position.X, (int)checkingTile.Position.Y, (int)checkingTile.Sprite.Size.X, (int)checkingTile.Sprite.Size.Y);

            for (int i = 0; i < GameTileList.Count; i++)
            {
                GameTile tile        = GameTileList[i];
                bool     isValidTile = false;
                for (int j = 0; j < includeTypes.Length; j++)
                {
                    if (includeTypes[j] == tile.GetType())
                    {
                        isValidTile = true;
                        break;
                    }
                }
                if (!isValidTile || checkingTile == tile)
                {
                    continue;
                }
                Rectangle targetRect = new Rectangle((int)tile.Position.X, (int)tile.Position.Y, (int)tile.Sprite.Size.X, (int)tile.Sprite.Size.Y);
                if (PlatformerMath.RectangleInRectangle(checkingRect, targetRect))
                {
                    return(true);
                }
            }
            return(false);
        }
Exemple #2
0
 /// <summary>
 /// checks for a collision against a rectangle and all other objects of the given name
 /// </summary>
 /// <param name="collider">the rectangle to check</param>
 /// <param name="name">the name of the object to check against</param>
 /// <returns>the object we collide with</returns>
 public GameObject FindCollision(Rectangle collider, string name)
 {
     for (int i = 0; i < GameObjectList.Count; i++)
     {
         GameObject obj = GameObjectList[i];
         if (obj.GetType().IsEquivalentTo(PEngine.GetTypeFromName(name)))
         {
             Rectangle objRect = new Rectangle((obj.Position + obj.Sprite.Offset).ToPoint(), obj.Sprite.Size.ToPoint());//new Rectangle((int)obj.Position.X, (int)obj.Position.Y, (int)(obj.Sprite.Size.X, (int)obj.Sprite.Size.Y);
             if (PlatformerMath.RectangleInRectangle(collider, objRect))
             {
                 return(obj);
             }
         }
     }
     return(null);
 }
 /// <summary>
 /// updates this game object
 /// </summary>
 public virtual void Update()
 {
     Touching = false;
     if (Velocity.X != 0 || Velocity.Y != 0)
     {
         for (int i = 0; i < Room.GameObjectList.Count; i++)
         {
             GameObject obj = Room.GameObjectList[i];
             if (obj.GetType().IsEquivalentTo(PEngine.GetTypeFromName("obj_block")))
             {
                 //feel like there should be a better way to do collisions but idk that way
                 Rectangle targetRect = obj.GetHitbox();
                 targetRect.Location += obj.Position.ToPoint();
                 Rectangle fromRect = PlatformerMath.AddVectorToRect(GetHitbox(), Position, PlatformerMath.VectorCeil(new Vector2(0, Velocity.Y)));
                 while (PlatformerMath.RectangleInRectangle(fromRect, targetRect))
                 {
                     Touching = true;
                     if (Math.Abs(Velocity.Y) < CollisionPrecision)
                     {
                         Velocity.Y = 0;
                         break;
                     }
                     else
                     {
                         Velocity.Y -= Math.Sign(Velocity.Y) * CollisionPrecision;
                         fromRect    = PlatformerMath.AddVectorToRect(GetHitbox(), Position, PlatformerMath.VectorCeil(new Vector2(0, Velocity.Y)));
                     }
                 }
                 fromRect = PlatformerMath.AddVectorToRect(GetHitbox(), Position, PlatformerMath.VectorCeil(new Vector2(Velocity.X, 0)));
                 while (PlatformerMath.RectangleInRectangle(fromRect, targetRect))
                 {
                     Touching = true;
                     if (Math.Abs(Velocity.X) < CollisionPrecision)
                     {
                         Velocity.X = 0;
                         break;
                     }
                     else
                     {
                         Velocity.X -= Math.Sign(Velocity.X) * CollisionPrecision;
                         fromRect    = PlatformerMath.AddVectorToRect(GetHitbox(), Position, PlatformerMath.VectorCeil(new Vector2(Velocity.X, 0)));
                     }
                 }
                 fromRect = PlatformerMath.AddVectorToRect(GetHitbox(), Position, PlatformerMath.VectorCeil(Velocity));
                 while (PlatformerMath.RectangleInRectangle(fromRect, targetRect))
                 {
                     Touching = true;
                     if (Math.Abs(Velocity.X) < CollisionPrecision)
                     {
                         Velocity.X = 0;
                         break;
                     }
                     else
                     {
                         Velocity.X -= Math.Sign(Velocity.X) * CollisionPrecision;
                     }
                     if (Math.Abs(Velocity.Y) < CollisionPrecision)
                     {
                         Velocity.Y = 0;
                         break;
                     }
                     else
                     {
                         Velocity.Y -= Math.Sign(Velocity.Y) * CollisionPrecision;
                     }
                     fromRect = PlatformerMath.AddVectorToRect(GetHitbox(), Position, PlatformerMath.VectorCeil(Velocity));
                 }
             }
         }
     }
     Position += Velocity;
     Sprite?.Update();
 }