-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameLogic.js
349 lines (251 loc) · 9.08 KB
/
gameLogic.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
333
334
335
336
337
338
339
340
341
342
343
344
// Arrows not really necessary but cool
const Direction = Object.freeze({
UP: "↑",
DOWN: "↓",
LEFT: "←",
RIGHT: "→",
STOPPED: ""
})
// Everything from the lower left corner of the sprite
class Sprite {
/*
Position values are from the lower left corner of the frame.
*/
constructor(id, imageURL, innerContent,
initXPosition, initYPosition, initDirection,
minXPosition, maxXPosition, minYPosition, maxYPosition, ) {
this.ID = id; // Element ID (str) -> Has to match your css id
this.image = imageURL; // Image url (str)
this.innerContent = innerContent; // Content Inside div (text str or html str)
this.initXPosition = initXPosition; // (float)
this.initYPosition = initYPosition; // (float)
this.initDirection = initDirection; // (Direction)
this.xPosition = initXPosition; // (float)
this.yPosition = initYPosition; // (float)
this.direction = initDirection; // (Direction)
this.MIN_X_POSITION = minXPosition; // (float)
this.MAX_X_POSITION = maxXPosition; // (float)
this.MIN_Y_POSITION = minYPosition; // (float)
this.MAX_Y_POSITION = maxYPosition; // (float)
// Render to screen
this.updatePosition(this.xPosition, this.yPosition)
this.updateImage(imageURL)
this.updateInnerContent(innerContent)
}
get position(){
return [this.xPosition, this.yPosition]
}
// Return min and max x position of the sprite
get xBounds() {
return [this.xPosition, this.xPosition + this.width]
}
// Return min and max y position of the sprite
get yBounds() {
return [this.yPosition, this.yPosition + this.height]
}
get width(){
return document.getElementById(this.ID).offsetWidth;
}
get height(){
return document.getElementById(this.ID).offsetHeight;
}
// Reset position and direction
reset() {
this.updatePosition(this.initXPosition, this.initYPosition)
this.direction = this.initDirection
}
updateImage(imageURL) {
this.image = imageURL;
let sprite = document.getElementById(this.ID);
sprite.src = imageURL
}
updatePosition(xPosition, yPosition) {
let sprite = document.getElementById(this.ID)
this.xPosition = xPosition
this.yPosition = yPosition
sprite.style.position = 'absolute'
sprite.style.left = xPosition + 'px';
sprite.style.bottom = yPosition + 'px';
}
updateXPosition(xPosition) {
let sprite = document.getElementById(this.ID)
this.xPosition = xPosition
sprite.style.position = 'absolute'
sprite.style.left = xPosition + 'px';
}
updateYPosition(yPosition) {
let sprite = document.getElementById(this.ID)
this.yPosition = yPosition
sprite.style.position = 'absolute'
sprite.style.bottom = yPosition + 'px';
}
updateInnerContent(content) {
self.content = content
let sprite = document.getElementById(this.ID)
sprite.innerHTML = content
}
}
class Game {
constructor() {
this.sprites = [] // Type Sprite
this.score = 0
}
// Add sprites to keep track of
trackSprite (sprite) {
this.sprites.push(sprite)
}
// Check if this sprite has collided with anything else in the game frame
isCollision(sprite) {
// To touch core of sprite
const BUFFER = 10;
// Make sure it is not the current sprite (10 for really touching)
for (let checkSprite of this.sprites) {
// Make sure it is not the current sprite
if (sprite.ID !== checkSprite.ID) {
const lowXBound = sprite.xBounds[0] + BUFFER
const highXBound = sprite.xBounds[1] - BUFFER
const lowYBound = sprite.yBounds[0] + BUFFER
const highYBound = sprite.yBounds[1] - BUFFER
const checkLowXBound = checkSprite.xBounds[0] + BUFFER
const checkHighXBound = checkSprite.xBounds[1] - BUFFER
const checkLowYBound = checkSprite.yBounds[0] + BUFFER
const checkHighYBound = checkSprite.yBounds[1] - BUFFER
// Check if sprite is to the left or right of checkSprite
if (highXBound < checkLowXBound || lowXBound > checkHighXBound) continue; // No collision on X axis
// Check if sprite is above or below checkSprite
if (highYBound < checkLowYBound || lowYBound > checkHighYBound) continue; // No collision on Y axis
// If we reach here, sprites are colliding
return true;
}
}
return false;
}
}
// Script
let xOffset = 0
let smallOffset = 0
let yOffset = 0
if (window.innerWidth <= 600) {
xOffset = 80
smallOffset = 35
yOffset = 60
}
gameText = new Sprite(
'gameText', '', 'Press the space bar or tap to jump!',
50 - smallOffset, 85 , Direction.STOPPED,
0, 0, 0, 0
)
frog = new Sprite(
'frog', 'assets/FrogSit.png', '',
100 - xOffset, 0, Direction.UP,
100, 100, 0, 200 - yOffset
)
// Start and end mushroom off screen
shroom = new Sprite(
'shroom', 'assets/ShroomPink.png', '',
window.innerWidth , 0, Direction.LEFT,
-100, window.innerWidth, 0, 0
)
// Start off screen and bring to screen when died
gameOverText = new Sprite(
'gameOver', 'assets/GameOver.png', '',
-300, 20 , Direction.STOPPED,
-300, window.innerWidth/2 - 145/2, 0, 0
)
// Start off screen and bring to screen when died
score = new Sprite(
'score', '', 'Score: 0',
window.innerWidth - 200 + xOffset, 85 , Direction.STOPPED,
0, 0, 0, 0
)
game = new Game()
game.trackSprite(frog)
game.trackSprite(shroom)
let restart = false;
let enable = true;
// Jump frog when space bar pressed
document.addEventListener('keydown', function(event) {
restart = true;
if (event.key === ' ') {
event.preventDefault() // Prevent scrolling w/ spacebar
gameText.updateInnerContent('') // Take away content when start jumping
if (enable) moveFrog(frog);
enable = true;
}
});
// Jump frog when space bar or when the screen is tapped
document.addEventListener('touchstart', function(event) {
var touchY = event.touches[0].clientY;
// If touch the lower part of the screen, then register the tap
if (touchY >= window.innerHeight - 100) {
gameText.updateInnerContent('') // Take away content when start jumping
restart = true;
if (enable) moveFrog(frog);
enable = true;
}
});
// Start Shroom Movement
moveShroom(shroom)
// Start checking crash
checkCollision()
// Check crash + count score
function checkCollision() {
setTimeout(function() {
// End game if collision
if (game.isCollision(shroom)) {
restart = false
shroom.direction = Direction.STOPPED;
frog.direction = Direction.STOPPED;
frog.updateImage('assets/FrogDead.png')
gameOverText.updateXPosition(gameOverText.MAX_X_POSITION)
enable = false;
checkRestart()
}
checkCollision()
}, 5); // Updates at half the speed of the other threads
}
function checkRestart() {
setTimeout(function() {
if (restart) {
frog.reset()
frog.updateImage('assets/FrogSit.png')
shroom.reset()
gameOverText.reset()
game.score = 0
}else checkRestart()
}, 5); // Updates at half the speed of the other threads
}
// Modify frog's position
function moveFrog() {
// Call recursively forever
setTimeout(function() {
if (frog.direction !== Direction.STOPPED) {
frog.updateImage('assets/FrogJump.png')
newYPos = frog.position[1]
if (frog.direction === Direction.DOWN) newYPos -= 4
else newYPos += 4.5
if (newYPos >= frog.MAX_Y_POSITION) frog.direction = Direction.DOWN
else if (newYPos <= frog.MIN_Y_POSITION) frog.direction = Direction.UP
frog.updateYPosition(newYPos)
// Only allow 1 jump at a time
if (newYPos > frog.MIN_Y_POSITION) moveFrog(frog); // Recursively call while jumping
else {
frog.updateImage('assets/FrogSit.png')
}
}
}, 10); // Frog updated every 10 msec
}
// Modify Mushrooms position (And add points)
function moveShroom() {
// Call recursively forever
setTimeout(function() {
if (shroom.direction !== Direction.STOPPED) {
game.score += 0.01
score.updateInnerContent("Points: " + Math.round(game.score))
let newXPosition = shroom.position[0] - 5
if (newXPosition <= shroom.MIN_X_POSITION) newXPosition = shroom.MAX_X_POSITION
shroom.updateXPosition(newXPosition)
}
moveShroom(shroom);
}, 10); // Shroom updated every msec
}