void CollideWithCharacters(PlayState map) { Rectanglef r = new Rectanglef(map._player.Position.X * Global.PixelsPerTile, map._player.Position.Y * Global.PixelsPerTile, map._player.Visual.Width * Global.PixelsPerTile, map._player.Visual.Height * Global.PixelsPerTile); if (HurtsPlayer && TopLeftCorner.X + Texture.Width * Scale >= r.X && TopLeftCorner.X <= r.X + r.Width && TopLeftCorner.Y + Texture.Height * Scale >= r.Y && TopLeftCorner.Y <= r.Y + r.Height) { map._player.Hp -= .02f * DamageRatio; map.hpBarFlashing = .1f; } foreach (Character c in map.Victims) { r = new Rectanglef(c.Position.X * Global.PixelsPerTile, c.Position.Y * Global.PixelsPerTile, c.Visual.Width * Global.PixelsPerTile, c.Visual.Height * Global.PixelsPerTile); if (HurtsPlayer && TopLeftCorner.X + Texture.Width >= r.X && TopLeftCorner.X <= r.X + r.Width && TopLeftCorner.Y + Texture.Height >= r.Y && TopLeftCorner.Y <= r.Y + r.Height) { c.Hp -= .02f * DamageRatio; } } }
//Assuming you are not already colliding with a tile private void CollideWithWalls(GameTime gameTime, PlayState map) { if (HurtsPlayer) { return; } float delta = (float)gameTime.ElapsedGameTime.TotalSeconds; KeepinBounds(map); if (Velocity.X > 0) { //Find the set of tiles that our movement will intersect with Rectanglef movementBox = new Rectanglef((TopLeftCorner.X + Texture.Width * Scale), TopLeftCorner.Y, Velocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds, Texture.Height * Scale); movementBox.Multiply(1 / Global.PixelsPerTile); int minX = (int)Math.Floor(movementBox.X); int maxX = (int)Math.Floor(movementBox.X + movementBox.Width); int minY = (int)Math.Floor(movementBox.Y); int maxY = (int)Math.Floor(movementBox.Y + movementBox.Height); //Itterate through every tile that we will cross through and see if one of them is solid for (int x = minX; x <= maxX; x++) { for (int y = minY; y <= maxY; y++) { if (map.TileIsSolid(x, y)) { //s += new Vector2(-Velocity.X, -Velocity.Y / 2); //TopRightCorner += new Vector2(-Texture.Width * Scale - 0.001f, 0); maxX = -999999; Age += (Lifetime - Age) / 1.5f; Velocity = new Vector2(-Velocity.X, Global.rand.Next(-500, 500)); break; } } } } else if (Velocity.X < 0) { //Find the set of tiles that our movement will intersect with Rectanglef movementBox = new Rectanglef((TopLeftCorner.X + (Velocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds)), TopLeftCorner.Y, -Velocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds, Texture.Height * Scale); movementBox.Multiply(1 / Global.PixelsPerTile); int maxX = (int)Math.Floor(movementBox.X); int minX = (int)Math.Floor(movementBox.X + movementBox.Width); int minY = (int)Math.Floor(movementBox.Y); int maxY = (int)Math.Floor(movementBox.Y + movementBox.Height); //Itterate through every tile that we will cross through and see if one of them is solid for (int x = maxX; x >= minX; x--) { for (int y = minY; y <= maxY; y++) { if (map.TileIsSolid(x, y)) { //Velocity += new Vector2(-Velocity.X, -Velocity.Y / 2); //TopRightCorner += new Vector2(1.001f, 0); minX = 999999; Age += (Lifetime - Age) / 1.5f; Velocity = new Vector2(-Velocity.X, Global.rand.Next(-500, 500)); break; } } } } if (Velocity.Y > 0) { //Find the set of tiles that our movement will intersect with Rectanglef movementBox = new Rectanglef(TopLeftCorner.X, TopLeftCorner.Y + Texture.Height * Scale, Texture.Width * Scale, Velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds); movementBox.Multiply(1 / Global.PixelsPerTile); int minX = (int)Math.Floor(movementBox.X); int maxX = (int)Math.Floor(movementBox.X + movementBox.Width); int minY = (int)Math.Floor(movementBox.Y); int maxY = (int)Math.Floor(movementBox.Y + movementBox.Height); //Itterate through every tile that we will cross through and see if one of them is solid for (int y = minY; y <= maxY; y++) { for (int x = minX; x <= maxX; x++) { if (map.TileIsSolid(x, y)) { //Velocity += new Vector2(-Velocity.X / 2, -Velocity.Y); //TopRightCorner += new Vector2(0, -Texture.Height * Scale - 0.001f); maxY = -999999; Age += (Lifetime - Age) / 1.5f; Velocity = new Vector2(Global.rand.Next(-500, 500), -Velocity.Y); break; } } } } else if (Velocity.Y < 0) { //Find the set of tiles that our movement will intersect with Rectanglef movementBox = new Rectanglef(TopLeftCorner.X, TopLeftCorner.Y + (Velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds), Texture.Width * Scale, -Velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds); movementBox.Multiply(1 / Global.PixelsPerTile); int minX = (int)Math.Floor(movementBox.X); int maxX = (int)Math.Floor(movementBox.X + movementBox.Width); int minY = (int)Math.Floor(movementBox.Y); int maxY = (int)Math.Floor(movementBox.Y + movementBox.Height); //Itterate through every tile that we will cross through and see if one of them is solid for (int y = maxY; y >= minY; y--) { for (int x = minX; x <= maxX; x++) { if (map.TileIsSolid(x, y)) { //Velocity += new Vector2(-Velocity.X / 2,-Velocity.Y); //TopRightCorner += new Vector2(0, 1.001f); //get us out of these loops minY = 999999; Velocity = new Vector2(Global.rand.Next(-500, 500), -Velocity.Y); Age += (Lifetime - Age) / 1.5f; break; } } } } }
//Assuming you are not already colliding with a tile private void CollideWithWalls(GameTime gameTime, PlayState map) { if (Velocity.X > 0) { //Find the set of tiles that our movement will intersect with Rectanglef movementBox = new Rectanglef(Position.X + Visual.Width, Position.Y, Velocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds, Visual.Height); int minX = (int)Math.Floor(movementBox.X); int maxX = (int)Math.Floor(movementBox.X + movementBox.Width); int minY = (int)Math.Floor(movementBox.Y); int maxY = (int)Math.Floor(movementBox.Y + movementBox.Height); //Itterate through every tile that we will cross through and see if one of them is solid for (int x = minX; x <= maxX; x++) { for (int y = minY; y <= maxY; y++) { if (map.TileIsSolid(x, y)) { SetVelocityX(0); SetPositionX((float)x - Visual.Width - 0.001f); maxX = -999999; break; } } } } else if (Velocity.X < 0) { //Find the set of tiles that our movement will intersect with Rectanglef movementBox = new Rectanglef(Position.X + (Velocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds), Position.Y, -Velocity.X * (float)gameTime.ElapsedGameTime.TotalSeconds, Visual.Height); int maxX = (int)Math.Floor(movementBox.X); int minX = (int)Math.Floor(movementBox.X + movementBox.Width); int minY = (int)Math.Floor(movementBox.Y); int maxY = (int)Math.Floor(movementBox.Y + movementBox.Height); //Itterate through every tile that we will cross through and see if one of them is solid for (int x = maxX; x >= minX; x--) { for (int y = minY; y <= maxY; y++) { if (map.TileIsSolid(x, y)) { SetVelocityX(0); SetPositionX(x + 1.001f); minX = 999999; break; } } } } if (Velocity.Y > 0) { //Find the set of tiles that our movement will intersect with Rectanglef movementBox = new Rectanglef(Position.X, Position.Y + Visual.Height, Visual.Width, Velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds); int minX = (int)Math.Floor(movementBox.X); int maxX = (int)Math.Floor(movementBox.X + movementBox.Width); int minY = (int)Math.Floor(movementBox.Y); int maxY = (int)Math.Floor(movementBox.Y + movementBox.Height); //Itterate through every tile that we will cross through and see if one of them is solid for (int y = minY; y <= maxY; y++) { for (int x = minX; x <= maxX; x++) { if (map.TileIsSolid(x, y)) { SetVelocityY(0); SetPositionY((float)y - Visual.Height - 0.001f); maxY = -999999; break; } } } } else if (Velocity.Y < 0) { //Find the set of tiles that our movement will intersect with Rectanglef movementBox = new Rectanglef(Position.X, Position.Y + (Velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds), Visual.Width, -Velocity.Y * (float)gameTime.ElapsedGameTime.TotalSeconds); int minX = (int)Math.Floor(movementBox.X); int maxX = (int)Math.Floor(movementBox.X + movementBox.Width); int minY = (int)Math.Floor(movementBox.Y); int maxY = (int)Math.Floor(movementBox.Y + movementBox.Height); //Itterate through every tile that we will cross through and see if one of them is solid for (int y = maxY; y >= minY; y--) { for (int x = minX; x <= maxX; x++) { if (map.TileIsSolid(x, y)) { SetVelocityY(0); SetPositionY(y + 1.001f); //get us out of these loops minY = 999999; break; } } } } }