/// <summary> /// Detects if tank is intersecting a impassible tile /// </summary> /// <param name="tankObject">Clients Tank</param> /// <returns>Collision</returns> private bool DetectCollision(PlayerTank tankObject) { // The 3 below this comment is an offset to make sure we only check for collisions with tiles // adjacent to us. Checks are done to a range of 3 tiles. int numTilesX = (viewportRect.X / Constants.TILE_SIZE) + 3; int numTilesY = (viewportRect.Y / Constants.TILE_SIZE) + 3; int minimumX = (int)(tankObject.Position.X / Constants.TILE_SIZE) - numTilesX; int minimumY = (int)(-tankObject.Position.Y / Constants.TILE_SIZE) - numTilesY; int maximumX = (int)(tankObject.Position.X / Constants.TILE_SIZE) + numTilesX; int maximumY = (int)(-tankObject.Position.Y / Constants.TILE_SIZE) + numTilesY; for (int y = minimumY; y < map.Height && y <= maximumY; y++) { if (y < 0) { continue; } for (int x = minimumX; x < map.Width && x <= maximumX; x++) { if (x < 0) { continue; } Options.KeyBindings keys = ServiceManager.Game.Options.KeySettings; DrawableTile tile = visibleTiles[y * map.Width + x]; if (tankObject.Name == PlayerManager.LocalPlayerName) { if (!tile.Passable && ( (Keyboard.GetState().IsKeyDown(keys.Forward) && tankObject.FrontSphere.Intersects(tile.BoundingBox)) || (Keyboard.GetState().IsKeyDown(keys.Backward) && tankObject.BackSphere.Intersects(tile.BoundingBox)))) { return(true); } } else { if (!tile.Passable && ((tankObject.FrontSphere.Intersects(tile.BoundingBox) && tankObject.DirectionMovement == VTankObject.Direction.FORWARD) || (tankObject.BackSphere.Intersects(tile.BoundingBox) && tankObject.DirectionMovement == VTankObject.Direction.REVERSE))) { return(true); } } } } return(false); }
/// <summary> /// Checks for key presses from the user /// </summary> private void CheckInput() { // Do not process input if not active. if (!ServiceManager.Game.IsActive) { // If the tank is moving/rotating, stop it. ChangeMovement(VTankObject.Direction.NONE); ChangeRotation(VTankObject.Direction.NONE); return; } Options.KeyBindings keys = ServiceManager.Game.Options.KeySettings; bool localClientCollision = PerformCollisionChecks(); if (KeyPressHelper.IsPressed(keys.Camera)) { if (input.Visible == false) { if (renderer.ActiveScene.CurrentCamera == renderer.ActiveScene.AccessCamera(ChaseCamera)) { renderer.ActiveScene.CurrentCamera = renderer.ActiveScene.AccessCamera(OverheadCamera); } else if (renderer.ActiveScene.CurrentCamera == renderer.ActiveScene.AccessCamera(OverheadCamera)) { renderer.ActiveScene.CurrentCamera = renderer.ActiveScene.AccessCamera(ChaseCamera); } } } if (!input.Visible) { if (localClientCollision && !stuck) { ChangeMovement(VTankObject.Direction.NONE); previousCollision = true; } else { if (Keyboard.GetState().IsKeyDown(keys.Forward)) { ChangeMovement(VTankObject.Direction.FORWARD); } else if (Keyboard.GetState().IsKeyDown(keys.Backward)) { ChangeMovement(VTankObject.Direction.REVERSE); } else { ChangeMovement(VTankObject.Direction.NONE); } if (previousCollision) { previousCollision = false; Resync(); } } if (Keyboard.GetState().IsKeyDown(keys.RotateRight)) { ChangeRotation(VTankObject.Direction.RIGHT); } else if (Keyboard.GetState().IsKeyDown(keys.RotateLeft)) { ChangeRotation(VTankObject.Direction.LEFT); } else { ChangeRotation(VTankObject.Direction.NONE); } if (KeyPressHelper.IsPressed(keys.Minimap)) { miniMap.Enabled = !miniMap.Enabled; } if (Keyboard.GetState().IsKeyDown(keys.Score)) { Scores.Enabled = true; } else { Scores.Enabled = false; } if (KeyPressHelper.IsPressed(Keys.F1)) { helpOverlay.Enabled = !helpOverlay.Enabled; } } }