-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel.java
More file actions
288 lines (263 loc) · 9.4 KB
/
Level.java
File metadata and controls
288 lines (263 loc) · 9.4 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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Level {
private static final int WATER_LEVEL = 65;
private String mapFilePath;
private Chunk[][] chunks;
private Game game;
private int seed;
public Level(Game game, String mapFilePath) {
this.game = game;
this.mapFilePath = mapFilePath;
this.chunks = new Chunk[game.getRenderDistance() * 2 + 1][game.getRenderDistance() * 2 + 1];
updateChunks(game.getPlayer().getChunkX(), game.getPlayer().getChunkZ());
this.seed = (int) (Math.random() * Integer.MAX_VALUE);
}
public Level(Game game) {
this(game, null);
this.mapFilePath = "map";
File mapFolder = new File(mapFilePath);
if (mapFolder.exists()) {
// delete all files in the folder
for (File file : mapFolder.listFiles()) {
file.delete();
}
mapFolder.delete();
}
mapFolder.mkdir();
}
private void saveChunk(Chunk chunk) {
File file = new File(mapFilePath + "/" + chunk.getX() + "_" + chunk.getZ() + ".chunk");
if (file.exists()) {
file.delete();
}
try {
file.createNewFile();
FileWriter writer = new FileWriter(file);
writer.write(chunk.toSaveString());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private Chunk loadChunk(int x, int z) {
File file = new File(mapFilePath + "/" + x + "_" + z + ".chunk");
if (!file.exists()) {
return generateChunk(x, z);
}
try {
Scanner reader = new Scanner(file);
String[] chunkData = reader.nextLine().split(";");
if (chunkData.length != 2) {
reader.close();
throw new RuntimeException("Invalid chunk file");
}
Block[][][] blocks = new Block[Chunk.CHUNK_SIZE][Integer.parseInt(chunkData[0])][Chunk.CHUNK_SIZE];
String[] blocStrings = chunkData[1].split("_");
for (int i = 0; i < blocStrings.length; i++) {
String[] blockData = blocStrings[i].split("-");
int blockX = Integer.parseInt(blockData[0]);
int blockY = Integer.parseInt(blockData[1]);
int blockZ = Integer.parseInt(blockData[2]);
int blockTypeId = Integer.parseInt(blockData[3]);
blocks[blockX][blockY][blockZ] = new Block(new Vector3(blockX + x * Chunk.CHUNK_SIZE, blockY,
blockZ + z * Chunk.CHUNK_SIZE), BlockType.getBlockType(blockTypeId));
}
reader.close();
return new Chunk(x, z, blocks);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return null;
}
private Chunk generateChunk(int x, int z) {
Chunk chunk = new Chunk(x, z);
for (int i = 0; i < Chunk.CHUNK_SIZE; i++) {
for (int j = 0; j < Chunk.CHUNK_SIZE; j++) {
int xBlock = i + x * Chunk.CHUNK_SIZE;
int zBlock = j + z * Chunk.CHUNK_SIZE;
int topY = (int) Math.floor((WorldGenUtils.fractalNoise2(xBlock, zBlock, 6) + 1) * 30) + 35;
for (int k = 0; k < topY; k++) {
BlockType blockType = BlockType.STONE;
if (k == topY - 1) {
if (k <= WATER_LEVEL) {
blockType = BlockType.SAND;
} else {
blockType = BlockType.GRASS;
}
} else if (k > topY - 5) {
if (k <= WATER_LEVEL) {
blockType = BlockType.SAND;
} else {
blockType = BlockType.DIRT;
}
}
chunk.setBlock(new Block(new Vector3(i + x * Chunk.CHUNK_SIZE, k, j + z * Chunk.CHUNK_SIZE),
blockType));
}
for (int k = topY; k <= WATER_LEVEL; k++) {
chunk.setBlock(new Block(new Vector3(i + x * Chunk.CHUNK_SIZE, k, j + z * Chunk.CHUNK_SIZE),
BlockType.WATER));
}
}
}
return chunk;
}
private void updateChunks(int chunkX, int chunkZ) {
Chunk[][] newChunks = new Chunk[game.getRenderDistance() * 2 + 1][game.getRenderDistance() * 2 + 1];
for (int i = 0; i < chunks.length; i++) {
for (int j = 0; j < chunks[0].length; j++) {
if (chunks[i][j] == null) {
continue;
}
int newI = chunks[i][j].getX() - chunkX + game.getRenderDistance();
int newJ = chunks[i][j].getZ() - chunkZ + game.getRenderDistance();
if (newI >= 0 && newI < newChunks.length && newJ >= 0 && newJ < newChunks[0].length) {
newChunks[newI][newJ] = chunks[i][j];
} else {
saveChunk(chunks[i][j]);
}
}
}
chunks = newChunks;
Thread loadChunksThread = new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < newChunks.length; i++) {
for (int j = 0; j < newChunks[0].length; j++) {
if (chunks[i][j] == null) {
chunks[i][j] = loadChunk(chunkX + i - game.getRenderDistance(),
chunkZ + j - game.getRenderDistance());
;
if (i > 0 && chunks[i - 1][j] != null) {
chunks[i - 1][j].setMeshDirty();
}
if (i < chunks.length - 1 && chunks[i + 1][j] != null) {
chunks[i + 1][j].setMeshDirty();
}
if (j > 0 && chunks[i][j - 1] != null) {
chunks[i][j - 1].setMeshDirty();
}
if (j < chunks[0].length - 1 && chunks[i][j + 1] != null) {
chunks[i][j + 1].setMeshDirty();
}
}
}
}
updateMeshes();
}
});
loadChunksThread.start();
}
public void updateChunksAfterCameraMovement(Vector3 cameraMovement, int oldChunkX, int oldChunkZ, int newChunkX,
int newChunkZ) {
if (cameraMovement.getX() == 0 && cameraMovement.getY() == 0 && cameraMovement.getZ() == 0) {
return;
}
if (cameraMovement.getY() != 0) {
for (int i = 0; i < chunks.length; i++) {
for (int j = 0; j < chunks[0].length; j++) {
if (chunks[i][j] != null) {
chunks[i][j].setMeshDirty();
}
}
}
} else {
if (cameraMovement.getX() != 0) {
int middleChunk = game.getRenderDistance();
for (int i = 0; i < chunks[0].length; i++) {
if (chunks[middleChunk][i] != null) {
chunks[middleChunk][i].setMeshDirty();
}
}
}
if (cameraMovement.getZ() != 0) {
int middleChunk = game.getRenderDistance();
for (int i = 0; i < chunks[0].length; i++) {
if (chunks[i][middleChunk] != null) {
chunks[i][middleChunk].setMeshDirty();
}
}
}
}
// if changed chunks, update the chunks array so that the player is still at its
// center, and generate new chunks if necessary
if (oldChunkX != newChunkX || oldChunkZ != newChunkZ) {
updateChunks(newChunkX, newChunkZ);
} else {
new Thread(new Runnable() {
@Override
public void run() {
updateMeshes();
}
}).start();
}
}
// Assumes that the player is in the center of the chunks array
// So assumes that updateChunksAfterCameraMovement has been called and was
// successful
//
// TODO: Save a copy so that we don't have to recalculate every time but only
// when the player moves to a new chunk
private int getSortedChunksHelper(Chunk[] sortedChunks, int index, int x, int z) {
if (chunks[x][z] == null) {
return index;
}
sortedChunks[index] = chunks[x][z];
return index + 1;
}
public Chunk[] getSortedChunks() {
Chunk[] sortedChunks = new Chunk[chunks.length * chunks[0].length];
int index = 0;
for (int x = 0; x < chunks.length / 2; x++) {
for (int z = 0; z < chunks[0].length / 2; z++) {
index = getSortedChunksHelper(sortedChunks, index, x, z);
}
for (int z = chunks[0].length - 1; z >= chunks[0].length / 2; z--) {
index = getSortedChunksHelper(sortedChunks, index, x, z);
}
}
for (int x = chunks.length - 1; x >= chunks.length / 2; x--) {
for (int z = 0; z < chunks[0].length / 2; z++) {
index = getSortedChunksHelper(sortedChunks, index, x, z);
}
for (int z = chunks[0].length - 1; z >= chunks[0].length / 2; z--) {
index = getSortedChunksHelper(sortedChunks, index, x, z);
}
}
return sortedChunks;
}
public Chunk getChunk(int x, int z) {
if (x < game.getPlayer().getChunkX() - game.getRenderDistance()
|| x > game.getPlayer().getChunkX() + game.getRenderDistance()
|| z < game.getPlayer().getChunkZ() - game.getRenderDistance()
|| z > game.getPlayer().getChunkZ() + game.getRenderDistance()) {
return null;
}
return chunks[x - game.getPlayer().getChunkX() + game.getRenderDistance()][z - game.getPlayer().getChunkZ()
+ game.getRenderDistance()];
}
public Block getBlock(int x, int y, int z) {
Chunk c = getChunk((x / Chunk.CHUNK_SIZE), (z / Chunk.CHUNK_SIZE));
if (c != null) {
return c.getBlock(x % Chunk.CHUNK_SIZE, y, z % Chunk.CHUNK_SIZE);
}
return null;
}
public Block getBlock(Vector3 position) {
return getBlock((int) position.getX(), (int) position.getY(), (int) position.getZ());
}
public void updateMeshes() {
Vector3 cameraPosition = game.getPlayer().getCamera().getPosition();
for (int i = 0; i < chunks.length; i++) {
for (int j = 0; j < chunks[0].length; j++) {
if (chunks[i][j] != null) {
chunks[i][j].updateMesh(cameraPosition);
}
}
}
}
}