This repository has been archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Structure placement code (multi-structure support)
- Loading branch information
1 parent
7e0f6f8
commit 77917dc
Showing
10 changed files
with
232 additions
and
30 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
core-api/src/main/java/dev/compactmods/machines/api/util/AABBAligner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package dev.compactmods.machines.api.util; | ||
|
||
import net.minecraft.core.Direction; | ||
import net.minecraft.world.phys.AABB; | ||
import net.minecraft.world.phys.Vec3; | ||
import org.joml.Vector3d; | ||
|
||
public class AABBAligner { | ||
|
||
private final AABB outerBounds; | ||
private final Vector3d size; | ||
private Vector3d center; | ||
|
||
private AABBAligner(AABB outerBounds, Vector3d center, Vector3d size) { | ||
this.outerBounds = outerBounds; | ||
this.size = size; | ||
this.center = center; | ||
} | ||
|
||
public static AABBAligner create(AABB boundaries, AABB pendingAlign) { | ||
return new AABBAligner(boundaries, VectorUtils.convert3d(pendingAlign.getCenter()), AABBHelper.sizeOf(pendingAlign)); | ||
} | ||
|
||
public static AABBAligner create(AABB boundaries, Vector3d size) { | ||
return new AABBAligner(boundaries, new Vector3d(), size); | ||
} | ||
|
||
public AABBAligner center(Vec3 center) { | ||
this.center = VectorUtils.convert3d(center); | ||
return this; | ||
} | ||
|
||
public AABBAligner center(Vector3d center) { | ||
this.center = center; | ||
return this; | ||
} | ||
|
||
public AABBAligner center() { | ||
this.center = VectorUtils.convert3d(outerBounds.getCenter()); | ||
return this; | ||
} | ||
|
||
public AABBAligner boundedDirection(Direction direction) { | ||
var outerEdge = DirectionalMath.directionalEdge(direction, outerBounds); | ||
var targetPoint = DirectionalMath.directionalEdge(direction, center, size); | ||
|
||
var offset = outerEdge.sub(targetPoint); | ||
|
||
center.add(offset); | ||
|
||
return this; | ||
} | ||
|
||
public AABB align() { | ||
return AABB.ofSize(VectorUtils.convert3d(center), size.x, size.y, size.z); | ||
} | ||
|
||
public static AABB center(AABB source, Vec3 center) { | ||
final var size = AABBHelper.sizeOf(source); | ||
return AABB.ofSize(center, size.x, size.y, size.z); | ||
} | ||
|
||
public static AABB center(AABB source, Vector3d center) { | ||
return center(source, VectorUtils.convert3d(center)); | ||
} | ||
|
||
public static AABB floor(AABB source, AABB within) { | ||
double targetY = within.minY; | ||
return floor(source, targetY); | ||
} | ||
|
||
public static AABB floor(AABB source, double targetY) { | ||
double offset = source.minY - targetY; | ||
return source.move(0, offset * -1, 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
core-api/src/main/java/dev/compactmods/machines/api/util/DirectionalMath.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package dev.compactmods.machines.api.util; | ||
|
||
import net.minecraft.core.Direction; | ||
import net.minecraft.world.phys.AABB; | ||
import org.joml.Vector3d; | ||
|
||
public class DirectionalMath { | ||
|
||
public static Vector3d directionalEdge(Direction direction, Vector3d origin, Vector3d size) { | ||
final var normal = direction.getNormal(); | ||
|
||
var x = origin.x + ((size.x / 2) * normal.getX()); | ||
var y = origin.y + ((size.y / 2) * normal.getY()); | ||
var z = origin.z + ((size.z / 2) * normal.getZ()); | ||
|
||
return new Vector3d(x, y, z); | ||
} | ||
|
||
public static Vector3d directionalEdge(Direction direction, AABB aabb) { | ||
var center = VectorUtils.convert3d(aabb.getCenter()); | ||
return directionalEdge(direction, center, AABBHelper.sizeOf(aabb)); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
core-api/src/main/java/dev/compactmods/machines/api/util/VectorUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dev.compactmods.machines.api.util; | ||
|
||
import net.minecraft.core.Vec3i; | ||
import net.minecraft.world.phys.Vec3; | ||
import org.joml.Vector3d; | ||
|
||
public class VectorUtils { | ||
|
||
public static Vector3d convert3d(Vec3i vector) { | ||
return new Vector3d(vector.getX(), vector.getY(), vector.getZ()); | ||
} | ||
|
||
public static Vector3d convert3d(Vec3 vector) { | ||
return new Vector3d(vector.x, vector.y, vector.z); | ||
} | ||
|
||
public static Vec3 convert3d(Vector3d vector) { | ||
return new Vec3(vector.x, vector.y, vector.z); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
room-api/src/main/java/dev/compactmods/machines/api/room/RoomStructureInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package dev.compactmods.machines.api.room; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.resources.ResourceLocation; | ||
import net.minecraft.util.StringRepresentable; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public record RoomStructureInfo(ResourceLocation template, RoomStructurePlacement placement) { | ||
public static final Codec<RoomStructureInfo> CODEC = RecordCodecBuilder.create(inst -> inst.group( | ||
ResourceLocation.CODEC.fieldOf("template").forGetter(RoomStructureInfo::template), | ||
RoomStructurePlacement.CODEC.fieldOf("placement").forGetter(RoomStructureInfo::placement) | ||
).apply(inst, RoomStructureInfo::new)); | ||
|
||
public enum RoomStructurePlacement implements StringRepresentable { | ||
CENTERED_CEILING("centered_ceiling"), | ||
CENTERED("centered"), | ||
CENTERED_FLOOR("centered_floor"); | ||
|
||
public static final StringRepresentable.StringRepresentableCodec<RoomStructurePlacement> CODEC = StringRepresentable.fromEnum(RoomStructurePlacement::values); | ||
|
||
private final String name; | ||
|
||
RoomStructurePlacement(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public @NotNull String getSerializedName() { | ||
return name; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters