Add classic camera controller
This commit is contained in:
@@ -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) {
|
||||||
|
if(maxSpeed < LOWER_SPEED || maxSpeed > UPPER_SPEED) return;
|
||||||
|
MAX_SPEED = maxSpeed;
|
||||||
|
THRESHOLD = MAX_SPEED / 20;
|
||||||
|
DECAY = 5;
|
||||||
|
}
|
||||||
|
|
||||||
// public static final double SPEED_CHANGE = 0.5;
|
private void forwardCameraMovement(boolean forward, boolean backward, boolean left, boolean right, boolean up, boolean down) {
|
||||||
// public static final double LOWER_SPEED = 2;
|
if(forward && !backward) {
|
||||||
// public static final double UPPER_SPEED = 20;
|
setMovement(MoveDirection.FORWARD);
|
||||||
//
|
} else if(backward && !forward) {
|
||||||
// private static double MAX_SPEED = 10;
|
setMovement(MoveDirection.BACKWARD);
|
||||||
// private static double THRESHOLD = MAX_SPEED / 20;
|
}
|
||||||
// private static double DECAY = MAX_SPEED/3;
|
|
||||||
//
|
if(left && !right) {
|
||||||
// public static void modifyCameraSpeed(boolean increase) {
|
setMovement(MoveDirection.LEFT);
|
||||||
// setCameraMaximumSpeed(getCameraMaximumSpeed() + (increase ? 1 : -1) * SPEED_CHANGE);
|
} else if(right && !left) {
|
||||||
// }
|
setMovement(MoveDirection.RIGHT);
|
||||||
//
|
}
|
||||||
// public static void setCameraMaximumSpeed(double maxSpeed) {
|
|
||||||
// if(maxSpeed < LOWER_SPEED || maxSpeed > UPPER_SPEED) return;
|
if(up && !down) {
|
||||||
// MAX_SPEED = maxSpeed;
|
setMovement(MoveDirection.UP);
|
||||||
// THRESHOLD = MAX_SPEED / 20;
|
} else if(down && !up) {
|
||||||
// DECAY = 5;
|
setMovement(MoveDirection.DOWN);
|
||||||
// }
|
}
|
||||||
//
|
}
|
||||||
// public static double getCameraMaximumSpeed() {
|
|
||||||
// return MAX_SPEED;
|
private void updateMovement() {
|
||||||
// }
|
long frac = Sys.getTime() - lastCall;
|
||||||
//
|
|
||||||
// private Vec3 direction;
|
if(frac == 0) return;
|
||||||
// private Vec3 dirBefore;
|
|
||||||
// private double motion;
|
double decFac = Math.max(0, 1 - (DECAY * (frac / 1000D)));
|
||||||
// private long lastCall = 0;
|
|
||||||
//
|
if(speedup) {
|
||||||
// private boolean speedup = false;
|
if(motion < THRESHOLD) motion = THRESHOLD;
|
||||||
//
|
motion /= decFac;
|
||||||
// //frac = time since last tick
|
} else {
|
||||||
// public void updateMovement() {
|
motion *= decFac;
|
||||||
//
|
}
|
||||||
// long frac = Sys.getTime() - lastCall;
|
|
||||||
//
|
motion = Math.min(motion, MAX_SPEED);
|
||||||
// if(frac == 0) return;
|
|
||||||
//
|
lastCall = Sys.getTime();
|
||||||
// double decFac = Math.max(0, 1 - (DECAY * (frac / 1000D)));
|
|
||||||
//
|
if(direction == null || motion < THRESHOLD) {
|
||||||
// if(speedup) {
|
return;
|
||||||
// if(motion < THRESHOLD) motion = THRESHOLD;
|
}
|
||||||
// motion /= decFac;
|
|
||||||
// } else {
|
Vec3 movement = direction.normalize();
|
||||||
// motion *= decFac;
|
double factor = motion * (frac / 1000D);
|
||||||
// }
|
|
||||||
//
|
camera.moveCamera(movement.xCoord * factor, movement.yCoord * factor, movement.zCoord * factor);
|
||||||
// motion = Math.min(motion, MAX_SPEED);
|
}
|
||||||
//
|
|
||||||
// lastCall = Sys.getTime();
|
private void setMovement(MoveDirection dir) {
|
||||||
//
|
float rotationPitch = camera.rotationPitch, rotationYaw = camera.rotationYaw;
|
||||||
// if(direction == null || motion < THRESHOLD) {
|
switch(dir) {
|
||||||
// return;
|
case BACKWARD:
|
||||||
// }
|
direction = this.getVectorForRotation(-rotationPitch, rotationYaw - 180);
|
||||||
//
|
break;
|
||||||
// Vec3 movement = direction.normalize();
|
case DOWN:
|
||||||
// double factor = motion * (frac / 1000D);
|
direction = this.getVectorForRotation(90, 0);
|
||||||
//
|
break;
|
||||||
// moveCamera(movement.xCoord * factor, movement.yCoord * factor, movement.zCoord * factor);
|
case FORWARD:
|
||||||
// }
|
direction = this.getVectorForRotation(rotationPitch, rotationYaw);
|
||||||
//
|
break;
|
||||||
// public void setMovement(MoveDirection dir) {
|
case LEFT:
|
||||||
// switch(dir) {
|
direction = this.getVectorForRotation(0, rotationYaw - 90);
|
||||||
// case BACKWARD:
|
break;
|
||||||
// direction = this.getVectorForRotation(-rotationPitch, rotationYaw - 180);
|
case RIGHT:
|
||||||
// break;
|
direction = this.getVectorForRotation(0, rotationYaw + 90);
|
||||||
// case DOWN:
|
break;
|
||||||
// direction = this.getVectorForRotation(90, 0);
|
case UP:
|
||||||
// break;
|
direction = this.getVectorForRotation(-90, 0);
|
||||||
// case FORWARD:
|
break;
|
||||||
// direction = this.getVectorForRotation(rotationPitch, rotationYaw);
|
}
|
||||||
// break;
|
|
||||||
// case LEFT:
|
Vec3 dbf = direction;
|
||||||
// direction = this.getVectorForRotation(0, rotationYaw - 90);
|
|
||||||
// break;
|
if(dirBefore != null) {
|
||||||
// case RIGHT:
|
direction = dirBefore.normalize().add(direction);
|
||||||
// direction = this.getVectorForRotation(0, rotationYaw + 90);
|
}
|
||||||
// break;
|
|
||||||
// case UP:
|
dirBefore = dbf;
|
||||||
// direction = this.getVectorForRotation(-90, 0);
|
|
||||||
// break;
|
updateMovement();
|
||||||
// }
|
}
|
||||||
//
|
|
||||||
// Vec3 dbf = direction;
|
private Vec3 getVectorForRotation(float pitch, float yaw) {
|
||||||
//
|
float f2 = MathHelper.cos(-yaw * 0.017453292F - (float) Math.PI);
|
||||||
// if(dirBefore != null) {
|
float f3 = MathHelper.sin(-yaw * 0.017453292F - (float)Math.PI);
|
||||||
// direction = dirBefore.normalize().add(direction);
|
float f4 = -MathHelper.cos(-pitch * 0.017453292F);
|
||||||
// }
|
float f5 = MathHelper.sin(-pitch * 0.017453292F);
|
||||||
//
|
return new Vec3((double)(f3 * f4), (double)f5, (double)(f2 * f4));
|
||||||
// dirBefore = dbf;
|
}
|
||||||
//
|
|
||||||
// updateMovement();
|
public enum MoveDirection {
|
||||||
// }
|
UP, DOWN, LEFT, RIGHT, FORWARD, BACKWARD
|
||||||
//
|
}
|
||||||
// public enum MoveDirection {
|
|
||||||
// UP, DOWN, LEFT, RIGHT, FORWARD, BACKWARD
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user