/// <summary> /// The block all collisions. /// </summary> /// <param name="a"> /// The <see cref="BaseActor"/> colliding with an object. /// </param> /// <param name="loadedactors"> /// The <see cref="BaseActor"/> that is being collided with. /// </param> public static void BlockAllCollisions(BaseActor a, List <BaseActor> loadedactors) { var collisions = GetAllCollisions(loadedactors, a); bool isCoined = false; foreach (var c in collisions.Where(c => c.CollisionAction == BaseActor.CollisionType.Block)) { if (IsActorStandingOnAnother(a, c)) { // Standing a.Position = new Vector2D_Dbl(a.Position.X, c.Position.Y - a.Height); a.Velocity = new Vector2D_Dbl(a.Velocity.X, a.Velocity.Y < 0 ? 0 : a.Velocity.Y); } else if (IsActorPushingAnotherFromLeft(a, c)) { // OnLeftSide a.Position = new Vector2D_Dbl(c.Position.X - a.Width, a.Position.Y); a.Velocity = new Vector2D_Dbl(0, a.Velocity.Y); if (a.GetType() == typeof(Mario) && c.GetType() == typeof(Teleport)) { var kok = (Teleport)c; kok.env.isNewLevel = true; } } else if (IsActorPushingAnotherFromRight(a, c)) { // OnRightSide a.Position = new Vector2D_Dbl(c.Position.X + c.Width, a.Position.Y); a.Velocity = new Vector2D_Dbl(0, a.Velocity.Y); } else if (IsActorPushingAnotherFromBottom(a, c)) { // OnBottom a.Position = new Vector2D_Dbl(a.Position.X, c.Position.Y + c.Height); a.Velocity = new Vector2D_Dbl(a.Velocity.X, 0); if (a.GetType() == typeof(Mario) && c.GetType() == typeof(QuestionBox)) { var box = (QuestionBox)c; box.ActivateBox(); } } } }
public static bool CheckWallCollision(BaseActor actor) { var actorOrigin = new Vector2(actor.Sprite.Width / 2, actor.Sprite.Height / 2); Matrix actorTransform = Matrix.CreateTranslation(new Vector3(-actorOrigin, 0.0f)) * Matrix.CreateTranslation(new Vector3(actor.Position, 0.0f)); var actorRectangle = CalculateBoundingRectangle( new Rectangle(0, 0, actor.Sprite.Width, actor.Sprite.Height), actorTransform); for (int i = 0; i < GhostGame.Game.wallBoundingBoxes.Length; i++) { var currentRectangle = GhostGame.Game.wallBoundingBoxes[i]; bool collisionfound = false; if (TestBoundingBoxCollision(actorRectangle, currentRectangle)) { Color[] imagePiece = GetImageData(GhostGame.Game.mapTextureData, GhostGame.Game.mapTexture.Width, currentRectangle); var subImageOrigin = new Vector2(currentRectangle.Width / 2, currentRectangle.Height / 2); Matrix subImageTransform = Matrix.CreateTranslation(new Vector3(-subImageOrigin, 0.0f)) * Matrix.CreateTranslation(new Vector3(new Vector2(currentRectangle.X, currentRectangle.Y), 0.0f)); Color[] actorTexturedata; // Extract collision data if (actor.GetType() == typeof(Hunter)) { actorTexturedata = GhostGame.Game.hunterTextureData; } else { actorTexturedata = GhostGame.Game.ghostTextureData; } collisionfound = IntersectPixels(actorRectangle, actorTexturedata, currentRectangle, imagePiece); } if (collisionfound) { return(true); } } return(false); }