Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using Microsoft.Xna.Framework;
- using Microsoft.Xna.Framework.Audio;
- using Microsoft.Xna.Framework.Content;
- using Microsoft.Xna.Framework.GamerServices;
- using Microsoft.Xna.Framework.Graphics;
- using Microsoft.Xna.Framework.Input;
- using Microsoft.Xna.Framework.Media;
- namespace TestGame
- {
- static class Program
- {
- static void Main(string[] args)
- {
- using (TestGame game = new TestGame())
- {
- game.Run();
- }
- }
- }
- public class Player
- {
- public Player(Texture2D texture, Vector2 position, Camera2D camera, Vector2 screenDimensions)
- {
- Texture = texture;
- Position = position;
- ScreenDimension = screenDimensions;
- Camera = camera;
- }
- protected void Move(float x, float y)
- {
- Position.X += x;
- if ((Position.X < Camera.Min.X))
- Position.X = Camera.Min.X;
- if ((Position.X + Width) > Camera.Max.X)
- Position.X = Camera.Max.X - Width;
- float centerX = Position.X + (Width / 2);
- if (centerX > (Camera.Min.X + (ScreenDimension.X / 2)))
- {
- if (centerX < (Camera.Max.X - (ScreenDimension.X / 2)))
- {
- Camera.Move(-x, 0);
- }
- else
- {
- Camera.SetPosition(-(Camera.Max.X - ScreenDimension.X), Camera.Position.Y);
- }
- }
- else
- {
- Camera.SetPosition(Camera.Min.X, Camera.Position.Y);
- }
- Position.Y += y;
- if ((Position.Y < Camera.Min.Y))
- Position.Y = Camera.Min.Y;
- if ((Position.Y + Texture.Width) > Camera.Max.Y)
- Position.Y = Camera.Max.Y - Height;
- float centerY = Position.Y + (Height / 2);
- if (centerY > (Camera.Min.Y + (ScreenDimension.Y / 2)))
- {
- if (centerY < (Camera.Max.Y - (ScreenDimension.Y / 2)))
- {
- Camera.Move(0, -y);
- }
- else
- {
- Camera.SetPosition(Camera.Position.X, -(Camera.Max.Y - ScreenDimension.Y));
- }
- }
- else
- {
- Camera.SetPosition(Camera.Position.X, Camera.Min.Y);
- }
- }
- public float Width { get { return Texture.Width; } }
- public float Height { get { return Texture.Height; } }
- public Camera2D Camera;
- public Texture2D Texture;
- public Vector2 Position;
- public Vector2 ScreenDimension;
- public void Update(GameTime gameTime)
- {
- if (Keyboard.GetState().IsKeyDown(Keys.Left))
- Move(-gameTime.ElapsedGameTime.Milliseconds, 0);
- if (Keyboard.GetState().IsKeyDown(Keys.Right))
- Move(gameTime.ElapsedGameTime.Milliseconds, 0);
- if (Keyboard.GetState().IsKeyDown(Keys.Up))
- Move(0, -gameTime.ElapsedGameTime.Milliseconds);
- if (Keyboard.GetState().IsKeyDown(Keys.Down))
- Move(0, gameTime.ElapsedGameTime.Milliseconds);
- }
- public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
- {
- spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearClamp, DepthStencilState.None, RasterizerState.CullCounterClockwise, null, Camera.Transformation);
- // Static object to show that the camera is moving
- spriteBatch.Draw(Texture, new Vector2(0, (ScreenDimension.Y / 2) - (Texture.Height / 2)), Color.White);
- spriteBatch.Draw(Texture, Position, Color.White);
- spriteBatch.Draw(Texture, new Vector2(Camera.Max.X - (Texture.Width), (ScreenDimension.Y / 2) - (Texture.Height / 2)), Color.White);
- spriteBatch.End();
- }
- }
- public class Camera2D
- {
- public Camera2D(Vector2 min, Vector2 max)
- {
- Min = min;
- Max = max;
- Position = Vector2.Zero;
- Scale = 1f;
- Rotation = 0;
- }
- public void Move(float x, float y)
- {
- position.X += x;
- position.Y += y;
- }
- public void Zoom(float value) { Scale = Math.Max(Scale + value, 0.01f); }
- public void Rotate(float value) { Rotation += value; }
- public void SetPosition(float x, float y) { position.X = x; position.Y = y; }
- private Vector2 min, max, position;
- public Vector2 Min { get { return min; } set { min = value; } }
- public Vector2 Max { get { return max; } set { max = value; } }
- public Vector2 Position
- {
- get { return position; }
- protected set { position = value; }
- }
- public float Rotation { get; protected set; }
- public float Scale { get; protected set; }
- public Matrix Transformation
- {
- get
- {
- return
- Matrix.CreateScale(Scale) *
- Matrix.CreateRotationZ(MathHelper.ToRadians(Rotation)) *
- Matrix.CreateTranslation(Position.X, Position.Y, 0);
- }
- }
- }
- public class TestGame : Microsoft.Xna.Framework.Game
- {
- GraphicsDeviceManager graphics;
- SpriteBatch spriteBatch;
- Player player;
- Camera2D camera;
- public TestGame()
- {
- graphics = new GraphicsDeviceManager(this);
- graphics.PreferredBackBufferWidth = 1280;
- graphics.PreferredBackBufferHeight = 720;
- Content.RootDirectory = "Content";
- }
- protected override void Initialize()
- {
- base.Initialize();
- }
- protected override void LoadContent()
- {
- spriteBatch = new SpriteBatch(GraphicsDevice);
- camera = new Camera2D(Vector2.Zero, new Vector2(2560, 720));
- player = new Player(Content.Load<Texture2D>("player"), new Vector2(100, 0), camera, new Vector2(graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight));
- }
- protected override void UnloadContent()
- {
- }
- protected override void Update(GameTime gameTime)
- {
- if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
- this.Exit();
- this.Window.Title = player.Position.ToString() + " / " + camera.Position.ToString();
- player.Update(gameTime);
- base.Update(gameTime);
- }
- protected override void Draw(GameTime gameTime)
- {
- GraphicsDevice.Clear(Color.CornflowerBlue);
- player.Draw(spriteBatch, gameTime);
- base.Draw(gameTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment