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

No comments:

Post a Comment