我的相机类就是这个,我在我的屏幕类中使用它来渲染地图。我需要能够做到这一点,这样我才能绘制水银粒子引擎粒子,而不会让它看起来像是被拖过屏幕。
using System;
using System.Threading;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using TiledLib;
namespace TiledLib
{
public class Camera
{
public Vector2 Position;
public int Width;
public int Height;
public Vector2 Target;
public Rectangle ClampRect;
public Vector2 Origin = new Vector2(0,0);
float Speed = 0.2f;
public Camera(Viewport vp, Map map)
: this(vp, new Rectangle(0, 0, (map.Width * map.TileWidth), (map.Height * map.TileHeight)))
{ }
public Camera(Viewport vp, Rectangle clampRect)
{
Position = Origin;
Width = vp.Width;
Height = vp.Height;
ClampRect = clampRect;
// Set initial position and target
Position.X = ClampRect.X;
Position.Y = ClampRect.Y;
Target = new Vector2(ClampRect.X, ClampRect.Y);
}
public void Update()
{
// Clamp target to map/camera bounds
Target.X = (int)MathHelper.Clamp(Target.X, ClampRect.X, ClampRect.Width - Width);
Target.Y = (int)MathHelper.Clamp(Target.Y, ClampRect.Y, ClampRect.Height - Height);
// Move camera toward target
Position = Vector2.SmoothStep(Position, Target, Speed);
}
}
}