chunk: handle intervaltree<->array conversion
parent
9c6cf30120
commit
5bff4bc367
67
README.txt
67
README.txt
|
@ -1,67 +0,0 @@
|
|||
# Voxel
|
||||
|
||||
This is the readme for Voxel, try to keep it up to date with any information future-you will wish past-you
|
||||
remembered to write down
|
||||
|
||||
## Project set up
|
||||
This is a gradle project using JMonkey Engine and other java libraries
|
||||
|
||||
## How to run (for development)
|
||||
You'll want a java 11 JDK installed on your machine (Your IDE may do this for you, IntelliJ does)
|
||||
|
||||
Open this application in your preferred IDE (IntelliJ and Eclipse will support Gradle by default, netbeans will support it with a plugin). The remaining instructions are for IntelliJ but the basic principle will be the same for any IDE)
|
||||
|
||||
|
||||
### Development in IntelliJ
|
||||
- Download the latest version of IntelliJ Community (IntelliJ Ultimate is a paid for version the features of which you may consider useful but are not essential for a JMonkey project)
|
||||
- File > Open > select the top level folder of this project ( i.e. Voxel) > Ok.
|
||||
- The project will open with your project files on the left had side (IntelliJ may need to "think" for a couple of seconds before they appear)
|
||||
- IntelliJ may say "No SDK set up" and prompt you to download one, follow its instructions and allow it to download a java 11 JDK. A JDK is used for compiling java applications, a JRE is used for running them.
|
||||
- You can now add more java source files or assets to the project
|
||||
- To run the project find Voxel.java (which will be in under src/main/java/com/emamaker/voxeltest/intervaltrees) and right click > Run 'Voxel'
|
||||
|
||||
## How to package the game
|
||||
|
||||
### Distribute without a JRE
|
||||
|
||||
Either:
|
||||
|
||||
In your IDE execute the gradle task distZip (which you'll find under gradle > distributions > distZip)
|
||||
|
||||
Or:
|
||||
|
||||
In the command line open at the root of this project enter the following command: gradlew distZip
|
||||
|
||||
Then you will find a zip in the build/distributions folder. This zip will contain your game, all the libraries to run it and in the bin folder launch files (for windows and linux).
|
||||
|
||||
Note that the distribution does not contain a JRE, so java will need to be installed on the machine of anyone you give this distribution to. Alternatively you may wish to bundle a JRE with your game to remove this requirement.
|
||||
|
||||
|
||||
### Distribute with a JRE
|
||||
|
||||
Distributing with a JRE means you'll need to provide an operating specific bundle for each OS you are
|
||||
targeting (which is a disadvantage) but your end use will not have to have a JRE locally installed
|
||||
(which is an advantage).
|
||||
|
||||
Either:
|
||||
|
||||
In your IDE execute the gradle task distZip (which you'll find under gradle > distributions > buildAllDistributions)
|
||||
|
||||
Or:
|
||||
|
||||
In the command line open at the root of this project enter the following command: gradlew buildAllDistributions
|
||||
|
||||
Then you will find a series of zip in the build/distributions folder. These zip will contain your game, all the libraries to run it and an
|
||||
OS specific JRE. (The same files will also be available unzipped in a folder, which may be useful if distributing via steampipe or similar).
|
||||
|
||||
|
||||
## Next Steps
|
||||
You may wish to commit your project to a git repository to keep track of your changes (so you can roll back if anything goes wrong)
|
||||
|
||||
## Adding more libraries
|
||||
During the JMonkey Initializer you chose from a small subset of the available java libraries.
|
||||
You can add more by editing the dependencies section in the build.gradle file
|
||||
|
||||
## txt vs md
|
||||
|
||||
This readme is provided as a .txt as that is a common format openable on any machine. However, it would more normally be a .md, this will allow it to be nicely formatted by most git repositories (assuming you commit it to git). Just change the extension from .txt to .md, the syntax is already correct for an md file
|
|
@ -1,43 +1,50 @@
|
|||
package com.emamaker.voxeltest.intervaltrees;
|
||||
|
||||
import com.emamaker.voxeltest.intervaltrees.utils.Config;
|
||||
import com.emamaker.voxeltest.intervaltrees.world.WorldManager;
|
||||
import com.jme3.app.SimpleApplication;
|
||||
import com.jme3.font.BitmapText;
|
||||
import com.jme3.math.Vector3f;
|
||||
import com.jme3.renderer.RenderManager;
|
||||
import com.jme3.util.BufferUtils;
|
||||
|
||||
/**
|
||||
* This is the Main Class of your Game. It should boot up your game and do initial initialisation
|
||||
* Move your Logic into AppStates or Controls or other java classes
|
||||
* This is the Main Class of your Game. It should boot up your game and do
|
||||
* initial initialisation Move your Logic into AppStates or Controls or other
|
||||
* java classes
|
||||
*/
|
||||
public class Voxel extends SimpleApplication {
|
||||
|
||||
WorldManager worldManager = new WorldManager(this);
|
||||
Vector3f oldCamPos = new Vector3f(), pos = new Vector3f();
|
||||
|
||||
public static void main(String[] args) {
|
||||
Voxel app = new Voxel();
|
||||
app.setShowSettings(false); //Settings dialog not supported on mac
|
||||
app.setShowSettings(false); // Settings dialog not supported on mac
|
||||
app.start();
|
||||
|
||||
|
||||
BufferUtils.setTrackDirectMemoryEnabled(true);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void simpleInitApp() {
|
||||
getFlyByCamera().setMoveSpeed(20f);
|
||||
|
||||
getCamera().setLocation(new Vector3f(32f, 32f, 32f));
|
||||
getCamera().lookAt(new Vector3f(Config.CHUNK_SIZE,Config.CHUNK_SIZE, Config.CHUNK_SIZE), Vector3f.UNIT_Y);
|
||||
|
||||
worldManager.initWorld();
|
||||
worldManager.render();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void simpleUpdate(float tpf) {
|
||||
// System.out.println(this.getCamera().getLocation());
|
||||
pos.set(this.getCamera().getLocation());
|
||||
// if (!(pos.equals(oldCamPos))) System.out.println(pos);
|
||||
oldCamPos.set(pos);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void simpleRender(RenderManager rm) {
|
||||
//add render code here (if any)
|
||||
// add render code here (if any)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -321,7 +321,7 @@ public class ChunkRenderer {
|
|||
* To see the actual rendered quads rather than the wireframe, just
|
||||
* comment outthis line.
|
||||
*/
|
||||
// mat.getAdditionalRenderState().setWireframe(true);
|
||||
mat.getAdditionalRenderState().setWireframe(true);
|
||||
// mat.getAdditionalRenderState().setFaceCullMode(FaceCullMode.Front);
|
||||
|
||||
geo.setMaterial(mat);
|
||||
|
|
|
@ -5,7 +5,7 @@ import com.emamaker.voxeltest.intervaltrees.Voxel;
|
|||
public class Config {
|
||||
public Voxel game;
|
||||
|
||||
public static int CHUNK_SIZE = 4;
|
||||
public static int CHUNK_SIZE = 16;
|
||||
// return x + maxX * (y + z * maxY);
|
||||
public static int CHUNK_3DCOORD_MAX_INDEX = (CHUNK_SIZE-1) + CHUNK_SIZE * ( (CHUNK_SIZE - 1) + (CHUNK_SIZE - 1) * CHUNK_SIZE);
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.emamaker.voxeltest.intervaltrees.world;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Arrays;
|
||||
import java.util.Queue;
|
||||
|
||||
import com.emamaker.voxeltest.intervaltrees.data.Interval;
|
||||
|
@ -54,16 +55,40 @@ public class Chunk {
|
|||
for (int i = 0; i < Config.CHUNK_SIZE; i++) blocks.insertValue(Blocks.DIRT, new Interval(coord3DTo1D(0, 4, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 4, i)));
|
||||
for (int i = 0; i < Config.CHUNK_SIZE; i++) blocks.insertValue(Blocks.GRASS, new Interval(coord3DTo1D(0, 5, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 5, i)));
|
||||
}
|
||||
|
||||
public void generateStair() {
|
||||
for (int i = 0; i < Config.CHUNK_SIZE; i++) {
|
||||
for (int j = 0; j < Config.CHUNK_SIZE; j++) {
|
||||
blocks.insertValue(j < Config.CHUNK_SIZE - i - 1 ? Blocks.STONE : Blocks.GRASS, new Interval(coord3DTo1D(0, i, j), coord3DTo1D(Config.CHUNK_SIZE - 1, i, j)));
|
||||
}
|
||||
}
|
||||
|
||||
blocks.insertValue(Blocks.DIRT, new Interval(Chunk.coord3DTo1D(0, 0, 0), Chunk.coord3DTo1D(2, 0, 0)));
|
||||
blocks.insertValue(Blocks.DIRT, new Interval(Chunk.coord3DTo1D(1, 0, 2), Chunk.coord3DTo1D(2, 0, 2)));
|
||||
}
|
||||
|
||||
public void arrayGenerateCorner() {
|
||||
Blocks[] array = new Blocks[Config.CHUNK_3DCOORD_MAX_INDEX + 1];
|
||||
|
||||
int dist;
|
||||
|
||||
public void generateStair() {
|
||||
for (int i = 0; i < Config.CHUNK_SIZE; i++) {
|
||||
for (int j = 0; j < Config.CHUNK_SIZE; j++) {
|
||||
blocks.insertValue(j < Config.CHUNK_SIZE - i - 1 ? Blocks.STONE : Blocks.GRASS, new Interval(coord3DTo1D(0, i, j), coord3DTo1D(Config.CHUNK_SIZE - 1, i, j)));
|
||||
for (int k = 0; k < Config.CHUNK_SIZE; k++) {
|
||||
// Distance from 0,0,0
|
||||
dist = (int) Math.sqrt((i * i) + (j * j) + (k * k));
|
||||
|
||||
if (dist <= (int) (Config.CHUNK_SIZE / 3)) array[Chunk.coord3DTo1D(i, j, k)] = Blocks.STONE;
|
||||
else if (dist > (int) (Config.CHUNK_SIZE / 3) && dist <= (int) (2 * Config.CHUNK_SIZE / 3)) array[Chunk.coord3DTo1D(i, j, k)] = Blocks.DIRT;
|
||||
else if (dist > (int) (2 * Config.CHUNK_SIZE / 3) && dist <= (int) (Config.CHUNK_SIZE)) array[Chunk.coord3DTo1D(i, j, k)] = Blocks.GRASS;
|
||||
else array[Chunk.coord3DTo1D(i, j, k)] = Blocks.AIR;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
blocks.insertValue(Blocks.DIRT, new Interval(Chunk.coord3DTo1D(0, 0, 0), Chunk.coord3DTo1D(2, 0, 0)));
|
||||
blocks.insertValue(Blocks.DIRT, new Interval(Chunk.coord3DTo1D(1, 0, 2), Chunk.coord3DTo1D(2, 0, 2)));
|
||||
this.treeFrom1DArray(array);
|
||||
}
|
||||
|
||||
public Blocks[] treeTo1DArray() {
|
||||
|
@ -85,4 +110,24 @@ public class Chunk {
|
|||
return result;
|
||||
}
|
||||
|
||||
public void treeFrom1DArray(Blocks[] array) {
|
||||
this.blocks = new IntervalTree<>();
|
||||
|
||||
int start = 0;
|
||||
Blocks prev = array[0];
|
||||
|
||||
for (int i = 0; i < array.length; i++) {
|
||||
if (array[i] != prev) {
|
||||
// System.out.println("From " + start + " to " + i + ": " + prev);
|
||||
this.blocks.insertValue(prev == null ? Blocks.AIR : prev, new Interval(start, i));
|
||||
start = i;
|
||||
}
|
||||
|
||||
prev = array[i];
|
||||
}
|
||||
|
||||
// System.out.println("From " + start + " to end :" + prev);
|
||||
this.blocks.insertValue(prev, new Interval(start, array.length - 1));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ public class WorldManager {
|
|||
|
||||
public void initWorld() {
|
||||
chunks.put(Vector3f.ZERO, new Chunk());
|
||||
chunks.get(Vector3f.ZERO).generateStair();
|
||||
chunks.get(Vector3f.ZERO).arrayGenerateCorner();
|
||||
|
||||
chunkToModel.add(chunks.get(Vector3f.ZERO));
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue