1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2025-06-13 13:04:03 +00:00

Add axis control for camera move

This is simple linear control.

It needs some more tuning of the sensitivity, but works ok.
This commit is contained in:
Lukas Dolezal 2020-12-27 21:43:59 +01:00
parent e7981d485e
commit 301a841955
2 changed files with 39 additions and 0 deletions

View File

@ -220,6 +220,24 @@ export class InputDistributor {
}
}
getGamepadAxes() {
if (this.connectedGamepadIndex === null) {
return {
x: 0,
y: 0,
};
}
const gamepad = navigator.getGamepads()[this.connectedGamepadIndex];
// Threshold 0.25 as the joysticks never return to exact 0
// power to 9 function to decrease the sensitivity, to be ~0.25 at 85%
return {
x: Math.abs(gamepad.axes[0]) < 0.25 ? 0 : gamepad.axes[0],
y: Math.abs(gamepad.axes[1]) < 0.25 ? 0 : gamepad.axes[1],
};
}
/**
* @param {Event} event
*/

View File

@ -778,6 +778,7 @@ export class Camera extends BasicSerializableObject {
this.internalUpdateCentering(now, physicsStepSizeMs);
this.internalUpdateShake(now, physicsStepSizeMs);
this.internalUpdateKeyboardForce(now, physicsStepSizeMs);
this.internalUpdateGamepadForce(now, physicsStepSizeMs);
}
this.clampZoomLevel();
}
@ -1008,4 +1009,24 @@ export class Camera extends BasicSerializableObject {
this.center.y += moveAmount * forceY * movementSpeed;
}
}
/**
* Updates the gamepad axis forces
* @param {number} now
* @param {number} dt Delta time
*/
internalUpdateGamepadForce(now, dt) {
const inputMgr = this.root.app.inputMgr;
if (inputMgr.connectedGamepadIndex !== null && !this.currentlyMoving && this.desiredCenter == null) {
const limitingDimension = Math.min(this.root.gameWidth, this.root.gameHeight);
const moveAmount = ((limitingDimension / 2048) * dt) / this.zoomLevel;
let { x, y } = inputMgr.getGamepadAxes();
this.center.x += moveAmount * x * 2;
this.center.y += moveAmount * y * 2;
}
}
}