/// <summary> /// Adds a new camera to the state's cameras list. /// </summary> /// <param name="camera">The <c>GenCamera</c> to add.</param> /// <returns>The newly added <c>GenCamera</c>.</returns> public GenCamera AddCamera(GenCamera camera) { Cameras.Add(camera); // Assign the new camera to the initial camera if it has not already been assigned. if (Camera == null) Camera = camera; return camera; }
/// <summary> /// Checks if the object can be drawn to the specified camera. /// </summary> /// <param name="camera">The camera to check.</param> /// <returns>True if the object is associated with the specified camera, false if not.</returns> public bool CanDraw(GenCamera camera) { // If the cameras list has not been initialized, associate the object with each camera in the current state. if (Cameras == null) { Cameras = new List<GenCamera>(); foreach (GenCamera stateCamera in GenG.State.Cameras) Cameras.Add(stateCamera); } return Cameras.Contains(camera); }
/// <summary> /// Calls Draw on each of the tiles in the tilemap which overlap the current camera's view area. /// </summary> /// <param name="camera">The camera used to draw.</param> public override void Draw(GenCamera camera) { if ((camera == null) || !CanDraw(camera)) return; // Prevent drawing tiles outside of the camera view to increase performance. // If the camera is rotated, extend the drawing bounds to prevent culling tiles rotated into the camera view. float extendBounds = 0f; if (camera.Rotation != 0) { extendBounds = (float)Math.Sqrt((camera.CameraView.HalfWidth * camera.CameraView.HalfWidth) + (camera.CameraView.HalfHeight * camera.CameraView.HalfHeight)) - Math.Min(camera.CameraView.HalfWidth, camera.CameraView.HalfHeight); } GetTileBounds( camera.CameraView.Left - extendBounds, camera.CameraView.Right + extendBounds, camera.CameraView.Top - extendBounds, camera.CameraView.Bottom + extendBounds); for (int y = _tileBounds[2]; y <= _tileBounds[3]; y++) { for (int x = _tileBounds[0]; x <= _tileBounds[1]; x++) { if ((Tiles[y][x] != null) && Tiles[y][x].Exists && Tiles[y][x].Visible) { Tiles[y][x].Draw(); if (GenG.AllowDebug && GenG.IsDebug) Tiles[y][x].DrawDebug(null); } } } }
/// <summary> /// Draws a box representing the collision area of the object. /// </summary> /// <param name="camera">The camera used to draw.</param> public override void DrawDebug(GenCamera camera) { if ((camera != null) && !CanDraw(camera)) return; GenG.SpriteBatch.Draw( GenG.Pixel, _position, _boundingRect, ((Immovable) ? Color.Red : Color.Lime) * 0.5f); }
/// <summary> /// Gets the x and y positions of where the top-left corner of a sprite will be drawn within the given camera. /// Calculates the draw position from the position, origin, offset, camera scroll, and scroll factor values. /// </summary> /// <param name="camera">The camera to use when calculating the sprite's draw position.</param> /// <returns>The sprite's draw position within the given camera.</returns> protected Vector2 GetDrawPosition(GenCamera camera) { Vector2 drawPosition = Vector2.Zero; drawPosition.X = _drawPosition.X - camera.ScrollX + (camera.ScrollX * ScrollFactor.X); drawPosition.Y = _drawPosition.Y - camera.ScrollY + (camera.ScrollY * ScrollFactor.Y); // TODO: Integrate motion of sprites to adjust for timescale. //drawPosition.X = _drawPosition.X - camera.ScrollX + (camera.ScrollX * ScrollFactor.X) + _moveDistance.X * (GenG._updateTimer / GenG.TimeStep); //drawPosition.Y = _drawPosition.Y - camera.ScrollY + (camera.ScrollY * ScrollFactor.Y) + _moveDistance.Y * (GenG._updateTimer / GenG.TimeStep); if (GenG.DrawMode == GenG.DrawType.Pixel) { drawPosition.X = (int)drawPosition.X; drawPosition.Y = (int)drawPosition.Y; } return drawPosition; }
/// <summary> /// Draws the sprite to a specified camera. /// </summary> /// <param name="camera">The camera used to draw.</param> public override void Draw(GenCamera camera) { if (_texture != null) { if ((camera != null) && !CanDraw(camera)) return; Vector2 drawPosition = (camera == null) ? _drawPosition : GetDrawPosition(camera); GenG.SpriteBatch.Draw( _texture, drawPosition, (_currentAnimation == null) ? _sourceRect : Animations[_currentAnimation].FrameRect, _drawColor, (DrawRotated) ? _rotation : 0f, _origin, Scale, _spriteEffect, 0f); } }
/// <summary> /// Removes a camera from the cameras list. /// </summary> /// <param name="camera">The camera to remove.</param> /// <returns>The camera that was removed. Null if the camera does not exist in the cameras list.</returns> public GenCamera RemoveCamera(GenCamera camera) { if (Cameras.Remove(camera)) { // If the camera being removed is the same as the initial camera, assign the initial camera to the first camera in the cameras list. if (Camera == camera) Camera = Cameras[0]; return camera; } return null; }
/// <summary> /// Override this method to add debug drawing logic. /// </summary> /// <param name="camera">The camera used to draw.</param> public virtual void DrawDebug(GenCamera camera) { }
/// <summary> /// Override this method to add draw logic. /// </summary> /// <param name="camera">The camera used to draw.</param> public virtual void Draw(GenCamera camera) { }
/// <summary> /// Calls Draw on each of the objects in the active members list. /// </summary> /// <param name="camera">The camera used to draw.</param> public override void Draw(GenCamera camera) { if (!Exists || !Visible) return; foreach (GenBasic member in _activeMembers) { if (!member.Exists) continue; if (member.Visible) member.Draw(camera); if (GenG.AllowDebug && GenG.IsDebug) member.DrawDebug(camera); } if (GenG.AllowDebug && GenG.IsDebug && (Quadtree != null)) Quadtree.DrawDebug(); }
/// <summary> /// Draws the text sprite. /// </summary> /// <param name="camera">The camera used to draw.</param> public override void Draw(GenCamera camera) { base.Draw(camera); if ((camera != null) && !CanDraw(camera)) return; Vector2 drawPosition = (camera == null) ? _drawPosition : GetDrawPosition(camera); // Draw the text shadow. if (HasShadow) { GenG.SpriteBatch.DrawString( Font, _text, drawPosition + ShadowPosition, ShadowColor * _alpha, (DrawRotated) ? _rotation : 0f, _textOrigin, Scale, SpriteEffects.None, 0f); } // Draw the text. GenG.SpriteBatch.DrawString( Font, _text, drawPosition, _drawColor, (DrawRotated) ? _rotation : 0f, _textOrigin, Scale, SpriteEffects.None, 0f); }