-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.js
More file actions
71 lines (56 loc) · 1.89 KB
/
camera.js
File metadata and controls
71 lines (56 loc) · 1.89 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Camera {
w = screenWidth;
h = screenHeight;
widthOffset = 100;
heightOffset = 60;
// to use only int number to position
xRemainder = 0;
yRemainder = 0;
constructor(x, y) {
this.x = x;
this.y = y;
}
debugRender(ctx) {
ctx.save();
ctx.strokeStyle = "#e3d";
// offset
ctx.strokeRect(
this.x + (this.w - this.widthOffset)/2,
this.y + (this.h - this.heightOffset)/2,
this.widthOffset, this.heightOffset
);
ctx.restore();
}
getCenter() {
return {x: this.x + this.w/2, y: this.y + this.h/2};
}
setCenter(x, y) {
this.x = Math.floor(x) - this.w/2;
this.y = Math.floor(y) - this.h/2;
this.collideOnBorders();
}
moveCenterTo(destX, destY, speed) {
const camCenter = this.getCenter();
let moveHorz = destX < camCenter.x - this.widthOffset/2 ||
destX > camCenter.x + this.widthOffset/2;
let moveVert = destY < camCenter.y - this.heightOffset/2 ||
destY > camCenter.y + this.heightOffset/2;
if (!moveHorz && !moveVert) return;
let dirX = destX - this.w/2 - this.x;
let dirY = destY - this.h/2 - this.y;
const dist = Math.sqrt(dirX**2 + dirY**2);
const newX = this.x + this.xRemainder + dirX/dist * speed;
const newY = this.y + this.yRemainder + dirY/dist * speed;
this.x = Math.floor(newX);
this.y = Math.floor(newY);
this.xRemainder = newX - this.x;
this.yRemainder = newY - this.y;
this.collideOnBorders();
}
collideOnBorders() {
if (this.x < 0) this.x = 0;
if (this.x + this.w > worldWidth) this.x = worldWidth - this.w;
if (this.y < 0) this.y = 0;
if (this.y + this.h > worldHeight) this.y = worldHeight - this.h;
}
}