Simple camera
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Input;
|
||||
|
||||
namespace DepthsBelow
|
||||
{
|
||||
class Camera
|
||||
{
|
||||
public float Zoom;
|
||||
public Matrix Transform;
|
||||
public Vector2 Position;
|
||||
public float Rotation;
|
||||
|
||||
public float Speed;
|
||||
|
||||
private Core core;
|
||||
|
||||
public Camera(Core core)
|
||||
{
|
||||
this.core = core;
|
||||
|
||||
Zoom = 1.0f;
|
||||
Rotation = 0.0f;
|
||||
Position = Vector2.Zero;
|
||||
Speed = 5;
|
||||
}
|
||||
|
||||
public void Update(GameTime gameTime)
|
||||
{
|
||||
MouseState ms = Mouse.GetState();
|
||||
|
||||
if (ms.X < 20)
|
||||
Position.X -= Speed;
|
||||
if (ms.X > core.GraphicsDevice.Viewport.Width - 20)
|
||||
Position.X += Speed;
|
||||
|
||||
if (ms.Y < 20)
|
||||
Position.Y -= Speed;
|
||||
if (ms.Y > core.GraphicsDevice.Viewport.Height - 20)
|
||||
Position.Y += Speed;
|
||||
|
||||
Transform =
|
||||
Matrix.CreateTranslation(new Vector3(-Position.X, -Position.Y, 0))
|
||||
* Matrix.CreateRotationZ(Rotation)
|
||||
* Matrix.CreateScale(Zoom)
|
||||
//* Matrix.CreateTranslation(new Vector3(
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@ namespace DepthsBelow
|
||||
GraphicsDeviceManager graphics;
|
||||
SpriteBatch spriteBatch;
|
||||
|
||||
Camera camera;
|
||||
Soldier soldier;
|
||||
|
||||
public Core()
|
||||
@@ -28,7 +29,7 @@ namespace DepthsBelow
|
||||
|
||||
graphics.PreferredBackBufferWidth = 1280;
|
||||
graphics.PreferredBackBufferHeight = 720;
|
||||
graphics.IsFullScreen = true;
|
||||
graphics.IsFullScreen = false;
|
||||
|
||||
this.IsMouseVisible = true;
|
||||
}
|
||||
@@ -42,7 +43,7 @@ namespace DepthsBelow
|
||||
protected override void Initialize()
|
||||
{
|
||||
// TODO: Add your initialization logic here
|
||||
|
||||
camera = new Camera(this);
|
||||
base.Initialize();
|
||||
}
|
||||
|
||||
@@ -58,7 +59,6 @@ namespace DepthsBelow
|
||||
// TODO: use this.Content to load your game content here
|
||||
Soldier.LoadContent(this);
|
||||
soldier = new Soldier(this);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -81,6 +81,8 @@ namespace DepthsBelow
|
||||
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
|
||||
this.Exit();
|
||||
|
||||
camera.Update(gameTime);
|
||||
|
||||
// TODO: Add your update logic here
|
||||
soldier.Update(gameTime);
|
||||
|
||||
@@ -95,11 +97,13 @@ namespace DepthsBelow
|
||||
{
|
||||
GraphicsDevice.Clear(Color.CornflowerBlue);
|
||||
|
||||
// TODO: Add your drawing code here
|
||||
spriteBatch.Begin();
|
||||
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend, null, null, null, null, camera.Transform);
|
||||
|
||||
// TODO: Foreach etc...
|
||||
Component.SpriteRenderer rc = soldier.GetComponent<Component.SpriteRenderer>();
|
||||
if (rc != null)
|
||||
rc.Draw(spriteBatch);
|
||||
|
||||
spriteBatch.End();
|
||||
|
||||
base.Draw(gameTime);
|
||||
|
||||
@@ -120,6 +120,7 @@
|
||||
</Reference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="Camera.cs" />
|
||||
<Compile Include="Component\Component.cs" />
|
||||
<Compile Include="Component\GridTransform.cs" />
|
||||
<Compile Include="Core.cs" />
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<PublishUrlHistory />
|
||||
<PublishUrlHistory>publish\</PublishUrlHistory>
|
||||
<InstallUrlHistory />
|
||||
<SupportUrlHistory />
|
||||
<UpdateUrlHistory />
|
||||
|
||||
@@ -35,12 +35,11 @@ namespace DepthsBelow
|
||||
|
||||
public void Update(GameTime gameTime)
|
||||
{
|
||||
KeyboardState ks = Keyboard.GetState();
|
||||
/*KeyboardState ks = Keyboard.GetState();
|
||||
if (ks.IsKeyDown(Keys.Right) && lastKeyboardState.IsKeyUp(Keys.Right))
|
||||
gridTransform.Position.X += 1;
|
||||
lastKeyboardState = ks;
|
||||
lastKeyboardState = ks;*/
|
||||
|
||||
Console.WriteLine(pixelTransform.X + " " + gridTransform.X);
|
||||
if (pixelTransform.Position.X < gridTransform.X * 32)
|
||||
{
|
||||
pixelTransform.Position.X += 2;
|
||||
|
||||
Reference in New Issue
Block a user