-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
86 lines (64 loc) · 1.57 KB
/
index.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
import './style.css';
import { gameKeeper, reset as resetState, bricks, score, life } from './state';
import { ballStartPoint, paddleStartPoint, generateBricks } from './helpers';
import { moveBall, movePaddle } from './movment';
import * as painter from './painter';
import * as events from './events';
/**
* The game single frame animation
*/
const drawGameFrame = () => {
// Wipe board before next drawing frame
painter.wipe();
moveBall();
movePaddle();
painter.bricks(bricks);
painter.score(score);
painter.life(life);
gameKeeper.playing && requestAnimationFrame(drawGameFrame);
};
const stop = () => {
events.detach();
clearTimeout(gameKeeper.timer);
gameKeeper.playing = false;
};
const restart = () => {
if (gameKeeper.playing) {
stop();
}
resetState();
play();
};
const play = () => {
const startPoint = ballStartPoint();
const paddlePoint = paddleStartPoint();
painter.ball(startPoint);
painter.paddle(paddlePoint);
const bricks = generateBricks();
painter.bricks(bricks);
painter.score(score);
painter.life(life);
gameKeeper.playing = true;
drawGameFrame();
events.attach();
gameKeeper.timer = setTimeout(stop, 50000);
};
const win = () => {
stop();
setTimeout(() => {
painter.wipe();
painter.animateText('You Win :)', '#1f2f90');
});
};
const gameOver = () => {
stop();
setTimeout(() => {
painter.wipe();
painter.animateText('Game Over');
});
};
play();
events.bus.on('stop', stop);
events.bus.on('game-over', gameOver);
events.bus.on('win', win);
events.bus.on('restart', restart);