Add classic camera controller

This commit is contained in:
CrushedPixel
2015-11-07 18:19:02 +01:00
committed by johni0702
parent c2686f5d34
commit dd86cb8837

View File

@@ -1,116 +1,183 @@
package com.replaymod.replay.camera; package com.replaymod.replay.camera;
import net.minecraft.client.Minecraft;
import net.minecraft.client.settings.KeyBinding;
import net.minecraft.util.MathHelper;
import net.minecraft.util.Vec3;
import org.lwjgl.Sys;
// TODO: Marius is responsible for this. Please, someone clean it up.
public class ClassicCameraController implements CameraController { public class ClassicCameraController implements CameraController {
private static final double SPEED_CHANGE = 0.5;
private static final double LOWER_SPEED = 2;
private static final double UPPER_SPEED = 20;
private final CameraEntity camera;
private double MAX_SPEED = 10;
private double THRESHOLD = MAX_SPEED / 20;
private double DECAY = MAX_SPEED/3;
private Vec3 direction;
private Vec3 dirBefore;
private double motion;
private long lastCall = Sys.getTime();
private boolean speedup = false;
public ClassicCameraController(CameraEntity camera) {
this.camera = camera;
}
@Override @Override
public void update(float partialTicksPassed) { public void update(float partialTicksPassed) {
// TODO boolean forward = false, backward = false, left = false, right = false, up = false, down = false;
speedup = false;
for(KeyBinding kb : Minecraft.getMinecraft().gameSettings.keyBindings) {
if(!kb.isKeyDown()) continue;
if(kb.getKeyDescription().equals("key.forward")) {
forward = true;
speedup = true;
}
if(kb.getKeyDescription().equals("key.back")) {
backward = true;
speedup = true;
}
if(kb.getKeyDescription().equals("key.jump")) {
up = true;
speedup = true;
}
if(kb.getKeyDescription().equals("key.left")) {
left = true;
speedup = true;
}
if(kb.getKeyDescription().equals("key.right")) {
right = true;
speedup = true;
}
if(kb.getKeyDescription().equals("key.sneak")) {
down = true;
speedup = true;
}
}
forwardCameraMovement(forward, backward, left, right, up, down);
updateMovement();
} }
@Override @Override
public void increaseSpeed() { public void increaseSpeed() {
setCameraMaximumSpeed(MAX_SPEED + SPEED_CHANGE);
} }
@Override @Override
public void decreaseSpeed() { public void decreaseSpeed() {
setCameraMaximumSpeed(MAX_SPEED - SPEED_CHANGE);
} }
private void setCameraMaximumSpeed(double maxSpeed) {
// public static final double SPEED_CHANGE = 0.5; if(maxSpeed < LOWER_SPEED || maxSpeed > UPPER_SPEED) return;
// public static final double LOWER_SPEED = 2; MAX_SPEED = maxSpeed;
// public static final double UPPER_SPEED = 20; THRESHOLD = MAX_SPEED / 20;
// DECAY = 5;
// private static double MAX_SPEED = 10; }
// private static double THRESHOLD = MAX_SPEED / 20;
// private static double DECAY = MAX_SPEED/3; private void forwardCameraMovement(boolean forward, boolean backward, boolean left, boolean right, boolean up, boolean down) {
// if(forward && !backward) {
// public static void modifyCameraSpeed(boolean increase) { setMovement(MoveDirection.FORWARD);
// setCameraMaximumSpeed(getCameraMaximumSpeed() + (increase ? 1 : -1) * SPEED_CHANGE); } else if(backward && !forward) {
// } setMovement(MoveDirection.BACKWARD);
// }
// public static void setCameraMaximumSpeed(double maxSpeed) {
// if(maxSpeed < LOWER_SPEED || maxSpeed > UPPER_SPEED) return; if(left && !right) {
// MAX_SPEED = maxSpeed; setMovement(MoveDirection.LEFT);
// THRESHOLD = MAX_SPEED / 20; } else if(right && !left) {
// DECAY = 5; setMovement(MoveDirection.RIGHT);
// } }
//
// public static double getCameraMaximumSpeed() { if(up && !down) {
// return MAX_SPEED; setMovement(MoveDirection.UP);
// } } else if(down && !up) {
// setMovement(MoveDirection.DOWN);
// private Vec3 direction; }
// private Vec3 dirBefore; }
// private double motion;
// private long lastCall = 0; private void updateMovement() {
// long frac = Sys.getTime() - lastCall;
// private boolean speedup = false;
// if(frac == 0) return;
// //frac = time since last tick
// public void updateMovement() { double decFac = Math.max(0, 1 - (DECAY * (frac / 1000D)));
//
// long frac = Sys.getTime() - lastCall; if(speedup) {
// if(motion < THRESHOLD) motion = THRESHOLD;
// if(frac == 0) return; motion /= decFac;
// } else {
// double decFac = Math.max(0, 1 - (DECAY * (frac / 1000D))); motion *= decFac;
// }
// if(speedup) {
// if(motion < THRESHOLD) motion = THRESHOLD; motion = Math.min(motion, MAX_SPEED);
// motion /= decFac;
// } else { lastCall = Sys.getTime();
// motion *= decFac;
// } if(direction == null || motion < THRESHOLD) {
// return;
// motion = Math.min(motion, MAX_SPEED); }
//
// lastCall = Sys.getTime(); Vec3 movement = direction.normalize();
// double factor = motion * (frac / 1000D);
// if(direction == null || motion < THRESHOLD) {
// return; camera.moveCamera(movement.xCoord * factor, movement.yCoord * factor, movement.zCoord * factor);
// } }
//
// Vec3 movement = direction.normalize(); private void setMovement(MoveDirection dir) {
// double factor = motion * (frac / 1000D); float rotationPitch = camera.rotationPitch, rotationYaw = camera.rotationYaw;
// switch(dir) {
// moveCamera(movement.xCoord * factor, movement.yCoord * factor, movement.zCoord * factor); case BACKWARD:
// } direction = this.getVectorForRotation(-rotationPitch, rotationYaw - 180);
// break;
// public void setMovement(MoveDirection dir) { case DOWN:
// switch(dir) { direction = this.getVectorForRotation(90, 0);
// case BACKWARD: break;
// direction = this.getVectorForRotation(-rotationPitch, rotationYaw - 180); case FORWARD:
// break; direction = this.getVectorForRotation(rotationPitch, rotationYaw);
// case DOWN: break;
// direction = this.getVectorForRotation(90, 0); case LEFT:
// break; direction = this.getVectorForRotation(0, rotationYaw - 90);
// case FORWARD: break;
// direction = this.getVectorForRotation(rotationPitch, rotationYaw); case RIGHT:
// break; direction = this.getVectorForRotation(0, rotationYaw + 90);
// case LEFT: break;
// direction = this.getVectorForRotation(0, rotationYaw - 90); case UP:
// break; direction = this.getVectorForRotation(-90, 0);
// case RIGHT: break;
// direction = this.getVectorForRotation(0, rotationYaw + 90); }
// break;
// case UP: Vec3 dbf = direction;
// direction = this.getVectorForRotation(-90, 0);
// break; if(dirBefore != null) {
// } direction = dirBefore.normalize().add(direction);
// }
// Vec3 dbf = direction;
// dirBefore = dbf;
// if(dirBefore != null) {
// direction = dirBefore.normalize().add(direction); updateMovement();
// } }
//
// dirBefore = dbf; private Vec3 getVectorForRotation(float pitch, float yaw) {
// float f2 = MathHelper.cos(-yaw * 0.017453292F - (float) Math.PI);
// updateMovement(); float f3 = MathHelper.sin(-yaw * 0.017453292F - (float)Math.PI);
// } float f4 = -MathHelper.cos(-pitch * 0.017453292F);
// float f5 = MathHelper.sin(-pitch * 0.017453292F);
// public enum MoveDirection { return new Vec3((double)(f3 * f4), (double)f5, (double)(f2 * f4));
// UP, DOWN, LEFT, RIGHT, FORWARD, BACKWARD }
// }
public enum MoveDirection {
UP, DOWN, LEFT, RIGHT, FORWARD, BACKWARD
}
} }