Merge branch 'worldgen' of https://github.com/halotroop/Ginger3D into worldgen

pull/12/head
valoeghese 2020-03-01 13:11:01 +13:00
commit f0c85f0693
20 changed files with 46 additions and 38 deletions

View File

@ -64,7 +64,7 @@ public class Litecraft extends Game
System.exit(0);
}
/*
/**
* Things that ARE rendering: Anything that results in something being drawn to the frame buffer
* Things that are NOT rendering: Things that happen to update between frames but do not result in things being drawn to the screen
*/
@ -80,7 +80,7 @@ public class Litecraft extends Game
this.tps = 0;
this.frameTimer += 1000;
}
/*
/**
* And now, the actual rendering:
*/
// Render shadows
@ -153,7 +153,7 @@ public class Litecraft extends Game
MouseCallbackHandler.trackWindow(Window.getWindow());
}
/*
/**
* Things that should be ticked: Entities when deciding an action, in-game timers (such as smelting), the in-game time
* Things that should not be ticked: Rendering, input, player movement
*/

View File

@ -2,7 +2,7 @@ package com.github.halotroop.litecraft.logic;
import tk.valoeghese.sod.BinaryData;
public interface DataStorage
public interface SODSerializable
{
void read(BinaryData data);

View File

@ -4,7 +4,7 @@ import java.util.*;
import com.github.hydos.multithreading.GingerThread;
/*
/**
* @author Jack Wilsdon (Stack Exchange)
* https://codereview.stackexchange.com/questions/111855/ticker-for-game-timing
*/

View File

@ -57,7 +57,7 @@ public final class LitecraftSave
}
}
public Chunk readChunk(int chunkX, int chunkY, int chunkZ, int dimension)
public Chunk readChunk(World world, int chunkX, int chunkY, int chunkZ, int dimension)
{
// format: <save dir>/<dim>/<chunkX>/<chunkZ>/<chunkY>.sod
File chunkFile = new File(new StringBuilder(this.file.getPath())
@ -68,7 +68,7 @@ public final class LitecraftSave
if (chunkFile.isFile())
{
BinaryData data = BinaryData.read(chunkFile);
Chunk result = new Chunk(chunkX, chunkY, chunkZ, dimension); // create chunk
Chunk result = new Chunk(world, chunkX, chunkY, chunkZ, dimension); // create chunk
result.read(data); // load the chunk data we have just read into the chunk
return result;
}

View File

@ -14,7 +14,7 @@ import com.github.hydos.ginger.engine.font.GUIText;
import com.github.hydos.ginger.engine.io.Window;
import com.github.hydos.ginger.engine.screen.Screen;
/*
/**
* YeS
*/
public class TitleScreen extends Screen

View File

@ -1,6 +1,6 @@
package com.github.halotroop.litecraft.util.noise;
/*
/**
* OpenSimplex Noise in Java.
* (Using implementation by Kurt Spencer)
*

View File

@ -1,10 +1,11 @@
package com.github.halotroop.litecraft.world;
import java.util.Arrays;
import java.util.function.ToIntFunction;
import org.joml.Vector3f;
import com.github.halotroop.litecraft.logic.DataStorage;
import com.github.halotroop.litecraft.logic.SODSerializable;
import com.github.halotroop.litecraft.types.block.*;
import com.github.halotroop.litecraft.world.block.BlockRenderer;
import com.github.halotroop.litecraft.world.gen.WorldGenConstants;
@ -13,9 +14,9 @@ import it.unimi.dsi.fastutil.ints.*;
import it.unimi.dsi.fastutil.objects.*;
import tk.valoeghese.sod.*;
public class Chunk implements BlockAccess, WorldGenConstants, DataStorage
public class Chunk implements BlockAccess, WorldGenConstants, SODSerializable
{
/*
/**
* @param x in-chunk x coordinate.
* @param y in-chunk y coordinate.
* @param z in-chunk z coordinate.
@ -32,9 +33,10 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage
private boolean fullyGenerated = false;
public final int dimension;
private boolean dirty = true;
private BlockInstance[] renderedBlocks;
private World world;
private BlockInstance[] renderedBlocks = new BlockInstance[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
public Chunk(int chunkX, int chunkY, int chunkZ, int dimension)
public Chunk(World world, int chunkX, int chunkY, int chunkZ, int dimension)
{
this.chunkX = chunkX;
this.chunkY = chunkY;
@ -43,6 +45,7 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage
this.chunkStartY = chunkY << POS_SHIFT;
this.chunkStartZ = chunkZ << POS_SHIFT;
this.dimension = dimension;
this.world = world;
}
public boolean doRender()
@ -55,14 +58,14 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage
public Block getBlock(int x, int y, int z)
{
if (x > CHUNK_SIZE || y > CHUNK_SIZE || z > CHUNK_SIZE || x < 0 || y < 0 || z < 0)
{ throw new RuntimeException("Block [" + x + ", " + y + ", " + z + ", " + "] out of chunk bounds!"); }
{ throw new RuntimeException("Block [" + x + ", " + y + ", " + z + "] out of chunk bounds!"); }
return blocks[index(x, y, z)];
}
public BlockInstance getBlockInstance(int x, int y, int z)
{
if (x > CHUNK_SIZE || y > CHUNK_SIZE || z > CHUNK_SIZE || x < 0 || y < 0 || z < 0)
{ throw new RuntimeException("Block [" + x + ", " + y + ", " + z + ", " + "] out of chunk bounds!"); }
{ throw new RuntimeException("Block [" + x + ", " + y + ", " + z + "] out of chunk bounds!"); }
return this.blockEntities[index(x, y, z)];
}
@ -72,12 +75,13 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage
{
if (dirty)
dirty = false;
renderedBlocks = new BlockInstance[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE];
Arrays.fill(renderedBlocks, null);
for (int x = 0; x < CHUNK_SIZE; x++)
for (int y = 0; y < CHUNK_SIZE; y++)
for (int z = 0; z < CHUNK_SIZE; z++)
{
BlockInstance block = getBlockInstance(x, y, z);
if (x == 0 || x == CHUNK_SIZE - 1 || z == 0 || z == CHUNK_SIZE - 1 || y == 0 || y == CHUNK_SIZE - 1)
{
renderedBlocks[index(x, y, z)] = block;
@ -88,19 +92,21 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage
if (getBlockInstance(x - 1, y, z).getModel() == null || getBlockInstance(x + 1, y, z).getModel() == null ||
getBlockInstance(x, y - 1, z).getModel() == null || getBlockInstance(x, y + 1, z).getModel() == null ||
getBlockInstance(x, y, z - 1).getModel() == null || getBlockInstance(x, y, z + 1).getModel() == null)
{
renderedBlocks[index(x, y, z)] = block;
}
}
catch (NullPointerException e)
{ // this seems to be a hotspot for errors
e.printStackTrace(); // so I can add a debug breakpoint on this line
throw e;
throw e; // e
}
}
blockRenderer.render(renderedBlocks);
}
}
/*
/**
* Change the block in this exact position
* @param x, y, z The coordinate position of block to overwrite
* @param block The block to place there
@ -122,7 +128,7 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage
dirty = true;
}
/*
/**
* Set whether or not the chunk should render
*/
public void setRender(boolean render)

View File

@ -98,8 +98,8 @@ public class World implements BlockAccess, WorldGenConstants
{
Chunk chunk = this.chunks.computeIfAbsent(posHash(chunkX, chunkY, chunkZ), pos ->
{
Chunk readChunk = save.readChunk(chunkX, chunkY, chunkZ, this.dimension);
return readChunk == null ? this.chunkGenerator.generateChunk(chunkX, chunkY, chunkZ) : readChunk;
Chunk readChunk = save.readChunk(this, chunkX, chunkY, chunkZ, this.dimension);
return readChunk == null ? this.chunkGenerator.generateChunk(this, chunkX, chunkY, chunkZ) : readChunk;
});
if (chunk.isFullyGenerated()) return chunk;
this.populateChunk(chunkX, chunkY, chunkZ, chunk.chunkStartX, chunk.chunkStartY, chunk.chunkStartZ);
@ -114,9 +114,9 @@ public class World implements BlockAccess, WorldGenConstants
if (result != null)
return result;
// try read a chunk from memory
result = save.readChunk(chunkX, chunkY, chunkZ, this.dimension);
result = save.readChunk(this, chunkX, chunkY, chunkZ, this.dimension);
// if neither of those work, generate the chunk
return result == null ? this.chunkGenerator.generateChunk(chunkX, chunkY, chunkZ) : result;
return result == null ? this.chunkGenerator.generateChunk(this, chunkX, chunkY, chunkZ) : result;
}
/** @return whether the chunk was unloaded without errors. Will often, but not always, be equal to whether the chunk was already in memory. */
@ -145,7 +145,7 @@ public class World implements BlockAccess, WorldGenConstants
/** @return a chunk that has not neccesarily gone through chunk populating. Used in chunk populating to prevent infinite recursion. */
Chunk getGenChunk(int chunkX, int chunkY, int chunkZ)
{ return this.chunks.computeIfAbsent(posHash(chunkX, chunkY, chunkZ), pos -> this.chunkGenerator.generateChunk(chunkX, chunkY, chunkZ)); }
{ return this.chunks.computeIfAbsent(posHash(chunkX, chunkY, chunkZ), pos -> this.chunkGenerator.generateChunk(this, chunkX, chunkY, chunkZ)); }
long posHash(int chunkX, int chunkY, int chunkZ)
{ return ((long) chunkX & 0x3FF) | (((long) chunkY & 0x3FF) << 10) | (((long) chunkZ & 0x3FF) << 20); }

View File

@ -1,8 +1,9 @@
package com.github.halotroop.litecraft.world.gen;
import com.github.halotroop.litecraft.world.Chunk;
import com.github.halotroop.litecraft.world.World;
public interface ChunkGenerator
{
Chunk generateChunk(int chunkX, int chunkY, int chunkZ);
Chunk generateChunk(World world, int chunkX, int chunkY, int chunkZ);
}

View File

@ -5,6 +5,7 @@ import java.util.Random;
import com.github.halotroop.litecraft.types.block.*;
import com.github.halotroop.litecraft.util.noise.OctaveSimplexNoise;
import com.github.halotroop.litecraft.world.Chunk;
import com.github.halotroop.litecraft.world.World;
public class EarthChunkGenerator implements ChunkGenerator, WorldGenConstants
{
@ -21,9 +22,9 @@ public class EarthChunkGenerator implements ChunkGenerator, WorldGenConstants
private final int dimension;
@Override
public Chunk generateChunk(int chunkX, int chunkY, int chunkZ)
public Chunk generateChunk(World world, int chunkX, int chunkY, int chunkZ)
{
Chunk chunk = new Chunk(chunkX, chunkY, chunkZ, this.dimension);
Chunk chunk = new Chunk(world, chunkX, chunkY, chunkZ, this.dimension);
for (int x = 0; x < CHUNK_SIZE; x++)
{
double totalX = x + chunk.chunkStartX;

View File

@ -1,6 +1,6 @@
package com.github.hydos.ginger.engine.api;
/*
/**
* make your own resource manager if you want!
*/
public abstract class ResourceManager

View File

@ -8,7 +8,7 @@ import com.github.hydos.ginger.engine.cameras.Camera;
import com.github.hydos.ginger.engine.elements.GuiTexture;
import com.github.hydos.ginger.engine.elements.objects.*;
/*
/**
* Used for storing essential engine game data so main class isn't messy
* Also in general used with Game Class
*/

View File

@ -8,5 +8,5 @@ public class Constants
public static Vector3f gravity = new Vector3f(0, 0, 0);
public static float jumpPower = 0;
public static float turnSpeed = 0;
public static double movementSpeed = 1;
public static double movementSpeed = 2;
}

View File

@ -1,6 +1,6 @@
package tk.valoeghese.gateways.client.io;
/*
/**
* Author: Valoeghese
*/
public final class InitialPressHandler implements KeyListener

View File

@ -2,7 +2,7 @@ package tk.valoeghese.gateways.client.io;
import java.util.*;
/*
/**
* Author: Valoeghese
*/
public class Input

View File

@ -1,6 +1,6 @@
package tk.valoeghese.gateways.client.io;
/*
/**
* Author: Valoeghese
*/
public interface KeyCallback

View File

@ -2,7 +2,7 @@ package tk.valoeghese.gateways.client.io;
import org.lwjgl.glfw.*;
/*
/**
* Author: Valoeghese
*/
public class KeyCallbackHandler extends GLFWKeyCallback

View File

@ -1,6 +1,6 @@
package tk.valoeghese.gateways.client.io;
/*
/**
* Author: Valoeghese
*/
public interface KeyListener

View File

@ -2,7 +2,7 @@ package tk.valoeghese.gateways.client.io;
import org.lwjgl.glfw.GLFW;
/*
/**
* Author: Valoeghese
*/
public enum Keybind

View File

@ -2,7 +2,7 @@ package tk.valoeghese.gateways.client.io;
import org.lwjgl.glfw.*;
/*
/**
* Author: Valoeghese
*/
public class MouseCallbackHandler extends GLFWMouseButtonCallback