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
|
package net.tylermurphy.Minecraft.Scene;
import java.util.Collection;
import java.util.HashMap;
import java.util.stream.Collectors;
import net.tylermurphy.Minecraft.Chunk.Cube;
import net.tylermurphy.Minecraft.Scene.Objects.WorldOrigin;
import net.tylermurphy.Minecraft.Chunk.Chunk;
import net.tylermurphy.Minecraft.Tick.BlockUpdate;
public class World {
private static final HashMap<String,Chunk> chunks = new HashMap<>();
public static Camera camera;
public static Player player;
public static int seed;
public static int renderDistance;
public static WorldOrigin world_origin;
public static byte getBlock(int x, int y, int z) {
if(y<0 || y> 255) return Cube.AIR;
int cx = (int)Math.floor(x/16.0) + world_origin.x()/16;
int cz = (int)Math.floor(z/16.0) + world_origin.z()/16;
Chunk chunk = getChunk(cx, cz);
if(chunk==null) return Cube.NULL;
int rx = x-16*(int)Math.floor(x/16.0);
int rz = z-16*(int)Math.floor(z/16.0);
return chunk.cubes[rx][y][rz];
}
public static int getHighestBlock(int fx, int fz) {
for(int y=255;y>=0;y--) {
if(getBlock(fx,y,fz) != -1) return y;
}
return 255;
}
public static void setBlock(int x, int y, int z, byte id) {
if(y<0 || y> 255) return;
int cx = (int)Math.floor(x/16.0) + world_origin.x()/16;
int cz = (int)Math.floor(z/16.0) + world_origin.z()/16;
Chunk chunk = getChunk(cx, cz);
if(chunk==null) return;
int rx = x-16*(int)Math.floor(x/16.0);
int rz = z-16*(int)Math.floor(z/16.0);
BlockUpdate.createBlockUpdate(x, y, z, chunk.cubes[rx][y][rz], id);
chunk.scheduleFutureMeshUpdate();
chunk.cubes[rx][y][rz] = id;
if(id != -1 && y > 0 && chunk.cubes[rx][y - 1][rz] == 0)
chunk.cubes[rx][y -1][rz] = 1;
int ox = (rx%16) == 0 ? -1 : (rx%16) == 15 ? 1 : 0;
int oz = (rz%16) == 0 ? -1 : (rz%16) == 15 ? 1 : 0;
Chunk coc = null,coz = null;
if(ox != 0) coc = getChunk(chunk.gridX + ox, chunk.gridZ);
if(oz != 0) coz = getChunk(chunk.gridX, chunk.gridZ + oz);
if(coc != null) {
coc.scheduleFutureMeshUpdate();
}
if(coz != null) {
coz.scheduleFutureMeshUpdate();
}
}
public static Chunk getChunk(int x, int z){
Chunk c = chunks.get(x+":"+z);
return c == null ? null : c.isSafe() ? c : null;
}
public static Chunk getChunkUnsafe(int x, int z){
return chunks.get(x+":"+z);
}
public static void addChunk(Chunk c){
chunks.put(c.gridX+":"+c.gridZ, c);
}
public static void removeChunk(int x, int z){
chunks.remove(x+":"+z);
}
public static Collection<Chunk> getChunks(){
return chunks.values().stream().filter(Chunk::isSafe).collect(Collectors.toList());
}
}
|