Re-added block selection

This commit is contained in:
Randy 2019-03-24 15:13:58 +01:00
parent e395ab7bdd
commit 5e129fd28e
4 changed files with 77 additions and 30 deletions

View File

@ -25,7 +25,6 @@ import org.jungle.game.Game;
import org.jungle.game.IGameLogic;
import org.jungle.util.DirectionalLight;
import org.jungle.util.Material;
import org.jungle.util.MeshSelectionDetector;
import org.jungle.util.OBJLoader;
import org.lwjgl.Version;
import org.lwjgl.glfw.GLFW;
@ -66,12 +65,9 @@ public class Cubyz implements IGameLogic {
private Vector3f playerInc;
public static MouseInput mouse;
public static UISystem gameUI;
private static long lastFPSCheck = 0;
private static int currentFrames = 0;
private static int currentFPS = 0;
public static World world;
private MeshSelectionDetector msd;
private CubyzMeshSelectionDetector msd;
private int breakCooldown = 10;
@ -158,7 +154,7 @@ public class Cubyz implements IGameLogic {
e.printStackTrace();
System.exit(1);
}
msd = new MeshSelectionDetector(renderer);
msd = new CubyzMeshSelectionDetector(renderer);
window.setClearColor(new Vector4f(0.1F, 0.7F, 0.7F, 1.0F)); //NOTE: Normal > 0.1F || 0.7F || 0.7F || 1.0F
log.info("Renderer: OK!");
@ -226,7 +222,7 @@ public class Cubyz implements IGameLogic {
window.setFullscreen(!window.isFullscreen());
Keyboard.setKeyPressed(GLFW.GLFW_KEY_F11, false);
}
if (!gameUI.isGUIFullscreen()) {
if (!gameUI.isGUIFullscreen() && world != null) {
if (window.isKeyPressed(GLFW.GLFW_KEY_W)) {
playerInc.z = -1;
}
@ -270,7 +266,9 @@ public class Cubyz implements IGameLogic {
mouse.clearPos(window.getWidth() / 2, window.getHeight() / 2);
breakCooldown = 10;
}
msd.selectSpatial(ctx.getSpatials(), ctx.getCamera());
synchronized (world.visibleBlocks()) {
msd.selectSpatial(world.visibleBlocks(), ctx.getCamera());
}
}
mouse.input(window);
}
@ -291,7 +289,7 @@ public class Cubyz implements IGameLogic {
if (playerInc.x != 0) {
world.getLocalPlayer().vx = playerInc.x;
}
ctx.getCamera().setPosition(world.getLocalPlayer().getPosition().x, world.getLocalPlayer().getPosition().y + 1.76f, world.getLocalPlayer().getPosition().z);
ctx.getCamera().setPosition(world.getLocalPlayer().getPosition().x, world.getLocalPlayer().getPosition().y + 1.5f, world.getLocalPlayer().getPosition().z);
if (world.isEdited()) {
lastVisibleBlocks = world.visibleBlocks();
@ -304,7 +302,6 @@ public class Cubyz implements IGameLogic {
} else {
renderer.render(window, ctx, new Vector3f(0.3F, 0.3F, 0.3F), light, EMPTY_BLOCK_LIST);
}
countFPS();
gameUI.updateUI();
}
@ -312,25 +309,23 @@ public class Cubyz implements IGameLogic {
public void update(float interval) {
if (!gameUI.isGUIFullscreen() && world != null) {
Player lp = world.getLocalPlayer();
lp.move(playerInc.mul(0.11F), ctx.getCamera().getRotation()); //NOTE: Normal > 0.11F
if (breakCooldown > 0) { //NOTE: Normal > 0
lp.move(playerInc.mul(0.11F), ctx.getCamera().getRotation());
if (breakCooldown > 0) {
breakCooldown--;
}
if (mouse.isLeftButtonPressed() && mouse.isGrabbed()) {
//Breaking Blocks
if (breakCooldown == 0) {
breakCooldown = 10;
if (msd.getSelectedSpatial() instanceof BlockSpatial) {
BlockInstance bi = ((BlockSpatial) msd.getSelectedSpatial()).getBlock();
world.removeBlock(bi.getX(), bi.getY(), bi.getZ());
}
BlockInstance bi = msd.getSelectedBlockInstance();
world.removeBlock(bi.getX(), bi.getY(), bi.getZ());
}
}
if (mouse.isGrabbed()) {
ctx.getCamera().moveRotation(mouse.getDisplVec().x() * 0.51F, mouse.getDisplVec().y() * 0.51F, 0.0F);
mouse.clearPos(win.getWidth() / 2, win.getHeight() / 2);
}
playerInc.x = playerInc.y = playerInc.z = 0.0F; // Reset positions || NOTE: Normal > 0.0F
playerInc.x = playerInc.y = playerInc.z = 0.0F; // Reset positions
for (Entity en : world.getEntities()) {
en.update();
}
@ -344,17 +339,8 @@ public class Cubyz implements IGameLogic {
}
}
private void countFPS() {
currentFrames++;
if (System.nanoTime() > lastFPSCheck + 1000000000) { //NOTE: Normal > 1B ( 1000000000 )
lastFPSCheck = System.nanoTime();
currentFPS = currentFrames;
currentFrames = 0; //NOTE: Normal > 0
}
}
public static int getFPS() {
return currentFPS;
return Cubyz.instance.game.getFPS();
}
}

View File

@ -0,0 +1,63 @@
package io.cubyz.client;
import java.util.ArrayList;
import java.util.Map;
import org.joml.Intersectionf;
import org.joml.Vector2f;
import org.joml.Vector3f;
import org.jungle.Camera;
import org.jungle.Spatial;
import org.jungle.renderers.IRenderer;
import org.jungle.renderers.jungle.JungleRender;
import io.cubyz.blocks.Block;
import io.cubyz.blocks.BlockInstance;
import io.cubyz.world.BlockSpatial;
public class CubyzMeshSelectionDetector {
protected IRenderer render;
protected Vector3f dir = new Vector3f();
protected Vector3f min = new Vector3f(), max = new Vector3f();
protected Vector2f nearFar = new Vector2f();
protected BlockInstance selectedSpatial;
public CubyzMeshSelectionDetector(IRenderer render) {
this.render = render;
}
/**
* Return selected spatial
* @return selected spatial, or null if none.
*/
public BlockInstance getSelectedBlockInstance() {
return selectedSpatial;
}
public void selectSpatial(Map<Block, ArrayList<BlockInstance>> blockList, Camera camera) {
dir = render.getTransformation().getViewMatrix(camera).positiveZ(dir).negate();
selectSpatial(blockList, camera.getPosition(), dir);
}
public void selectSpatial(Map<Block, ArrayList<BlockInstance>> blockList, Vector3f position, Vector3f dir) {
float closestDistance = Float.POSITIVE_INFINITY;
for (Block block : blockList.keySet()) {
for (BlockInstance bi : blockList.get(block)) {
((BlockSpatial) bi.getSpatial()).setSelected(false);
min.set(bi.getPosition());
max.set(bi.getPosition());
min.add(-0.5f, -0.5f, -0.5f); // -scale, -scale, -scale
max.add(0.5f, 0.5f, 0.5f); // scale, scale, scale
if (Intersectionf.intersectRayAab(position, dir, min, max, nearFar) && nearFar.x < closestDistance) {
closestDistance = nearFar.x;
selectedSpatial = bi;
}
}
}
if (selectedSpatial != null) {
((BlockSpatial) selectedSpatial.getSpatial()).setSelected(true);
}
}
}

View File

@ -182,8 +182,6 @@ public class Chunk {
bi = new BlockInstance(bedrock);
}
bi.setPosition(new Vector3i(wx + px, j, wy + py));
//bi.getSpatial().setPosition(new Vector3i(wx + px, j, wy + py));
//bi.getSpatial().setScale(0.5F);
bi.setWorld(world);
//world.blocks().add(bi);
list.add(bi);

View File

@ -37,7 +37,7 @@ public class LocalWorld extends World {
private class ChunkGenerationThread extends Thread {
Deque<ChunkAction> loadList = new ArrayDeque<>(); // FIFO order (First In, First Out)
private static final int MAX_QUEUE_SIZE = 12;
private static final int MAX_QUEUE_SIZE = 16;
public void queue(ChunkAction ca) {
if (!isQueued(ca)) {