Guest User

XNA 2D Camera + Player Tracking

a guest
Apr 5th, 2013
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Audio;
  6. using Microsoft.Xna.Framework.Content;
  7. using Microsoft.Xna.Framework.GamerServices;
  8. using Microsoft.Xna.Framework.Graphics;
  9. using Microsoft.Xna.Framework.Input;
  10. using Microsoft.Xna.Framework.Media;
  11.  
  12. namespace TestGame
  13. {
  14.     static class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.             using (TestGame game = new TestGame())
  19.             {
  20.                 game.Run();
  21.             }
  22.         }
  23.     }
  24.  
  25.     public class Player
  26.     {
  27.         public Player(Texture2D texture, Vector2 position, Camera2D camera, Vector2 screenDimensions)
  28.         {
  29.             Texture = texture;
  30.             Position = position;
  31.             ScreenDimension = screenDimensions;
  32.             Camera = camera;
  33.         }
  34.  
  35.         protected void Move(float x, float y)
  36.         {
  37.  
  38.             Position.X += x;
  39.  
  40.             if ((Position.X < Camera.Min.X))
  41.                 Position.X = Camera.Min.X;
  42.  
  43.             if ((Position.X + Width) > Camera.Max.X)
  44.                 Position.X = Camera.Max.X - Width;
  45.  
  46.             float centerX = Position.X + (Width / 2);
  47.  
  48.             if (centerX > (Camera.Min.X + (ScreenDimension.X / 2)))
  49.             {
  50.                 if (centerX < (Camera.Max.X - (ScreenDimension.X / 2)))
  51.                 {
  52.                     Camera.Move(-x, 0);
  53.                 }
  54.                 else
  55.                 {
  56.                     Camera.SetPosition(-(Camera.Max.X - ScreenDimension.X), Camera.Position.Y);
  57.                 }
  58.             }
  59.             else
  60.             {
  61.                 Camera.SetPosition(Camera.Min.X, Camera.Position.Y);
  62.             }
  63.  
  64.             Position.Y += y;
  65.  
  66.             if ((Position.Y < Camera.Min.Y))
  67.                 Position.Y = Camera.Min.Y;
  68.  
  69.             if ((Position.Y + Texture.Width) > Camera.Max.Y)
  70.                 Position.Y = Camera.Max.Y - Height;
  71.  
  72.             float centerY = Position.Y + (Height / 2);
  73.  
  74.             if (centerY > (Camera.Min.Y + (ScreenDimension.Y / 2)))
  75.             {
  76.                 if (centerY < (Camera.Max.Y - (ScreenDimension.Y / 2)))
  77.                 {
  78.                     Camera.Move(0, -y);
  79.                 }
  80.                 else
  81.                 {
  82.                     Camera.SetPosition(Camera.Position.X, -(Camera.Max.Y - ScreenDimension.Y));
  83.                 }
  84.             }
  85.             else
  86.             {
  87.                 Camera.SetPosition(Camera.Position.X, Camera.Min.Y);
  88.             }
  89.         }
  90.  
  91.         public float Width { get { return Texture.Width; } }
  92.         public float Height { get { return Texture.Height; } }
  93.  
  94.         public Camera2D Camera;
  95.         public Texture2D Texture;
  96.         public Vector2 Position;
  97.         public Vector2 ScreenDimension;
  98.  
  99.         public void Update(GameTime gameTime)
  100.         {
  101.             if (Keyboard.GetState().IsKeyDown(Keys.Left))
  102.                 Move(-gameTime.ElapsedGameTime.Milliseconds, 0);
  103.  
  104.             if (Keyboard.GetState().IsKeyDown(Keys.Right))
  105.                 Move(gameTime.ElapsedGameTime.Milliseconds, 0);
  106.  
  107.             if (Keyboard.GetState().IsKeyDown(Keys.Up))
  108.                 Move(0, -gameTime.ElapsedGameTime.Milliseconds);
  109.  
  110.             if (Keyboard.GetState().IsKeyDown(Keys.Down))
  111.                 Move(0, gameTime.ElapsedGameTime.Milliseconds);
  112.  
  113.         }
  114.  
  115.         public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
  116.         {
  117.             spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Camera.Transformation);
  118.  
  119.             // Static object to show that the camera is moving
  120.             spriteBatch.Draw(Texture, new Vector2(0, (ScreenDimension.Y / 2) - (Texture.Height / 2)), Color.White);
  121.             spriteBatch.Draw(Texture, Position, Color.White);
  122.             spriteBatch.Draw(Texture, new Vector2(Camera.Max.X - (Texture.Width), (ScreenDimension.Y / 2) - (Texture.Height / 2)), Color.White);
  123.  
  124.             spriteBatch.End();
  125.         }
  126.     }
  127.  
  128.     public class Camera2D
  129.     {
  130.         public Camera2D(Vector2 min, Vector2 max)
  131.         {
  132.             Min = min;
  133.             Max = max;
  134.             Position = Vector2.Zero;
  135.             Scale = 1f;
  136.             Rotation = 0;
  137.         }
  138.  
  139.         public void Move(float x, float y)
  140.         {
  141.             position.X += x;
  142.             position.Y += y;
  143.         }
  144.  
  145.         public void Zoom(float value) { Scale = Math.Max(Scale + value, 0.01f); }
  146.         public void Rotate(float value) { Rotation += value; }
  147.         public void SetPosition(float x, float y) { position.X = x; position.Y = y; }
  148.  
  149.         private Vector2 min, max, position;
  150.  
  151.         public Vector2 Min { get { return min; } set { min = value; } }
  152.         public Vector2 Max { get { return max; } set { max = value; } }
  153.  
  154.         public Vector2 Position
  155.         {
  156.             get { return position; }
  157.             protected set { position = value; }
  158.         }
  159.  
  160.         public float Rotation { get; protected set; }
  161.         public float Scale { get; protected set; }
  162.  
  163.         public Matrix Transformation
  164.         {
  165.             get
  166.             {
  167.                 return
  168.                     Matrix.CreateScale(Scale) *
  169.                     Matrix.CreateRotationZ(MathHelper.ToRadians(Rotation)) *
  170.                     Matrix.CreateTranslation(Position.X, Position.Y, 0);
  171.             }
  172.         }
  173.     }
  174.  
  175.     public class TestGame : Microsoft.Xna.Framework.Game
  176.     {
  177.         GraphicsDeviceManager graphics;
  178.         SpriteBatch spriteBatch;
  179.  
  180.         Player player;
  181.         Camera2D camera;
  182.  
  183.         public TestGame()
  184.         {
  185.             graphics = new GraphicsDeviceManager(this);
  186.            
  187.             graphics.PreferredBackBufferWidth = 1280;
  188.             graphics.PreferredBackBufferHeight = 720;
  189.  
  190.             Content.RootDirectory = "Content";
  191.  
  192.         }
  193.  
  194.         protected override void Initialize()
  195.         {
  196.             base.Initialize();
  197.         }
  198.  
  199.         protected override void LoadContent()
  200.         {
  201.             spriteBatch = new SpriteBatch(GraphicsDevice);
  202.  
  203.             camera = new Camera2D(Vector2.Zero, new Vector2(2560, 720));
  204.             player = new Player(Content.Load<Texture2D>("player"), new Vector2(100, 0), camera, new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight));
  205.         }
  206.  
  207.         protected override void UnloadContent()
  208.         {
  209.         }
  210.  
  211.         protected override void Update(GameTime gameTime)
  212.         {
  213.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
  214.                 this.Exit();
  215.  
  216.             this.Window.Title = player.Position.ToString() + " / " + camera.Position.ToString();
  217.  
  218.             player.Update(gameTime);
  219.  
  220.             base.Update(gameTime);
  221.         }
  222.  
  223.         protected override void Draw(GameTime gameTime)
  224.         {
  225.             GraphicsDevice.Clear(Color.CornflowerBlue);
  226.  
  227.             player.Draw(spriteBatch, gameTime);
  228.  
  229.             base.Draw(gameTime);
  230.         }
  231.     }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment