/// <summary> /// Constructs a new level. /// </summary> /// <param name="serviceProvider"> /// The service provider that will be used to construct a ContentManager. /// </param> /// <param name="path"> /// The absolute path to the level file to be loaded. /// </param> public Level(string path, string name) { this.FilePath = path; this.Name = name; TileEngine = new TileEngine(this); TileEngine.LoadTiles(path); Hud = new Hud(); Camera = new LevelCamera(); // textBox = new Rectangle(10, 10, 300, 300); }
public void ScrollCamera(Viewport viewport, LevelPlayer player, TileEngine tileEngine) { #if ZUNE const float ViewMargin = 0.45f; #else const float ViewMargin = 0.35f; #endif // Calculate the edges of the screen. float marginHeight = viewport.Height + 20.0f; float marginWidth = viewport.Width * ViewMargin; float marginLeft = CameraPositionXAxis + marginWidth; float marginRight = CameraPositionXAxis + viewport.Width - marginWidth; // Calculate how far to scroll when the player is near the edges of the screen. float cameraMovement = 0.0f; if (player.Position.X < marginLeft) cameraMovement = player.Position.X - marginLeft; else if (player.Position.X > marginRight) cameraMovement = player.Position.X - marginRight; const float TopMargin = 0.3f; const float BottomMargin = 0.1f; float marginTop = cameraPositionYAxis + viewport.Height * TopMargin; float marginBottom = cameraPositionYAxis + viewport.Height - viewport.Height * BottomMargin; float cameraMovementY = 0.0f; if (player.Position.Y < marginTop) //above the top margin cameraMovementY = player.Position.Y - marginTop; else if (player.Position.Y > marginBottom) //below the bottom margin cameraMovementY = player.Position.Y - marginBottom; // Update the camera position, but prevent scrolling off the ends of the level. float maxCameraPositionXOffset = LevelTile.Width * tileEngine.Width - viewport.Width; CameraPositionXAxis = MathHelper.Clamp(CameraPositionXAxis + cameraMovement, 0.0f, maxCameraPositionXOffset); float maxCameraPositionYOffset = LevelTile.Height * tileEngine.Height - viewport.Height; cameraPositionYAxis = MathHelper.Clamp(cameraPositionYAxis + cameraMovementY, 0.0f, maxCameraPositionYOffset); }