Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 16th, 2014  |  syntax: None  |  size: 4.31 KB  |  views: 4  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.voxel.engine.core.Chunk;
  2.  
  3. import com.voxel.engine.core.Block.Block;
  4. import com.voxel.engine.core.Camera.FPCameraController;
  5.  
  6. import java.util.HashMap;
  7.  
  8. /**
  9.  * Created with IntelliJ IDEA.
  10.  * User: Toby's PC
  11.  * Date: 12/01/14
  12.  * Time: 16:04
  13.  * To change this template use File | Settings | File Templates.
  14.  */
  15. public class ChunkManager {
  16.  
  17.     private static ChunkManager instance = null;
  18.  
  19.     public synchronized static ChunkManager getInstance(){
  20.         if(instance == null){
  21.             instance = new ChunkManager();
  22.         }
  23.         return instance;
  24.     }
  25.  
  26.     static int cubeCountDrawn = 0;
  27.  
  28.     private HashMap<String, Chunk> chunkList = new HashMap<String, Chunk>();
  29.     private HashMap<String, Chunk> visibleList = new HashMap<String, Chunk>();
  30.  
  31.     public enum ChunkType{
  32.         CHUNK_TYPE_DEFAULT(0),
  33.         CHUNK_TYPE_LAND(1),
  34.         CHUNK_TYPE_SPHERE(2),
  35.         CHUNK_TYPE_FLAT(3),
  36.         CHUNK_TYPE_RANDOM(4),
  37.         CHUNK_TYPE_PYRAMID(5),
  38.         CHUNK_TYPE_DOME(6);
  39.  
  40.         private int chunkID;
  41.  
  42.         ChunkType(int i){
  43.             chunkID = i;
  44.         }
  45.  
  46.         public int getID(){
  47.             return chunkID;
  48.         }
  49.     }
  50.  
  51.     public void updateChunk(){
  52.         for(Chunk c : chunkList.values()){
  53.             if(FPCameraController.getInstance().frustum.cubeInFrustum(c.getX() << 4, c.getY() << 4, c.getZ() << 4, Chunk.CHUNK_SIZE)){
  54.                 if(!this.visibleList.containsKey(c.getKey())){
  55.                     this.visibleList.put(c.getKey(), c);
  56.                 }
  57.             }else{
  58.                 if(this.visibleList.containsKey(c.getKey())){
  59.                     this.visibleList.remove(c.getKey());
  60.                 }
  61.             }
  62.         }
  63.     }
  64.  
  65.     public void render(){
  66.         for(Chunk c : chunkList.values()){
  67.             if(FPCameraController.getInstance().frustum.cubeInFrustum(c.getX() << 4, c.getY() << 4, c.getZ() << 4, Chunk.CHUNK_SIZE)){
  68.                 c.render();
  69.             }
  70.         }
  71.     }
  72.  
  73.     public ChunkManager(){
  74.  
  75.     }
  76.  
  77.     public Block getBlockInChunk(String chunkKey, int x, int y, int z){
  78.         Block block = null;
  79.  
  80.         try{
  81.             Chunk chunk = null;
  82.             chunk = getChunk(chunkKey);
  83.             block = chunk.getBlock(x, y, z);
  84.         }catch(Exception e){
  85.  
  86.         }
  87.         return block;
  88.     }
  89.  
  90.     public Block getBlockInChunk(int chunk1, int x, int y, int z){
  91.         Block block = null;
  92.  
  93.         try{
  94.             Chunk chunk = null;
  95.             String key = String.format("%d%d%d", x, y, z);
  96.             chunk = getChunk(key);
  97.             block = chunk.getBlock(x, y, z);
  98.         }catch (Exception e){
  99.  
  100.         }
  101.         return block;
  102.     }
  103.  
  104.     public int getChunkCount(){
  105.         return chunkList.size();
  106.     }
  107.  
  108.     public Chunk getChunk(int index){
  109.         if(index <= chunkList.size()){
  110.             Chunk chunk = chunkList.get(index);
  111.             return chunk;
  112.         }
  113.         return null;
  114.     }
  115.  
  116.     public Chunk getChunk(String key){
  117.         Chunk chunk = null;
  118.  
  119. //        if(index <= chunkList.size())
  120.         {
  121.             chunk = chunkList.get(key);
  122.         }
  123.         return chunk;
  124.     }
  125.  
  126.     public void addChunk(ChunkType chunkType, int xOffset, int yOffset, int zOffset){
  127.         String key = String.format("%d%d%d", xOffset, yOffset, zOffset);
  128.         Chunk chunk = new Chunk(xOffset, yOffset, zOffset);
  129.         chunk.setKey(key);
  130.         chunkList.put(key, chunk);
  131.  
  132.         switch (chunkType){
  133.             case CHUNK_TYPE_LAND:
  134.                 cubeCountDrawn += chunk.SetupLandscape();
  135.                 break;
  136.             case CHUNK_TYPE_SPHERE:
  137.                 cubeCountDrawn += chunk.SetUpSphere();
  138.                 break;
  139.             case CHUNK_TYPE_DEFAULT:
  140.                 cubeCountDrawn += chunk.SetUpCube();
  141.                 break;
  142.             case CHUNK_TYPE_FLAT:
  143.                 cubeCountDrawn += chunk.SetUpFlatGround();
  144.                 break;
  145.             case CHUNK_TYPE_RANDOM:
  146.                 cubeCountDrawn += chunk.RandomLandscape(false);
  147.                 break;
  148.             case CHUNK_TYPE_PYRAMID:
  149.                 cubeCountDrawn += chunk.SetUpPyramid();
  150.                 break;
  151.             case CHUNK_TYPE_DOME:
  152.                 cubeCountDrawn += chunk.SetUpDome();
  153.                 break;
  154.             default:
  155.         }
  156.     }
  157. }