Skip to content

Instantly share code, notes, and snippets.

@nanonull
Created December 24, 2015 20:57
Show Gist options
  • Save nanonull/ce98ac45c76a74ce0858 to your computer and use it in GitHub Desktop.
Save nanonull/ce98ac45c76a74ce0858 to your computer and use it in GitHub Desktop.
public abstract class AbstractQuest {
protected static int QUEST_INIT_STATE = 0;
protected static int QUEST_COMPLETED_STATE = Integer.MAX_VALUE;
private boolean toBeCompleted;
private Array<QuestOption> choiceItems = PoolManager.ARRAYS_POOL.obtain();
private Array<String> descriptionRows = PoolManager.ARRAYS_POOL.obtain();
private Texture picture;
public QuestWindow questWindow;
protected int state = QUEST_INIT_STATE;
private Map<Integer, List> questStateMapSaved
QuestOption lastSelectedOption
boolean closed
public AbstractQuest(QuestWindow questWindow) {
this.questWindow = questWindow;
}
/**Simple template:<br>
return [
(QUEST_INIT_STATE):
[
'description'
, QUEST_OPTION_1
, QUEST_OPTION_2
]
]
*/
protected abstract Map<Integer, List> getQuestStateMap();
protected Map<Integer, List> buildQuestStateMap() {
if (!questStateMapSaved) {
questStateMapSaved = getQuestStateMap()
}
return questStateMapSaved
}
public Array<String> getDescriptionRows() {
return descriptionRows;
}
public Texture getPicture() {
return picture;
}
public void setPicture(Texture tex) {
picture = tex;
}
public Array<QuestOption> getChoiceItems() {
return choiceItems;
}
public void setPictureViewActive(boolean active) {
questWindow.setPictureViewEnabled(active);
}
public void setState(int state) {
this.state = state;
}
public void addChoiceOption(QuestOption option) {
choiceItems.add(option);
}
public void addDescriptionRow(String row) {
descriptionRows.add(row);
}
protected void initQuestState() {}
protected void runChoiceClosure(QuestChoiceEvent event) {
lastSelectedOption = event.getSelectedOption()
lastSelectedOption.getActionClosure().run();
}
def reComputeStates() {
computeStates()
}
/** Global quest state machine */
protected void computeStates() {
def options = buildQuestStateMap().get(state)
if (options == null) {
if (state == QUEST_COMPLETED_STATE) {
toBeCompleted = true;
} else {
throw new GdxRuntimeException(getClass().toString() + " could not resolve state " + state);
}
}
options.each { option ->
if (option instanceof String || option instanceof GString) {
addDescriptionRow((String) option)
} else if (option instanceof Closure) {
option.run()
} else if (option instanceof QuestOption) {
addChoiceOption(option)
} else {
UiLogger.addErrorLabel("Unknown option type: " + option?.getClass())
}
}
}
public static void start(AbstractQuest quest) {
quest.initQuestState();
quest.computeStates()
quest.questWindow.showFor(quest);
}
protected void complete() {
questWindow.close();
closed = true
}
public void selected(QuestChoiceEvent event) {
choiceItems.clear();
descriptionRows.clear();
// handle event
runChoiceClosure(event);
if (!toBeCompleted) {
computeStates();
}
// if completed during computeStates
if (closed) {
return
}
if (toBeCompleted) {
complete()
} else {
questWindow.refreshActiveQuest();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment