Skip to content

Commit

Permalink
Converge rotation and translation to transformations
Browse files Browse the repository at this point in the history
  • Loading branch information
oh-noey committed Aug 28, 2024
1 parent f8cc103 commit 99c4586
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 506 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

public class MovementController {
private static final @NotNull MovecraftLocation ZERO = new MovecraftLocation(0,0,0);
private static final @NotNull MovementData DEFAULT = new MovementData(ZERO, MovecraftRotation.NONE, null);
private static final @NotNull MovementData DEFAULT = new MovementData(AffineTransformation.NONE, null);
private final @NotNull CraftDataTagKey<MovementData> _dataTagKey;

public MovementController(Plugin plugin) {
Expand All @@ -28,8 +28,7 @@ public MovementController(Plugin plugin) {

public void move(@NotNull Craft craft, @Nullable MovecraftLocation translation, @Nullable MovecraftRotation rotation, @Nullable MovecraftWorld changeWorld){
craft.computeDataTag(_dataTagKey, (data) -> data.merge(new MovementData(
translation == null ? ZERO : translation,
rotation == null ? MovecraftRotation.NONE : rotation,
AffineTransformation.of(translation).mult(AffineTransformation.of(rotation)),
changeWorld)));
}

Expand All @@ -54,23 +53,20 @@ public MovementTask(@NotNull Craft craft){
}
}

private record MovementData(@NotNull MovecraftLocation translation, @NotNull MovecraftRotation rotation, @Nullable MovecraftWorld world){

private static MovecraftRotation add(MovecraftRotation a, MovecraftRotation b){
if(a == MovecraftRotation.NONE || a==b){
return b;
}
if(b == MovecraftRotation.NONE){
return a;
}

return MovecraftRotation.NONE;
}
// TODO: Implement int matrix or find library (most are for floating point)
private record AffineTransformation(){
public static @NotNull AffineTransformation NONE = null;
public static @NotNull AffineTransformation of(MovecraftLocation translation){ return null; }
public static @NotNull AffineTransformation of(MovecraftRotation rotation){ return null; }
public @NotNull AffineTransformation mult(AffineTransformation other){ return null; }
public @NotNull MovecraftLocation apply(MovecraftLocation location){ return location; }
}

private record MovementData(@NotNull AffineTransformation transformation, @Nullable MovecraftWorld world){
@Contract("_ -> new")
public @NotNull MovementData merge(@NotNull MovementData other){
// TODO: Correct this
return new MovementData(translation.add(other.translation), add(rotation, other.rotation), this.world == null ? other.world : this.world);
return new MovementData(transformation.mult(other.transformation), this.world == null ? other.world : this.world);
}
}
}
30 changes: 25 additions & 5 deletions api/src/main/java/net/countercraft/movecraft/WorldHandler.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package net.countercraft.movecraft;

import net.countercraft.movecraft.craft.Craft;
import net.countercraft.movecraft.util.AffineTransformation;
import net.countercraft.movecraft.util.hitboxes.HitBox;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
Expand All @@ -11,15 +13,33 @@
import org.jetbrains.annotations.Nullable;

public abstract class WorldHandler {
public abstract void rotateCraft(@NotNull Craft craft, @NotNull MovecraftLocation originLocation, @NotNull MovecraftRotation rotation);
public abstract void translateCraft(@NotNull Craft craft, @NotNull MovecraftLocation newLocation, @NotNull World world);
@Deprecated(forRemoval = true)
public void rotateCraft(@NotNull Craft craft, @NotNull MovecraftLocation originLocation, @NotNull MovecraftRotation rotation){
transformHitBox(
craft.getHitBox(),
AffineTransformation.of(originLocation)
.mult(AffineTransformation.of(rotation))
.mult(AffineTransformation.of(originLocation.scalarMultiply(-1))),
craft.getWorld(),
craft.getWorld());
}

@Deprecated(forRemoval = true)
public void translateCraft(@NotNull Craft craft, @NotNull MovecraftLocation displacement, @NotNull World world){
transformHitBox(craft.getHitBox(), AffineTransformation.of(displacement), craft.getWorld(), world);
}
public abstract void transformHitBox(@NotNull HitBox hitbox, @NotNull AffineTransformation transformation, @NotNull World originWorld, @NotNull World destinationWorld);
public abstract void setBlockFast(@NotNull Location location, @NotNull BlockData data);
public abstract void setBlockFast(@NotNull Location location, @NotNull MovecraftRotation rotation, @NotNull BlockData data);
@Deprecated(forRemoval = true)
public abstract @Nullable Location getAccessLocation(@NotNull InventoryView inventoryView); // Not needed for 1.20+, remove when dropping support for 1.18.2
public @Nullable Location getAccessLocation(@NotNull InventoryView inventoryView){
// Not needed for 1.20+, remove when dropping support for 1.18.2
return null;
}
@Deprecated(forRemoval = true)
public abstract void setAccessLocation(@NotNull InventoryView inventoryView, @NotNull Location location); // Not needed for 1.20+, remove when dropping support for 1.18.2

public void setAccessLocation(@NotNull InventoryView inventoryView, @NotNull Location location){
// Not needed for 1.20+, remove when dropping support for 1.18.2
}
public static @NotNull String getPackageName(@NotNull String minecraftVersion) {
String[] parts = minecraftVersion.split("\\.");
if (parts.length < 2)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package net.countercraft.movecraft.util;

import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.MovecraftRotation;
import org.jetbrains.annotations.NotNull;

// TODO: Implement with 4d matrix
public record AffineTransformation(){
public static @NotNull AffineTransformation NONE = null;
public static @NotNull AffineTransformation of(MovecraftLocation translation){ return null; }
public static @NotNull AffineTransformation of(MovecraftRotation rotation){ return null; }
public @NotNull AffineTransformation mult(AffineTransformation other){ return null; }
public @NotNull MovecraftLocation apply(MovecraftLocation location){ return location; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import com.google.common.collect.UnmodifiableIterator;
import net.countercraft.movecraft.MovecraftLocation;
import net.countercraft.movecraft.exception.EmptyHitBoxException;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.util.AbstractSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Stream;

public interface HitBox extends Iterable<MovecraftLocation>{
int getMinX();
Expand Down Expand Up @@ -94,6 +96,12 @@ default Set<MovecraftLocation> asSet(){
return new HitBoxSetView(this);
}

@NotNull
@Contract(pure = true)
default Stream<MovecraftLocation> stream(){
return asSet().stream();
}

@NotNull
HitBox difference(HitBox other);

Expand Down
Loading

0 comments on commit 99c4586

Please sign in to comment.