-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCharacter.java
409 lines (349 loc) · 9.98 KB
/
Character.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* Base class for all game characters
*
* @author (your name)
* @version (a version number or a date)
*/
public abstract class Character extends Entity
{
private Direction direction = Direction.SOUTH;
private List<Integer> eastSprites = new ArrayList<Integer>();
private List<Integer> westSprites = new ArrayList<Integer>();
private List<Integer> northSprites = new ArrayList<Integer>();
private List<Integer> southSprites = new ArrayList<Integer>();
private int animationSprite = 0;
private int animationFrame = 0;
private String characterImage;
private int speed = 10;
private int strength = 10;
private int health = 10;
private int power = 10;
private int money = 10;
private int inteligence = 10;
private boolean frozen = false;
private boolean dialoguing = false;
private boolean speaking = false;
private SpeechBubble speechBubble;
private boolean interacting;
private boolean touching;
private boolean clicking;
public void act() {
super.act();
interact();
touch();
click();
}
public Direction getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
resetAnimation();
}
public void setEastSprites(int begin, int end) {
eastSprites.clear();
for(int i = begin; i <= end; ++i) {
eastSprites.add(i);
}
}
public void setWestSprites(int begin, int end) {
westSprites.clear();
for(int i = begin; i <= end; ++i) {
westSprites.add(i);
}
}
/**
* Gets the character image file name.
* This image is showed in dialogues and combats.
* The image must be placed in ./images directory.
*
* @return Returns the character image file name.
*/
public String getCharacterImage() {
return characterImage;
}
/**
* Sets the character image. The image must be placed in ./images directory.
* This image is showed in dialogues and combats.
* @param characterImage The character image file name.
*/
public void setCharacterImage(String characterImage) {
this.characterImage = characterImage;
}
/**
* Test if we are close to one of the edges of the world. Return true is we are.
*/
public Direction atWorldEdge()
{
if(getX() < SceneManager.EDGE) {return Direction.WEST;}
if(getX() > SceneManager.getInstance().getScene().getWidth() - SceneManager.EDGE) {return Direction.EAST;}
if(getY() < SceneManager.EDGE) {return Direction.NORTH;}
if(getY() > SceneManager.getInstance().getScene().getHeight() - SceneManager.EDGE) {return Direction.SOUTH;}
return Direction.NONE;
}
public void setNorthSprites(int begin, int end) {
northSprites.clear();
for(int i = begin; i <= end; ++i) {
northSprites.add(i);
}
}
public void setSouthSprites(int begin, int end) {
southSprites.clear();
for(int i = begin; i <= end; ++i) {
southSprites.add(i);
}
}
public void freeze() {
this.frozen = true;
}
public void unfreeze() {
this.frozen = false;
}
public boolean isFrozen() {
return this.frozen;
}
/**
* @return the speed
*/
public int getSpeed() {
return speed;
}
/**
* @param speed the speed to set
*/
public void setSpeed(int speed) {
this.speed = speed;
}
/**
* @return the strength
*/
public int getStrength() {
return strength;
}
/**
* @param strength the strength to set
*/
public void setStrength(int strength) {
this.strength = strength;
}
/**
* @return the health
*/
public int getHealth() {
return health;
}
/**
* @param health the health to set
*/
public void setHealth(int health) {
this.health = health;
}
/**
* @return the power
*/
public int getPower() {
return power;
}
/**
* @param power the power to set
*/
public void setPower(int power) {
this.power = power;
}
/**
* @return the money
*/
public int getMoney() {
return money;
}
/**
* @param power the money to set
*/
public void setMoney(int money) {
this.money = money;
}
/**
* @return the inteligence
*/
public int getInteligence() {
return inteligence;
}
/**
* @param power the money to set
*/
public void setInteligence(int inteligence) {
this.inteligence = inteligence;
}
public void increaseSpeed(int ammount) {
if(speed + ammount < 0) {
speed = 0;
}
else {
speed += ammount;
}
}
public void increaseHealth(int ammount) {
if(health + ammount < 0) {
health = 0;
}
else {
health += ammount;
}
}
public void increasePower(int ammount) {
if(power + ammount < 0) {
power = 0;
}
else {
power += ammount;
}
}
public void increaseStrength(int ammount) {
if(strength + ammount < 0) {
strength = 0;
}
else {
strength += ammount;
}
}
public void increaseMoney(int ammount) {
if(money + ammount < 0) {
money = 0;
}
else {
money += ammount;
}
}
public void increaseInteligence(int ammount) {
if(inteligence + ammount < 0) {
inteligence = 0;
}
else {
inteligence += ammount;
}
}
public void reset() {
stopSpeak();
}
public void talkWith(Character character, String text) {
}
public void talkWith(Character character, String... args) {
}
public void speak(String text) {
if(getWorld() == null) return;
if (speechBubble == null) {
speechBubble = new SpeechBubble(this, text);
getWorld().addObject(speechBubble,
this.getX() + 50,
this.getY() - 50);
speechBubble.act();
speechBubble.paint();
}
else {
speechBubble.paint();
}
speaking = true;
}
public boolean isSpeaking() {
return this.speaking;
}
public void stopSpeak() {
if(getWorld() == null) return;
if (speechBubble != null) {
getWorld().removeObject(speechBubble);
speechBubble = null;
}
speaking = false;
}
public void interact() {
if(getWorld() == null) return;
if(Greenfoot.isKeyDown("space") && !interacting) {
PlayerCharacter player = (PlayerCharacter)getOneIntersectingObject(PlayerCharacter.class);
if(player != null) {
ScriptManager.invokeMethod("scripts", getScriptObject(), "interact",
player, this);
}
interacting = true;
}
else if (!Greenfoot.isKeyDown("space")){
interacting = false;
}
}
public void touch() {
if(getWorld() == null) return;
PlayerCharacter pc = (PlayerCharacter)getOneIntersectingObject(PlayerCharacter.class);
if(!touching && pc != null) {
ScriptManager.invokeMethod("scripts", getScriptObject(), "touch",
pc, this);
touching = true;
}
else if (pc == null){
touching = false;
}
}
public void click() {
if(getWorld() == null) return;
if (!clicking && Greenfoot.mousePressed(this))
{
ScriptManager.invokeMethod("scripts", getScriptObject(), "click", this);
clicking = true;
}
else if (clicking && !Greenfoot.mousePressed(this))
{
clicking = false;
}
}
/**
* Test if we can move forward. Return true if we can, false otherwise.
*/
public boolean hasAnyoneNear(int range)
{
List objects = getObjectsInRange(range, null);
if(objects.isEmpty()) {
return true;
}
return false;
}
public void resetAnimation() {
if(southSprites.size() == 0) return;
animationFrame = 0;
animationSprite = 0;
switch(getDirection()) {
case SOUTH :
setSprite(southSprites.get(0));
break;
case EAST :
setSprite(eastSprites.get(0));
break;
case NORTH :
setSprite(northSprites.get(0));
break;
case WEST :
setSprite(westSprites.get(0));
break;
}
}
public void animate() {
if(southSprites.size() == 0) return;
if((++animationFrame % 6) == 0)
{
switch(getDirection()) {
case SOUTH :
setSprite(southSprites.get(animationSprite++ % southSprites.size()));
break;
case EAST :
setSprite(eastSprites.get(animationSprite++ % eastSprites.size()));
break;
case NORTH :
setSprite(northSprites.get(animationSprite++ % northSprites.size()));
break;
case WEST :
setSprite(westSprites.get(animationSprite++ % westSprites.size()));
break;
}
animationFrame = 0;
}
}
}