Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.voxel.engine.core.Chunk;
- import com.voxel.engine.core.Block.Block;
- import com.voxel.engine.core.Camera.FPCameraController;
- import java.util.HashMap;
- /**
- * Created with IntelliJ IDEA.
- * User: Toby's PC
- * Date: 12/01/14
- * Time: 16:04
- * To change this template use File | Settings | File Templates.
- */
- public class ChunkManager {
- private static ChunkManager instance = null;
- public synchronized static ChunkManager getInstance(){
- if(instance == null){
- instance = new ChunkManager();
- }
- return instance;
- }
- static int cubeCountDrawn = 0;
- private HashMap<String, Chunk> chunkList = new HashMap<String, Chunk>();
- private HashMap<String, Chunk> visibleList = new HashMap<String, Chunk>();
- public enum ChunkType{
- CHUNK_TYPE_DEFAULT(0),
- CHUNK_TYPE_LAND(1),
- CHUNK_TYPE_SPHERE(2),
- CHUNK_TYPE_FLAT(3),
- CHUNK_TYPE_RANDOM(4),
- CHUNK_TYPE_PYRAMID(5),
- CHUNK_TYPE_DOME(6);
- private int chunkID;
- ChunkType(int i){
- chunkID = i;
- }
- public int getID(){
- return chunkID;
- }
- }
- public void updateChunk(){
- for(Chunk c : chunkList.values()){
- if(FPCameraController.getInstance().frustum.cubeInFrustum(c.getX() << 4, c.getY() << 4, c.getZ() << 4, Chunk.CHUNK_SIZE)){
- if(!this.visibleList.containsKey(c.getKey())){
- this.visibleList.put(c.getKey(), c);
- }
- }else{
- if(this.visibleList.containsKey(c.getKey())){
- this.visibleList.remove(c.getKey());
- }
- }
- }
- }
- public void render(){
- for(Chunk c : chunkList.values()){
- if(FPCameraController.getInstance().frustum.cubeInFrustum(c.getX() << 4, c.getY() << 4, c.getZ() << 4, Chunk.CHUNK_SIZE)){
- c.render();
- }
- }
- }
- public ChunkManager(){
- }
- public Block getBlockInChunk(String chunkKey, int x, int y, int z){
- Block block = null;
- try{
- Chunk chunk = null;
- chunk = getChunk(chunkKey);
- block = chunk.getBlock(x, y, z);
- }catch(Exception e){
- }
- return block;
- }
- public Block getBlockInChunk(int chunk1, int x, int y, int z){
- Block block = null;
- try{
- Chunk chunk = null;
- String key = String.format("%d%d%d", x, y, z);
- chunk = getChunk(key);
- block = chunk.getBlock(x, y, z);
- }catch (Exception e){
- }
- return block;
- }
- public int getChunkCount(){
- return chunkList.size();
- }
- public Chunk getChunk(int index){
- if(index <= chunkList.size()){
- Chunk chunk = chunkList.get(index);
- return chunk;
- }
- return null;
- }
- public Chunk getChunk(String key){
- Chunk chunk = null;
- // if(index <= chunkList.size())
- {
- chunk = chunkList.get(key);
- }
- return chunk;
- }
- public void addChunk(ChunkType chunkType, int xOffset, int yOffset, int zOffset){
- String key = String.format("%d%d%d", xOffset, yOffset, zOffset);
- Chunk chunk = new Chunk(xOffset, yOffset, zOffset);
- chunk.setKey(key);
- chunkList.put(key, chunk);
- switch (chunkType){
- case CHUNK_TYPE_LAND:
- cubeCountDrawn += chunk.SetupLandscape();
- break;
- case CHUNK_TYPE_SPHERE:
- cubeCountDrawn += chunk.SetUpSphere();
- break;
- case CHUNK_TYPE_DEFAULT:
- cubeCountDrawn += chunk.SetUpCube();
- break;
- case CHUNK_TYPE_FLAT:
- cubeCountDrawn += chunk.SetUpFlatGround();
- break;
- case CHUNK_TYPE_RANDOM:
- cubeCountDrawn += chunk.RandomLandscape(false);
- break;
- case CHUNK_TYPE_PYRAMID:
- cubeCountDrawn += chunk.SetUpPyramid();
- break;
- case CHUNK_TYPE_DOME:
- cubeCountDrawn += chunk.SetUpDome();
- break;
- default:
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement