Archive incari-client (client, 26.3, clean)
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
# freelook: Freelook
|
||||
|
||||
Hold (or toggle) the key to look around without turning your player; letting go puts the view back. ([ADR 0053](../../docs/adr/0053-visual-and-combat-modules.md))
|
||||
|
||||
| Setting | Kind | Default |
|
||||
| --- | --- | --- |
|
||||
| Freelook key | keybind | Left Alt |
|
||||
| Key | Hold, Toggle | Hold |
|
||||
| Camera | Behind, In front, First person | Behind |
|
||||
| Invert up and down | on/off | off |
|
||||
|
||||
```sh
|
||||
./gradlew :features:freelook:check
|
||||
```
|
||||
@@ -0,0 +1,16 @@
|
||||
plugins {
|
||||
`java-library`
|
||||
}
|
||||
|
||||
// Freelook (ADR 0053): hold a key to turn the camera round the player without turning them.
|
||||
// A feature module: it sees the module framework and nothing else.
|
||||
dependencies {
|
||||
api(project(":framework"))
|
||||
|
||||
testImplementation(testFixtures(project(":framework")))
|
||||
testImplementation(platform(libs.junit.bom))
|
||||
testImplementation(libs.junit.jupiter)
|
||||
testImplementation(libs.assertj)
|
||||
testImplementation(libs.archunit)
|
||||
testRuntimeOnly(libs.junit.launcher)
|
||||
}
|
||||
+161
@@ -0,0 +1,161 @@
|
||||
package incari.features.freelook;
|
||||
|
||||
import incari.framework.Category;
|
||||
import incari.framework.Module;
|
||||
import incari.framework.event.CameraAngles;
|
||||
import incari.framework.event.Events;
|
||||
import incari.framework.event.Turn;
|
||||
import incari.framework.game.Game;
|
||||
import incari.framework.game.Perspective;
|
||||
import incari.framework.setting.BooleanSetting;
|
||||
import incari.framework.setting.ChoiceSetting;
|
||||
import incari.framework.setting.Key;
|
||||
import incari.framework.setting.KeySetting;
|
||||
|
||||
/**
|
||||
* Freelook: while its key is held (or after a press, as a toggle), the mouse turns the camera round
|
||||
* the player instead of the player, so they can look behind them while running on. Letting go
|
||||
* snaps the camera back to where the player faces.
|
||||
*/
|
||||
public final class Freelook extends Module {
|
||||
|
||||
/** Left Alt's scancode. */
|
||||
static final int LEFT_ALT = 226;
|
||||
|
||||
/** How the key works. */
|
||||
public enum Mode {
|
||||
HOLD("Hold"),
|
||||
TOGGLE("Toggle");
|
||||
|
||||
private final String label;
|
||||
|
||||
Mode(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
/** What the settings screen calls it. */
|
||||
public String label() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
|
||||
/** Where the camera goes while looking. */
|
||||
public enum View {
|
||||
BACK("Behind"),
|
||||
FRONT("In front"),
|
||||
FIRST("First person");
|
||||
|
||||
private final String label;
|
||||
|
||||
View(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
/** What the settings screen calls it. */
|
||||
public String label() {
|
||||
return label;
|
||||
}
|
||||
}
|
||||
|
||||
final KeySetting key = settings()
|
||||
.key("look", "Freelook key", Key.keyboard(LEFT_ALT))
|
||||
.describedAs("Hold (or press) to look around without turning.");
|
||||
final ChoiceSetting<Mode> mode = settings()
|
||||
.choice("mode", "Key", Mode.values(), Mode.HOLD, Mode::label)
|
||||
.describedAs("Hold the key to look around, or press it once to start and again to stop.");
|
||||
final ChoiceSetting<View> view = settings()
|
||||
.choice("view", "Camera", View.values(), View.BACK, View::label)
|
||||
.describedAs("Where the camera goes while you look around.");
|
||||
final BooleanSetting invert = settings()
|
||||
.bool("invert", "Invert up and down", false)
|
||||
.describedAs("Moving the mouse up looks down while looking around.");
|
||||
|
||||
private boolean toggled;
|
||||
private boolean active;
|
||||
/** Whether the camera's angles are still to be taken from the player's on the next frame. */
|
||||
private boolean fresh;
|
||||
|
||||
private float yaw;
|
||||
private float pitch;
|
||||
private Perspective before = Perspective.FIRST_PERSON;
|
||||
|
||||
/** Freelook, off until the player turns it on. */
|
||||
public Freelook() {
|
||||
super("freelook", "Freelook", "Hold a key to look around without turning.", Category.VISUAL);
|
||||
key.onPress(() -> toggled = !toggled);
|
||||
on(Events.TICK, this::tick);
|
||||
on(Events.TURN, this::onTurn);
|
||||
on(Events.CAMERA, this::onCamera);
|
||||
}
|
||||
|
||||
/** Whether the camera's turning on its own now. */
|
||||
boolean looking() {
|
||||
return active;
|
||||
}
|
||||
|
||||
private void tick() {
|
||||
boolean want = system().game().player() != null && (mode.get() == Mode.HOLD ? key.isDown() : toggled);
|
||||
if (want != active) {
|
||||
if (want) {
|
||||
start();
|
||||
} else {
|
||||
stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void start() {
|
||||
Game game = system().game();
|
||||
active = true;
|
||||
fresh = true;
|
||||
before = game.perspective();
|
||||
Perspective wanted =
|
||||
switch (view.get()) {
|
||||
case BACK -> Perspective.THIRD_PERSON_BACK;
|
||||
case FRONT -> Perspective.THIRD_PERSON_FRONT;
|
||||
case FIRST -> Perspective.FIRST_PERSON;
|
||||
};
|
||||
if (wanted != before) {
|
||||
game.setPerspective(wanted);
|
||||
}
|
||||
}
|
||||
|
||||
private void stop() {
|
||||
active = false;
|
||||
toggled = false;
|
||||
Game game = system().game();
|
||||
if (game.perspective() != before) {
|
||||
game.setPerspective(before);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDisable() {
|
||||
if (active) {
|
||||
stop();
|
||||
}
|
||||
toggled = false;
|
||||
}
|
||||
|
||||
private void onTurn(Turn turn) {
|
||||
if (!active) {
|
||||
return;
|
||||
}
|
||||
yaw += turn.yaw();
|
||||
float down = invert.get() ? -turn.pitch() : turn.pitch();
|
||||
pitch = Math.max(-90, Math.min(90, pitch + down));
|
||||
turn.consume();
|
||||
}
|
||||
|
||||
private void onCamera(CameraAngles camera) {
|
||||
if (!active) {
|
||||
return;
|
||||
}
|
||||
if (fresh) {
|
||||
yaw = camera.yaw();
|
||||
pitch = camera.pitch();
|
||||
fresh = false;
|
||||
}
|
||||
camera.set(yaw, pitch);
|
||||
}
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
/** Freelook (ADR 0053): hold a key to look around without turning. */
|
||||
@NullMarked
|
||||
package incari.features.freelook;
|
||||
|
||||
import org.jspecify.annotations.NullMarked;
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
package incari.features.freelook;
|
||||
|
||||
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
|
||||
|
||||
import com.tngtech.archunit.core.importer.ImportOption;
|
||||
import com.tngtech.archunit.junit.AnalyzeClasses;
|
||||
import com.tngtech.archunit.junit.ArchTest;
|
||||
import com.tngtech.archunit.lang.ArchRule;
|
||||
|
||||
/** A feature sees the module framework and the JDK's basics: no game, platform or other feature. */
|
||||
@AnalyzeClasses(packages = "incari.features.freelook", importOptions = ImportOption.DoNotIncludeTests.class)
|
||||
class BoundaryTest {
|
||||
|
||||
@ArchTest
|
||||
static final ArchRule onlyTheFramework = classes()
|
||||
.should()
|
||||
.onlyDependOnClassesThat()
|
||||
.resideInAnyPackage(
|
||||
"java.lang..", "java.util..", "org.jspecify..", "incari.framework..", "incari.features.freelook..");
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package incari.features.freelook;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import incari.framework.event.CameraAngles;
|
||||
import incari.framework.event.Turn;
|
||||
import incari.framework.game.Perspective;
|
||||
import incari.framework.setting.ChoiceSetting;
|
||||
import incari.framework.setting.Key;
|
||||
import incari.framework.testing.TestGame;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class FreelookTest {
|
||||
|
||||
private static final Key ALT = Key.keyboard(Freelook.LEFT_ALT);
|
||||
|
||||
private final Freelook module = new Freelook();
|
||||
private final TestGame game = new TestGame(module);
|
||||
|
||||
@Test
|
||||
void holdingTheKeyTurnsTheCameraNotThePlayer() {
|
||||
module.setEnabled(true);
|
||||
game.join();
|
||||
game.hold(ALT, true);
|
||||
game.tick();
|
||||
assertThat(game.perspective).isEqualTo(Perspective.THIRD_PERSON_BACK);
|
||||
|
||||
CameraAngles start = game.camera(90, 10);
|
||||
assertThat(start.changed()).isTrue();
|
||||
Turn turn = game.turn(30, -5);
|
||||
assertThat(turn.consumed()).isTrue();
|
||||
CameraAngles after = game.camera(90, 10);
|
||||
assertThat(after.yaw()).isEqualTo(120);
|
||||
assertThat(after.pitch()).isEqualTo(5);
|
||||
|
||||
game.hold(ALT, false);
|
||||
game.tick();
|
||||
assertThat(game.perspective).isEqualTo(Perspective.FIRST_PERSON);
|
||||
assertThat(game.turn(30, 0).consumed()).isFalse();
|
||||
assertThat(game.camera(90, 10).changed()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void theToggleStaysOnUntilPressedAgain() {
|
||||
module.setEnabled(true);
|
||||
@SuppressWarnings("unchecked")
|
||||
ChoiceSetting<Freelook.Mode> mode =
|
||||
(ChoiceSetting<Freelook.Mode>) module.settings().get("mode");
|
||||
mode.set(Freelook.Mode.TOGGLE);
|
||||
game.join();
|
||||
game.press(ALT);
|
||||
game.tick();
|
||||
game.tick();
|
||||
assertThat(module.looking()).isTrue();
|
||||
game.press(ALT);
|
||||
game.tick();
|
||||
assertThat(module.looking()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void lookingStraightUpIsAsFarAsItGoes() {
|
||||
module.setEnabled(true);
|
||||
game.join();
|
||||
game.hold(ALT, true);
|
||||
game.tick();
|
||||
game.camera(0, 0);
|
||||
game.turn(0, -500);
|
||||
assertThat(game.camera(0, 0).pitch()).isEqualTo(-90);
|
||||
}
|
||||
|
||||
@Test
|
||||
void turningOffPutsTheViewBack() {
|
||||
module.setEnabled(true);
|
||||
game.join();
|
||||
game.hold(ALT, true);
|
||||
game.tick();
|
||||
module.setEnabled(false);
|
||||
assertThat(game.perspective).isEqualTo(Perspective.FIRST_PERSON);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user