chris.software     development     reviews     feed

Making a Player Class

One of the things I really wanted to accomplish this week was creating a player class to take over my bouncing box, as the code was pretty much my engine, which is a bunch of global things organized in a way that makes sense to me, and my XNA Game1 class, which housed the code for pretty much everything else - initialization, drawing, the debug view drawing, camera setup, etc… so it was a mess, a slightly organized one as I kept all the different parts separated by whitespace, but a mess nonetheless. So I set out to pull out the bouncing box - leave the platform where it is in the Game1 class, but everything involving my dynamic Farseer Fixture needed to move.

I first created an InputComponent, and wanted this component to handle player input that affects the box, which in my small project is jumping, or floating. I set out to do this, and had some code that checks if the jumping keys or buttons in the Configuration class are being pressed. I built the project for Windows just fine, but then realized that I was only compiling the Key code in the Input reader if we were on a Windows platform, but doing the check both ways. So I ripped out that stuff so that I just return false if I’m checking the keyboard on an Xbox.

Then, I somehow managed to forget about my actual InputComponent and moved on to the PhysicsComponent, which basically is used to create a polygon, throw it into the Farseer simulation, and then update the game world position on every update for my player. That worked perfectly, although the drawing order of the code screwed up my debug view and I ripped that out into the Physics part of the engine and made that a drawable component as well, which draws the debug view last. So my player class instantiates an input component and a physics component, and then uses just the physics component (for now) to update its position. It also loads its texture and draws it, which is pretty standard XNA stuff.

The next thing I have to do is make it actually use its InputComponent, and then I will move onto using Tiled and TiledLib to draw the placeholder platform like it’s loading a whole map, and then I can decide how I want the flow of the game to be using all of my new classes instead of hard-coded stuff in the main class. I also need to create a particle effect component so that I can easily attach particle effects to objects, which will instantly make things a lot cooler, although I still need to learn how to create my own effects with the Mercury editor.