:::: MENU ::::
Monthly Archives: December 2013

Pucaj Update – Shooting, Major Refactoring

Refactoring

I’ve been slacking off a bit lately, but not long after my previous update, I did add some improvements to the existing code.

The first thing I did was to apply a sort of MVC pattern to the code structure. For this, I’ve created a Level class and paired it with LevelRenderer and LevelController classes. The Level class is the base model in this situation. It contains all the properties required to represent a single level in my future game. As such, it’s always instantiated first.

The Level class contains the player, which is an instance of a Tank, which, in turn, contains a Weapon. Since Tank and Weapon are still relatively simple, they don’t have dedicated Controllers or Renderers yet.

Once an instance of a Level is created, the LevelRenderer and LevelController are created by passing in this instance to their constructors. The renderer is in charge of drawing the level, while the controller handles updating everything related to the level. This means I actually took out part of the code in the render method and placed it into a new update method. This makes the game loop more akin to the standard loop I’m familiar from my stuff in XNA. There’s initialization/creation, updating, drawing and destruction.

I decided to make my level Orthographic camera much simpler. Since levels should be sized in tiles, I  made it’s inner coordinate system an 8×8 grid, simply to avoid dealing with complex calculations in the future. The level is also drawn in a separate gl viewport, again, to make things simpler. This way, I can draw the level without worrying about drawing the interface or anything like that – the interface will be drawn within the global viewport.

With that, resizing is now working much better. The graphics scale with window size and resolution and follow an internal, virtual resolution to keep things simple.

Shooting

I’ve also added basic shooting to the basic weapon. My initial plan was to make a bullet factory/manager which would be initialized at level creation and handle all the bullets currently live within the level. After some time spent badly implementing this, I realized there’s no need for that. Since the game will be turn based, it would probably be much better to simply have each weapon handle their own ammo. Firing will simply be an effect to show on the screen, while the actual damage dealing, aiming, hitting and missing will be handled by math in the background.

Of course, there’s no real turn based system implemented yet, so the basic weapon now simply shoots on whichever part of the screen I double-tap. The double-tap is actually faked to. The controller simply keeps track of how long it’s been since I last tapped the screen, and fires a bullet if conditions are set. Because of that, it’s possible to spam bullets if you just keep tapping.

Screens

Finally, I started using the screen system libGDX offers through the com.badlogic.gdx.Screen namespace. This is done by creating the initial GameScreen class and setting it to implement the Screen class from this namespace.

All the bottom-tier logic was moved from the main Game class to this one, making the Game class extremely simple now:

import com.badlogic.gdx.Game;

public class PucajGame extends Game {

	@Override
	public void create() {
        setScreen(new GameScreen());
	}
}

setScreen will allow me to switch from main menu, to options menu, to game, etc. very easily, so I’m glad I started using it this early.

GameScreen also implements the  InputProcessor class, which brings with it methods required to handle user input, such as keyDown, keyUp, keyTyped, touchDown, touchUp, touchDragged, mouseMoved and scrolled. Since GameScreen is supposed to be the screen where the currently active level is handled, these methods communicate with LevelController to delegate the input to the level. On the other hand, Screen methods (render, resize) communicate with LevelRenderer to handle the drawing.

The Result

The game now scales as I resize the window, I can move and shoot and it looks pretty cool (at least from my point of view). Here it is in action…

As usual, you can see every line of code in my repository – Pucaj on GitHub.