Hy everyone, so I made this Camera class, and can't find nothing wrong with it,
so please take a quick look at it:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace Killer
{
class Camera
{
protected float zoom;
protected Matrix transform;
protected Viewport viewport;
Vector2 center;
public Matrix Transform
{
get { return transform; }
set { transform = value; }
}
public Camera(Viewport viewport)
{
zoom = 1.0f;
this.viewport = viewport;
}
public void Update()
{
// Center - center of window, with player in center of window, window size: 800x600
center = new Vector2(Player.playerPos.X + (EntityAnimation.rectangle.Width / 2) - 400,
Player.playerPos.Y + (EntityAnimation.rectangle.Height / 2) - 300);
// center.X = playerposition.X + frameWidth/2 - 400
// center.X = 100 + 24 - 400
// center.Y = playerposition.Y + frameHeight/2 - 300
// center.Y = 100 + 24 - 300
transform = Matrix.CreateScale(new Vector3(zoom, zoom, 0))
* Matrix.CreateTranslation(new Vector3(-center.X, -center.Y, 0));
}
}
}
I putted some comments so you would know what values are at first when game is freshly run, now in my
Game1.cs (main XNA game file) I made a simple global variable:
Camera cam;
And in Game1.cs Initialize() method:
cam = new Camera(GraphicsDevice.Viewport);
and last but not least, here are my 2 draw methods in Game1.cs:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.White);
spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, cam.Transform);
// Player
player.PlayerAnimation.PlayerDraw(spriteBatch);
spriteBatch.End();
DrawHud(gameTime);
base.Draw(gameTime);
}
protected void DrawHud(GameTime gameTime)
{
spriteBatch.Begin();
// Player Related Drawing --------------------------------------------------------------------
player.playerStamina.DrawStatHelpers(spriteBatch, gameTime); // Stamina Help Messages, also Player.X and Player.Y
player.playerStamina.DrawBar(spriteBatch); // Stamina Health Bar
//--------------------------------------------------------------------------------------------
spriteBatch.End();
}
Now, when I build and run, I cant see my player, just my HUD,
BUT if I put player.PlayerAnimation.PlayerDraw(spriteBatch); in DrawHud() I can see him.
I dont know what is wrong with my Camera, please help if you can :)