/// <summary> /// Centers Camera on Sprite /// </summary> /// <param name="target">IDrawable to be targeted</param> public void FocusOn(Graphics.IDrawable target) { Vector2 targetpos = new Vector2(); targetpos.X = (target.Position.X - Camera.Bounds.Width / 2 + target.Area.Width / 2); targetpos.Y = (target.Position.Y - Camera.Bounds.Height / 2 + target.Area.Height / 2); var cmd = new CameraCommand(INF_TIME, targetpos, true, targetpos); //Thanks to the wonders of references this keeps the target up to date cmd.spriteTarget = target; this.commands.Add(cmd); }
/// <summary> /// Moves the Camera in a straight line to a point over a number of seconds /// </summary> /// <param name="target"><see cref="Graphics.IDrawable"/>Target to be moved to</param> /// <param name="time">Time in seconds</param> public void MoveTo(Graphics.IDrawable target, double time) { Vector2 targetpos = new Vector2(); targetpos.X = (target.Position.X - Camera.Bounds.Width / 2 + target.Area.Width / 2); targetpos.Y = (target.Position.Y - Camera.Bounds.Height / 2 + target.Area.Height / 2); var cmd = new CameraCommand(time, targetpos, false, Vector2.Zero); //Thanks to the wonders of references this keeps the target up to date cmd.spriteTarget = target; this.commands.Add(cmd); }
public void StopCurrentCommand() { this.current.movementTimer = -1; //Swap out the current to the next command if (commands.Count > 0) { //Pop the next command in line InMotion = false; current = commands[0]; commands.RemoveAt(0); //If the target start is null then start where the Camera is now current.targetStart = (!current.targetStartDiffer ? this.Camera.Position : current.targetStart); this.Camera.Position = current.targetStart; } }
public void Update(GameTime gameTime) { //An infinite timed command is used until another command is issued if (current.movementTimerTotal == INF_TIME) { this.Camera.Position = current.target; } //If the target is a sprite, update the target position if (current.spriteTarget != null) { current.target.X = (current.spriteTarget.Position.X - Camera.Bounds.Width / 2 + current.spriteTarget.Area.Width / 2); current.target.Y = (current.spriteTarget.Position.Y - Camera.Bounds.Height / 2 + current.spriteTarget.Area.Height / 2); } if (commands.Count > 0 && current.movementTimer <= 0) { //Pop the next command in line InMotion = false; current = commands[0]; commands.RemoveAt(0); //If the target start is Vector.Zero then start where the Camera is now current.targetStart = (!current.targetStartDiffer ? Camera.Position : current.targetStart); this.Camera.Position = current.targetStart; } if (current.movementTimer > 0) { current.movementTimer -= gameTime.ElapsedGameTime.TotalSeconds; if (current.movementTimer < 0) { //Corrects for any calculation error that occurred during the movement Camera.Position = current.target; } else { //Get the Percentage of how far the movement should be through Camera.Position = new Vector2((float)((current.target.X - current.targetStart.X) * (current.movementTimerTotal - current.movementTimer) / current.movementTimerTotal + current.targetStart.X), (float)((current.target.Y - current.targetStart.Y) * (current.movementTimerTotal - current.movementTimer) / current.movementTimerTotal + current.targetStart.Y)); InMotion = true; } } }