This repository has been archived by the owner on Oct 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
137 lines (125 loc) · 4.08 KB
/
main.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
// define global variables and run setup
let scale = 1;
let map; // testing
let mapInfo = {
x: 0,
y: 0,
};
let assets = [];
let keys = [];
// Inputs
let mouseX = 0;
let mouseY = 0;
setup();
async function setup() {
setInterval(() => {
checkCombinations();
}, 30);
window.addEventListener('keydown', e => {
keys[e.keyCode] = true;
}, false);
window.addEventListener('keyup', e => {
keys[e.keyCode] = false;
}, false);
window.addEventListener('mousemove', e => {
mouseX = e.clientX;
mouseY = e.clientY;
});
c.addEventListener('mousedown', (ev) => {
let t = translate((ev.clientX - (c.width / 2)) * scale, (ev.clientY - (c.height / 2)) * scale, -mapInfo.x, -mapInfo.y + (osC.height / 2)); // Inverts translation
let matrixI = [
[0.5237827897071838, 1.0878565311431885, -3998.033935546875],
[-0.5237827897071838, 1.0878565311431885, 3998.033935546875],
[0, 0, 1]
];
let point = [0, 0, 1];
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
point[i] += matrixI[i][j] * t[j];
}
if (i == 0) {
point[i] += 4000;
} else if (i == 1) {
point[i] += -4000;
}
}
let square = map.whichSquare(point[0], point[1], true);
map.highlight(square[0], square[1]);
});
// load img assets into array in background
window.addEventListener('load', () => {
document.querySelector('#loading').style.opacity = 1;
document.querySelector('#loading').style.transition = "opacity 1s ease-in-out";
document.querySelector('#loading').style.opacity = 0;
setTimeout(()=>{document.querySelector('#loading').style.display = "none";},6000);
setupAssets();
map = new Mapper(256, 256, [1.5, 1.3, 1.7, 0.5, 1]); // [grassland, forest, ocean, desert, mountains]
drawMap();
draw.translate(c.width / 2, c.height / 2);
requestAnimationFrame(drawLoop);
});
window.addEventListener('resize', (ev) => {
c.width = document.body.clientWidth;
c.height = document.body.clientHeight;
mapInfo.x = 0;
mapInfo.y = 0;
scale = 1;
draw.restore();
draw.translate(c.width / 2, c.height / 2);
});
drawLoop();
}
function setupAssets() {
let imgdiv = document.getElementById('loaded-images');
for (let i = 0; i < imgdiv.childElementCount; i++) {
assets.push({
name: imgdiv.children[i].name,
img: imgdiv.children[i]
});
}
}
function drawMap() {
map.draw(osDraw2);
redraw();
}
function redraw() {
mixed2dDraw.drawImage(osC2, 0, 0);
map.draw3d();
// osDraw.clearRect(0,0,osC.width,osC.height); // this makes things HECKING SLOW by like 150ms on my comp
osDraw.drawImage(mixed2dC, 0, 0);
osDraw.drawImage(mixed3dC, 0, 0);
}
function drawLoop() {
draw.clearRect(-osC.width, -osC.height, osC.width * 10, osC.height * 10);
draw.drawImage(osC, (-osC.width / 2) + mapInfo.x, (-osC.height / 2) + mapInfo.y);
// draw.fillStyle = "rgb(0, 0, 0)";
// draw.beginPath();
// draw.arc(0, 0, 100, 0, Math.PI * 2);
// draw.fill()
(drawLoopy == true) ? requestAnimationFrame(drawLoop): null;
}
function noLoop() {
drawLoopy = false;
}
function checkCombinations() {
// osDraw.clearRect(-Infinity,-Infinity,Infinity,Infinity);
(keys[87] && mapInfo.y < 5000) ? mapInfo.y += 10 * scale: null;
(keys[65] && mapInfo.x < 9000) ? mapInfo.x += 10 * scale: null;
(keys[83] && mapInfo.y < 5000) ? mapInfo.y += -10 * scale: null;
(keys[68] && mapInfo.x < 9000) ? mapInfo.x += -10 * scale: null;
if (keys[61] && scale > 0.5) {
draw.scale(1.1, 1.1);
scale /= 1.1;
}
if (keys[173] && scale < 13) {
draw.scale(1 / 1.1, 1 / 1.1);
scale /= 1 / 1.1;
}
}
function translate(x1, y1, x2, y2) {
return [x1 + x2, y1 + y2, 1];
}
async function updateOsDraw() {
osDraw.drawImage(mixed2dC,0,0);
osDraw.drawImage(mixed3dC,0,0);
}