9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-19 15:09:15 +00:00

添加硬编码旋转属性

This commit is contained in:
XiaoMoMi
2025-11-06 00:30:51 +08:00
parent 94a19c61a2
commit 4240d01c1f
2 changed files with 75 additions and 2 deletions

View File

@@ -4,6 +4,8 @@ import net.momirealms.craftengine.core.block.ImmutableBlockState;
import net.momirealms.craftengine.core.item.context.BlockPlaceContext;
import net.momirealms.craftengine.core.util.Direction;
import net.momirealms.craftengine.core.util.HorizontalDirection;
import net.momirealms.craftengine.core.util.MiscUtils;
import net.momirealms.craftengine.core.util.SegmentedAngle;
import net.momirealms.sparrow.nbt.Tag;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -33,7 +35,7 @@ public abstract class Property<T extends Comparable<T>> {
Property<Direction> directionProperty = (Property<Direction>) property;
return (context, state) -> state.with(directionProperty, context.getNearestLookingDirection().opposite());
} else {
throw new IllegalArgumentException("Unsupported property type used in hard-coded `facing` property: " + property.valueClass());
return (context, state) -> state;
}
}));
HARD_CODED_PLACEMENTS.put("facing_clockwise", (property -> {
@@ -41,13 +43,26 @@ public abstract class Property<T extends Comparable<T>> {
Property<HorizontalDirection> directionProperty = (Property<HorizontalDirection>) property;
return (context, state) -> state.with(directionProperty, context.getHorizontalDirection().clockWise().toHorizontalDirection());
} else {
throw new IllegalArgumentException("Unsupported property type used in hard-coded `facing_clockwise` property: " + property.valueClass());
return (context, state) -> state;
}
}));
HARD_CODED_PLACEMENTS.put("waterlogged", (property -> {
Property<Boolean> waterloggedProperty = (Property<Boolean>) property;
return (context, state) -> state.with(waterloggedProperty, context.isWaterSource());
}));
HARD_CODED_PLACEMENTS.put("rotation", property -> {
if (property.valueClass() == Integer.class) {
IntegerProperty rotationProperty = (IntegerProperty) property;
if (rotationProperty.min == 0 && rotationProperty.max > 0) {
return (context, state) -> {
float rotation = context.getRotation();
SegmentedAngle segmentedAngle = new SegmentedAngle(rotationProperty.max + 1);
return state.with(rotationProperty, segmentedAngle.fromDegrees(rotation + 180));
};
}
}
return (context, state) -> state;
});
}
private final Class<T> clazz;

View File

@@ -0,0 +1,58 @@
package net.momirealms.craftengine.core.util;
public class SegmentedAngle {
private final int segments;
private final float degreeToAngle;
private final float angleToDegree;
public SegmentedAngle(int segments) {
if (segments < 2) {
throw new IllegalArgumentException("Segments cannot be less than 2");
} else if (segments > 360) {
throw new IllegalArgumentException("Segments cannot be greater than 360");
} else {
this.segments = segments;
this.degreeToAngle = (float) segments / 360.0F;
this.angleToDegree = 360.0F / (float) segments;
}
}
public boolean isSameAxis(int alpha, int beta) {
int halfSegments = this.segments / 2;
return (alpha % halfSegments) == (beta % halfSegments);
}
public int fromDirection(Direction direction) {
if (direction.axis().isVertical()) {
return 0;
} else {
int i = direction.data2d();
return i * (this.segments / 4);
}
}
public int fromDegreesWithTurns(float degrees) {
return Math.round(degrees * this.degreeToAngle);
}
public int fromDegrees(float degrees) {
return this.normalize(this.fromDegreesWithTurns(degrees));
}
public float toDegreesWithTurns(int rotation) {
return (float) rotation * this.angleToDegree;
}
public float toDegrees(int rotation) {
float f = this.toDegreesWithTurns(this.normalize(rotation));
return f >= 180.0F ? f - 360.0F : f;
}
public int normalize(int rotationBits) {
return ((rotationBits % this.segments) + this.segments) % this.segments;
}
public int getSegments() {
return this.segments;
}
}