Advertisement
Guest User

Untitled

a guest
Jan 16th, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.66 KB | None | 0 0
  1. package com.voxel.engine.core.Chunk;
  2.  
  3. import com.voxel.engine.core.Block.Block;
  4. import com.voxel.engine.core.Noise.SimplexNoise;
  5. import org.lwjgl.opengl.GL11;
  6.  
  7. import java.util.Random;
  8.  
  9. import static org.lwjgl.opengl.GL11.*;
  10.  
  11. /**
  12. * Created with IntelliJ IDEA.
  13. * User: Toby's PC
  14. * Date: 07/01/14
  15. * Time: 19:25
  16. */
  17. public class Chunk implements ChunkProtocol{
  18.  
  19. private int chunkList = -1;
  20.  
  21. public int maxHeight = 0;
  22. private int tx = 0;
  23. private int ty = 0;
  24. private int tz = 0;
  25.  
  26. public static final int CHUNK_SIZE = 16;
  27.  
  28. public Block[][][] blocks;
  29.  
  30. private int xOffset = 0;
  31. private int yOffset = 0;
  32. private int zOffset = 0;
  33.  
  34. private String key;
  35.  
  36. public String getKey(){
  37. return key;
  38. }
  39.  
  40. public void setKey(String key){
  41. this.key = key;
  42. }
  43.  
  44. public int getX(){
  45. return xOffset;
  46. }
  47.  
  48. public int getY(){
  49. return yOffset;
  50. }
  51.  
  52. public int getZ(){
  53. return xOffset;
  54. }
  55.  
  56. public Block getBlock(int x, int y, int z){
  57. return blocks[x][y][z];
  58. }
  59.  
  60. public int rebuildChunk(){
  61. int count = 0;
  62. if(chunkList <= -1){
  63. chunkList = glGenLists(1);
  64. }
  65. glNewList(chunkList, GL_COMPILE);
  66. count = createChunkList();
  67. glEndList();
  68.  
  69. return count;
  70. }
  71.  
  72. private int createChunkList(){
  73. tx = 0;
  74. ty = 0;
  75. tz = 0;
  76.  
  77. int iHidden = 0;
  78. int iBlockCount = 0;
  79.  
  80. boolean bLeft = !false;
  81. boolean bRight = !false;
  82. boolean bAbove = !false;
  83. boolean bBelow = !false;
  84. boolean bFront = !false;
  85. boolean bBack = !false;
  86.  
  87. boolean bDefault = true;
  88.  
  89. glEnable(GL11.GL_CULL_FACE);
  90. glBegin(GL11.GL_QUADS);
  91.  
  92. for(int x = 0;x < CHUNK_SIZE;x++){
  93. for(int y = 0;y < CHUNK_SIZE;y++){
  94. for(int z = 0;z < CHUNK_SIZE;z++){
  95. if (!blocks[x][y][z].isActive()) {
  96. continue;
  97. }
  98. bLeft = bDefault;
  99. if (x > 0) {
  100. bLeft = blocks[x - 1][y][z].isActive();
  101. } else
  102. bLeft = false;
  103. bRight = bDefault;
  104. if (x < Chunk.CHUNK_SIZE - 1) {
  105. bRight = blocks[x + 1][y][z].isActive();
  106. } else
  107. bRight = false;
  108. bAbove = bDefault;
  109. if (y > 0) {
  110. bAbove = blocks[x][y - 1][z].isActive();
  111. } else
  112. bAbove = false;
  113. bBelow = bDefault;
  114. if (y < Chunk.CHUNK_SIZE - 1) {
  115. bBelow = blocks[x][y + 1][z].isActive();
  116. } else
  117. bBelow = false;
  118. bFront = bDefault;
  119. if (z > 0) {
  120. bFront = blocks[x][y][z - 1].isActive();
  121. } else
  122. bFront = false;
  123. bBack = bDefault;
  124. if (z < Chunk.CHUNK_SIZE - 1) {
  125. bBack = blocks[x][y][z + 1].isActive();
  126. } else
  127. bBack = false;
  128.  
  129. boolean bResult = bLeft & bRight & bAbove & bBelow & bFront
  130. & bBack;
  131.  
  132. if (!bResult) // Block is not hidden by neighbouring blocks
  133. {
  134. tx = ((xOffset * Chunk.CHUNK_SIZE)) + (x);// << 1);
  135. ty = ((yOffset * Chunk.CHUNK_SIZE)) + (y);// << 1);
  136. tz = ((zOffset * Chunk.CHUNK_SIZE)) + (z);// << 1);
  137.  
  138. renderBlock(blocks[x][y][z].getType(), x, y, z);
  139.  
  140. iBlockCount++; // total of blocks that can be seen
  141. } else
  142. iHidden++; // amount of blocks that are surrounded
  143. }
  144. }
  145. }
  146.  
  147. glEnd();
  148. glDisable(GL11.GL_CULL_FACE);
  149.  
  150. return iBlockCount;
  151. }
  152.  
  153. private void renderBlock(Block.BlockType type, int x, int y, int z) {
  154.  
  155. // GL11.glDisable(GL_TEXTURE_2D);
  156. if (z <= Chunk.CHUNK_SIZE - 1) { //
  157. if (z != 0 && (z == Chunk.CHUNK_SIZE - 1) || (!blocks[x][y][z + 1].isActive())) {
  158. off_glVertex3f(-0.5f, -0.5f, 0.5f);
  159. off_glVertex3f(0.5f, -0.5f, 0.5f);
  160. off_glVertex3f(0.5f, 0.5f, 0.5f);
  161. off_glVertex3f(-0.5f, 0.5f, 0.5f);
  162. }
  163. }
  164.  
  165. // Back Face
  166. if (z >= 0) { //
  167. if (z != Chunk.CHUNK_SIZE - 1 && (z == 0) || (!blocks[x][y][z - 1].isActive())) {
  168. off_glVertex3f(-0.5f, -0.5f, -0.5f);
  169. off_glVertex3f(-0.5f, 0.5f, -0.5f);
  170. off_glVertex3f(0.5f, 0.5f, -0.5f);
  171. off_glVertex3f(0.5f, -0.5f, -0.5f);
  172. }
  173. }
  174.  
  175. // Top Face
  176. if (y <= Chunk.CHUNK_SIZE - 1) { //
  177. if (y != 0 && (y == Chunk.CHUNK_SIZE - 1) || (!blocks[x][y + 1][z].isActive())) {
  178. off_glVertex3f(-0.5f, 0.5f, -0.5f);
  179. off_glVertex3f(-0.5f, 0.5f, 0.5f);
  180. off_glVertex3f(0.5f, 0.5f, 0.5f);
  181. off_glVertex3f(0.5f, 0.5f, -0.5f);
  182. }
  183. }
  184.  
  185. // Bottom Face
  186. if (y >= 0) { //
  187. if (y != Chunk.CHUNK_SIZE - 1 && (y == 0) || (!blocks[x][y - 1][z].isActive())) {
  188. off_glVertex3f(-0.5f, -0.5f, -0.5f);
  189. off_glVertex3f(0.5f, -0.5f, -0.5f);
  190. off_glVertex3f(0.5f, -0.5f, 0.5f);
  191. off_glVertex3f(-0.5f, -0.5f, 0.5f);
  192. }
  193. }
  194.  
  195. // Right face
  196. if (x <= Chunk.CHUNK_SIZE - 1) { //
  197. if (x != 0 && (x == Chunk.CHUNK_SIZE - 1) || (!blocks[x + 1][y][z].isActive())) {
  198. off_glVertex3f(0.5f, -0.5f, -0.5f);
  199. off_glVertex3f(0.5f, 0.5f, -0.5f);
  200. off_glVertex3f(0.5f, 0.5f, 0.5f);
  201. off_glVertex3f(0.5f, -0.5f, 0.5f);
  202. }
  203. }
  204. // Left Face
  205. if (x >= 0) { //
  206. if (x != Chunk.CHUNK_SIZE - 1 && (x == 0) || (!blocks[x - 1][y][z].isActive())) {
  207. off_glVertex3f(-0.5f, -0.5f, -0.5f);
  208. off_glVertex3f(-0.5f, -0.5f, 0.5f);
  209. off_glVertex3f(-0.5f, 0.5f, 0.5f);
  210. off_glVertex3f(-0.5f, 0.5f, -0.5f);
  211. glColor3f(1, 1, 1);
  212. }
  213. }
  214. }
  215.  
  216. // Draw a vertex with a offset.
  217. private void off_glVertex3f(float x, float y, float z) {
  218. glVertex3f(tx + x, ty + y, tz + z);
  219. }
  220.  
  221. public void Update() {
  222.  
  223. }
  224.  
  225. public void render() {
  226. GL11.glCallList(chunkList);
  227. }
  228.  
  229. public void remove() {
  230. GL11.glDeleteLists(chunkList, 1);
  231. chunkList = -1;
  232. }
  233.  
  234. public int SetUpFlatGround() {
  235. Random rr = new Random();
  236. for (int x = 0; x < CHUNK_SIZE; x++) {
  237. for (int y = 0; y < CHUNK_SIZE; y++) {
  238. for (int z = 0; z < CHUNK_SIZE; z++) // set these blocks on
  239. {
  240. if (y == 0)
  241. blocks[x][y][z].setActive(true);
  242. else
  243. blocks[x][y][z].setActive(false);
  244. if(rr.nextBoolean())
  245. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_GRASS);
  246. else
  247. if(rr.nextBoolean())
  248. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_DIRT);
  249. else
  250. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_STONE);
  251. }
  252. }
  253. }
  254. return this.rebuildChunk();
  255. }
  256.  
  257. // Simple tree
  258. public void drawTree(int xpos, int ypos, int zpos, int height) {
  259.  
  260. Random rr = new Random();
  261. xpos = (int) Math.abs(rr.nextInt() % 10);
  262. zpos = (int) Math.abs(rr.nextInt() % 10);
  263. if (xpos == 0)
  264. xpos = 1;
  265. if (zpos <= 0)
  266. zpos = 1;
  267. if (height + 3 < Chunk.CHUNK_SIZE - this.maxHeight) {
  268. for (int y = this.maxHeight - 1; y < height + this.maxHeight - 1; y++) {
  269. blocks[xpos][ypos + y][zpos].setActive(true);
  270. blocks[xpos][ypos + y][zpos].setType(Block.BlockType.BLOCK_TYPE_TRUNK);
  271. }
  272. drawLeaf(xpos, ypos, zpos, this.maxHeight + height, Block.BlockType.BLOCK_TYPE_TREE_LEAF_ONE);
  273. }
  274. }
  275.  
  276. public int drawLeaf(int xpos, int ypos, int zpos, int height, Block.BlockType type) {
  277.  
  278. blocks[xpos][ypos + height - 2][zpos].setActive(true);
  279. blocks[xpos][ypos + height - 2][zpos].setType(type);
  280.  
  281. blocks[xpos - 1][ypos + height - 3][zpos].setActive(true);
  282. blocks[xpos - 1][ypos + height - 3][zpos].setType(type);
  283.  
  284. blocks[xpos + 1][ypos + height - 3][zpos].setActive(true);
  285. blocks[xpos + 1][ypos + height - 3][zpos].setType(type);
  286.  
  287. blocks[xpos][ypos + height - 3][zpos + 1].setActive(true);
  288. blocks[xpos][ypos + height - 3][zpos + 1].setType(type);
  289.  
  290. blocks[xpos][ypos + height - 3][zpos - 1].setActive(true);
  291. blocks[xpos][ypos + height - 3][zpos - 1].setType(type);
  292.  
  293. return this.rebuildChunk();
  294. }
  295.  
  296. // Simple tree
  297. public void drawTree2(int xpos, int ypos, int zpos, int height, Block.BlockType type) {
  298.  
  299. Random rr = new Random();
  300. xpos = (int) Math.abs(rr.nextInt() % 8) + 2;
  301. zpos = (int) Math.abs(rr.nextInt() % 8) + 2;
  302. if (xpos == 0)
  303. xpos = 1;
  304. if (zpos <= 0)
  305. zpos = 1;
  306. if (height + 3 < Chunk.CHUNK_SIZE - this.maxHeight) {
  307. for (int y = this.maxHeight - 1; y < height + this.maxHeight - 1; y++) {
  308. blocks[xpos][ypos + y][zpos].setActive(true);
  309. blocks[xpos][ypos + y][zpos].setType(Block.BlockType.BLOCK_TYPE_TRUNK);
  310. }
  311. drawLeaf2(xpos, ypos, zpos, this.maxHeight + height, type);
  312. }
  313. }
  314.  
  315. public int drawLeaf2(int xpos, int ypos, int zpos, int height, Block.BlockType type) {
  316.  
  317. blocks[xpos][ypos + height - 1][zpos].setActive(true);
  318. blocks[xpos][ypos + height - 1][zpos].setType(type);
  319.  
  320. blocks[xpos][ypos + height - 2][zpos].setActive(true);
  321. blocks[xpos][ypos + height - 2][zpos].setType(type);
  322.  
  323. blocks[xpos][ypos + height - 3][zpos].setActive(true);
  324. blocks[xpos][ypos + height - 3][zpos].setType(type);
  325.  
  326. blocks[xpos][ypos + height - 4][zpos].setActive(true);
  327. blocks[xpos][ypos + height - 4][zpos].setType(type);
  328.  
  329. // Blocks[xpos][ypos + height-5][zpos].SetActive(true);
  330. // Blocks[xpos][ypos + height-5][zpos].SetType(type);
  331.  
  332. blocks[xpos + 1][ypos + height - 2][zpos].setActive(true);
  333. blocks[xpos + 1][ypos + height - 2][zpos].setType(type);
  334. blocks[xpos - 1][ypos + height - 2][zpos].setActive(true);
  335. blocks[xpos - 1][ypos + height - 2][zpos].setType(type);
  336.  
  337. blocks[xpos][ypos + height - 2][zpos - 1].setActive(true);
  338. blocks[xpos][ypos + height - 2][zpos - 1].setType(type);
  339. blocks[xpos][ypos + height - 2][zpos + 1].setActive(true);
  340. blocks[xpos][ypos + height - 2][zpos + 1].setType(type);
  341.  
  342. blocks[xpos + 1][ypos + height - 3][zpos].setActive(true);
  343. blocks[xpos + 1][ypos + height - 3][zpos].setType(type);
  344. blocks[xpos - 1][ypos + height - 3][zpos].setActive(true);
  345. blocks[xpos - 1][ypos + height - 3][zpos].setType(type);
  346.  
  347. blocks[xpos][ypos + height - 3][zpos - 1].setActive(true);
  348. blocks[xpos][ypos + height - 3][zpos - 1].setType(type);
  349. blocks[xpos][ypos + height - 3][zpos + 1].setActive(true);
  350. blocks[xpos][ypos + height - 3][zpos + 1].setType(type);
  351.  
  352. blocks[xpos + 2][ypos + height - 3][zpos].setActive(true);
  353. blocks[xpos + 2][ypos + height - 3][zpos].setType(type);
  354. blocks[xpos + 1][ypos + height - 3][zpos + 1].setActive(true);
  355. blocks[xpos + 1][ypos + height - 3][zpos + 1].setType(type);
  356.  
  357. blocks[xpos - 2][ypos + height - 3][zpos].setActive(true);
  358. blocks[xpos - 2][ypos + height - 3][zpos].setType(type);
  359. blocks[xpos - 1][ypos + height - 3][zpos - 1].setActive(true);
  360. blocks[xpos - 1][ypos + height - 3][zpos - 1].setType(type);
  361.  
  362. blocks[xpos][ypos + height - 3][zpos + 2].setActive(true);
  363. blocks[xpos][ypos + height - 3][zpos + 2].setType(type);
  364. blocks[xpos + 1][ypos + height - 3][zpos - 1].setActive(true);
  365. blocks[xpos + 1][ypos + height - 3][zpos - 1].setType(type);
  366.  
  367. blocks[xpos][ypos + height - 3][zpos - 2].setActive(true);
  368. blocks[xpos][ypos + height - 3][zpos - 2].setType(type);
  369. blocks[xpos - 1][ypos + height - 3][zpos + 1].setActive(true);
  370. blocks[xpos - 1][ypos + height - 3][zpos + 1].setType(type);
  371.  
  372. return this.rebuildChunk();
  373. }
  374.  
  375. public int drawFoliage(int xpos, int zpos, Block.BlockType type) {
  376. blocks[xpos][this.maxHeight + 1][zpos].setActive(true);
  377. blocks[xpos][this.maxHeight + 1][zpos].setType(type);
  378. return this.rebuildChunk();
  379. }
  380.  
  381. public int drawRandomFoliage(Block.BlockType type) {
  382. Random rr = new Random();
  383. int amountEnd = Math.abs(rr.nextInt() % 6);
  384. for (int amount = 0; amount < amountEnd; amount++) {
  385. int xpos = Math.abs(rr.nextInt() % 14);
  386. int zpos = Math.abs(rr.nextInt() % 14);
  387. blocks[xpos][this.maxHeight][zpos].setActive(true);
  388. blocks[xpos][this.maxHeight][zpos].setType(type);
  389. }
  390. return this.rebuildChunk();
  391. }
  392.  
  393. public int drawRandomBlock(Block.BlockType type, int randomMax) {
  394. Random rr = new Random();
  395. int amountEnd = Math.abs(rr.nextInt() % randomMax);
  396. for (int amount = 0; amount < amountEnd; amount++) {
  397. int xpos = Math.abs(rr.nextInt() % 14);
  398. int zpos = Math.abs(rr.nextInt() % 14);
  399. blocks[xpos][this.maxHeight][zpos].setActive(true);
  400. blocks[xpos][this.maxHeight][zpos].setType(type);
  401. }
  402. return this.rebuildChunk();
  403. }
  404.  
  405. public int drawPole(int xpos, int zpos, int ypos, int height, Block.BlockType type) {
  406. for (int y = 0; y < height; y++) {
  407. blocks[xpos][ypos + y][zpos].setActive(true);
  408. blocks[xpos][ypos + y][zpos].setType(type);
  409. }
  410. return this.rebuildChunk();
  411. }
  412.  
  413. public int drawBlock(int xpos, int zpos, int ypos, Block.BlockType type) {
  414. blocks[xpos][ypos][zpos].setActive(true);
  415. blocks[xpos][ypos][zpos].setType(type);
  416. return this.rebuildChunk();
  417. }
  418.  
  419. public int SetupLandscape() {
  420. Random rr = new Random();
  421. for (int x = 0; x < CHUNK_SIZE; x++) {
  422. for (int z = 0; z < CHUNK_SIZE; z++) {
  423. int height = (int) (SimplexNoise.noise(x / 16f, z / 16f) * 16f);
  424. if (height < 1)
  425. height = 1;
  426. for (int y = 0; y <= height; y++) // set these blocks on
  427. {
  428. blocks[x][y][z].setActive(true);
  429. if (rr.nextBoolean())
  430. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_GRASS);
  431. else
  432. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_DIRT);
  433.  
  434. }
  435. }
  436. }
  437. return this.rebuildChunk();
  438. }
  439.  
  440. public int RandomLandscape(boolean tree) {
  441. Random rr = new Random();
  442.  
  443. int height = -1;
  444. while (height < 0)
  445. height = (int) rr.nextInt() % 4;
  446.  
  447. if (height >= this.maxHeight)
  448. this.maxHeight = height + 1;
  449.  
  450. for (int x = 0; x < CHUNK_SIZE; x++) {
  451. for (int z = 0; z < CHUNK_SIZE; z++) {
  452. for (int y = 0; y <= height; y++) // set these blocks on
  453. {
  454. blocks[x][y][z].setActive(true);
  455. if (y == 0) {
  456. if (rr.nextBoolean())
  457. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_GRASS2);
  458. else {
  459. if (rr.nextBoolean())
  460. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_GRASS);
  461. else
  462. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_GRASS3);
  463. }
  464. // Blocks[x][y][z].SetType(BlockType.BlockType_Stone);
  465. }
  466. if (y == height && y != 0) {
  467. if (rr.nextBoolean())
  468. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_GRASS);
  469. else {
  470. if (rr.nextBoolean())
  471. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_GRASS2);
  472. else
  473. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_GRASS3);
  474. }
  475.  
  476. } else if (y != 0) {
  477. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_DIRT);
  478. }
  479. }
  480. }
  481. }
  482. if (tree && rr.nextBoolean()) {
  483.  
  484. int treeHeight = CHUNK_SIZE - height;
  485. try {
  486. if (rr.nextBoolean())
  487. this.drawTree(0, 1, 0, 6);// Math.abs(rr.nextInt() %
  488. // treeHeight));
  489. else {
  490. if (rr.nextBoolean())
  491. this.drawTree2(0, 1, 0, 6,
  492. Block.BlockType.BLOCK_TYPE_TREE_LEAF_TWO);// Math.abs(rr.nextInt()
  493. // %
  494. // treeHeight));
  495. else
  496. this.drawTree2(0, 1, 0, height + 4,
  497. Block.BlockType.BLOCK_TYPE_TREE_LEAF_ONE);// Math.abs(rr.nextInt()
  498. // %
  499. // treeHeight));
  500. }
  501.  
  502. } catch (ArrayIndexOutOfBoundsException ex) {
  503. System.out.println("Tree too high");
  504. }
  505. }
  506. return this.rebuildChunk();
  507. }
  508.  
  509. public int SetUpCube() {
  510. for (int x = 0; x < CHUNK_SIZE; x++) {
  511. for (int y = 0; y < CHUNK_SIZE; y++) {
  512. for (int z = 0; z < CHUNK_SIZE; z++) // set these blocks on
  513. {
  514. blocks[x][y][z].setActive(true);
  515. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_GRASS);
  516. }
  517. }
  518. }
  519. return this.rebuildChunk();
  520. }
  521.  
  522. public int SetUpSpriral() {
  523. double angle = 0;
  524. float r = 10;
  525. float xrot = r * (float) Math.cos(angle);
  526. float zrot = r * (float) Math.cos(angle);
  527.  
  528. for (int x = 0; x < CHUNK_SIZE; x++) {
  529. for (int y = 0; y < CHUNK_SIZE; y++) {
  530. for (int z = 0; z < CHUNK_SIZE; z++) // set these blocks on
  531. {
  532. blocks[x][y][z].setActive(true);
  533. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_GRASS);
  534. }
  535. }
  536. }
  537. return this.rebuildChunk();
  538. }
  539.  
  540. public int SetUpDome() {
  541. for (int x = 0; x < CHUNK_SIZE; x++) {
  542. for (int y = CHUNK_SIZE - 1; y > CHUNK_SIZE / 2; y--) {
  543. for (int z = 0; z < CHUNK_SIZE; z++) {
  544. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_STONE);
  545.  
  546. if (Math.sqrt((float) (x - CHUNK_SIZE / 2)
  547. * (x - CHUNK_SIZE / 2) + (y - CHUNK_SIZE / 2)
  548. * (y - CHUNK_SIZE / 2) + (z - CHUNK_SIZE / 2)
  549. * (z - CHUNK_SIZE / 2)) <= CHUNK_SIZE / 2) {
  550. blocks[x][y-8][z].setActive(true);
  551. blocks[x][y-8][z].setType(Block.BlockType.BLOCK_TYPE_GRASS3);
  552.  
  553. } else {
  554. blocks[x][0][z].setType(Block.BlockType.BLOCK_TYPE_GRASS);
  555. blocks[x][0][z].setActive(true);
  556. }
  557.  
  558. }
  559. }
  560. }
  561. return this.rebuildChunk();
  562. }
  563.  
  564. public int SetUpSphere() {
  565. for (int x = 0; x < CHUNK_SIZE; x++) {
  566. for (int y = 0; y < CHUNK_SIZE; y++) {
  567. for (int z = 0; z < CHUNK_SIZE; z++) {
  568. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_STONE);
  569.  
  570. if (Math.sqrt((float) (x - CHUNK_SIZE / 2)
  571. * (x - CHUNK_SIZE / 2) + (y - CHUNK_SIZE / 2)
  572. * (y - CHUNK_SIZE / 2) + (z - CHUNK_SIZE / 2)
  573. * (z - CHUNK_SIZE / 2)) <= CHUNK_SIZE / 2) {
  574. blocks[x][y][z].setActive(true);
  575. } else {
  576. blocks[x][y][z].setActive(false);
  577. }
  578.  
  579. }
  580. }
  581. }
  582. return this.rebuildChunk();
  583. }
  584.  
  585. public int SetUpPyramid() {
  586. Random rr = new Random();
  587. boolean b = rr.nextBoolean();
  588. float xIndex = 0;
  589. float zIndex = 0;
  590. for (int y = 1; y < CHUNK_SIZE - 2; y++) {
  591. for (int z = (int) zIndex; z < CHUNK_SIZE - (int) zIndex; z++) {
  592. for (int x = (int) xIndex; x < CHUNK_SIZE - (int) xIndex; x++) {
  593. blocks[x][y][z].setActive(true);
  594. if (rr.nextBoolean())
  595. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_GRASS);
  596. else
  597. blocks[x][y][z].setType(Block.BlockType.BLOCK_TYPE_GRASS3);
  598. }
  599. }
  600. if (b) {
  601. xIndex += 0.5f;
  602. zIndex += 0.5f;
  603. } else {
  604. xIndex++;
  605. zIndex++;
  606. }
  607.  
  608. }
  609. return this.rebuildChunk();
  610. }
  611.  
  612. public Chunk(int xOffset, int yOffset, int zOffset) {
  613. this.xOffset = xOffset;
  614. this.yOffset = yOffset;
  615. this.zOffset = zOffset;
  616. blocks = new Block[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
  617. for (int x = 0; x < CHUNK_SIZE; x++) {
  618. for (int y = 0; y < CHUNK_SIZE; y++) {
  619. for (int z = 0; z < CHUNK_SIZE; z++) {
  620. blocks[x][y][z] = new Block(Block.BlockType.BLOCK_TYPE_DIRT);
  621. blocks[x][y][z].setActive(false);
  622. }
  623. }
  624. }
  625. }
  626. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement