Hi. I made a quest system, hope you like it.
Code:
package org.intel.game.quests;
import org.intel.game.player.Player;
import org.intel.game.player.controlers.Controller;
/**
* @author Taylor Moon
*
* @since Dec 24, 2012 | Elixrr 2.0
*/
public abstract class Quest {
/**
* Contstructs this quest
*
* @param questName
* The name of this quest
* @param difficulty
* The difficulty of this quest
*/
public Quest(String questName, QuestDifficulty difficulty) {
this.questName = questName;
this.difficulty = difficulty;
}
/** The name of this quest */
protected String questName;
/** The difficulty of this quest */
protected QuestDifficulty difficulty;
/** The player associated with this quest */
protected Player player;
/** The controlers of this quest */
private Controller[] controller;
/** The reward given to the player when this quest has been completed */
private Object[] reward;
/** The progress of this quest at this moment */
private QuestProgress progress;
/**
* Called when this quest is started. If the quest has already been
* finished, or has already been started, the process will terminate
*/
public abstract void start();
/**
* Typically called when the quest has been finished. Gives the player their
* reward after completing this quest
*/
public abstract void giveReward();
/**
* Called when this quest has been finished. This method usally invoked the
* {@code giveReward}, not to be confused with {@code getReward}
*/
public abstract void finish();
/**
* Starts one of the specified controllers in the array of previously set
* controllers. If the player is already using the specified controller, or
* the controller corresponding to the index is equal to <i>null</i>, the
* return value will be <i>null</i>
*
* @param index
* The index in the array of the controller you're attempting to
* start
* @return The controller that was started when this method is called, or
* <i>null</i> reason stated above
*/
public final Controller startController(int index) {
if (player.getControlerManager().getControler()
.equals(controller[index]))
return null;
controller[index].start();
return controller[index];
}
/**
* Sets the controllers that will be used during this quest.
*
* @param controllers
* The controllers to set for this quest
*/
public final void setControllers(Controller[] controllers) {
controller = controllers;
}
/**
* Returns the controllers associated with this quest. If there are no
* <i>set</i> controllers, then it will return <i>null</i>
*
* @return The controllers associated with this quest, or <i>null</i>
*/
public final Controller[] getControllers() {
return controller;
}
/**
* Sets the reward that will be given to a player when this quest has been
* completed. If the specified reward is equal to <i>null</i> then the
* method will return <i>null</i>, and the reward will not be set
*
* @param reward
* The object that the reward will be set to
* @return The set reward, or <i>null</i>; reason stated above
*/
public final Object setReward(Object[] reward) {
if (reward == null)
return null;
this.reward = reward;
return reward;
}
/**
* Returns the current quest progress of this quest. If the progress has not
* yet been set, the method will return <i>null</i>
*
* @return The previously set quest progress, or <i>null</i>; reason stated
* above
*/
public final QuestProgress getProgress() {
return progress;
}
/**
* Sets the progress of the quest. If the specified progress is equal to
* <i>null</i>, then the progress will not be set.
*
* @param qp
* The progress of the quest
*/
public final void setProgress(QuestProgress qp) {
if (qp == null)
return;
progress = qp;
}
/**
* Returns the reward that was previously set. If the reward has not yet
* been set, or was somehow set to <i>null</i>, then the method will return
* <i>null</i>
*
* @return The previously set reward, or <i>null</i>; reason stated above
*/
public final Object[] reward() {
return reward;
}
/**
* Returns the name of this quest. The quest name is assigned when the class
* is constructed
*
* @return The name of this quest
*/
public final String getQuestName() {
return questName;
}
/**
* Returns the pre defined difficulty of this quest. The quest difficulty is
* assigned when this class is constructed
*
* @return The difficulty of this quest
*/
public final QuestDifficulty getDifficulty() {
return difficulty;
}
}
Code:
package org.intel.game.quests;
/**
* @author Taylor Moon
*
* @since Dec 24, 2012 | Elixrr 2.0
*/
public enum QuestDifficulty {
/**
* Represents an array of different quest difficulties.
*/
NOVICE, INTERMEDIATE, EXPERIENCED, MASTER, GRANDMASTER, SPECIAL
}
Code:
package org.intel.game.quests;
import java.io.IOException;
import java.util.HashMap;
import org.intel.utils.Logger;
import org.intel.utils.Utils;
/**
* @author Taylor Moon
*
* @since Dec 24, 2012 | Elixrr 2.0
*/
public class QuestHandler {
/**
* The list of quests in the implementation package
*/
protected static final HashMap<Object, Class<Quest>> QUESTS = new HashMap<Object, Class<Quest>>();
/**
* Initiates this quest handler
*/
@SuppressWarnings({ "unchecked", "static-access" })
public static void init() {
try {
Class<?>[] classes = Utils.getClasses("org.intel.game.quests.impl");
if (classes == null)
return;
for (Class<?> c : classes) {
if (!c.getSuperclass().getName().equals("Quest"))
return;
QUESTS.put(c.getName(),
(Class<Quest>) c.forName(c.getCanonicalName()));
}
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
}
/**
* Returns the quest in the hashmap that corresponds to the specified key in
* the parameter.
*
* @param key
* The quest key
* @return The quest corresponding to the key in the map
*/
protected final Quest getQuest(Object key) {
if (key instanceof Quest)
return (Quest) key;
Class<Quest> q = QUESTS.get(key);
if (q == null)
return null;
try {
return q.newInstance();
} catch (Throwable e) {
Logger.handle(e);
}
return null;
}
}
Code:
package org.intel.game.quests;
import java.util.ArrayList;
import org.intel.game.player.Player;
/**
* @author Taylor Moon
*
* @since Dec 24, 2012 | Elixrr 2.0
*/
public class QuestManager extends QuestHandler {
/** The quest associated with the player */
private static final ArrayList<Quest> STARTED_QUESTS = new ArrayList<Quest>();
// Because a player could have 2 or more quests started but not finished
/** The player associated with this quest manager */
private static Player player;
/**
* Processes the quest as a player is attempting to do it.
*/
public void process() {
for (Quest q : STARTED_QUESTS) {
while (player != null || !player.hasFinished()) {
if (q.getProgress().equals(QuestProgress.COMPLETE)) {
q.giveReward();
q.finish();
STARTED_QUESTS.remove(q);
}
}
}
}
/**
* Starts a quest
*
* @param questName
* The name of the quest you wish to start
*/
public void startQuest(String questName) {
Quest q = getQuest(questName);
if (q == null || STARTED_QUESTS.contains(q))
return;
STARTED_QUESTS.add(q);
q.start();
}
/**
* Returns the player associated with this quest manager
*
* @return The player
*/
public Player getPlayer() {
return player;
}
/**
* Sets the player of this class to the specified player
*
* @param p
* The player to set
*/
public void setPlayer(Player p) {
player = p;
}
/**
* Returns an arraylist of quests that the player has started
*
* @return The player's started quests.
*/
public ArrayList<Quest> getStartedQuests() {
return STARTED_QUESTS;
}
}
Code:
package org.intel.game.quests;
/**
* @author Taylor Moon
*
* @since Dec 24, 2012 | Elixrr 2.0
*/
public enum QuestProgress {
/**
* The types of progresses that a player has when a quest has been started
* or not
*/
NOT_STARTED, STARTED, COMPLETE;
}
Things like completed quest configs, requirements, etc are still in development. But that's the skeleton. 