public override void HandleCollisions(GameTime gameTime) { int xOrigin = (int)(Position.X) / Tile.Width; int yOrigin = (int)(Position.Y) / Tile.Height; int xStart = xOrigin - 15; int yStart = yOrigin - 15; int xEnd = xOrigin + 15; int yEnd = yOrigin + 15; enemyToChar = new LiangBarsky(Position, new Vector2(level.Player.Position.X, level.Player.Position.Y - 32.0f)); wallInWay = false; for (int y = yStart; y < yEnd; ++y) { for (int x = xStart; x < xEnd; ++x) { TileCollision collision = level.GetCollision(x, y); if (collision != TileCollision.Passable && collision != TileCollision.Patrol) { Rectangle tileBounds = level.GetBounds(x, y); if (enemyToChar.Intersect(tileBounds) == true) { wallInWay = true; } } } } Rectangle bounds = BoundingRectangle; Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, level.Player.BoundingRectangle); if (depth.X < -2.0f || depth.X > 2.0f) { velocity = Vector2.Zero; if (level.Player.StopPlayerCollision == false) { level.Rumble.RumbleSetup(500.0f, 0.5f); level.Player.Strikes -= 1; level.Player.IsHit = true; } } }
public override void HandleCollisions(GameTime gameTime) { Rectangle bounds = BoundingRectangle; Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, level.Player.BoundingRectangle); if (depth != Vector2.Zero) { velocity = Vector2.Zero; float absDepthX = Math.Abs(depth.X); float absDepthY = Math.Abs(depth.Y); if (depth.X > 0 && level.Player.StopPlayerCollision == false) { if (StopEnemy == false) { level.Rumble.RumbleSetup(500.0f, 0.5f); direction = FaceDirection.Left; level.Player.Strikes -= 1; level.Player.IsHit = true; } } else if (depth.X < 0 && level.Player.StopPlayerCollision == false) { if (StopEnemy == false) { level.Rumble.RumbleSetup(500.0f, 0.5f); direction = FaceDirection.Right; level.Player.Strikes -= 1; level.Player.IsHit = true; } } } }
private void HandlePlayerCollision(GameTime gameTime) { Rectangle bounds = BoundingRectangle; Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, level.Player.BoundingRectangle); if (direction == FaceDirection.Right) { enemySight = new Rectangle((int)Position.X, (int)Position.Y - 54, 400, 20); enemySense = new Rectangle((int)Position.X - 150, (int)Position.Y - 54, 150, 20); } else if (direction == FaceDirection.Left) { enemySight = new Rectangle((int)Position.X - 400, (int)Position.Y - 54, 400, 20); enemySense = new Rectangle((int)Position.X, (int)Position.Y - 54, 150, 20); } if (depth.X < -2.0f || depth.X > 2.0f) { velocity = Vector2.Zero; if (level.Player.StopPlayerCollision == false) { level.Rumble.RumbleSetup(500.0f, 0.5f); level.Player.Strikes -= 1; level.Player.IsHit = true; attackPlayer = true; level.Player.StopPlayerCollision = true; } //StopEnemy = true; } else { StopEnemy = false; } }
private void HandleCollisions(TileMap tileMap, GameTime gameTime) { // Get the player's bounding rectangle and find neighboring tiles. Rectangle bounds = BoundingRectangle; int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width); int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1; int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height); int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1; // Reset flag to search for ground collision. isOnGround = false; if (IsAlignedToLadder() == false) { isOnLadder = false; } // For each potentially colliding tile, for (int y = topTile; y <= bottomTile; ++y) { for (int x = leftTile; x <= rightTile; ++x) { // If this tile is collidable, TileCollision collision = level.GetCollision(x, y); if (collision != TileCollision.Passable) { // Determine collision depth (with direction) and magnitude. Rectangle tileBounds = level.GetBounds(x, y); Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds); if (depth != Vector2.Zero) { float absDepthX = Math.Abs(depth.X); float absDepthY = Math.Abs(depth.Y); if (collision == TileCollision.Ladder && movement.Y > 0.25f && IsAlignedToLadder() == true) { isOnLadder = true; } // Resolve the collision along the shallow axis. if (absDepthY < absDepthX && collision != TileCollision.Patrol || collision == TileCollision.Platform) { // If we crossed the top of a tile, we are on the ground. if (previousBottom <= tileBounds.Top && collision != TileCollision.Ladder) { isOnGround = true; camera.MoveTrapUp(tileBounds.Top); } // Ignore platforms, unless we are on the ground. if (collision == TileCollision.Impassable || IsOnGround) { // Resolve the collision along the Y axis. Position = new Vector2(Position.X, Position.Y + depth.Y); // Perform further collisions with the new bounds. bounds = BoundingRectangle; } } else if (collision == TileCollision.Impassable) // Ignore platforms. { // Resolve the collision along the X axis. Position = new Vector2(Position.X + depth.X, Position.Y); // Perform further collisions with the new bounds. bounds = BoundingRectangle; } } } } } // Save the new bounds bottom. previousBottom = bounds.Bottom; }
public override void Update(GameTime gameTime) { float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; Rectangle bounds = BoundingRectangle; HandleCollisions(); Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, level.Player.BoundingRectangle); if (EncounterRadius.Intersects(level.Player.BoundingRectangle) && level.Player.StopPlayerCollision == false && level.Player.IsOnLadder == false && wallInWay == false) { startChase = true; } if (depth != Vector2.Zero) { float absDepthX = Math.Abs(depth.X); float absDepthY = Math.Abs(depth.Y); if (/*startTimer == false &&*/ level.Player.StopPlayerCollision == false) { if (StopEnemy == false) { startChase = false; level.Rumble.RumbleSetup(500.0f, 0.5f); level.Player.Strikes -= 1; level.Player.IsHit = true; level.Player.StopPlayerCollision = true; } startTimer = true; } //if (startTimer == true && level.Player.StopPlayerCollision == false) //{ // timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; // if (timer >= 500) // { // if (StopEnemy == false) // { // startChase = false; // level.Rumble.RumbleSetup(500.0f, 0.5f); // level.player.Strikes -= 10; // level.Player.IsHit = true; // level.Player.StopPlayerCollision = true; // } // timer = 0; // } //} if (absDepthY < absDepthX && level.Player.StopPlayerCollision == false) { Position = new Vector2(Position.X, Position.Y + depth.Y); bounds = BoundingRectangle; } else if (level.Player.StopPlayerCollision == false) { Position = new Vector2(Position.X + depth.X, Position.Y); bounds = BoundingRectangle; } } if (startChase == true) { Vector2 targetDirection = new Vector2(level.Player.Position.X, level.Player.Position.Y - 60) - Position; targetDirection.Normalize(); velocity = targetDirection * MoveSpeed * elapsed; Position = Position + velocity; Position = new Vector2((float)Math.Round(Position.X), (float)Math.Round(Position.Y)); } else { Vector2 targetDirection = startingPoint - Position; if (targetDirection.X < 0.0f || targetDirection.Y < 0.0f) { targetDirection.Normalize(); velocity = targetDirection * MoveSpeed * elapsed; Position = Position + velocity; Position = new Vector2((float)Math.Round(Position.X), (float)Math.Round(Position.Y)); } } }