Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/cr0s/warpdrive/api/ICamera.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cr0s.warpdrive.api;

public interface ICamera extends IVideoChannel {

String CAMERA_YAW_TAG = "cameraYaw";
String CAMERA_PITCH_TAG = "cameraPitch";

boolean hasCameraOrientation();

float getCameraYaw();

float getCameraPitch();

void setCameraOrientation(final float yaw, final float pitch);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cr0s.warpdrive.Commons;
import cr0s.warpdrive.WarpDrive;
import cr0s.warpdrive.api.ICamera;
import cr0s.warpdrive.api.IVideoChannel;
import cr0s.warpdrive.api.WarpDriveText;
import cr0s.warpdrive.block.BlockAbstractRotatingContainer;
Expand Down Expand Up @@ -73,8 +74,12 @@ public boolean onBlockActivated(@Nonnull final World world, @Nonnull final Block
camera.blockPos.getX(),
camera.blockPos.getY(),
camera.blockPos.getZ() ));
final TileEntity tileEntityCamera = world.getTileEntity(camera.blockPos);
final ICamera cameraTile = tileEntityCamera instanceof ICamera ? (ICamera) tileEntityCamera : null;
ClientCameraHandler.setupViewpoint(
camera.type, entityPlayer, entityPlayer.rotationYaw, entityPlayer.rotationPitch,
camera.type, entityPlayer,
cameraTile != null && cameraTile.hasCameraOrientation() ? cameraTile.getCameraYaw() : entityPlayer.rotationYaw,
cameraTile != null && cameraTile.hasCameraOrientation() ? cameraTile.getCameraPitch() : entityPlayer.rotationPitch,
blockPos, blockState,
camera.blockPos, world.getBlockState(camera.blockPos));
return true;
Expand All @@ -83,4 +88,4 @@ public boolean onBlockActivated(@Nonnull final World world, @Nonnull final Block

return super.onBlockActivated(world, blockPos, blockState, entityPlayer, enumHand, enumFacing, hitX, hitY, hitZ);
}
}
}
41 changes: 39 additions & 2 deletions src/main/java/cr0s/warpdrive/block/detection/TileEntityCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cr0s.warpdrive.Commons;
import cr0s.warpdrive.WarpDrive;
import cr0s.warpdrive.api.ICamera;
import cr0s.warpdrive.api.IVideoChannel;
import cr0s.warpdrive.api.WarpDriveText;
import cr0s.warpdrive.block.TileEntityAbstractMachine;
Expand Down Expand Up @@ -49,9 +50,11 @@
import net.minecraftforge.common.util.Constants.NBT;
import net.minecraftforge.fml.common.Optional;

public class TileEntityCamera extends TileEntityAbstractMachine implements IVideoChannel {
public class TileEntityCamera extends TileEntityAbstractMachine implements ICamera {

private int videoChannel = -1;
private float cameraYaw = Float.NaN;
private float cameraPitch = Float.NaN;

private static final int REGISTRY_UPDATE_INTERVAL_TICKS = 15 * 20;
private static final int PACKET_SEND_INTERVAL_TICKS = 60 * 20;
Expand Down Expand Up @@ -430,6 +433,28 @@ public void setVideoChannel(final int parVideoChannel) {
registryUpdateTicks = 0;
}
}

@Override
public boolean hasCameraOrientation() {
return Float.isFinite(cameraYaw) && Float.isFinite(cameraPitch);
}

@Override
public float getCameraYaw() {
return cameraYaw;
}

@Override
public float getCameraPitch() {
return cameraPitch;
}

@Override
public void setCameraOrientation(final float yaw, final float pitch) {
cameraYaw = yaw;
cameraPitch = pitch;
markDirty();
}

@Override
public void invalidate() {
Expand All @@ -454,6 +479,14 @@ public void readFromNBT(@Nonnull final NBTTagCompound tagCompound) {
super.readFromNBT(tagCompound);

videoChannel = tagCompound.getInteger("frequency") + tagCompound.getInteger(VIDEO_CHANNEL_TAG);
if ( tagCompound.hasKey(CAMERA_YAW_TAG)
&& tagCompound.hasKey(CAMERA_PITCH_TAG) ) {
cameraYaw = tagCompound.getFloat(CAMERA_YAW_TAG);
cameraPitch = tagCompound.getFloat(CAMERA_PITCH_TAG);
} else {
cameraYaw = Float.NaN;
cameraPitch = Float.NaN;
}
if (WarpDriveConfig.LOGGING_VIDEO_CHANNEL) {
WarpDrive.logger.info(this + " readFromNBT");
}
Expand Down Expand Up @@ -486,6 +519,10 @@ public NBTTagCompound writeToNBT(@Nonnull NBTTagCompound tagCompound) {
tagCompound = super.writeToNBT(tagCompound);

tagCompound.setInteger(VIDEO_CHANNEL_TAG, videoChannel);
if (hasCameraOrientation()) {
tagCompound.setFloat(CAMERA_YAW_TAG, cameraYaw);
tagCompound.setFloat(CAMERA_PITCH_TAG, cameraPitch);
}
if (WarpDriveConfig.LOGGING_VIDEO_CHANNEL) {
WarpDrive.logger.info(this + " writeToNBT");
}
Expand Down Expand Up @@ -662,4 +699,4 @@ public String toString() {
videoChannel,
Commons.format(world, pos) );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cr0s.warpdrive.Commons;
import cr0s.warpdrive.WarpDrive;
import cr0s.warpdrive.api.ICamera;
import cr0s.warpdrive.api.IVideoChannel;
import cr0s.warpdrive.block.TileEntityLaser;
import cr0s.warpdrive.config.WarpDriveConfig;
Expand All @@ -18,9 +19,11 @@

import net.minecraftforge.fml.common.Optional;

public class TileEntityLaserCamera extends TileEntityLaser implements IVideoChannel {
public class TileEntityLaserCamera extends TileEntityLaser implements ICamera {

private int videoChannel = -1;
private float cameraYaw = Float.NaN;
private float cameraPitch = Float.NaN;

private static final int REGISTRY_UPDATE_INTERVAL_TICKS = 15 * 20;
private static final int PACKET_SEND_INTERVAL_TICKS = 60 * 20;
Expand Down Expand Up @@ -83,18 +86,52 @@ public void setVideoChannel(final int parVideoChannel) {
registryUpdateTicks = 0;
}
}

@Override
public boolean hasCameraOrientation() {
return Float.isFinite(cameraYaw) && Float.isFinite(cameraPitch);
}

@Override
public float getCameraYaw() {
return cameraYaw;
}

@Override
public float getCameraPitch() {
return cameraPitch;
}

@Override
public void setCameraOrientation(final float yaw, final float pitch) {
cameraYaw = yaw;
cameraPitch = pitch;
markDirty();
}

@Override
public void readFromNBT(@Nonnull final NBTTagCompound tagCompound) {
super.readFromNBT(tagCompound);
setVideoChannel(tagCompound.getInteger("cameraFrequency") + tagCompound.getInteger(VIDEO_CHANNEL_TAG));
if ( tagCompound.hasKey(CAMERA_YAW_TAG)
&& tagCompound.hasKey(CAMERA_PITCH_TAG) ) {
cameraYaw = tagCompound.getFloat(CAMERA_YAW_TAG);
cameraPitch = tagCompound.getFloat(CAMERA_PITCH_TAG);
} else {
cameraYaw = Float.NaN;
cameraPitch = Float.NaN;
}
}

@Nonnull
@Override
public NBTTagCompound writeToNBT(@Nonnull NBTTagCompound tagCompound) {
tagCompound = super.writeToNBT(tagCompound);
tagCompound.setInteger(VIDEO_CHANNEL_TAG, videoChannel);
if (hasCameraOrientation()) {
tagCompound.setFloat(CAMERA_YAW_TAG, cameraYaw);
tagCompound.setFloat(CAMERA_PITCH_TAG, cameraPitch);
}
return tagCompound;
}

Expand Down Expand Up @@ -145,4 +182,4 @@ public String toString() {
videoChannel,
Commons.format(world, pos));
}
}
}
89 changes: 89 additions & 0 deletions src/main/java/cr0s/warpdrive/network/MessageCameraOrientation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package cr0s.warpdrive.network;

import cr0s.warpdrive.api.ICamera;
import cr0s.warpdrive.api.IVideoChannel;
import cr0s.warpdrive.block.detection.TileEntityMonitor;

import io.netty.buffer.ByteBuf;

import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.WorldServer;

import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class MessageCameraOrientation implements IMessage, IMessageHandler<MessageCameraOrientation, IMessage> {

private BlockPos blockPosMonitor;
private BlockPos blockPosCamera;
private float yaw;
private float pitch;

@SuppressWarnings("unused")
public MessageCameraOrientation() {
// required on receiving side
}

public MessageCameraOrientation(final BlockPos blockPosMonitor, final BlockPos blockPosCamera, final float yaw, final float pitch) {
this.blockPosMonitor = blockPosMonitor;
this.blockPosCamera = blockPosCamera;
this.yaw = yaw;
this.pitch = pitch;
}

@Override
public void fromBytes(final ByteBuf buffer) {
blockPosMonitor = BlockPos.fromLong(buffer.readLong());
blockPosCamera = BlockPos.fromLong(buffer.readLong());
yaw = buffer.readFloat();
pitch = buffer.readFloat();
}

@Override
public void toBytes(final ByteBuf buffer) {
buffer.writeLong(blockPosMonitor.toLong());
buffer.writeLong(blockPosCamera.toLong());
buffer.writeFloat(yaw);
buffer.writeFloat(pitch);
}

private void handle(final EntityPlayerMP entityPlayerMP) {
if (!Float.isFinite(yaw) || !Float.isFinite(pitch)) {
return;
}

final WorldServer world = entityPlayerMP.getServerWorld();
if ( !world.isBlockLoaded(blockPosMonitor, false)
|| !world.isBlockLoaded(blockPosCamera, false)
|| entityPlayerMP.getDistanceSq(blockPosMonitor.getX() + 0.5D, blockPosMonitor.getY() + 0.5D, blockPosMonitor.getZ() + 0.5D) > 64.0D ) {
return;
}

final TileEntity tileEntityMonitor = world.getTileEntity(blockPosMonitor);
final TileEntity tileEntityCamera = world.getTileEntity(blockPosCamera);
if ( !(tileEntityMonitor instanceof TileEntityMonitor)
|| !(tileEntityCamera instanceof ICamera) ) {
return;
}

final int videoChannel = ((TileEntityMonitor) tileEntityMonitor).getVideoChannel();
final ICamera camera = (ICamera) tileEntityCamera;
if ( !IVideoChannel.isValid(videoChannel)
|| videoChannel != camera.getVideoChannel() ) {
return;
}

camera.setCameraOrientation(MathHelper.wrapDegrees(yaw), MathHelper.clamp(pitch, -90.0F, 90.0F));
}

@Override
public IMessage onMessage(final MessageCameraOrientation message, final MessageContext context) {
final EntityPlayerMP entityPlayerMP = context.getServerHandler().player;
entityPlayerMP.getServerWorld().addScheduledTask(() -> message.handle(entityPlayerMP));
return null;
}
}
8 changes: 7 additions & 1 deletion src/main/java/cr0s/warpdrive/network/PacketHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public static void init() {
} catch (final Exception exception) {
throw new RuntimeException(exception);
}
simpleNetworkManager.registerMessage(MessageCameraOrientation.class , MessageCameraOrientation.class , 104, Side.SERVER);
}

// Beam effect sent to client side
Expand Down Expand Up @@ -235,6 +236,11 @@ public static void sendLaserTargetingPacket(final int x, final int y, final int
x, y, z, yaw, pitch));
}
}

public static void sendCameraOrientationPacket(final BlockPos blockPosMonitor, final BlockPos blockPosCamera,
final float yaw, final float pitch) {
simpleNetworkManager.sendToServer(new MessageCameraOrientation(blockPosMonitor, blockPosCamera, yaw, pitch));
}

// Sending cloaking area definition (server -> client)
public static void sendCloakPacket(final EntityPlayerMP entityPlayerMP, final CloakedArea area, final boolean isUncloaking) {
Expand Down Expand Up @@ -348,4 +354,4 @@ public static void sendUnseating() {
WarpDrive.logger.info("Sent unseating packet");
}
}
}
}
20 changes: 14 additions & 6 deletions src/main/java/cr0s/warpdrive/render/EntityCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.minecraft.util.EnumHandSide;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.world.World;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
Expand All @@ -36,6 +37,8 @@ public final class EntityCamera extends EntityLivingBase {
private int cameraZ;

private EntityPlayer player;
private float playerYawLast;
private float playerPitchLast;

private int dx = 0, dy = 0, dz = 0;

Expand All @@ -62,6 +65,8 @@ public EntityCamera(final World world, final int x, final int y, final int z, fi
cameraY = y;
cameraZ = z;
this.player = player;
playerYawLast = player.rotationYaw;
playerPitchLast = player.rotationPitch;
}

@Override
Expand Down Expand Up @@ -91,6 +96,7 @@ private void closeCamera() {
return;
}

PacketHandler.sendCameraOrientationPacket(ClientCameraHandler.blockPosCheck1, new BlockPos(cameraX, cameraY, cameraZ), rotationYaw, rotationPitch);
ClientCameraHandler.resetViewpoint();
world.removeEntity(this);
isActive = false;
Expand All @@ -113,13 +119,15 @@ public void onEntityUpdate() {
return;
}

final float deltaYaw = MathHelper.wrapDegrees(player.rotationYaw - playerYawLast);
final float deltaPitch = player.rotationPitch - playerPitchLast;
rotationYaw = MathHelper.wrapDegrees(rotationYaw + deltaYaw);
rotationPitch = MathHelper.clamp(rotationPitch + deltaPitch, -90.0F, 90.0F);
playerYawLast = player.rotationYaw;
playerPitchLast = player.rotationPitch;

final Block block = world.getBlockState(new BlockPos(cameraX, cameraY, cameraZ)).getBlock();
final Minecraft mc = Minecraft.getMinecraft();
if (mc.getRenderViewEntity() != null) {
mc.getRenderViewEntity().rotationYaw = player.rotationYaw;
// mc.renderViewEntity.rotationYawHead = player.rotationYawHead;
mc.getRenderViewEntity().rotationPitch = player.rotationPitch;
}

ClientCameraHandler.overlayLoggingMessage = "Mouse " + Mouse.isButtonDown(0) + " " + Mouse.isButtonDown(1) + " " + Mouse.isButtonDown(2) + " " + Mouse.isButtonDown(3)
+ "\nBackspace " + Keyboard.isKeyDown(Keyboard.KEY_BACKSLASH)
Expand Down Expand Up @@ -274,4 +282,4 @@ public ItemStack getItemStackFromSlot(@Nonnull final EntityEquipmentSlot slotIn)
public void setItemStackToSlot(@Nonnull final EntityEquipmentSlot slotIn, @Nullable final ItemStack itemStack) {

}
}
}