- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapa.java
More file actions
Latest commit
269 lines (219 loc) · 8.11 KB
/
Copy pathMapa.java
File metadata and controls
269 lines (219 loc) · 8.11 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
importjava.awt.Color;
importjava.io.BufferedReader;
importjava.io.FileReader;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
publicclassMapa {
privateList<String> mapa;
privateMap<Character, ElementoMapa> elementos;
privateintx = 50;
privateinty = 50;
privatefinalintTAMANHO_CELULA = 10;
privateboolean[][] areaRevelada;
privatefinalColorbrickColor = newColor(153, 76, 0);
privatefinalColorvegetationColor = newColor(34, 139, 34);
privatefinalColoraxeColor = newColor(80,80,80); // COR CINZA PARA O MACHADO
privatefinalColorcupimColor = newColor(255,0,0); // COR VERMELHA PARA O CUPIM
privatefinalColoramigoColor = newColor(0,0,255); // COR AZUL PARA O AMIGO
privatefinalColorplacaColor = newColor(255,0,255); // COR BEGE PARA A PLACA
privatefinalColorarvoreCortColor = newColor(0,204,102); // COR VERDE CLARO PARA ÁRVORE CORTÁVEL
privatefinalColorportaColor = newColor(51,25,0); // COR MARROM PRA PORTA
privatefinalColorchaveColor = newColor(240,234,51); // COR AMARELO PARA CHAVE
privatefinalintRAIO_VISAO = 5;
privateintcontador;
privatebooleanmachado;
publicMapa(StringarquivoMapa) {
mapa = newArrayList<>();
elementos = newHashMap<>();
this.contador = 7;
this.machado = false;
registraElementos();
carregaMapa(arquivoMapa);
areaRevelada = newboolean[mapa.size()+1000][mapa.get(0).length()+1000];
atualizaCelulasReveladas();
}
publicintgetX() {
returnx;
}
publicintgetY() {
returny;
}
publicintgetTamanhoCelula() {
returnTAMANHO_CELULA;
}
publicintgetNumLinhas() {
returnmapa.size();
}
publicintgetNumColunas() {
returnmapa.get(0).length();
}
publicElementoMapagetElemento(intx, inty) {
Characterid = mapa.get(y).charAt(x);
returnelementos.get(id);
}
publicbooleanestaRevelado(intx, inty) {
returnareaRevelada[y][x];
}
publicbooleanmove(Direcaodirecao) {
intdx = 0, dy = 0;
switch (direcao) {
caseCIMA:
dy = -TAMANHO_CELULA;
break;
caseBAIXO:
dy = TAMANHO_CELULA;
break;
caseESQUERDA:
dx = -TAMANHO_CELULA;
break;
caseDIREITA:
dx = TAMANHO_CELULA;
break;
default:
returnfalse;
}
if (!podeMover(x + dx, y + dy)) {
System.out.println("Não pode mover");
returnfalse;
}
x += dx;
y += dy;
atualizaCelulasReveladas();
returntrue;
}
privatebooleanpodeMover(intnextX, intnextY) {
intmapX = nextX / TAMANHO_CELULA;
intmapY = nextY / TAMANHO_CELULA - 1;
if (mapa == null)
returnfalse;
if (mapX >= 0 && mapX < mapa.get(0).length() && mapY >= 1 && mapY <= mapa.size()) {
charid;
try {
id = mapa.get(mapY).charAt(mapX);
} catch (StringIndexOutOfBoundsExceptione) {
returnfalse;
}
if (id == ' ')
returntrue;
ElementoMapaelemento = elementos.get(id);
if (elemento != null) {
//System.out.println("Elemento: " + elemento.getSimbolo() + " " + elemento.getCor());
returnelemento.podeSerAtravessado();
}
}
returnfalse;
}
publicStringinterage() {
// A sequência utilizada para a interação é a mesma passada pelos for, ou seja, por linha, da esquerda para direita
intraioInteracao = 1; // Raio de interação 3x3 centrado no personagem
for (inti = y / TAMANHO_CELULA - 1 - raioInteracao; i <= y / TAMANHO_CELULA - 1 + raioInteracao; i++) {
for (intj = x / TAMANHO_CELULA - raioInteracao; j <= x / TAMANHO_CELULA + raioInteracao; j++) {
if (i >= 0 && i < mapa.size() && j >= 0 && j < mapa.get(i).length()) {
ElementoMapaelemento = getElemento(j, i);
if (elemento != null && elemento.podeInteragir()) {
returnverificaInteracao(elemento, j, i);
}
}
}
}
return"Não há nenhum elemento para interagir";
}
privateStringverificaInteracao(ElementoMapaelemento, intj, inti) {
StringmensagemInteracao = elemento.interage();
Stringnome = elemento.getClass().getName();
if(nome.equals("Inimigo")) {
diminuiContador();
}
if(nome.equals("Machado")) {
setMachado(true);
}
if(nome.equals("Amigo")) {
intx = 16;
while (x<20) {
removeElemento(x, 5);
x++;
}
}
if (elemento.aposInteracao()) {
if(nome.equals("VegetacaoCortavel")) {
if (getMachado()) {
removeElemento(j,i);
}
else {
return"Você não possui um machado";
}
}
if(getContador() == 2) {
removeElemento(44, 21);
}
removeElemento(j, i); // Remove o elemento se necessário
}
returnmensagemInteracao;
}
privatevoidremoveElemento(intx, inty) {
if (y >= 0 && y < mapa.size() && x >= 0 && x < mapa.get(y).length()) {
StringBuilderlinha = newStringBuilder(mapa.get(y));
linha.setCharAt(x, ' ');
mapa.set(y, linha.toString());
}
}
publicbooleangetMachado() {
returnmachado;
}
publicvoidsetMachado(booleanstatus) {
this.machado = status;
}
publicintgetContador() {
returncontador;
}
publicvoiddiminuiContador() {
this.contador--;
}
publicStringataca() {
return"Acho que isso não faz nada...";
}
privatevoidcarregaMapa(Stringfilename) {
try (BufferedReaderbr = newBufferedReader(newFileReader(filename))) {
Stringline;
while ((line = br.readLine()) != null) {
mapa.add(line);
// Se character 'P' está contido na linha atual, então define a posição inicial do personagem
if (line.contains("P")) {
x = line.indexOf('P') * TAMANHO_CELULA;
y = mapa.size() * TAMANHO_CELULA;
// Remove o personagem da linha para evitar que seja desenhado
mapa.set(mapa.size() - 1, line.replace('P', ' '));
}
}
} catch (IOExceptione) {
e.printStackTrace();
}
}
// Método para atualizar as células reveladas
privatevoidatualizaCelulasReveladas() {
if (mapa == null)
return;
for (inti = Math.max(0, y / TAMANHO_CELULA - RAIO_VISAO); i < Math.min(mapa.size(), y / TAMANHO_CELULA + RAIO_VISAO + 1); i++) {
for (intj = Math.max(0, x / TAMANHO_CELULA - RAIO_VISAO); j < Math.min(mapa.get(i).length(), x / TAMANHO_CELULA + RAIO_VISAO + 1); j++) {
areaRevelada[i][j] = true;
}
}
}
// Registra os elementos do mapa
privatevoidregistraElementos() {
// Parede
elementos.put('#', newParede('▣', brickColor));
// Vegetação
elementos.put('V', newVegetacao('♣', vegetationColor));
//Adicionais
elementos.put('X', newVegetacaoCortavel('♣', arvoreCortColor));
elementos.put('M', newMachado('¶', axeColor));
elementos.put('C', newInimigo('ϫ', cupimColor));
elementos.put('A', newAmigo('ʘ', amigoColor));
elementos.put('D', newPlaca('Ƥ', placaColor));
elementos.put('G', newPorta('Ħ', portaColor));
}
}