-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChara.java
More file actions
441 lines (390 loc) · 12 KB
/
Chara.java
File metadata and controls
441 lines (390 loc) · 12 KB
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/*
* Created on 8/18/2018
*
*/
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
/**
* @author nyatadecocoa
*/
public class Chara implements Common {
// キャラクターの移動スピード
private static final int SPEED = 4;
// 移動確率
public static final double PROB_MOVE = 0.02;
// キャラクターのイメージ
private static Image charaImage;
// キャラクター番号
private int charaNo;
// キャラクターの座標
private int x, y; // 単位:マス
private int px, py; // 単位:ピクセル
// 勇者のアニメーションカウンタ
private int count;
// 勇者の向いている方向(LEFT,RIGHT,UP,DOWNのどれか)
private int direction;
// 移動中(スクロール中)か
private boolean isMoving;
// 移動中の場合の移動ピクセル数
private int movingLength;
// 移動方法
private int moveType;
// はなすメッセージ
private String message;
// マップへの参照
private Map map;
// パネルへの参照
//private MainPanel panel;
// キャラクターのアニメーション(足踏み)への参照
Thread thread;
public Chara(int x, int y, int charaNo, int direction, int moveType, Map map) {
this.x = x;
this.y = y;
px = x * CS;
py = y * CS;
this.direction = direction;
count = 0;
this.moveType = moveType;
this.map = map;
//this.panel = panel;
// 初回の呼び出しのみイメージをロード
if (charaImage == null) {
loadImage();
}
this.charaNo = charaNo;
// キャラクターのアニメーション(足踏み) ←このスレッドに問題あり(ウィンドウを閉じても終わらない)
thread = new Thread(() -> {
while (!map.finished) {
//System.out.println(Thread.currentThread().getName());
// countを切り替える
if (count == 0) {
count = 1;
} else if (count == 1) {
count = 0;
}
//panel.repaint();
// 300ミリ秒休止=300ミリ秒おきに勇者の絵を切り替える
try {
Thread.sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "CharacterAnimation");
thread.start();
}
void draw(GraphicsContext gc, int offsetX, int offsetY) {
int cx = (charaNo % 8) * (CS * 2);
int cy = (charaNo / 8) * (CS * 4);
// countとdirectionの値に応じて表示する画像を切り替える
gc.drawImage(charaImage, cx + count * CS, cy + direction * CS, CS, CS, px + offsetX, py + offsetY, CS, CS);
}
/**
* 移動処理。
* @return 1マス移動が完了したらtrueを返す。移動中はfalseを返す。
*/
public boolean move() {
switch (direction) {
case LEFT:
if (moveLeft()) {
// 移動が完了した
return true;
}
break;
case RIGHT:
if (moveRight()) {
// 移動が完了した
return true;
}
break;
case UP:
if (moveUp()) {
// 移動が完了した
return true;
}
break;
case DOWN:
if (moveDown()) {
// 移動が完了した
return true;
}
break;
}
// 移動が完了していない
return false;
}
/**
* 左へ移動する。
* @return 1マス移動が完了したらtrueを返す。移動中はfalseを返す。
*/
private boolean moveLeft() {
// 1マス先の座標
int nextX = x - 1;
int nextY = y;
if (nextX < 0) nextX = 0;
// その場所に障害物がなければ移動を開始
if (!map.isHit(nextX, nextY)) {
// SPEEDピクセル分移動
px -= Chara.SPEED;
if (px < 0) px = 0;
// 移動距離を加算
movingLength += Chara.SPEED;
// 移動が1マス分を超えていたら
if (movingLength >= CS) {
// 移動する
x--;
if (x < 0) x = 0;
px = x * CS;
// 移動が完了
isMoving = false;
return true;
}
} else {
isMoving = false;
// 元の位置に戻す
px = x * CS;
py = y * CS;
}
return false;
}
/**
* 右へ移動する。
* @return 1マス移動が完了したらtrueを返す。移動中はfalseを返す。
*/
private boolean moveRight() {
// 1マス先の座標
int nextX = x + 1;
int nextY = y;
if (nextX > map.getCol() - 1) nextX = map.getCol() - 1;
// その場所に障害物がなければ移動を開始
if (!map.isHit(nextX, nextY)) {
// SPEEDピクセル分移動
px += Chara.SPEED;
if (px > map.getWidth() - CS)
px = map.getWidth() - CS;
// 移動距離を加算
movingLength += Chara.SPEED;
// 移動が1マス分を超えていたら
if (movingLength >= CS) {
// 移動する
x++;
if (x > map.getCol() - 1) x = map.getCol() - 1;
px = x * CS;
// 移動が完了
isMoving = false;
return true;
}
} else {
isMoving = false;
px = x * CS;
py = y * CS;
}
return false;
}
/**
* 上へ移動する。
* @return 1マス移動が完了したらtrueを返す。移動中はfalseを返す。
*/
private boolean moveUp() {
// 1マス先の座標
int nextX = x;
int nextY = y - 1;
if (nextY < 0) nextY = 0;
// その場所に障害物がなければ移動を開始
if (!map.isHit(nextX, nextY)) {
// SPEEDピクセル分移動
py -= Chara.SPEED;
if (py < 0) py = 0;
// 移動距離を加算
movingLength += Chara.SPEED;
// 移動が1マス分を超えていたら
if (movingLength >= CS) {
// 移動する
y--;
if (y < 0) y = 0;
py = y * CS;
// 移動が完了
isMoving = false;
return true;
}
} else {
isMoving = false;
px = x * CS;
py = y * CS;
}
return false;
}
/**
* 下へ移動する。
* @return 1マス移動が完了したらtrueを返す。移動中はfalseを返す。
*/
private boolean moveDown() {
// 1マス先の座標
int nextX = x;
int nextY = y + 1;
if (nextY > map.getRow() - 1) nextY = map.getRow() - 1;
// その場所に障害物がなければ移動を開始
if (!map.isHit(nextX, nextY)) {
// SPEEDピクセル分移動
py += Chara.SPEED;
if (py > map.getHeight() - CS)
py = map.getHeight() - CS;
// 移動距離を加算
movingLength += Chara.SPEED;
// 移動が1マス分を超えていたら
if (movingLength >= CS) {
// 移動する
y++;
if (y > map.getRow() - 1) y = map.getRow() - 1;
py = y * CS;
// 移動が完了
isMoving = false;
return true;
}
} else {
isMoving = false;
px = x * CS;
py = y * CS;
}
return false;
}
/**
* キャラクターが向いている方向のとなりにキャラクターがいるか調べる
* @return キャラクターがいたらそのCharaオブジェクトを返す
*/
public Chara talkWith() {
int nextX = 0;
int nextY = 0;
// キャラクターの向いている方向の1歩となりの座標
switch (direction) {
case LEFT:
nextX = x - 1;
nextY = y;
break;
case RIGHT:
nextX = x + 1;
nextY = y;
break;
case UP:
nextX = x;
nextY = y - 1;
break;
case DOWN:
nextX = x;
nextY = y + 1;
break;
}
// その方向にキャラクターがいるか調べる
Chara chara;
chara = map.charaCheck(nextX, nextY);
// キャラクターがいれば話しかけたキャラクターの方へ向ける
if (chara != null) {
switch (direction) {
case LEFT:
chara.setDirection(RIGHT);
break;
case RIGHT:
chara.setDirection(LEFT);
break;
case UP:
chara.setDirection(DOWN);
break;
case DOWN:
chara.setDirection(UP);
break;
}
}
return chara;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getPx() {
return px;
}
public int getPy() {
return py;
}
public void setDirection(int dir) {
direction = dir;
}
public boolean isMoving() {
return isMoving;
}
public void setMoving(boolean flag) {
isMoving = flag;
// 移動距離を初期化
movingLength = 0;
}
/**
* キャラクターのメッセージを返す
* @return メッセージ
*/
public String getMessage() {
return message;
}
/**
* キャラクターのメッセージを返す
* @param message メッセージ
*/
public void setMessage(String message) {
this.message = message;
}
public int getMoveType() {
return moveType;
}
/**
* あしもとに宝箱があるか調べる
* @return あしもとにあるTreasureEventオブジェクト
*/
public TreasureEvent search() {
Event event = map.eventCheck(x, y);
if (event instanceof TreasureEvent) {
return (TreasureEvent)event;
}
return null;
}
/**
* 目の前にドアがあるか調べる
* @return 目の前にあるDoorEventオブジェクト
*/
public DoorEvent open() {
int nextX = 0;
int nextY = 0;
// キャラクターの向いている方向の1歩となりの座標
switch (direction) {
case LEFT:
nextX = x - 1;
nextY = y;
break;
case RIGHT:
nextX = x + 1;
nextY = y;
break;
case UP:
nextX = x;
nextY = y - 1;
break;
case DOWN:
nextX = x;
nextY = y + 1;
break;
}
// その方向にドアがあるか調べる
Event event = map.eventCheck(nextX, nextY);
if (event instanceof DoorEvent) {
return (DoorEvent)event;
}
return null;
}
private void loadImage() {
try {
charaImage = new Image("image/chara.gif");
} catch (Exception e) {
e.printStackTrace();
}
}
}