-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
160 lines (144 loc) · 4.15 KB
/
game.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
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const ctxblocks = document.getElementById('blocks').getContext('2d');
const colorPicker = document.querySelector('input');
const ball = document.querySelector('.fa-circle');
const newGameBtn = document.querySelectorAll('.newgame-btn');
const localStorageObject = JSON.parse(localStorage.getItem('gameStatus'));
colorPicker.value = localStorage.ballColor || colorPicker.value;
ball.style.color = colorPicker.value;
let balls;
let blocks;
let launched;
let numOfBalls;
let launchAngle;
let startTime;
let count;
let currentScore;
let gameOver;
let ballColor = colorPicker.value;
let highScore = localStorage.getItem('BlockBreakerHighScore') || 0;
const game = (() => {
const initGameValues = () => {
localStorage.removeItem("gameStatus");
balls = [];
blocks = [];
gameOver = false;
launched = false;
launchAngle = 0;
numOfBalls = 0;
startTime = 0;
currentScore = 0;
}
const loadGame = () => {
initGameValues();
Objects.loadBlocks(localStorageObject['blocks']);
numOfBalls = localStorageObject['numOfBalls'];
currentScore = localStorageObject['currentScore'];
View.updateInfo();
}
const startGame = () => {
initGameValues();
View.updateCurrentScore();
initTurn();
}
const launch = () => {
launched = true;
count = 0;
}
const rotate = (direction) => {
if (launched === false) {
if (direction === 'Left' && launchAngle > -85) {
launchAngle -= 5;
} else if (direction === 'Right' && launchAngle < 85) {
launchAngle += 5;
}
}
}
const initTurn = () => {
launched = false;
count = 0;
startTime = 0;
numOfBalls += 1;
View.moveBlocks();
Objects.createBlocks();
View.updateBallCount();
if (gameOver) {
View.gameOver();
};
}
const settings = () => {
document.querySelector('.settings').classList.toggle('display-modal');
}
const setupEventListeners = () => {
document.addEventListener("keydown", key => {
if (key.keyCode === 32 && !launched && !gameOver) {
launch();
}
if (key.keyCode === 37) {
rotate('Left');
}
if (key.keyCode === 39) {
rotate('Right');
}
});
document.querySelector('.settings-btn').addEventListener('click', settings);
document.getElementById('close-btn').addEventListener('click', settings);
document.getElementById('continue-btn').addEventListener('click', settings);
for (let i = 0; i < newGameBtn.length; i += 1) {
newGameBtn[i].addEventListener('click', () => {
startGame();
document.querySelector('.settings').classList.remove('display-modal');
document.querySelector('.game-over').classList.remove('display-modal');
document.getElementById('blocks').classList.remove('game-is-over');
});
}
colorPicker.addEventListener('change', () => {
ballColor = colorPicker.value;
ball.style.color = colorPicker.value;
localStorage.setItem("ballColor", ballColor);
});
window.addEventListener('unload', () => {
const gameObject = {
numOfBalls,
blocks,
currentScore,
};
if (!gameOver && currentScore > 0) {
localStorage.setItem("gameStatus", JSON.stringify(gameObject));
}
});
}
const updateCanvas = (timestamp) => {
requestAnimationFrame(updateCanvas);
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (launched) {
Objects.createBalls(timestamp);
balls.length > 0 ? (balls = View.moveBalls(balls)) : (initTurn());
} else if (!gameOver) {
ctx.save();
View.drawLauncher();
ctx.restore();
}
}
const updateBlocks = () => {
requestAnimationFrame(updateBlocks);
ctxblocks.clearRect(0, 0, canvas.width, canvas.height);
Collision.checkCollisions();
View.drawBlocks();
}
return {
init() {
setupEventListeners();
View.updateHighScore();
if (localStorage.hasOwnProperty('gameStatus')) {
loadGame();
} else {
startGame();
}
updateBlocks();
updateCanvas();
},
};
})();
game.init();