It's very common for software developers to share code between each other if one person figured out
how to do one thing, why should everyone else have to figure it out for themselves?
When someone writes software that they want to share with the world to allow others to use it and expand
upon it?
This is called open source software.
There's a wide range of publicly available code on the Internet that programmers have uploaded, and
it's in your best interest to utilize this code once in a while.
We're going to be utilizing a piece of open source software in order to implement the camera for our
game, the code that will be using can be found on GitHub, which is a very common site that hosts open
source repositories.
If you go to this, you well at the very top, you will end at the same site that I'm on right now.
This repo camera is a camera implementation specifically made for Muno game, and we can see over here
that it's under the MIT license, which means that we're free to use it in our own projects.
Whenever you arrive at a GitHub page like this, if you scroll down, you'll typically see a read me
document.
Essentially, what the rehme does is it provides a bunch of sample code, useful info and anything that
you might need to know about this project or this repo.
Feel free to take a quick look through this page, but as soon as we have a good idea of what we're
working with, let's go ahead and add this code to our game for Munno game.
I find that the easiest way to include external packages is to utilize Nugget.
What Nugget does is it allows developers to easily share packages with other developers, and it makes
it very easy for us to grab this camera code.
On this GitHub page, we can see that there's actually a nugget link down in the README, which I can
just click on, and it should take us to the Nugget dog site.
Once here, we can see a little bit more information.
But what we're interested in is this package manager command.
I'm going to copy this.
And back in Visual Studio, I'm going to go up to Tool's.
Newgate, package manager and then package manager console.
This will open up this window down here at the bottom.
Which allows us to type in those Newgate commands, like the same one that I just copied.
So what you're gonna do is right here next to this RPM, you're going to paste in the command and press
enter.
And is this going to install camera to our project and it's done, it's very quick, it only took one
point three or four seconds.
Once it's done, we can verify that the package installed correctly.
If we go over here up to this dependencies folder and opening this, we can go down two packages and
we will see that camera zero five oh, which is the current version as I'm recording this is installed
to our project.
So now we're able to use it.
And at this point we're ready to start implementing this code into our game.
The first step is to call this package using one of these using statements.
So up at the top, I'm going to type in using camera.
So now we can use all those methods within our own project, then we need to initialize a camera into
our game, a camera object will put right here.
I'm just going to call this camera camera.
So this camera type comes directly from the camera package.
Then next, we need to initialize it in the initialize method after this stuff, I can say this that
camera equals new camera, uppercase C and we need to pass at the graphics device, which you can find
with underscore graphics, dot capital, G graphics device.
So now our camera is initialized in our game, we now need to update it every frame.
So down in our update method, I'm going to put this down here.
I'm just going to say this camera dot update and we need to pass it gametime like most other update
methods.
And the last thing that we need to do is we need to attach the camera to our Sprite batch begin so we're
here where we have Sprite batch begin.
We just need to pass it this camera.
And that's all we need to do to get a camera functioning in our game.
If I were to save and then I'm going to run the project now.
We'll see how this looks.
So right off the bat, the game doesn't really look too different, but you'll notice that the portion
of the map that we were looking at is quite different from before.
This is because now we're viewing the game through the lens of the camera.
All we need to do is point the camera at the player and have it update its position every frame to match
the player's position, because now our Sprite badge is being considered through the lens of this camera.
So to point this at the player, we can go up to our update function and before we call this camera
to update, we can do this, that camera, that position, we're just going to set the position of our
camera equal to player position.
Now the camera is more focused on the player, if I save and run, we will see.
That we're now focused on the player and if I move around, the camera follows us.
However, you'll notice that there is a bit of an offset issue where the camera isn't exactly centered
on the player, this is because the player's position is actually set to be this upper left hand corner
of the player's sprite, which is the same sort of issue that we've had before.
We're going to adjust this offset in the next lecture where we'll also be adding animations for our
player.