blob: aaeeea9aaf4a78ad13f2b58e7a32954da66ccd72 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
const Camera = {
x: 0,
y: 0,
centerOn(hex) {
const pixelCoord = HexGrid.toPixel(hex);
this.x = pixelCoord.x - state.canvas.width / 2;
this.y = pixelCoord.y - state.canvas.height / 2;
},
smoothFollow(target) {
const targetPixel = HexGrid.toPixel(target);
const screenX = targetPixel.x - this.x;
const screenY = targetPixel.y - this.y;
const centerX = state.canvas.width / 2;
const centerY = state.canvas.height / 2;
// Distance from center of the screen
const deltaX = screenX - centerX;
const deltaY = screenY - centerY;
// Only move the camera if the player is outside of the deadzone
if (Math.abs(deltaX) > Config.camera.DEADZONE_X) {
const adjustX = deltaX - (deltaX > 0 ? Config.camera.DEADZONE_X : -Config.camera.DEADZONE_X);
this.x += adjustX * Config.camera.FOLLOW_SPEED;
}
if (Math.abs(deltaY) > Config.camera.DEADZONE_Y) {
const adjustY = deltaY - (deltaY > 0 ? Config.camera.DEADZONE_Y : -Config.camera.DEADZONE_Y);
this.y += adjustY * Config.camera.FOLLOW_SPEED;
}
}
};
|