private Vector2 howLongToMove(Vector2 originalPos, Vector2 destination, Rectangle bounds) { Vector2 movementToTry = destination - originalPos; Vector2 furthestAvailableLocationSoFar = originalPos; int numberOfStepsToBreakMovementInto = (int)(movementToTry.Length() * 2) + 1; Vector2 oneStep = movementToTry / numberOfStepsToBreakMovementInto; for (int i = 1; i <= numberOfStepsToBreakMovementInto; i++) { Vector2 positionToTry = originalPos + oneStep * i; Rectangle newBoundary = createRectAtPos(positionToTry, bounds.Width, bounds.Height); if (CollisionManager.checkCollision(newBoundary)) { furthestAvailableLocationSoFar = positionToTry; } else { bool isDiagonalMove = movementToTry.X != 0 && movementToTry.Y != 0; if (isDiagonalMove) { int stepsLeft = numberOfStepsToBreakMovementInto - (i - 1); Vector2 remainingHorizontalMovement = oneStep.X * Vector2.UnitX * stepsLeft; Vector2 finalPositionIfMovingHorizontally = furthestAvailableLocationSoFar + remainingHorizontalMovement; furthestAvailableLocationSoFar = howLongToMove(furthestAvailableLocationSoFar, finalPositionIfMovingHorizontally, bounds); Vector2 remainingVerticalMovement = oneStep.Y * Vector2.UnitY * stepsLeft; Vector2 finalPositionIfMovingVertically = furthestAvailableLocationSoFar + remainingVerticalMovement; furthestAvailableLocationSoFar = howLongToMove(furthestAvailableLocationSoFar, finalPositionIfMovingVertically, bounds); } break; } } return(furthestAvailableLocationSoFar); }
public override void update(GameTime gameTime) { base.update(gameTime); jCooldown -= (float)gameTime.ElapsedGameTime.TotalSeconds; sCooldown -= (float)gameTime.ElapsedGameTime.TotalSeconds; CollisionManager.bulletCollision(bullets); CollisionManager.enemyBulletCollision(Enemy.enemyBullets); handleMovement(); applyFrictionAndGravity(); keepOnScreen(); moveIfPossible(); stopIfBlocked(); if (jCooldown <= 0) { jCooldown = 0; } if (Velocity.X >= 100) { Velocity = new Vector2(100, 0); } for (int i = 0; i < bullets.Count; i++) { bullets[i].update(gameTime); if (bullets[i].timer <= 0) { bullets.RemoveAt(i); } } if (hit) { switch (currentHit) { case HitType.SOLDIER: health -= 100; break; case HitType.MAGIC: health -= 250; break; case HitType.REAPER: health -= 500; break; } hit = false; } if (health <= 0) { MainGame.currentState = MainGame.state.EXIT; } playerPosStatic = Position; playerRectangleStatic = Hitbox; }