forked from madhavkGitHub/Quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.js
More file actions
235 lines (220 loc) · 8.55 KB
/
draw.js
File metadata and controls
235 lines (220 loc) · 8.55 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
var playerSize = 20;
var blockSize = 50;
let canvas = document.getElementById("playerLayer");
let bcanv = document.getElementById("bgLayer");
let lvlCanvas = document.getElementById("levelLayer");
const play_norm = new Image()
play_norm.src = "static/mc_norm.png"
function onLoadPlay_norm(context,x,y,size){
context.drawImage(play_norm, 0, 0, 64, 64,x, y, size, size )
}
const play_jump = new Image()
play_jump.src = "static/mc_jump.png"
function onLoadPlay_jump(context,x,y,size){
context.drawImage(play_jump, 0, 0, 64, 64,x, y, size, size )
}
function drawPlayer() {
let pL = canvas.getContext("2d");
canvas.width = levels[player.currentLevel].length * blockSize; //calling player class
canvas.height = levels[player.currentLevel][0].length * blockSize;
pL.clearRect(0, 0, canvas.width, canvas.height); //Resets it?
let ratio = player.currentJumps / player.maxJumps;
if (player.maxJumps === Infinity) ratio = 1;
if (player.maxJumps === 0) ratio = 0;
if (ratio === 1 ){
// pL.fillStyle = `#324376`;
onLoadPlay_norm(pL,player.x, player.y, playerSize)
} // Changing the Color
else{
onLoadPlay_jump(pL,player.x, player.y, playerSize)
// pL.fillStyle = `#FCFCFC`; // Changing the Color
}
if (player.isDead) pL.fillStyle += "88";
// pL.fillRect(
// Math.floor(player.x),
// Math.floor(player.y),
// playerSize,
// playerSize
// );
adjustScreen();
}
function drawLevel() {
let layerContext = lvlCanvas.getContext("2d");
let backContext = bcanv.getContext("2d");
lvlCanvas.width = levels[player.currentLevel].length * blockSize;
lvlCanvas.height = levels[player.currentLevel][0].length * blockSize;
bcanv.width = levels[player.currentLevel].length * blockSize;
bcanv.height = levels[player.currentLevel][0].length * blockSize;
layerContext.clearRect(0, 0, lvlCanvas.width, lvlCanvas.height);
backContext.clearRect(0, 0, lvlCanvas.width, lvlCanvas.height);
backContext.fillStyle = "rgba(255, 255, 255, .3)";
for (let x in levels[player.currentLevel]){
for (let y in levels[player.currentLevel][x]){
layerContext.lineWidth = (blockSize * 3)/25;
let xb = x*blockSize;
let yb = y*blockSize;
let type = getBlockType(x, y);
let props = type;
if (typeof type === "object") type = type[0];
switch(type){
case X:
const X_img = new Image();
X_img.src = "static/gates/x.png";
X_img.onload = () => {
backContext.drawImage( X_img, 0, 0, 64, 64,xb, yb, blockSize, blockSize )
};
break;
case -5000:
const T_img = new Image();
if(player.solid == true) T_img.src = "static/gates/transparent.png";
else T_img.src = "static/gates/solid.png";
T_img.onload = () => {
layerContext.drawImage( T_img, 0, 0, 64, 64,xb, yb, blockSize, blockSize )
};
break;
case 5000:
const S_img = new Image();
if(player.solid == true) S_img.src = "static/gates/solid.png";
else S_img.src = "static/gates/transparent.png";
S_img.onload = () => {
layerContext.drawImage( S_img, 0, 0, 64, 64,xb, yb, blockSize, blockSize )
};
break;
case Y:
const Y_img = new Image();
Y_img.src = "static/gates/y.png";
Y_img.onload = () => {
backContext.drawImage( Y_img, 0, 0, 64, 64,xb, yb, blockSize, blockSize )
};
break;
case Z:
const Z_img = new Image();
Z_img.src = "static/gates/z.png";
Z_img.onload = () => {
backContext.drawImage( Z_img, 0, 0, 64, 64,xb, yb, blockSize, blockSize )
};
break;
case 1:
const img = new Image()
img.src = "static/blocks2.png"
img.onload = () => {
layerContext.drawImage(img, 0, 0, 64, 64,xb, yb, blockSize, blockSize )
}
break;
case 2:
const img3 = new Image()
img3.src = "static/death.png"
img3.onload = () => {
layerContext.drawImage(img3, 0, 0, 64, 64,xb, yb, blockSize, blockSize )
}
break;
case 3:
if (isSpawn(x, y)) {
layerContext.fillStyle = "#00FFFF88";
} else layerContext.fillStyle = "#00888888";
const life = new Image();
life.src = "static/life.png";
life.onload = () => {
layerContext.drawImage(life, 0, 0, 64, 64,xb, yb, blockSize, blockSize )
};
break;
case -9:
layerContext.fillStyle = 'rgba(255,255,255,0)'
backContext.fillStyle = "rgba(255, 255, 255, 0)";
break;
default:
backContext.fillStyle = "rgba(255, 255, 255, .3)";
layerContext.fillStyle = 'rgba(255,255,255,0)';
break;
}
layerContext.fillRect(xb, yb, blockSize, blockSize);
backContext.fillRect(xb, yb, blockSize, blockSize);
switch(type){
case 2:
layerContext.strokeStyle = "#880000";
layerContext.beginPath();
layerContext.moveTo(xb + (blockSize / 25) * 3, yb + (blockSize / 25) * 3);
layerContext.lineTo(
xb + blockSize - (blockSize / 25) * 3,
yb + blockSize - (blockSize / 25) * 3
);
layerContext.stroke();
layerContext.beginPath();
layerContext.moveTo(
xb + (blockSize / 25) * 3,
yb + blockSize - (blockSize / 25) * 3
);
layerContext.lineTo(
xb + blockSize - (blockSize / 25) * 3,
yb + (blockSize / 25) * 3
);
layerContext.stroke();
break;
// case 3:
// if (isSpawn(x, y)) {
// layerContext.strokeStyle = "#00888888";
// } else layerContext.strokeStyle = "#00444488";
// layerContext.beginPath();
// layerContext.moveTo(xb + (blockSize / 25) * 3, yb + blockSize / 2);
// layerContext.lineTo(xb + blockSize / 2, yb + blockSize - (blockSize / 25) * 3);
// layerContext.lineTo(
// xb + blockSize - (blockSize / 25) * 3,
// yb + (blockSize / 25) * 3
// );
// layerContext.stroke();
// break;
default:
break;
}
}
}
adjustScreen();
}
var lvlx = 0;
var lvly = 0;
var camx = 0;
var camy = 0;
var camDelay = 15;
function adjustScreen(instant = false) { //Camera mechanics
lvlx = Math.floor(
(window.innerWidth - levels[player.currentLevel].length * blockSize) / 2
);
if (lvlx < 0) {
lvlx =
Math.floor(window.innerWidth / 2) - Math.floor(player.x + playerSize / 2);
if (lvlx > 0) lvlx = 0;
if (
lvlx <
window.innerWidth - levels[player.currentLevel].length * blockSize
)
lvlx = Math.floor(
window.innerWidth - levels[player.currentLevel].length * blockSize
);
}
lvly = Math.floor(
(window.innerHeight - levels[player.currentLevel][0].length * blockSize) / 2
);
if (lvly < 0) {
lvly =
Math.floor(window.innerHeight / 2) -
Math.floor(player.y + playerSize / 2);
if (lvly > 0) lvly = 0;
if (
lvly <
window.innerHeight - levels[player.currentLevel][0].length * blockSize
)
lvly = Math.floor(
window.innerHeight - levels[player.currentLevel][0].length * blockSize
);
}
camx = (camx * (camDelay - 1) + lvlx) / camDelay;
camy = (camy * (camDelay - 1) + lvly) / camDelay;
if (Math.abs(camx - lvlx) < 1 || instant) camx = lvlx;
if (Math.abs(camy - lvly) < 1 || instant) camy = lvly;
bcanv.style.left = camx + "px";
bcanv.style.top = camy + "px";
canvas.style.left = camx + "px";
lvlCanvas.style.left = camx + "px";
canvas.style.top = camy + "px";
lvlCanvas.style.top = camy + "px";
}