aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/lodmod/renderer/LODChunk.java
blob: 5ee5f358f4b6b653ba8d143b178f11362dc2558d (plain)
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
package makamys.lodmod.renderer;

import java.util.List;

import makamys.lodmod.LODMod;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagEnd;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.world.chunk.Chunk;

public class LODChunk {
	
	int x, z;
	Chunk chunk;
	public boolean waitingForData = false;
	int lod = 0;
	boolean visible;
	
	SimpleChunkMesh[] simpleMeshes = new SimpleChunkMesh[2];
	ChunkMesh[] chunkMeshes = new ChunkMesh[32];
	
	LODRenderer renderer = LODMod.renderer;
	
	public LODChunk(int x, int z) {
		this.x = x;
		this.z = z;
	}
	
	public LODChunk(NBTTagCompound nbt, List<String> spriteList) {
	    this.x = nbt.getInteger("x");
	    this.z = nbt.getInteger("z");
	    
	    NBTTagCompound chunkMeshesCompound = nbt.getCompoundTag("chunkMeshes");
	    for(Object o : chunkMeshesCompound.func_150296_c()) {
	        String key = (String)o;
	        int keyInt = Integer.parseInt(key);
	        
	        byte[] data = chunkMeshesCompound.getByteArray(key);
	        
	        chunkMeshes[keyInt] = new ChunkMesh(x, keyInt / 2, z, new ChunkMesh.Flags(true, true, true, false), data.length / (2 + 4 * (3 + 2 + 2 + 4)), data, spriteList, keyInt % 2);
	    }
	    
	}
	
	@Override
	public String toString() {
		return "LODChunk(" + x + ", " + z + ")";
	}
	
	public double distSq(Entity entity) {
		return Math.pow(entity.posX - x * 16, 2) + Math.pow(entity.posZ - z * 16, 2);
	}
	
	public void putChunkMeshes(int cy, List<ChunkMesh> newChunkMeshes) {
		for(int i = 0; i < 2; i++) {
			if(chunkMeshes[cy * 2 + i] != null) {
			    renderer.setMeshVisible(chunkMeshes[cy * 2 + i], false);
			    chunkMeshes[cy * 2 + i].destroy();
				chunkMeshes[cy * 2 + i] = null;
			}
		}
		
		for(int i = 0; i < newChunkMeshes.size(); i++) {
			chunkMeshes[cy * 2 + i] = newChunkMeshes.get(i);
			if(newChunkMeshes.get(i) != null) {
			    chunkMeshes[cy * 2 + i].pass = i;
			}
		}
	}
	
	public void putSimpleMeshes(List<SimpleChunkMesh> meshes) {
	    for(SimpleChunkMesh sm : simpleMeshes) {
	        if(sm!= null) {
	            renderer.setMeshVisible(sm, false);
	        }
	    }
	    
	    simpleMeshes = meshes.toArray(new SimpleChunkMesh[0]);
	}
	
	public boolean hasChunkMeshes() {
		for(ChunkMesh cm : chunkMeshes) {
			if(cm != null) {
				return true;
			}
		}
		return false;
	}
	
	public void tick(Entity player) {
		double distSq = distSq(player);
		if(distSq < Math.pow((LODMod.renderer.renderRange / 2) * 16, 2)) {
		    renderer.setLOD(this, 2);
		} else if(distSq < Math.pow((LODMod.renderer.renderRange) * 16, 2)) {
		    renderer.setLOD(this, 1);
		} else {
		    renderer.setLOD(this, 0);
		}
	}
	
	public NBTTagCompound saveToNBT() {
	    NBTTagCompound nbt = new NBTTagCompound();
	    nbt.setInteger("x", x);
	    nbt.setInteger("z", z);
	    NBTTagCompound chunkMeshesCompound = new NBTTagCompound();
	    for(int i = 0; i < chunkMeshes.length; i++) {
	        if(chunkMeshes[i] != null) {
	            chunkMeshesCompound.setTag(String.valueOf(i), chunkMeshes[i].nbtData);
	        }
	    }
	    nbt.setTag("chunkMeshes", chunkMeshesCompound);
	    return nbt;
	}
	
}