-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcanvas.js
171 lines (122 loc) · 3.83 KB
/
canvas.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
let canvas = document.getElementById('canvas');
let context = document.getElementById('canvas').getContext('2d');
let monkeyArr = [] // array of all obstacles
let frameCounter = 0
let gameRunning = true
// A cross-browser requestAnimationFrame
// See https://hacks.mozilla.org/2011/08/animating-with-javascript-from-setinterval-to-requestanimationframe/
var requestAnimFrame = (function(){
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback){
window.setTimeout(callback, 1000 / 60);
};
})();
class GameCanvas {
constructor(width, height) {
this.canvas = document.getElementById('canvas')
this.context = document.getElementById('canvas').getContext('2d');
this.canvas.width = width;
this.canvas.height = height;
this.width = this.canvas.width;
this.height = this.canvas.height;
}
clearBoard() {
this.context.clearRect(0, 0, this.width, this.height)
}
createBoard() {
this.clearBoard()
this.draw()
}
// When player has won:
nirvana() {
playAudio(soundsLikeTeenSpirit)
$restartBtn.style.display = 'inherit';
$restartBtn.innerText = "REINCARNATE ANYWAYS"
$messageOverlay.style.display = 'inherit';
$messageOverlay.innerText = "CONGRATS! YOU HAVE REACHED NIRVANA."
}
// When collision between player + obstacle:
gameOver() {
backgroundImage.speed = 0
playAudio(soundGameOver)
playAudio(soundPain)
pauseAudio(soundMantra)
$restartBtn.style.display = 'inherit';
$restartBtn.innerText = "REINCARNATE"
$messageOverlay.style.display = 'inherit';
$messageOverlay.innerText = "MAY YOU BE HAPPY."
}
// Resets game to original state
reset() {
gameRunning = true;
monkeyArr = [];
monk.x = 40;
monk.y = 363;
monk.stateLookRight = true;
monk.stateLookLeft = false;
monk.stateGiveOffering = false;
monk.stateFallOver = false;
backgroundImage.x = 0;
backgroundImage.totalX = 0;
backgroundImage.speed = -0.3;
pauseAudio(soundsLikeTeenSpirit);
pauseAudio(soundPain);
pauseAudio(soundGameOver);
$message.style.display = 'none';
}
// Draws game board
draw() {
frameCounter++
gameCanvas.clearBoard()
// -----------PLAY AUDIO----------
playAudio(soundMantra)
playAudio(soundForest)
// ----------DRAW BACKGROUND---------
backgroundImage.move();
if (backgroundImage.totalX > -(canvas.width * 0)) {
backgroundImage.drawLandscape();
} else {
backgroundImage.drawTransition();
}
// -----------DRAW PLAYER-----------
monk.update()
// -----------DRAW OBSTACLES-----------
// One monkey falls every 2 second
if (frameCounter % 120 === 0) {
playAudio(soundMonkey)
let randomPosX = Math.floor(Math.random() * 600)
monkeyArr.push(new Monkey(randomPosX))
}
// -----------CHECK COLLISION-----------
monkeyArr.forEach((monkey) => {
if (monk.crashWith(monkey)) {
monk.stateFallOver = true
gameCanvas.gameOver()
monkey.updateWinningMonkey()
setTimeout(function () {
gameRunning = false;
}, 1000);
return
}
monkey.updateAnimatedMonkey()
})
// -----------CHECK IF PLAYER WINS-----------
if (monk.x > 434 && monk.stateGiveOffering && backgroundImage.speed === 0) {
gameCanvas.nirvana()
setTimeout(function () {
gameRunning = false;
}, 1000);
}
// -----------DRAWS ON EVERY FRAMECOUNT
// AS LONG AS GAME IS RUNNING-----------
if (gameRunning) {
requestAnimFrame(gameCanvas.draw)
// setTimeout(gameCanvas.draw, 24)
// window.requestAnimationFrame(gameCanvas.draw)
}
}
}