-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.html
503 lines (426 loc) · 12.8 KB
/
page.html
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
<html>
<head>
<title>Multiplayer Mario!</title>
<script type="text/javascript">
// function httpPost(url, payload, callback) {
// let request = new XMLHttpRequest();
// request.onreadystatechange = function () {
// if (request.readyState == 4) {
// if (request.status == 200)
// callback(request.responseText);
// else {
// if (request.status == 0 && request.statusText.length == 0)
// alert("Connection failed");
// else
// alert("Server returned status " + request.status + ", " + request.statusText);
// }
// }
// };
// request.open('post', url, true);
// request.setRequestHeader('Content-Type',
// 'application/x-www-form-urlencoded');
// request.send(payload);
// }
// function cb(response) {
// alert("The back-end server replied: " + response);
// alert("The server's favorite number is: " + ob.fav_num);
// }
// function sayhi() {
// // Find the text field
// let msg = document.getElementById("mymessage");
// // Make a JSON blob
// let ob = {};
// ob.message = msg.value;
// ob.fav_num = 3.14159;
// let json_string = JSON.stringify(ob);
// console.log(ob);
// // Send the JSON blob to the server
// httpPost("ajax_handler.html", json_string, cb);
// }
</script>
</head>
<body>
<br>
<canvas id="myCanvas" width="1000" height="500" style="border:1px solid #cccccc;"></canvas>
<script type="text/javascript">
function cb(response) {
// alert("The back-end server replied: " + response);
// Parse the JSON
let ob = JSON.parse(response);
myID = ob.id;
marioX = ob.marioX;
marioY = ob.marioY;
yoshiX = ob.yoshiX;
yoshiY = ob.yoshiY;
if (ob.createYoshi == true && createYoshi != true)
createYoshi = true;
// alert("The server's favorite number is: " + ob.fav_num);
}
function httpPost(url, payload, callback) {
let request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4) {
if (request.status == 200)
callback(request.responseText);
else {
if (request.status == 0 && request.statusText.length == 0)
alert("Connection failed");
else
alert("Server returned status " + request.status + ", " + request.statusText);
}
}
};
request.open('post', url, true);
request.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded');
request.send(payload);
}
let horizontal_speed = 10;
let vertical_speed = 15;
let clientCount = 0;
let myID = -1;
let marioX = 100;
let marioY = 100;
let yoshiX = 200;
let yoshiY = 100;
let createYoshi = false;
class Sprite {
constructor(newModel, x, y, w, h, spriteType, image_url, update_method, onclick_method) {
this.model = newModel;
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.sprite_type = spriteType;
this.vertical_velocity = 0;
this.image = new Image();
this.original_image_url = image_url;
this.image.src = image_url;
this.update = update_method;
this.onclick = onclick_method;
// Server
this.framcounter = 0;
this.id = -1;
// Mario/Yoshi Attributes
this.solid_count = 0;
this.mario_sprite_val = 1;
this.previous_x = 0;
this.previous_y = 0;
// Coin Block Attributes
this.coin_sprite_val = 0;
this.coin_block_val = 0;
this.coin_block_timer = 0;
this.coin_block_hit = false;
// Coin Attributes
let multiplier = Math.random();
if (multiplier >= .5)
multiplier = 1;
else
multiplier = -1;
this.coin_horizontal_velocity = Math.random() * 18 * multiplier;
this.coin_vertical_velocity = -20;
}
waitToContactServer(framcounter) {
// Do networking stuff
if (this.framcounter == undefined)
this.framcounter = 0;
if (this.framcounter < 10) {
this.framcounter++;
return false;
}
else {
this.framcounter = 0;
return true;
}
}
ignore_click(x, y) {
}
sit_still() {
}
// MARIO FUNCTIONS
marioUpdate() {
this.vertical_velocity += 5;
this.y += this.vertical_velocity;
this.solid_count++;
if (this.y >= 295) {
this.vertical_velocity = 0;
this.y = 295;
this.solid_count = 0;
}
for (let i = 0; i < this.model.sprites.length; i++) {
let s = this.model.sprites[i];
if (s.sprite_type == "brick" || s.sprite_type == "coin_block") {
if (this.checkCollision(this, s)) {
this.setBarrier(s);
}
}
}
if (myID < 1) {
if (this.waitToContactServer(this.framcounter)) {
let ob = {};
ob.id = myID;
ob.marioX = this.model.camera_position;
ob.marioY = this.y;
let json_string = JSON.stringify(ob);
httpPost("ajax_handler.html", json_string, cb);
console.log(this.model.sprites);
if (this.model.yoshi != undefined) {
this.model.yoshi.x = yoshiX;
this.model.yoshi.y = yoshiY;
}
}
}
if (createYoshi == true && this.model.yoshi == undefined) {
this.model.addYoshi();
createYoshi = false;
}
}
setBarrier(b) {
// Left of Brick Barrier
if ((this.x + this.model.camera_position + this.w >= b.x) && (this.model.previous_camera_position + this.previous_x + this.w < b.x)) {
this.model.camera_position -= horizontal_speed;
}
// Right of Brick Barrier
if ((this.model.camera_position + this.x <= b.x + b.w) && (this.previous_x + this.model.previous_camera_position > b.x + b.w)) {
this.model.camera_position += horizontal_speed;
}
// Top of Brick Barrier
if ((this.y + this.h >= b.y) && (this.previous_y + this.h <= b.y)) {
this.vertical_velocity = 0;
this.y = b.y - this.h - 1;
this.solid_count = 0;
}
// Bottom of Brick Barrier
if ((this.y <= b.y + b.h) && (this.previous_y > b.y + b.h)) {
this.y = b.y + b.h + 1;
this.vertical_velocity = 0;
if (b.sprite_type == "coin_block") {
b.generate_coin();
}
}
}
checkCollision(a, b) {
// Check left of brick
if (a.x + this.model.camera_position + a.w < b.x)
return false;
// Check right of brick
else if (a.x + this.model.camera_position > b.x + b.w)
return false;
// Check top of the brick
else if (a.y + a.h < b.y)
return false;
// Check bottom of brick
else if (a.y > b.y + b.h)
return false;
else
return true;
}
jump() {
if (this.solid_count < 4) {
this.vertical_velocity -= 15;
}
}
update_mario_image_val() {
this.mario_sprite_val++;
if (this.mario_sprite_val == 5)
this.mario_sprite_val = 1;
this.change_image_url(this.mario_sprite_val);
}
change_image_url(num) {
if (this.sprite_type == "mario")
this.image.src = this.original_image_url.slice(0, 5) + num + this.original_image_url.slice(6, 10);
if (this.sprite_type == "coin_block")
this.image.src = "coinBlockEmpty.png";
}
set_previous() {
this.previous_x = this.x;
this.previous_y = this.y;
this.model.previous_camera_position = this.model.camera_position;
}
// BRICK FUNCTIONS
// COIN BLOCK FUNCTIONS
coin_block_update() {
if (this.coin_block_val == 5)
this.change_image_url(1);
if (this.coin_block_hit) {
this.coin_block_hit = !this.coin_block_hit;
this.coin_block_timer++;
}
else
this.coin_block_timer = 0;
}
generate_coin() {
if (!this.coin_block_hit && this.coin_block_timer < 1 && this.coin_block_val < 5) {
this.coin_block_hit = !this.coin_block_hit;
this.coin_block_val++;
this.model.addCoin(this);
}
}
// COIN FUNCTIONS
coin_update() {
this.coin_vertical_velocity += 3.14159;
this.y += this.coin_vertical_velocity;
this.x += this.coin_horizontal_velocity;
if (this.y > 400)
this.model.remove_coin(this);
}
// Yoshi Functions
yoshiUpdate() {
this.vertical_velocity += 5;
this.y += this.vertical_velocity;
this.solid_count++;
if (this.y >= 300) {
this.vertical_velocity = 0;
this.y = 300;
this.solid_count = 0;
}
for (let i = 0; i < this.model.sprites.length; i++) {
let s = this.model.sprites[i];
if (s.sprite_type == "brick" || s.sprite_type == "coin_block") {
if (this.checkCollision(this, s)) {
this.setBarrier(s);
}
}
}
if (myID == 1) {
if (this.waitToContactServer(this.framcounter)) {
let ob = {};
ob.id = myID;
ob.yoshiX = this.model.camera_position;
ob.yoshiY = this.y;
let json_string = JSON.stringify(ob);
httpPost("ajax_handler.html", json_string, cb);
}
this.model.mario.x = marioX;
this.model.mario.y = marioY;
}
}
}
class Model {
constructor() {
this.camera_position = 0;
this.previous_camera_position = 0;
this.sprites = [];
this.mario = new Sprite(this, 200, 100, 60, 95, "mario", "mario1.png", Sprite.prototype.marioUpdate, Sprite.prototype.ignore_click);
this.sprites.push(this.mario);
}
update() {
for (let i = 0; i < this.sprites.length; i++) {
this.sprites[i].update();
}
}
onclick(x, y) {
for (let i = 0; i < this.sprites.length; i++) {
this.sprites[i].onclick(x, y);
}
}
jump() {
this.mario.jump();
}
move_left() {
this.camera_position -= horizontal_speed;
this.mario.update_mario_image_val();
}
move_right() {
this.camera_position += horizontal_speed;
this.mario.update_mario_image_val();
}
addYoshi() {
this.yoshi = new Sprite(this, 200, 225, 51, 54, "yoshi", "yoshi.png", Sprite.prototype.yoshiUpdate, Sprite.prototype.ignore_click)
this.sprites.push(this.yoshi);
}
addCoin(b) {
this.sprites.push(new Sprite(this, b.x, b.y, b.w - 10, b.h - 10, "coin", "coin.png", Sprite.prototype.coin_update, Sprite.prototype.ignore_click));
}
remove_coin(b) {
let index = this.sprites.indexOf(b);
this.sprites.splice(index, 1);
}
}
class View {
constructor(model) {
this.model = model;
this.canvas = document.getElementById("myCanvas");
this.background = new Image();
this.background.src = "background.png";
}
update() {
let ctx = this.canvas.getContext("2d");
ctx.clearRect(0, 0, 700, 500);
for (let i = -10; i < 10; i++) {
ctx.drawImage(this.background, (800 * i) - this.model.camera_position / 2, 0);
}
for (let i = 0; i < this.model.sprites.length; i++) {
let sprite = this.model.sprites[i];
if (sprite.sprite_type == "mario" && myID == 0)
ctx.drawImage(sprite.image, sprite.x, sprite.y, sprite.w, sprite.h);
else if (sprite.sprite_type == "yoshi" && myID == 1)
ctx.drawImage(sprite.image, sprite.x, sprite.y, sprite.w, sprite.h);
else
ctx.drawImage(sprite.image, sprite.x - this.model.camera_position, sprite.y, sprite.w, sprite.h);
}
}
}
class Controller {
constructor(model, view) {
this.model = model;
this.view = view;
this.key_right = false;
this.key_left = false;
// this.key_up = false;
this.key_down = false;
this.key_space = false;
let self = this;
view.canvas.addEventListener("click", function (event) { self.onClick(event); });
view.canvas.addEventListener("mouseDown", function (event) { self.mouseDown(event); });
view.canvas.addEventListener("mouseUp", function (event) { self.mouseUp(event); });
document.addEventListener('keydown', function (event) { self.keyDown(event); }, false);
document.addEventListener('keyup', function (event) { self.keyUp(event); }, false);
}
onClick(event) {
this.model.onclick(event.pageX - this.view.canvas.offsetLeft, event.pageY - this.view.canvas.offsetTop);
}
keyDown(event) {
if (event.keyCode == 39) this.key_right = true;
else if (event.keyCode == 37) this.key_left = true;
// else if (event.keyCode == 38) this.key_up = true;
else if (event.keyCode == 40) this.key_down = true;
else if (event.keyCode == 32) this.key_space = true;
}
keyUp(event) {
if (event.keyCode == 39) this.key_right = false;
else if (event.keyCode == 37) this.key_left = false;
// else if (event.keyCode == 38) this.key_up = false;
else if (event.keyCode == 40) this.key_down = false;
else if (event.keyCode == 32) this.key_space = false;
}
update() {
this.model.mario.set_previous();
if (this.key_right) {
this.model.move_right();
}
if (this.key_left) {
this.model.move_left();
}
if (this.key_space) {
this.model.jump();
}
}
}
class Game {
constructor() {
this.model = new Model();
this.view = new View(this.model);
this.controller = new Controller(this.model, this.view);
}
onTimer() {
this.controller.update();
this.model.update();
this.view.update();
}
}
let game = new Game();
let timer = setInterval(function () { game.onTimer(); }, 40);
</script>
</body>
</html>