This repository has been archived by the owner on Jun 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexA.html
285 lines (272 loc) · 9.34 KB
/
indexA.html
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<!DOCTYPE html>
<!-- Свали последната версия от: https://github.com/StanislavNikolov/gamedev_pishtov -->
<!-- Download the latest verion from: https://github.com/StanislavNikolov/gamedev_pishtov -->
<html>
<head>
<style>
body, canvas {
margin: 0;
padding: 0;
overflow: hidden;
}
</style>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<meta charset="UTF-8"></meta> <!-- allows for cyrillic and other exotic text in console.logs -->
</head>
<body onload="init()">
<canvas id="canvas-id" width="800" height="600">
<script>
function areColliding(Ax, Ay, Awidth, Aheight, Bx, By, Bwidth, Bheight) {
if (Bx <= Ax + Awidth) {
if (Ax <= Bx + Bwidth) {
if (By <= Ay + Aheight) {
if (Ay <= By + Bheight) {
return 1;
}
}
}
}
return 0;
};
function randomInteger(upTo){
return Math.floor(Math.random()*upTo);
}
function drawLine(startX, startY, endX, endY) {
// For better performance bunch calls to lineTo without beginPath() and stroke() inbetween.
context.beginPath(); // resets the current path
context.moveTo(startX, startY);
context.lineTo(endX, endY);
context.stroke();
}
function tryToLoad(imageNameWithoutDotPng, backupColor) {
result={};
result.img=new Image();
result.img.src = "images/"+imageNameWithoutDotPng + ".png";
result.color=backupColor;
return result;
}
function tryToLoadWithFullPath(imageNameAndPath, backupColor) {
result={};
result.img=new Image();
result.img.src = imageNameAndPath;
result.color=backupColor;
return result;
}
function drawImage(imageWithBackupColorObject, x, y, xs, ys) {
try {
if (xs!==undefined){
context.drawImage(imageWithBackupColorObject.img, x, y, xs, ys);
} else {
context.drawImage(imageWithBackupColorObject.img, x, y);
}
} catch(e) {
//context.fillStyle = imageWithBackupColorObject.color;
if (xs == null) {
xs = 100;
ys = 100;
}
context.fillRect(x, y, xs, ys);
}
}
function setCanvasSize(width_, height_) {
canvas.width = width_;
canvas.height = height_;
}
function setFullscreen() {
setCanvasSize(window.innerWidth, window.innerHeight);
}
let updateTime = 10; // in ms
const canvas = document.getElementById("canvas-id");
canvas.width = 800;
canvas.height = 600;
let isUpdatePaused_ = false,
updateInterval = undefined;
function isUpdatePaused() {
return isUpdatePaused_;
}
function pauseUpdate() {
clearInterval(updateInterval);
isUpdatePaused_ = true;
}
function startUpdate() {
updateInterval = setInterval(update, updateTime);
isUpdatePaused_ = false;
redraw();
}
// POLYGON FUNCTIONS
// Distance between two points
function distance(x1, y1, x2, y2) {
return Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2));
}
function getTriangleArea(x1, y1, x2, y2, x3, y3) {
let a = distance(x1, y1, x2, y2),
b = distance(x2, y2, x3, y3),
c = distance(x1, y1, x3, y3),
p = (a+b+c)/2;
return Math.sqrt(p*(p-a)*(p-b)*(p-c));
}
function isInTriangle(pX, pY, x1, y1, x2, y2, x3, y3) {
let S1 = getTriangleArea(pX, pY, x1, y1, x2, y2),
S2 = getTriangleArea(pX, pY, x2, y2, x3, y3),
S3 = getTriangleArea(pX, pY, x3, y3, x1, y1),
S = getTriangleArea(x1,y1,x2,y2,x3,y3);
console.log(S1 + S2 + S3, S);
return Math.abs(S1 + S2 + S3 - S) < 5;
}
function isInHexagon(cX, cY, strana, pX, pY) {
let x = [],
y = [];
for(let i = 0; i < 6; i++) {
x[i] = Math.cos((i/3)*Math.PI)*strana + cX;
y[i] = Math.sin((i/3)*Math.PI)*strana + cY;
}
let vutreLiSum = false;
for(let i = 0; i < 5; i++) {
vutreLiSum = vutreLiSum || isInTriangle(pX, pY, cX, cY, x[i], y[i], x[i+1], y[i+1]);
}
vutreLiSum = vutreLiSum || isInTriangle(pX, pY, cX, cY, x[0], y[0], x[5], y[5]);
return vutreLiSum;
}
// Creates a path for a hexagon
// Use with context.fill() ot context.stroke()
function traceHexagonPath(cX, cY, strana) {
context.beginPath();
for(let i = 0; i < 6; i++) {
let ugul = (Math.PI/3)*i;
let deltaX = Math.cos(ugul)*strana;
let deltaY = Math.sin(ugul)*strana;
let tX = cX + deltaX, tY = cY + deltaY;
context.lineTo(tX, tY);
}
context.closePath();
}
</script>
<script src="levels.js"></script>
<!-- user's game file -->
<script src="gameA.js"></script>
<script>
const context = canvas.getContext("2d");
context.fillStyle = "#0000ff";
// global variables with mouse coordinates
let mouseX = 0;
let mouseY = 0;
// some keycodes
let key_w = 87;
let key_a = 65;
let key_s = 83;
let key_d = 68;
let key_left = 37;
let key_up = 38;
let key_right = 39;
let key_down = 40;
let key_z = 90;
let isKeyPressed = [];
for (i = 0; i < 256; ++i) {
isKeyPressed.push(0);
}
// gridSize = 50; // uncomment or add to game.js if you want a grid
const reqAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function (callback) {
setTimeout(callback, 1000 / 30);
};
function redraw() {
context.clearRect(0, 0, canvas.width, canvas.height);
draw(); // call progammer's draw() function
context.globalAlpha = 1;
// draw grid
//context.fillStyle = "#FF0000";
context.font = "10px Arial";
if (typeof gridSize != "undefined" && gridSize >= 25) {
context.fillText(0, 4, 10);
context.beginPath();
for(i = gridSize;i < canvas.width;i += gridSize) {
context.moveTo(i, 0);
context.lineTo(i, canvas.height);
context.fillText(i, i + 4, 10);
}
for(i = gridSize;i < canvas.height;i += gridSize) {
context.moveTo(0, i);
context.lineTo(canvas.width, i);
context.fillText(i, 4, i + 10);
}
context.stroke();
}
if(!isUpdatePaused_) {
reqAnimationFrame(redraw);
}
};
function init() {
if ('ontouchstart' in window || navigator.maxTouchPoints) {
isMobile = true;
window.addEventListener("touchstart", function (e) {
let touchobj = e.changedTouches[0];
mouseX = parseInt(touchobj.pageX - canvas.offsetLeft);
mouseY = parseInt(touchobj.pageY - canvas.offsetTop);
mousedown();
});
window.addEventListener("touchend", function (e) {
let touchobj = e.changedTouches[0];
mouseX = parseInt(touchobj.pageX - canvas.offsetLeft);
mouseY = parseInt(touchobj.pageY - canvas.offsetTop);
mouseup();
});
window.addEventListener("touchmove", function (e) {
let touchobj = e.changedTouches[0];
mouseX = parseInt(touchobj.pageX - canvas.offsetLeft);
mouseY = parseInt(touchobj.pageY - canvas.offsetTop);
});
window.addEventListener("onscroll", function (e) {
onscroll();
});
}
window.addEventListener("mousemove", function (e) {
mouseX = e.pageX - canvas.offsetLeft;
mouseY = e.pageY - canvas.offsetTop;
});
if(typeof mousemove != "undefined") {
window.addEventListener("mousemove", mousemove);
}
if(typeof mouseup != "undefined") {
window.addEventListener("mouseup", mouseup);
}
if(typeof mousedown != "undefined") {
window.addEventListener("mousedown", mousedown);
}
if(typeof keydown != "undefined") {
window.addEventListener("keydown", function (e) {
isKeyPressed[e.keyCode] = 1;
keydown(e.keyCode);
});
} else {
window.addEventListener("keydown", function (e) {
isKeyPressed[e.keyCode] = 1;
});
}
if(typeof keyup != "undefined") {
window.addEventListener("keyup", function (e) {
isKeyPressed[e.keyCode] = 0;
keyup(e.keyCode);
});
} else {
window.addEventListener("keyup", function (e) {
isKeyPressed[e.keyCode] = 0;
});
}
if(typeof onscroll != "undefined") {
window.addEventListener("onscroll", onscroll);
}
if(typeof draw == "undefined") {
redraw = function () {
context.clearRect(0, 0, canvas.width, canvas.height);
context.globalAlpha = 1;
context.fillStyle = "#FF0000";
context.font = "20px Arial";
context.fillText("Press <F12> for error info!", 40, 40);
};
}
redraw();
updateInterval = setInterval(update, updateTime);
}
</script>
</canvas>
</body>
</html>