mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-14 09:56:37 -04:00
add better BlockRotation system
This commit is contained in:
parent
cbf73f02d1
commit
c6a19ebf79
@ -265,11 +265,6 @@ public enum BlockProperties {
|
||||
BOTTOM,
|
||||
NOT_BOTTOM,
|
||||
|
||||
// log, portal
|
||||
AXIS_X,
|
||||
AXIS_Y,
|
||||
AXIS_Z,
|
||||
|
||||
// trapwire
|
||||
DISARMED,
|
||||
ARMED,
|
||||
|
@ -40,5 +40,10 @@ public enum BlockRotation {
|
||||
ASCENDING_SOUTH,
|
||||
|
||||
UP,
|
||||
DOWN
|
||||
DOWN,
|
||||
|
||||
// log, portal
|
||||
AXIS_X,
|
||||
AXIS_Y,
|
||||
AXIS_Z,
|
||||
}
|
||||
|
@ -336,12 +336,6 @@ public class Blocks {
|
||||
propertyHashMap.put("right", BlockProperties.HINGE_RIGHT);
|
||||
propertiesMapping.put("hinge", propertyHashMap);
|
||||
|
||||
propertyHashMap = new HashMap<>();
|
||||
propertyHashMap.put("x", BlockProperties.AXIS_X);
|
||||
propertyHashMap.put("y", BlockProperties.AXIS_Y);
|
||||
propertyHashMap.put("z", BlockProperties.AXIS_Z);
|
||||
propertiesMapping.put("axis", propertyHashMap);
|
||||
|
||||
propertyHashMap = new HashMap<>();
|
||||
propertyHashMap.put("floor", BlockProperties.FLOOR);
|
||||
propertyHashMap.put("wall", BlockProperties.WALL);
|
||||
@ -434,6 +428,9 @@ public class Blocks {
|
||||
rotationMapping.put("ascending_south", BlockRotation.ASCENDING_SOUTH);
|
||||
rotationMapping.put("north_south", BlockRotation.NORTH_SOUTH);
|
||||
rotationMapping.put("east_west", BlockRotation.EAST_WEST);
|
||||
rotationMapping.put("x", BlockRotation.AXIS_X);
|
||||
rotationMapping.put("y", BlockRotation.AXIS_Y);
|
||||
rotationMapping.put("z", BlockRotation.AXIS_Z);
|
||||
}
|
||||
|
||||
public static Block getBlockByLegacy(int protocolId, int protocolMetaData) {
|
||||
@ -471,6 +468,9 @@ public class Blocks {
|
||||
} else if (propertiesJSON.has("rotation")) {
|
||||
rotation = rotationMapping.get(propertiesJSON.get("rotation").getAsString());
|
||||
propertiesJSON.remove("rotation");
|
||||
} else if (propertiesJSON.has("axis")) {
|
||||
rotation = rotationMapping.get(propertiesJSON.get("axis").getAsString());
|
||||
propertiesJSON.remove("axis");
|
||||
}
|
||||
HashSet<BlockProperties> properties = new HashSet<>();
|
||||
for (String propertyName : propertiesJSON.keySet()) {
|
||||
|
@ -19,15 +19,13 @@ import de.bixilon.minosoft.render.texture.InFaceUV;
|
||||
import javafx.util.Pair;
|
||||
|
||||
public class Face {
|
||||
int textureRotation;
|
||||
SubBlockPosition[] positions;
|
||||
final SubBlockPosition[] positions;
|
||||
InFaceUV uv;
|
||||
|
||||
public Face(Pair<Float, Float> texture, InFaceUV uv, SubBlockPosition[] facePositions, int textureRotation) {
|
||||
public Face(Pair<Float, Float> texture, InFaceUV uv, SubBlockPosition[] facePositions) {
|
||||
positions = facePositions;
|
||||
this.uv = uv;
|
||||
this.uv.prepare(texture);
|
||||
this.textureRotation = textureRotation;
|
||||
}
|
||||
|
||||
public Face() {
|
||||
@ -36,7 +34,7 @@ public class Face {
|
||||
|
||||
public void draw(BlockPosition pos) {
|
||||
for (int i = 0; i < positions.length; i++) {
|
||||
uv.draw(i + textureRotation);
|
||||
uv.draw(i);
|
||||
positions[i].draw(pos);
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ package de.bixilon.minosoft.render.blockModels.subBlocks;
|
||||
|
||||
// some 3d object with 8 corners, 6 faces and 12 edges (example: cube, but can be deformed)
|
||||
|
||||
import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.BlockRotation;
|
||||
import de.bixilon.minosoft.render.blockModels.Face.FaceOrientation;
|
||||
|
||||
import java.util.HashMap;
|
||||
@ -59,7 +60,15 @@ public class Cuboid {
|
||||
}
|
||||
}
|
||||
|
||||
public SubBlockPosition[] getFacePositions(FaceOrientation orientation) {
|
||||
return facePositionMap.get(orientation);
|
||||
public SubBlockPosition[] getFacePositions(FaceOrientation orientation, BlockRotation rotation) {
|
||||
SubBlockPosition[] positions = facePositionMap.get(orientation);
|
||||
if (rotation == BlockRotation.NONE || rotation == BlockRotation.NORTH) {
|
||||
return positions;
|
||||
}
|
||||
SubBlockPosition[] result = new SubBlockPosition[positions.length];
|
||||
for (int i = 0; i < positions.length; i++) {
|
||||
result[i] = positions[i].rotated(rotation);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,6 @@ package de.bixilon.minosoft.render.blockModels.subBlocks;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.Block;
|
||||
import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.BlockProperties;
|
||||
import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.BlockRotation;
|
||||
import de.bixilon.minosoft.render.blockModels.Face.Face;
|
||||
import de.bixilon.minosoft.render.blockModels.Face.FaceOrientation;
|
||||
@ -60,7 +59,7 @@ public class SubBlock {
|
||||
orientation, variables);
|
||||
}
|
||||
}
|
||||
isFull = (from.x == 0 && from.y == 0 && from.z == 0) && (to.x == 16 && to.y == 16 && to.z == 16);
|
||||
isFull = (from.x == 0 && from.y == 0 && from.z == 0) && (to.x == 16 && to.y == 16 && to.z == 16) && rotation == null;
|
||||
}
|
||||
|
||||
private static String getRealTextureName(String textureName, HashMap<String, String> variables) {
|
||||
@ -112,98 +111,18 @@ public class SubBlock {
|
||||
if (!textureCoordinates.containsKey(orientation)) {
|
||||
continue;
|
||||
}
|
||||
if (block.getRotation().equals(BlockRotation.DOWN)) {
|
||||
if (orientation.equals(FaceOrientation.UP)) {
|
||||
result.add(prepareFace(FaceOrientation.DOWN, orientation,
|
||||
adjacentBlocks));
|
||||
continue;
|
||||
}
|
||||
if (orientation.equals(FaceOrientation.DOWN)) {
|
||||
result.add(prepareFace(FaceOrientation.UP, orientation,
|
||||
adjacentBlocks));
|
||||
continue;
|
||||
}
|
||||
} else if (block.getProperties().contains(BlockProperties.AXIS_X)) {
|
||||
if (orientation.equals(FaceOrientation.EAST)) {
|
||||
result.add(prepareFace(FaceOrientation.UP, orientation,
|
||||
adjacentBlocks));
|
||||
continue;
|
||||
}
|
||||
if (orientation.equals(FaceOrientation.WEST)) {
|
||||
result.add(prepareFace(FaceOrientation.DOWN, orientation,
|
||||
adjacentBlocks));
|
||||
continue;
|
||||
}
|
||||
if (orientation.equals(FaceOrientation.UP)) {
|
||||
result.add(prepareFace(FaceOrientation.EAST, orientation,
|
||||
adjacentBlocks, 1));
|
||||
continue;
|
||||
}
|
||||
if (orientation.equals(FaceOrientation.DOWN)) {
|
||||
result.add(prepareFace(FaceOrientation.WEST, orientation,
|
||||
adjacentBlocks, 1));
|
||||
continue;
|
||||
}
|
||||
if (orientation.equals(FaceOrientation.NORTH)) {
|
||||
result.add(prepareFace(orientation, orientation,
|
||||
adjacentBlocks, 1));
|
||||
continue;
|
||||
}
|
||||
if (orientation.equals(FaceOrientation.SOUTH)) {
|
||||
result.add(prepareFace(orientation, orientation,
|
||||
adjacentBlocks, 1));
|
||||
continue;
|
||||
}
|
||||
} else if (block.getProperties().contains(BlockProperties.AXIS_Z)) {
|
||||
if (orientation.equals(FaceOrientation.EAST)) {
|
||||
result.add(prepareFace(orientation, orientation,
|
||||
adjacentBlocks, 1));
|
||||
continue;
|
||||
}
|
||||
if (orientation.equals(FaceOrientation.WEST)) {
|
||||
result.add(prepareFace(orientation, orientation,
|
||||
adjacentBlocks, 1));
|
||||
continue;
|
||||
}
|
||||
if (orientation.equals(FaceOrientation.UP)) {
|
||||
result.add(prepareFace(FaceOrientation.EAST, orientation,
|
||||
adjacentBlocks));
|
||||
continue;
|
||||
}
|
||||
if (orientation.equals(FaceOrientation.DOWN)) {
|
||||
result.add(prepareFace(FaceOrientation.WEST, orientation,
|
||||
adjacentBlocks));
|
||||
continue;
|
||||
}
|
||||
if (orientation.equals(FaceOrientation.NORTH)) {
|
||||
result.add(prepareFace(FaceOrientation.UP, orientation,
|
||||
adjacentBlocks));
|
||||
continue;
|
||||
}
|
||||
if (orientation.equals(FaceOrientation.SOUTH)) {
|
||||
result.add(prepareFace(FaceOrientation.DOWN, orientation,
|
||||
adjacentBlocks));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
result.add(prepareFace(orientation, orientation, adjacentBlocks));
|
||||
result.add(prepareFace(orientation, block.getRotation(), adjacentBlocks));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Face prepareFace(FaceOrientation textureDirection, FaceOrientation faceDirection,
|
||||
HashMap<FaceOrientation, Boolean> adjacentBlocks, int textureRotation) {
|
||||
private Face prepareFace(FaceOrientation faceDirection, BlockRotation rotation,
|
||||
HashMap<FaceOrientation, Boolean> adjacentBlocks) {
|
||||
if (adjacentBlocks.get(faceDirection) && !cullFaceTextures.get(faceDirection)) {
|
||||
return new Face();
|
||||
}
|
||||
return new Face(textureCoordinates.get(textureDirection), uv.get(textureDirection),
|
||||
cuboid.getFacePositions(faceDirection), textureRotation);
|
||||
}
|
||||
|
||||
private Face prepareFace(FaceOrientation textureDirection, FaceOrientation faceDirection,
|
||||
HashMap<FaceOrientation, Boolean> adjacentBlocks) {
|
||||
return prepareFace(textureDirection, faceDirection,
|
||||
adjacentBlocks, 0);
|
||||
return new Face(textureCoordinates.get(faceDirection), uv.get(faceDirection),
|
||||
cuboid.getFacePositions(faceDirection, rotation));
|
||||
}
|
||||
|
||||
public boolean isFull() {
|
||||
|
@ -14,7 +14,9 @@
|
||||
package de.bixilon.minosoft.render.blockModels.subBlocks;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import de.bixilon.minosoft.game.datatypes.objectLoader.blocks.BlockRotation;
|
||||
import de.bixilon.minosoft.game.datatypes.world.BlockPosition;
|
||||
import de.bixilon.minosoft.render.blockModels.Face.Axis;
|
||||
|
||||
import static de.bixilon.minosoft.render.blockModels.Face.RenderConstants.blockRes;
|
||||
import static org.lwjgl.opengl.GL11.glVertex3f;
|
||||
@ -22,6 +24,17 @@ import static org.lwjgl.opengl.GL11.glVertex3f;
|
||||
public class SubBlockPosition {
|
||||
float x, y, z;
|
||||
|
||||
public 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);
|
||||
|
||||
public static final SubBlockRotation xAxisRotator = new SubBlockRotation(middlePos, Axis.Z, 90);
|
||||
public static final SubBlockRotation zAxisRotator = new SubBlockRotation(middlePos, Axis.X, 90);
|
||||
|
||||
public static final SubBlockRotation downRotator = new SubBlockRotation(middlePos, Axis.X, 180);
|
||||
|
||||
public SubBlockPosition(JsonArray json) {
|
||||
x = json.get(0).getAsFloat();
|
||||
y = json.get(1).getAsFloat();
|
||||
@ -54,4 +67,24 @@ public class SubBlockPosition {
|
||||
pos.getY() + y / blockRes,
|
||||
pos.getZ() + z / blockRes);
|
||||
}
|
||||
|
||||
public SubBlockPosition rotated(BlockRotation rotation) {
|
||||
|
||||
switch (rotation) {
|
||||
case EAST:
|
||||
return eastRotator.apply(this);
|
||||
case WEST:
|
||||
return westRotator.apply(this);
|
||||
case SOUTH:
|
||||
return southRotator.apply(this);
|
||||
case AXIS_X:
|
||||
return xAxisRotator.apply(this);
|
||||
case AXIS_Z:
|
||||
return zAxisRotator.apply(this);
|
||||
case DOWN:
|
||||
return downRotator.apply(this);
|
||||
default:
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -25,6 +25,12 @@ public class SubBlockRotation {
|
||||
Axis direction;
|
||||
float angle;
|
||||
|
||||
public SubBlockRotation(SubBlockPosition origin, Axis direction, float angle) {
|
||||
this.origin = origin;
|
||||
this.direction = direction;
|
||||
this.angle = angle;
|
||||
}
|
||||
|
||||
public SubBlockRotation(JsonObject rotation) {
|
||||
origin = new SubBlockPosition(rotation.get("origin").getAsJsonArray());
|
||||
String axis = rotation.get("axis").getAsString();
|
||||
@ -37,14 +43,19 @@ public class SubBlockRotation {
|
||||
break;
|
||||
case "z":
|
||||
direction = Axis.Z;
|
||||
break;
|
||||
}
|
||||
angle = rotation.get("angle").getAsFloat();
|
||||
}
|
||||
|
||||
private static Pair<Float, Float> rotate(float x, float y, float angle) {
|
||||
return new Pair<Float, Float>(
|
||||
x * (float) cos(angle) + y * (float) sin(angle),
|
||||
-x * (float) sin(angle) + y * (float) cos(angle)
|
||||
public static Pair<Float, Float> rotate(float x, float y, float angle) {
|
||||
float angleRad = (float) Math.toRadians(angle);
|
||||
float newX = x * (float) cos(angleRad) + y * (float) sin(angleRad);
|
||||
float newY = -x * (float) sin(angleRad) + y * (float) cos(angleRad);
|
||||
|
||||
return new Pair<>(
|
||||
newX,
|
||||
newY
|
||||
);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user