-
Notifications
You must be signed in to change notification settings - Fork 11
/
PlayerCharacter.java
206 lines (184 loc) · 6.16 KB
/
PlayerCharacter.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* Write a description of class PlayableCharacter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public abstract class PlayerCharacter extends Character implements Dialogable
{
private Map<Character, Queue<Dialogue>> dialogues = new HashMap<Character, Queue<Dialogue>>();
private Map<Character, Queue<Dialogue>> finalizedDialogues = new HashMap<Character, Queue<Dialogue>>();
private boolean inventoryIsOpen;
public PlayerCharacter(String name) {
this.setName(name);
}
public void act() {
super.act();
move();
inventory();
Direction direction = atWorldEdge();
if(direction != Direction.NONE) {
SceneManager.getInstance().setScene(direction);
}
}
public void inventory() {
if(getWorld() == null) return;
if(Greenfoot.isKeyDown("i") && !inventoryIsOpen) {
Game.openInventory();
inventoryIsOpen = true;
}
else if (!Greenfoot.isKeyDown("i")){
inventoryIsOpen = false;
}
}
/**
* Test if we can move forward. Return true if we can, false otherwise.
*/
public boolean canMove(Direction direction)
{
int width = SceneManager.getInstance().getScene().getWidth();
int height = SceneManager.getInstance().getScene().getHeight();
int x = getX();
int y = getY();
switch(direction) {
case SOUTH :
y++;
break;
case EAST :
x++;
break;
case NORTH :
y--;
break;
case WEST :
x--;
break;
}
int minX = 0;
int minY = 0;
int maxX = width - 1;
int maxY = height - 1;
if (x < minX || y < minY || x > maxX || y > maxY)
{
return false;
}
return true;
}
/**
* Test if we can move forward. Return true if we can, false otherwise.
*/
public boolean canMove()
{
return canMove(this.getDirection());
}
public void move() {
if(isFrozen()) return;
if(Greenfoot.isKeyDown("up"))
{
if (getDirection() != Direction.NORTH) {
setDirection(Direction.NORTH);
}
else {
if(!canMove()) return;
}
animate();
setLocation(getX(), getY() - (getSpeed() / 10));
}
else if(Greenfoot.isKeyDown("down"))
{
if (getDirection() != Direction.SOUTH) {
setDirection(Direction.SOUTH);
}
else {
if(!canMove()) return;
}
animate();
setLocation(getX(), getY() + (getSpeed() / 10));
}
else if(Greenfoot.isKeyDown("left"))
{
if (getDirection() != Direction.WEST) {
setDirection(Direction.WEST);
}
else {
if(!canMove()) return;
}
animate();
setLocation(getX() - (getSpeed() / 10), getY());
}
else if(Greenfoot.isKeyDown("right"))
{
if (getDirection() != Direction.EAST) {
setDirection(Direction.EAST);
}
else {
if(!canMove()) return;
}
animate();
setLocation(getX() + (getSpeed() / 10), getY());
}
}
/**
* Initiate a dialogue with interlocutor.
* The dialogue is configured byscript and returned.
*
* @param id The dialogue id
* @param interlocutor The dialogue interlocutor
* @return The dialogue initiated.
*/
public Dialogue initiateDialogueWith(String id, Character interlocutor) {
Dialogue d = DialogueManager.getInstance().openDialogue(id, this, interlocutor);
if(d == null || d.isClosed()) return null;
addDialogue(d, interlocutor);
return d;
}
public Dialogue queueDialogue(String id, Character interlocutor) {
Dialogue d = DialogueManager.getInstance().openDialogue(id, this, interlocutor);
if(d == null || d.isClosed()) return null;
addDialogue(d, interlocutor);
return d;
}
public boolean hasQueuedDialogue(Character interlocutor) {
Queue<Dialogue> queue = dialogues.get(interlocutor);
if(queue != null) {
return queue.size() > 0;
}
return false;
}
public Dialogue initiateNextDialogueWith(Character interlocutor) {
//Dialogue d = DialogueManager.createDialogue(id, this, interlocutor);
//addDialogue(d, interlocutor);
return null;
}
public void addDialogue(Dialogue dialogue, Character interlocutor) {
if (dialogue == null) { return;}
Queue<Dialogue> queue = null;
if(dialogues.get(interlocutor) == null) {
queue = new ArrayDeque<Dialogue>();
dialogues.put(dialogue.getInterlocutor(), queue);
}
queue.offer(dialogue);
}
public Dialogue getCurrentDialogue(Character interlocutor) {
return dialogues.get(interlocutor).peek();
}
public void closeCurrentDialogue(Character interlocutor) {
Dialogue current = getCurrentDialogue(interlocutor);
if(current != null) {
current.close();
Dialogue dialogue = dialogues.get(interlocutor).poll();
if(dialogue.hasNextDialogue()) {
Dialogue nextDialogue = DialogueManager.getInstance().getDialogue(dialogue.getNextDialogue());
addDialogue(nextDialogue, interlocutor);
}
Queue<Dialogue> queue = null;
if(finalizedDialogues.get(interlocutor) == null) {
queue = new ArrayDeque<Dialogue>();
finalizedDialogues.put(dialogue.getInterlocutor(), queue);
}
queue.offer(dialogue);
}
}
}