-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.java
More file actions
350 lines (309 loc) · 10.6 KB
/
Map.java
File metadata and controls
350 lines (309 loc) · 10.6 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
/*
* Created on 8/18/2018
*
*/
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Vector;
/**
* @author nyatadecocoa
*/
public class Map implements Common {
// マップの行数・列数(単位:マス)
private int row;
private int col;
// マップ全体の大きさ(単位:ピクセル)
private int width;
private int height;
// マップ 0:床 1:壁
private int[][] map;
private static Image chipImage;
// このマップにいるキャラクターたち
private Vector charas = new Vector();
// このマップにあるイベント
private Vector events = new Vector();
// キャラスレッド終了フラグ
public boolean finished = false;
// メインパネルへの参照
private MainPanel panel;
public Map(String mapFile, String eventFile, MainPanel panel) {
// マップをロード
load(mapFile);
// イベントをロード
loadEvent(eventFile);
// 初回の呼び出しのみイメージをロード
if (chipImage == null) {
loadImage();
}
}
void draw(GraphicsContext gc, int offsetX, int offsetY) {
// オフセットを元に描画範囲を求める
int firstTileX = pixelsToTiles(-offsetX);
int lastTileX = firstTileX + pixelsToTiles(MainPanel.WIDTH) + 1;
// 描画範囲がマップの大きさより大きくならないように調整
lastTileX = Math.min(lastTileX, col);
int firstTileY = pixelsToTiles(-offsetY);
int lastTileY = firstTileY + pixelsToTiles(MainPanel.HEIGHT) + 1;
// 描画範囲がマップの大きさより大きくならないように調整
lastTileY = Math.min(lastTileY, row);
for (int i = firstTileY; i < lastTileY; i++) {
for (int j = firstTileX; j < lastTileX; j++) {
int mapChipNo = map[i][j];
// イメージ上の位置を求める
// マップチップイメージは8x8を想定
int cx = (mapChipNo % 8) * CS;
int cy = (mapChipNo / 8) * CS;
gc.drawImage(chipImage, cx, cy, CS, CS, tilesToPixels(j) + offsetX, tilesToPixels(i) + offsetY, CS, CS);
// (j, i) にあるイベントを描画
for (int n=0; n<events.size(); n++) {
Event event = (Event)events.get(n);
// イベントが(j, i)にあれば描画
if (event.x == j && event.y == i) {
mapChipNo = event.chipNo;
cx = (mapChipNo % 8) * CS;
cy = (mapChipNo / 8) * CS;
gc.drawImage(chipImage, cx, cy, CS, CS, tilesToPixels(j) + offsetX, tilesToPixels(i) + offsetY, CS, CS);
}
}
}
}
// このマップにいるキャラクターを描画
for (int n=0; n<charas.size(); n++) {
Chara chara = (Chara)charas.get(n);
chara.draw(gc, offsetX, offsetY);
}
}
/**
* (x,y)にぶつかるものがあるか調べる。
* @param x マップのx座標
* @param y マップのy座標
* @return (x,y)にぶつかるものがあったらtrueを返す。
*/
public boolean isHit(int x, int y) {
// (x,y)に壁か玉座があったらぶつかる
if (map[y][x] == 1 || map[y][x] == 2) {
return true;
}
// 他のキャラクターがいたらぶつかる
for (int i = 0; i < charas.size(); i++) {
Chara chara = (Chara) charas.get(i);
if (chara.getX() == x && chara.getY() == y) {
return true;
}
}
// ぶつかるイベントがあるか
for (int i = 0; i < events.size(); i++) {
Event event = (Event)events.get(i);
if (event.x == x && event.y == y) {
return event.isHit;
}
}
// なければぶつからない
return false;
}
/**
* キャラクターをこのマップに追加する
* @param chara キャラクター
*/
public void addChara(Chara chara) {
charas.add(chara);
}
/**
* (x,y)にキャラクターがいるか調べる
* @param x X座標
* @param y Y座標
* @return (x,y)にいるキャラクター、いなかったらnull
*/
public Chara charaCheck(int x, int y) {
for (int i=0; i<charas.size(); i++) {
Chara chara = (Chara)charas.get(i);
if (chara.getX() == x && chara.getY() == y) {
return chara;
}
}
return null;
}
/**
* (x,y)にイベントがあるか調べる
* @param x X座標
* @param y Y座標
* @return (x,y)にいるイベント、いなかったらnull
*/
public Event eventCheck(int x, int y) {
for (int i=0; i<events.size(); i++) {
Event event = (Event)events.get(i);
if (event.x == x && event.y == y) {
return event;
}
}
return null;
}
/**
* 登録されているイベントを削除する
* @param event 削除したいイベント
*/
public void removeEvent(Event event) {
events.remove(event);
}
/**
* ピクセル単位をマス単位に変更する
* @param pixels ピクセル単位
* @return マス単位
*/
public static int pixelsToTiles(double pixels) {
return (int)Math.floor(pixels / CS);
}
/**
* マス単位をピクセル単位に変更する
* @param tiles マス単位
* @return ピクセル単位
*/
public static int tilesToPixels(int tiles) {
return tiles * CS;
}
public int getRow() {
return row;
}
public int getCol() {
return col;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Vector getCharas() {
return charas;
}
/**
* ファイルからマップを読み込む
* @param filename 読み込むマップデータのファイル名
*/
private void load(String filename) {
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(getClass().getResourceAsStream(filename)));
// rowを読み込む
String line = br.readLine();
row = Integer.parseInt(line);
// colを読む
line = br.readLine();
col = Integer.parseInt(line);
// マップサイズを設定
width = col * CS;
height = row * CS;
// マップを作成
map = new int[row][col];
for (int i=0; i<row; i++) {
line = br.readLine();
for (int j=0; j<col; j++) {
map[i][j] = Integer.parseInt(line.charAt(j) + "");
}
}
show();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* イベントをロードする
* @param filename イベントファイル
*/
private void loadEvent(String filename) {
try {
BufferedReader br = new BufferedReader(new InputStreamReader(
getClass().getResourceAsStream(filename), "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
// 空行は読み飛ばす
if (line.equals("")) continue;
// コメント行は読み飛ばす
if (line.startsWith("#")) continue;
StringTokenizer st = new StringTokenizer(line, ",");
// イベント情報を取得する
// イベントタイプを取得してイベントごとに処理する
String eventType = st.nextToken();
if (eventType.equals("CHARA")) { // キャラクターイベント
makeCharacter(st);
} else if (eventType.equals("TREASURE")) { // 宝箱イベント
makeTreasure(st);
} else if (eventType.equals("DOOR")) { // とびらイベント
makeDoor(st);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void loadImage() {
try {
chipImage = new Image("image/mapchip01.gif");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* キャラクターイベントを作成
*/
private void makeCharacter(StringTokenizer st) {
// イベントの座標
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
// キャラクタ番号
int charaNo = Integer.parseInt(st.nextToken());
// 向き
int dir = Integer.parseInt(st.nextToken());
// 移動タイプ
int moveType = Integer.parseInt(st.nextToken());
// メッセージ
String message = st.nextToken();
// キャラクターを作成
Chara c = new Chara(x, y, charaNo, dir, moveType, this);
// メッセージを登録
c.setMessage(message);
// キャラクターベクトルに登録
charas.add(c);
}
/**
* 宝箱イベントを作成
*/
private void makeTreasure(StringTokenizer st) {
// 宝箱の座標
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
// アイテム名
String itemName = st.nextToken();
// 宝箱イベントを作成
TreasureEvent t = new TreasureEvent(x, y, itemName);
// 宝箱イベントを登録
events.add(t);
}
/**
* とびらイベントを作成
*/
private void makeDoor(StringTokenizer st) {
// とびらの座標
int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
// とびらイベントを作成
DoorEvent d = new DoorEvent(x, y);
// とびらイベントを登録
events.add(d);
}
/**
* マップをコンソールに表示。デバッグ用。
*/
public void show() {
for (int i=0; i<row; i++) {
for (int j=0; j<col; j++) {
System.out.print(map[i][j]);
}
System.out.println();
}
}
}