2D Camera Class

From XNAWiki
Jump to: navigation, search


The camera supports translation, rotation, and zoom.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
 
public class Camera2d : GameComponent, IGameObject, IMovable
    {
        protected string _id;
        protected string _name;
 
        protected Vector2 _position;
        protected Vector2 _velocity;
        protected float _rotation;
 
        protected float _zoom;
        protected Matrix _transform;
 
        public float Zoom
        {
            get { return _zoom; }
            set { _zoom = value; }
        }
 
        public Matrix Transform
        {
            get
            {
                _transform = Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0)) *
                    Matrix.CreateRotationZ(Rotation) *
                    Matrix.CreateScale(new Vector3(Zoom, Zoom, 0)) *
                    Matrix.CreateTranslation(new Vector3(Game.GraphicsDevice.Viewport.Width * 0.5f, Game.GraphicsDevice.Viewport.Height * 0.5f, 0));
 
                return _transform;
            }
        }
 
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="game"></param>
        public Camera2d(Game game)
            : base(game)
        {
            _id = Guid.NewGuid().ToString();
            _name = "2D Camera";
 
            _zoom = 1.0f;
            _rotation = 0.0f;
            _position = Vector2.Zero;
            _velocity = Vector2.Zero;
        }
 
        public override void Update(GameTime gameTime)
        {
            // add velocity
            Position = Vector2.Add(Position, Velocity);
 
            base.Update(gameTime);
        }
 
        #region IGameObject Members
 
        public string ID
        {
            get
            {
                return _id;
            }
            set
            {
                _id = value;
            }
        }
 
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }
 
        #endregion
 
        #region IMovable Members
 
        public Vector2 Position
        {
            get
            {
                return _position;
            }
 
            set
            {
                // notify subscribers of change
                OnPositionChanged(this, new PositionChangedEventArgs() { OldPosition = _position, NewPosition = value });
                // update the value
                _position = value;
            }
        }
 
        protected void OnPositionChanged(object sender, PositionChangedEventArgs e)
        {
            if(PositionChanged != null)
                PositionChanged(sender, e);
        }
 
        public Vector2 Velocity
        {
            get
            {
                return _velocity;
            }
            set
            {
                // notify subscribers
                OnVelocityChanged(this, new VelocityChangedEventArgs() { OldVelocity = _velocity, NewVelocity = value });
                // update value
                _velocity = value;
            }
        }
 
        protected void OnVelocityChanged(object sender, VelocityChangedEventArgs e)
        {
            if (VelocityChanged != null)
                VelocityChanged(sender, e);
        }
 
        public float Rotation
        {
            get
            {
                return _rotation;
            }
            set
            {
                // notify subscribers
                OnRotationChanged(this, new RotationChangedEventArgs() { OldRotation = _rotation, NewRotation = value });
                // update value
                _rotation = value;
            }
        }
 
        protected void OnRotationChanged(object sender, RotationChangedEventArgs e)
        {
            if (RotationChanged != null)
                RotationChanged(sender, e);
        }
 
        public event PositionChangedEventHandler PositionChanged;
 
        public event VelocityChangedEventHandler VelocityChanged;
 
        public event RotationChangedEventHandler RotationChanged;
 
        #endregion
}

The IMovable Interface:

public delegate void PositionChangedEventHandler(object sender, PositionChangedEventArgs args);
    public delegate void VelocityChangedEventHandler(object sender, VelocityChangedEventArgs args);
    public delegate void RotationChangedEventHandler(object sender, RotationChangedEventArgs args);
 
    interface IMovable
    {
        Vector2 Position { get; set; }
        Vector2 Velocity { get; set; }
        float Rotation { get; set; }
 
        event PositionChangedEventHandler PositionChanged;
        event VelocityChangedEventHandler VelocityChanged;
        event RotationChangedEventHandler RotationChanged;
    }
 
    public class PositionChangedEventArgs : EventArgs
    {
        public Vector2 OldPosition { get; set; }
        public Vector2 NewPosition { get; set; }
    }
 
    public class VelocityChangedEventArgs : EventArgs
    {
        public Vector2 OldVelocity { get; set; }
        public Vector2 NewVelocity { get; set; }
    }
 
    public class RotationChangedEventArgs : EventArgs
    {
        public float OldRotation { get; set; }
        public float NewRotation { get; set; }
    }

The IGameObject Interface:

public interface IGameObject
    {
        string ID { get; set; }
        string Name { get; set; }
    }