-
Notifications
You must be signed in to change notification settings - Fork 0
/
camera.js
140 lines (120 loc) · 4.58 KB
/
camera.js
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
class Camera
{
constructor(){
this.position = new Vector2(128, 128);
this.size = 0.01;
this.backgroundColor = "#000000";
this.config = {
drawGrid: true,
drawQuadTree: false,
}
}
translate(offset){
this.position = this.position.add(offset);
}
applyZoom(percent){
this.size *= percent;
}
worldPosToScreenPoint(position){
let result = position.sub(this.position).scale(1 / this.size).add(Vector2.screenCenter());
return result;
}
screenPointToWorldPos(point){
let result = point.sub(Vector2.screenCenter()).scale(this.size).add(this.position);
return result;
}
worldLengthToScreenLength(length){
return length / this.size;
}
screenLengthToWorldLength(length){
return length * this.size;
}
drawBackground(){
ctx.beginPath();
ctx.fillStyle = this.backgroundColor;
ctx.fillRect(0, 0, canvas.width, canvas.height);
}
drawGrid(){
if (this.size > 0.2){
return;
}
let width = Math.ceil(this.screenLengthToWorldLength(canvas.width)) + 1;
let height = Math.ceil(this.screenLengthToWorldLength(canvas.height)) + 1;
let size = this.worldLengthToScreenLength(1);
let cornerSquare = this.worldPosToScreenPoint(this.screenPointToWorldPos(new Vector2()).floor());
ctx.beginPath();
let grayscale = lerp(0, 0.1, 1 - (clamp(this.size * 100, 10, 20) - 10) / 10) * 255;
ctx.strokeStyle = `rgb(${grayscale}, ${grayscale}, ${grayscale})`;
for (let x = 0; x < width; x++){
ctx.moveTo(cornerSquare.x + size * x, cornerSquare.y);
ctx.lineTo(cornerSquare.x + size * x, cornerSquare.y + size * height);
}
for (let y = 0; y < height; y++){
ctx.moveTo(cornerSquare.x, cornerSquare.y + size * y);
ctx.lineTo(cornerSquare.x + size * width, cornerSquare.y + size * y);
}
ctx.stroke();
}
drawQuadTreeLevel(quad){
ctx.strokeStyle = "#444444";
let verts = [
this.worldPosToScreenPoint(quad.position.add(new Vector2(-quad.size / 2, quad.size / 2))),
this.worldPosToScreenPoint(quad.position.add(new Vector2(quad.size / 2, quad.size / 2))),
this.worldPosToScreenPoint(quad.position.add(new Vector2(quad.size / 2, -quad.size / 2))),
this.worldPosToScreenPoint(quad.position.add(new Vector2(-quad.size / 2, -quad.size / 2))),
];
for (let i = 0; i < 4; i++){
ctx.beginPath();
ctx.moveTo(verts[i].x, verts[i].y);
ctx.lineTo(verts[(i + 1) % 4].x, verts[(i + 1) % 4].y)
ctx.stroke();
}
}
drawQuad(quad){
let position = this.worldPosToScreenPoint(quad.position.sub(new Vector2(quad.size / 2, quad.size / 2))).sub(new Vector2(0.25, 0.25));
let length = this.worldLengthToScreenLength(quad.size) + 0.5;
if (length / quad.size > 1){
ctx.imageSmoothingEnabled = false;
} else {
ctx.imageSmoothingEnabled = true;
}
ctx.drawImage(quad.canvas, position.x, position.y, length, length);
}
drawCellPreview(cell){
let position = this.worldPosToScreenPoint(cell).sub(new Vector2(0.25, 0.25));
let length = this.worldLengthToScreenLength(1) + 0.5;
ctx.beginPath();
ctx.fillStyle = "#444444";
ctx.fillRect(position.x, position.y, length, length);
}
drawSelectedStructure(){
if (!Controller.selectedStructure){
return;
}
let positions = Structures.getCellPositions(
Controller.selectedStructure,
Controller.lastMousePos,
Controller.structureOrientation
);
for (let cell of positions){
this.drawCellPreview(cell);
}
}
saveScreenshot(){
const link = document.createElement('a');
link.download = 'screenshot.png';
canvas.width = 1920; canvas.height = 1080; camera.draw();
link.href = canvas.toDataURL();
canvasFitScreen();
link.click();
link.delete;
}
draw(){
ctx.clearRect(0, 0, canvas.width, canvas.height);
this.drawBackground();
if (this.config.drawGrid) this.drawGrid();
if (this.config.drawQuadTree) world.data.drawQuadTree();
this.drawSelectedStructure();
world.data.drawQuads();
}
}