Pages

Saturday, February 8, 2014

Programming a Role Playing Game - Part 1: The Player

This case study is much like a personal journal I'm writing while programming the engine for a turn based role playing game. It is not a tutorial but I hope you will find it useful nonetheless.

I'm using LibGDX as the framework (programmed with Java), Tiled as the map editor and some placeholder graphics I made. The project loosely follows MVC pattern as explained in The MVC pattern tutorial by Tamas Jano.

Code is formatted with hilite.me

DISCLAIMER: I'm prototyping as I go so some design choices and practices are not the best ones. Feel free to comment. :)


Where to start


Building a turn based role playing game is as complex as you make it. That's why I keep it simple for now. I start with the Player class that will grow with these articles. To follow the MVC pattern, I place it in the models package.


Stats

 

RPG characters must have stats; hit points, attack, etc. so let's give them to our Player:


package com.atsiitech.rpg.models;

public class Player {

   private String name;
   private int hpMax; // Maximum amount of hitpoints
   private int hitpoints; // Current amount of hitpoints
   private int attack; // Could be also damage, base attack  
}


I'm omitting the other stats for now because this is a prototype. You should initialize them in the constructor.

RPG characters usually have an inventory and journal as well so let's create instances of those and implement them later.


...

public static final Inventory inventory = new Inventory();
public static final Journal journal = new Journal();

...


I made them static so they are accessible to the game engine. That way we avoid passing the player as a reference back and forth. It's an issue to debate about but after a lot of spaghetti code I decided to do it like this.


Sprite

 


private Sprite sprite;
 
   ...
 
public Player() {
 
   ...
 
   sprite = new Sprite(
      new Texture(
         Gdx.files.internal("data/player_texture.png")
      )
   );

   ...
}

Nothing new here if you already know what it is. We just instantiate a new Sprite with a texture found in the assets/data folder in the Android project of LibGDX. We'll change this into an animation later on but it could be a static image too if that is what you fancy.


Positions

 


...

private Vector2 position;
private Vector2 locationOnMap;
private Facing facing;

...



The position -attribute is the one that we use to draw our sprite on the screen and locationOnMap is used for calculating collisions and other map-related data. Facing is an enumerator that we will implement next. It basically just tells what way our character is facing.


Enums


For the facing we will create Facing.java file. I like to keep them in the models.enums package like so:


package com.atsiitech.rpg.models.enums;

public enum Facing {
   UP,
   DOWN,
   LEFT,
   RIGHT
}


Enums are a really convenient way to tell the state of objects, the game and modes so we'll have a lot of those.

That's all for now. I will write more on the Player class later with rendering, animation, inventory, journal, etc. Thanks for reading. :)

Edit: Part 2 - Journal and Quests

No comments:

Post a Comment