code cleanup

This commit is contained in:
Lukas 2020-09-23 22:17:47 +02:00
parent aa6ec55792
commit 4feb25621c
28 changed files with 100 additions and 145 deletions

View File

@ -30,7 +30,7 @@ public class GameWindow {
private static Connection connection;
private static PlayerController playerController;
static boolean running = false;
private static boolean running = false;
public static boolean paused = false;
public static void prepare() {

View File

@ -1,6 +1,6 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
* Copyright (C) 2020 Lukas Eisenhauer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -28,7 +28,7 @@ import static org.lwjgl.system.MemoryUtil.NULL;
public class OpenGLWindow {
private final boolean fullscreen;
long window;
private long window;
private int width, height;
private double mouseX;
private double mouseY;

View File

@ -32,7 +32,7 @@ public class WorldRenderer {
private BlockModelLoader modelLoader;
private LinkedBlockingQueue<Pair<ChunkLocation, Chunk>> queuedChunks;
Thread chunkLoadThread;
private Thread chunkLoadThread;
public WorldRenderer() {
faces = new HashMap<>();

View File

@ -23,9 +23,8 @@ import java.util.HashMap;
import java.util.HashSet;
public class BlockConfiguration {
BlockRotations rotation;
HashSet<BlockProperties> blockProperties;
private BlockRotations rotation;
private HashSet<BlockProperties> blockProperties;
public BlockConfiguration(JsonObject json) {
if (json.has("facing")) {

View File

@ -1,6 +1,6 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
* Copyright (C) 2020 Lukas Eisenhauer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*

View File

@ -33,7 +33,7 @@ import static de.bixilon.minosoft.util.Util.readJsonAsset;
public class BlockModelLoader {
private final HashMap<String, HashMap<String, BlockModelInterface>> blockDescriptionMap;
TextureLoader textureLoader;
private TextureLoader textureLoader;
public BlockModelLoader() {
blockDescriptionMap = new HashMap<>();

View File

@ -1,6 +1,6 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
* Copyright (C) 2020 Lukas Eisenhauer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*

View File

@ -1,6 +1,6 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
* Copyright (C) 2020 Lukas Eisenhauer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -18,9 +18,9 @@ import de.bixilon.minosoft.render.blockModels.subBlocks.SubBlockPosition;
import de.bixilon.minosoft.render.texture.InFaceUV;
public class Face {
final SubBlockPosition[] positions;
int rotation;
InFaceUV uv;
private final SubBlockPosition[] positions;
private int rotation;
private InFaceUV uv;
public Face(float texture, InFaceUV uv, SubBlockPosition[] facePositions) {
this(texture, uv, facePositions, 0);

View File

@ -1,6 +1,6 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
* Copyright (C) 2020 Lukas Eisenhauer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*

View File

@ -1,6 +1,6 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
* Copyright (C) 2020 Lukas Eisenhauer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*

View File

@ -1,6 +1,6 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Lukas Eisenhauer
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -13,12 +13,9 @@
package de.bixilon.minosoft.render.blockModels.specialModels;
import com.google.common.collect.HashBiMap;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import de.bixilon.minosoft.Config;
import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.Block;
import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.BlockRotations;
import de.bixilon.minosoft.logging.Log;
import de.bixilon.minosoft.render.blockModels.BlockConfiguration;
import de.bixilon.minosoft.render.blockModels.BlockConfigurationTrue;
@ -28,16 +25,13 @@ import de.bixilon.minosoft.render.blockModels.Face.FaceOrientation;
import de.bixilon.minosoft.render.blockModels.subBlocks.SubBlock;
import de.bixilon.minosoft.render.texture.TextureLoader;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import static de.bixilon.minosoft.util.Util.readJsonFromFile;
public class BlockModel implements BlockModelInterface {
HashMap<BlockConfiguration, HashSet<SubBlock>> blockConfigurationStates;
boolean isFull;
private final HashMap<BlockConfiguration, HashSet<SubBlock>> blockConfigurationStates;
private final boolean isFull;
public BlockModel(JsonObject block, String mod) {
blockConfigurationStates = new HashMap<>();
@ -57,10 +51,6 @@ public class BlockModel implements BlockModelInterface {
isFull = true;
}
public BlockModel() {
}
public static HashSet<Face> prepareBlockState(HashSet<SubBlock> subBlocks,
HashMap<FaceOrientation, Boolean> adjacentBlocks, Block block) {
HashSet<Face> result = new HashSet<>();

View File

@ -29,7 +29,7 @@ import java.util.HashSet;
import static de.bixilon.minosoft.render.blockModels.specialModels.BlockModel.*;
public class CropModel implements BlockModelInterface {
HashMap<String, HashSet<SubBlock>> modelMap;
private final HashMap<String, HashSet<SubBlock>> modelMap;
public CropModel(JsonObject block, String mod) {
int stages = block.get("stages").getAsInt();

View File

@ -30,11 +30,11 @@ import java.util.HashSet;
import static de.bixilon.minosoft.render.blockModels.specialModels.BlockModel.*;
public class DoorModel implements BlockModelInterface {
HashSet<SubBlock> bottom;
HashSet<SubBlock> bottom_hinge;
private final HashSet<SubBlock> bottom;
private final HashSet<SubBlock> bottom_hinge;
HashSet<SubBlock> top;
HashSet<SubBlock> top_hinge;
private final HashSet<SubBlock> top;
private final HashSet<SubBlock> top_hinge;
public DoorModel(JsonObject block, String mod) {
bottom = BlockModelInterface.load(mod, block.get("bottom").getAsString());

View File

@ -27,10 +27,9 @@ import java.util.HashMap;
import java.util.HashSet;
public class FireModel implements BlockModelInterface {
HashSet<SubBlock> floor;
HashSet<SubBlock> side;
HashSet<SubBlock> up;
private final HashSet<SubBlock> floor;
private final HashSet<SubBlock> side;
private final HashSet<SubBlock> up;
public FireModel(JsonObject block, String mod) {
floor = BlockModelInterface.load(mod, block.get("floor").getAsString());

View File

@ -27,9 +27,9 @@ import java.util.HashMap;
import java.util.HashSet;
public class StairsModel implements BlockModelInterface {
HashSet<SubBlock> straight;
HashSet<SubBlock> inner;
HashSet<SubBlock> outer;
private final HashSet<SubBlock> straight;
private final HashSet<SubBlock> inner;
private final HashSet<SubBlock> outer;
public StairsModel(JsonObject block, String mod) {
straight = BlockModelInterface.load(mod, block.get("straight").getAsString());

View File

@ -32,7 +32,7 @@ public class Cuboid {
FaceOrientation.NORTH, new int[]{5, 4, 0, 1}
);
HashMap<FaceOrientation, SubBlockPosition[]> facePositionMap;
private final HashMap<FaceOrientation, SubBlockPosition[]> facePositionMap;
public Cuboid(SubBlockPosition from, SubBlockPosition to, SubBlockRotation rotation) {
SubBlockPosition[] positions = new SubBlockPosition[8];

View File

@ -25,16 +25,16 @@ import java.util.HashSet;
import java.util.Map;
public class SubBlock {
SubBlockRotation rotation;
private SubBlockRotation rotation;
HashMap<FaceOrientation, Float> textureCoordinates;
HashMap<FaceOrientation, String> textures;
HashMap<FaceOrientation, Integer> textureRotations;
HashMap<FaceOrientation, Boolean> cullFaceTextures;
private final HashMap<FaceOrientation, Float> textureCoordinates;
private final HashMap<FaceOrientation, String> textures;
private final HashMap<FaceOrientation, Integer> textureRotations;
private final HashMap<FaceOrientation, Boolean> cullFaceTextures;
HashMap<FaceOrientation, InFaceUV> uv;
private final HashMap<FaceOrientation, InFaceUV> uv;
Cuboid cuboid;
private final Cuboid cuboid;
private final boolean isFull;

View File

@ -22,20 +22,22 @@ import static de.bixilon.minosoft.render.blockModels.Face.RenderConstants.blockR
import static org.lwjgl.opengl.GL11.glVertex3f;
public class SubBlockPosition {
float x, y, z;
public float x;
public float y;
public float z;
public static final SubBlockPosition middlePos = new SubBlockPosition(8, 8, 8);
private static final SubBlockPosition middlePos = new SubBlockPosition(8, 8, 8);
public static final SubBlockRotation westRotator = new SubBlockRotation(middlePos, Axis.Y, 90);
public static final SubBlockRotation eastRotator = new SubBlockRotation(middlePos, Axis.Y, 270);
public static final SubBlockRotation southRotator = new SubBlockRotation(middlePos, Axis.Y, 180);
private static final SubBlockRotation westRotator = new SubBlockRotation(middlePos, Axis.Y, 90);
private static final SubBlockRotation eastRotator = new SubBlockRotation(middlePos, Axis.Y, 270);
private static final SubBlockRotation southRotator = new SubBlockRotation(middlePos, Axis.Y, 180);
public static final SubBlockRotation xAxisRotator = new SubBlockRotation(middlePos, Axis.Z, 90);
public static final SubBlockRotation zAxisRotator = new SubBlockRotation(middlePos, Axis.X, 90);
private static final SubBlockRotation xAxisRotator = new SubBlockRotation(middlePos, Axis.Z, 90);
private static final SubBlockRotation zAxisRotator = new SubBlockRotation(middlePos, Axis.X, 90);
public static final SubBlockRotation downRotator = new SubBlockRotation(middlePos, Axis.X, 90);
public static final SubBlockRotation downAltRotator = new SubBlockRotation(middlePos, Axis.X, 180);
public static final SubBlockRotation upRotator = new SubBlockRotation(middlePos, Axis.X, -90);
private static final SubBlockRotation downRotator = new SubBlockRotation(middlePos, Axis.X, 90);
private static final SubBlockRotation downAltRotator = new SubBlockRotation(middlePos, Axis.X, 180);
private static final SubBlockRotation upRotator = new SubBlockRotation(middlePos, Axis.X, -90);
public SubBlockPosition(JsonArray json) {

View File

@ -1,6 +1,6 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
* Copyright (C) 2020 Lukas Eisenhauer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -21,9 +21,9 @@ import static java.lang.StrictMath.cos;
import static java.lang.StrictMath.sin;
public class SubBlockRotation {
SubBlockPosition origin;
Axis direction;
float angle;
private final SubBlockPosition origin;
private Axis direction;
private final float angle;
public SubBlockRotation(SubBlockPosition origin, Axis direction, float angle) {
this.origin = origin;
@ -35,15 +35,9 @@ public class SubBlockRotation {
origin = new SubBlockPosition(rotation.get("origin").getAsJsonArray());
String axis = rotation.get("axis").getAsString();
switch (axis) {
case "x":
direction = Axis.X;
break;
case "y":
direction = Axis.Y;
break;
case "z":
direction = Axis.Z;
break;
case "x" -> direction = Axis.X;
case "y" -> direction = Axis.Y;
case "z" -> direction = Axis.Z;
}
angle = rotation.get("angle").getAsFloat();
}
@ -63,21 +57,21 @@ public class SubBlockRotation {
SubBlockPosition transformed = SubBlockPosition.subtract(position, origin);
Pair<Float, Float> rotated;
switch (direction) {
case X:
case X -> {
rotated = rotate(transformed.y, transformed.z, angle);
transformed.y = rotated.getKey();
transformed.z = rotated.getValue();
break;
case Y:
}
case Y -> {
rotated = rotate(transformed.x, transformed.z, angle);
transformed.x = rotated.getKey();
transformed.z = rotated.getValue();
break;
case Z:
}
case Z -> {
rotated = rotate(transformed.x, transformed.y, angle);
transformed.x = rotated.getKey();
transformed.y = rotated.getValue();
break;
}
}
return SubBlockPosition.add(transformed, origin);
}

View File

@ -21,7 +21,8 @@ import static java.lang.StrictMath.*;
import static org.lwjgl.opengl.GL11.glRotatef;
public class CameraMovement {
Vec3 cameraFront = new Vec3(0.0f, 0.0f, -1.0f);
private static final float sensitivity = 0.1f;
private Vec3 cameraFront = new Vec3(0.0f, 0.0f, -1.0f);
private boolean firstMouse = false;
private float lastX;
@ -30,11 +31,11 @@ public class CameraMovement {
private float pitch;
public void mouseCallback(long l, double xPos, double yPos) {
// variable l is unused but always given by openGL so it is needed in the method signature
if (GameWindow.paused) {
return;
}
// variable l is unused but always given by openGL so we need it in the method signature
if (firstMouse) {
lastX = (float) xPos;
lastY = (float) yPos;
@ -46,18 +47,18 @@ public class CameraMovement {
lastX = (float) xPos;
lastY = (float) yPos;
float sensitivity = 0.1f; // change this value to your liking
xoffset *= sensitivity;
yoffset *= sensitivity;
yaw += xoffset;
pitch += yoffset;
// make sure that pitch does not get out of bounds
if (pitch > 89.0f)
if (pitch > 89.0f) {
pitch = 89.0f;
if (pitch < -89.0f)
}
if (pitch < -89.0f) {
pitch = -89.0f;
}
Vec3 front = new Vec3();
front.x = (float) -(sin(toRadians(yaw)) * cos(toRadians(pitch)));

View File

@ -23,9 +23,9 @@ import static de.bixilon.minosoft.render.utility.AdditionalMath.betterRound;
import static de.bixilon.minosoft.render.utility.AdditionalMath.valuesBetween;
public class CollisionHandler {
World world;
PlayerController controller;
BlockModelLoader modelLoader;
private final World world;
private final PlayerController controller;
private final BlockModelLoader modelLoader;
public CollisionHandler(PlayerController controller) {
world = GameWindow.getConnection().getPlayer().getWorld();
@ -35,7 +35,7 @@ public class CollisionHandler {
public void handleCollisions() {
if (isPositionValid(controller.playerPos)) {
// we aren't collided with anything so the player Position does not have to be adjusted
// the player currently isn't colliding with with anything so the player Position does not have to be adjusted
return;
}
xAxisCollision();
@ -73,7 +73,7 @@ public class CollisionHandler {
controller.playerVelocity.y = 0;
if (deltaY < 0) {
controller.onGround = true;
controller.setOnGround(true);
}
}

View File

@ -22,14 +22,15 @@ import static de.bixilon.minosoft.render.utility.Vec3.mul;
import static org.lwjgl.opengl.GL11.glTranslatef;
public class PlayerController {
private final float playerHeight = 1.8f;
private final float playerWidth = 0.25f;
private static final float playerHeight = 1.8f;
private static final float playerWidth = 0.25f;
private static final float gravity = 13;
CameraMovement cameraMovement;
PlayerMovement playerMovement;
Vec3 playerPos = new Vec3(); // the feet position of the player
Vec3 playerVelocity = new Vec3();
float gravity = 9.81f;
boolean onGround;
private boolean onGround;
private boolean enableGravity;
private CollisionHandler collisionHandler;
public Vec3 oldPos;
@ -121,4 +122,8 @@ public class PlayerController {
public float getPlayerHeight() {
return playerHeight;
}
public void setOnGround(boolean onGround) {
this.onGround = onGround;
}
}

View File

@ -11,10 +11,6 @@
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
/*
* A simple camera controller to fly around the scene
*/
package de.bixilon.minosoft.render.movement;
import de.bixilon.minosoft.render.GameWindow;
@ -25,20 +21,19 @@ import static de.bixilon.minosoft.render.utility.Vec3.mul;
import static org.lwjgl.glfw.GLFW.*;
public class PlayerMovement {
private static final float FLY_SPEED = 0.1f;
private static final Vec3 CAMERA_UP = new Vec3(0f, 1f, 0f);
private final long window;
Vec3 cameraFront;
Vec3 cameraUp = new Vec3(0f, 1f, 0f);
float flySpeed = 0.1f;
Vec3 playerPos;
private Vec3 cameraFront;
private Vec3 playerPos;
public PlayerMovement(long window) {
this.window = window;
}
private void processInput(float deltaTime) {
float cameraSpeed = flySpeed / deltaTime;
float cameraSpeed = FLY_SPEED / deltaTime;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
playerPos.add(mul(cameraFront, -cameraSpeed * deltaTime));
@ -47,10 +42,10 @@ public class PlayerMovement {
playerPos.add(mul(cameraFront, cameraSpeed * deltaTime));
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
playerPos.add(mul(cross(cameraUp, cameraFront), -cameraSpeed * deltaTime));
playerPos.add(mul(cross(CAMERA_UP, cameraFront), -cameraSpeed * deltaTime));
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
playerPos.add(mul(cross(cameraUp, cameraFront), cameraSpeed * deltaTime));
playerPos.add(mul(cross(CAMERA_UP, cameraFront), cameraSpeed * deltaTime));
}
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {

View File

@ -1,6 +1,6 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
* Copyright (C) 2020 Lukas Eisenhauer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -20,7 +20,7 @@ import static de.bixilon.minosoft.render.blockModels.Face.RenderConstants.textur
import static org.lwjgl.opengl.GL11.glTexCoord2f;
public class InFaceUV {
public int u1, v1, u2, v2;
public final int u1, v1, u2, v2;
public float realU1, realV1, realU2, realV2;

View File

@ -33,10 +33,10 @@ import static org.lwjgl.opengl.GL30.glGenerateMipmap;
public class TextureLoader {
private final HashMap<String, HashMap<String, Integer>> textureCoordinates;
int textureID;
float step;
int totalTextures = 0;
HashMap<String, HashMap<String, BufferedImage>> images;
private int textureID;
private float step;
private int totalTextures = 0;
private HashMap<String, HashMap<String, BufferedImage>> images;
public TextureLoader(HashMap<String, HashSet<String>> textures, HashMap<String, HashMap<String, float[]>> tints) {
textureCoordinates = new HashMap<>();

View File

@ -1,6 +1,6 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
* Copyright (C) 2020 Lukas Eisenhauer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -39,8 +39,4 @@ public class AdditionalMath {
}
return (int) x - 1;
}
public static boolean isBetween(double x, double a, double b) {
return x <= a && x >= b || x >= a && x <= b;
}
}
}

View File

@ -1,26 +0,0 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This software is not affiliated with Mojang AB, the original developer of Minecraft.
*/
package de.bixilon.minosoft.render.utility;
public class Triplet<T, T1, T2> {
public T item1;
public T1 item2;
public T2 item3;
public Triplet(T item1, T1 item2, T2 item3) {
this.item1 = item1;
this.item2 = item2;
this.item3 = item3;
}
}

View File

@ -1,6 +1,6 @@
/*
* Codename Minosoft
* Copyright (C) 2020 Moritz Zwerger
* Copyright (C) 2020 Lukas Eisenhauer
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*