BloodMagic/src/main/java/WayofTime/bloodmagic/item/sigil/ItemSigilPhantomBridge.java

89 lines
3.2 KiB
Java
Raw Normal View History

2015-12-27 19:38:12 -05:00
package WayofTime.bloodmagic.item.sigil;
import WayofTime.bloodmagic.apibutnotreally.util.helper.PlayerHelper;
2017-08-15 21:30:48 -07:00
import WayofTime.bloodmagic.core.RegistrarBloodMagicBlocks;
2015-12-27 19:38:12 -05:00
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
2016-03-18 15:38:26 -04:00
import net.minecraft.util.math.BlockPos;
2015-12-27 19:38:12 -05:00
import net.minecraft.world.World;
import org.apache.commons.lang3.tuple.Pair;
2017-08-15 21:30:48 -07:00
import java.util.HashMap;
import java.util.Map;
2015-12-27 19:38:12 -05:00
2017-08-15 21:30:48 -07:00
public class ItemSigilPhantomBridge extends ItemSigilToggleableBase {
private Map<EntityPlayer, Pair<Double, Double>> prevPositionMap = new HashMap<EntityPlayer, Pair<Double, Double>>();
private double expansionFactor = 2;
private int range = 3;
2017-08-15 21:30:48 -07:00
public ItemSigilPhantomBridge() {
2017-08-16 16:39:57 -07:00
super("phantom_bridge", 100);
2015-12-27 19:38:12 -05:00
}
@Override
2017-08-15 21:30:48 -07:00
public void onSigilUpdate(ItemStack stack, World world, EntityPlayer player, int itemSlot, boolean isSelected) {
if (PlayerHelper.isFakePlayer(player))
return;
2017-08-15 21:30:48 -07:00
if (!prevPositionMap.containsKey(player)) {
prevPositionMap.put(player, Pair.of(player.posX, player.posZ));
}
2017-08-15 21:30:48 -07:00
if ((!player.onGround && !player.isRiding()) && !player.isSneaking()) {
prevPositionMap.put(player, Pair.of(player.posX, player.posZ));
2015-12-27 19:38:12 -05:00
return;
}
2015-12-27 19:38:12 -05:00
int verticalOffset = -1;
if (player.isSneaking())
verticalOffset--;
Pair<Double, Double> prevPosition = prevPositionMap.get(player);
2015-12-27 19:38:12 -05:00
double playerVelX = player.posX - prevPosition.getLeft();
double playerVelZ = player.posZ - prevPosition.getRight();
double totalVel = Math.sqrt(playerVelX * playerVelX + playerVelZ * playerVelZ);
2017-08-15 21:30:48 -07:00
if (totalVel > 2) {
//I am SURE you are teleporting!
playerVelX = 0;
playerVelZ = 0;
totalVel = 0;
}
BlockPos playerPos = player.getPosition();
int posX = playerPos.getX();
int posY = playerPos.getY();
int posZ = playerPos.getZ();
double offsetPosX = posX + expansionFactor * playerVelX;
double offsetPosZ = posZ + expansionFactor * playerVelZ;
double avgX = (posX + offsetPosX) / 2;
double avgZ = (posZ + offsetPosZ) / 2;
double C = 2 * (range + expansionFactor * totalVel) + 1;
int truncC = (int) C;
//TODO: Make this for-loop better.
2017-08-15 21:30:48 -07:00
for (int ix = -truncC; ix <= truncC; ix++) {
for (int iz = -truncC; iz <= truncC; iz++) {
if (computeEllipse(ix + avgX, iz + avgZ, posX, posZ, offsetPosX, offsetPosZ) > C) {
continue;
}
BlockPos blockPos = new BlockPos(ix + posX, posY + verticalOffset, iz + posZ);
2015-12-27 19:38:12 -05:00
if (world.isAirBlock(blockPos))
world.setBlockState(blockPos, RegistrarBloodMagicBlocks.PHANTOM.getDefaultState());
2015-12-27 19:38:12 -05:00
}
}
prevPositionMap.put(player, Pair.of(player.posX, player.posZ));
}
2017-08-15 21:30:48 -07:00
public static double computeEllipse(double x, double z, double focusX1, double focusZ1, double focusX2, double focusZ2) {
return Math.sqrt((x - focusX1) * (x - focusX1) + (z - focusZ1) * (z - focusZ1)) + Math.sqrt((x - focusX2) * (x - focusX2) + (z - focusZ2) * (z - focusZ2));
2015-12-27 19:38:12 -05:00
}
}