Pages

Sunday, February 9, 2014

Programming a Role Playing Game - Part 2: Quests and Journal

This series started with the Player class:

Programming a Role Playing Game - Part 1: The Player

In the first part we created a Journal object. Now we need to implement the class.


package com.atsiitech.rpg.models.journal;

import com.badlogic.gdx.utils.Array;

public class Journal {

   private Array<Quest> quests;
 
   public Journal() {
      quests = new Array<Quest>();
   }

   public Array<Quest> getQuests() { return quests; }

   public void addQuest(Quest quest) {
      quests.add(quest);
   }
}


I put it in the models.journal package to keep everything organized. The journal at this stage is pretty simple, we will deal with the UI in another class. Here we will make an Array of the Quest class. You could add a Quest to the array by calling getQuests().add() but I made a convenience method for readability.

And now we have to create the Quest class.


package com.atsiitech.rpg.models.journal;

public class Quest {
 
   private String title;
   private String description;
   private int priority;

   private boolean finished;
 
   public Quest(String title, String description, int priority) {
  
      this.title = title;
      this.description = description;
      this.priority = priority;
  
      finished = false;
   }
 
   public String getTitle() { return title; }
   public String getDescription() { return description; }
   public int getPriority() { return priority; }
   public void setPriority(int priority) { this.priority = priority; }
 
   public boolean isFinished() { return finished; }
   public void finish() { finished = true; }
 
}


The class is pretty self explanatory if you know Java or other object-oriented languages already. The attribute priority is used in sorting. And attribute finished is also used to move the quest to the "Done Quests" -tab in the UI.

We can now add some test quests to the Journal from the Player class.



Player.journal.addQuest(
   new Quest(
      "Clear the basement",
      "The basement is filled with rats, you must clear them or this is not considered RPG",
      1
   )
);


Since the journal in the Player class is static, we must access it like that. This would be the initial quest that shows in the player's journal, with title, description and the 1st priority.

That's all for now, more to come. Thanks for reading!

Code formatted with hilite.me

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