-
Notifications
You must be signed in to change notification settings - Fork 11
/
DialogueManager.java
188 lines (168 loc) · 5.8 KB
/
DialogueManager.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import java.io.*;
import java.beans.XMLEncoder;
import java.beans.XMLDecoder;
import java.util.HashMap;
import java.util.Map;
/**
* Write a description of class DialogueManager here.
*
* @author (your name)
* @version (a version number or a date)
*/
public final class DialogueManager
{
/**
* All dialogs have an id
*/
private static Map<String, Dialogue> dialogues = new HashMap<String, Dialogue>();
/**
* Singleton object
*/
private final static DialogueManager instance = new DialogueManager();
/**
* Constructor for objects of class DialogueManager
*/
private DialogueManager() {
}
public static DialogueManager getInstance() {
return instance;
}
/**
* Returns a dialogue based on id
*
* @param id of dialogue
* @return return a dialogue
*/
public Dialogue getDialogue(String id) {
return dialogues.get(id);
}
/**
* Gets a value indicating if dialogue is closed.
*
* @param id A parameter
* @return The return value
*/
public boolean isDialogueClosed(String id) {
return dialogues.get(id).isClosed();
}
public Dialogue getDialogue(String id, PlayerCharacter player,
Character interlocutor) {
Dialogue d = dialogues.get(id);
if (d == null) {
d = (Dialogue)ScriptManager.invokeMethod("scripts",
interlocutor.getName(),
"getDialogue",
id, player,
interlocutor);
if (d == null) {return null;}
dialogues.put(id, d);
}
return d;
}
/**
* Open a dialogue based on id
*
* @param id A parameter
* @return The return value
*/
public Dialogue openDialogue(String id, PlayerCharacter player, Character interlocutor) {
Dialogue d = getDialogue(id, player, interlocutor);
if (d == null || d.isClosed()) {return d;}
d.setFeedbackMessage("");
if(SceneManager.getInstance().getCurrentWorld() instanceof DialogueWorld) {
DialogueWorld dw = (DialogueWorld)SceneManager.getInstance().getCurrentWorld();
dw.getDialoguePanel().updateDialogue(d);
}
else {
SceneManager.getInstance().setCurrentWorld(new DialogueWorld(d));
}
SoundManager.getInstance().stopSound(SceneManager.getInstance().getScene().getSoundToPlay());
SoundManager.getInstance().playSound(d.getSoundToPlay(), true);
return d;
}
/**
* Open a dialogue based on id
*
* @param id A parameter
* @return The return value
*/
public Dialogue redirectToDialogue(String id, PlayerCharacter player, Character interlocutor) {
return openDialogue(id, player, interlocutor);
}
public void updateDialogueMessages() {
if(SceneManager.getInstance().getCurrentWorld() instanceof DialogueWorld) {
DialogueWorld dw = (DialogueWorld)SceneManager.getInstance().getCurrentWorld();
dw.getDialoguePanel().updateDialogueMessages();
}
}
/**
* Close the dialogue.
* When a dialogue is closed it cannot be reopened.
*
* @param id The dialogue id
* @return true if dialogue could be closed or false if dialogue does not exists.
*/
public boolean closeDialogue(String id) {
if (dialogues.get(id) != null) {
dialogues.get(id).close();
return true;
}
return false;
}
public boolean exitDialogue(String id) {
if(SceneManager.getInstance().getCurrentWorld() instanceof DialogueWorld) {
DialogueWorld dw = (DialogueWorld)SceneManager.getInstance().getCurrentWorld();
dw.getDialoguePanel().dispose();
SoundManager.getInstance().stopSound(dialogues.get(id).getSoundToPlay());
return true;
}
return false;
}
public void enableDialogueChoices(boolean enabled) {
if(SceneManager.getInstance().getCurrentWorld() instanceof DialogueWorld) {
DialogueWorld dw = (DialogueWorld)SceneManager.getInstance().getCurrentWorld();
dw.getDialoguePanel().enableDialogueChoices(enabled);
}
}
/**
* Read a dialogue serialized in a xml file.
*
* @param id The id of the dialogue. The dialogue file must be placed in ./dialogues/"id".xml.
* @return The dialogue deserialized.
*/
public Dialogue readDialogue(String id) {
try{
XMLDecoder d = new XMLDecoder(
new BufferedInputStream(
new FileInputStream("./dialogues/" + id + ".xml")), null, null,
bluej.runtime.ExecServer.getCurrentClassLoader());
Object result = d.readObject();
d.close();
return (Dialogue)result;
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
return null;
}
/**
* Serialize a dialogue to a xml file.
* The dialogue will be serialized in ./dialogues/"id".xml.
*
* @param dialogue The dialogue to be serialized.
*/
public void writeDialogue(Dialogue dialogue) {
try{
XMLEncoder e = new XMLEncoder(
new BufferedOutputStream(
new FileOutputStream("./dialogues/" + dialogue.getId() + ".xml")));
e.writeObject(dialogue);
e.close();
}
catch(IOException ioEx)
{
ioEx.printStackTrace();
}
}
}