forked from cssa-gamejam-2014/thegame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-dev.js
703 lines (593 loc) · 17.5 KB
/
game-dev.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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
var tagBody = '(?:[^"\'>]|"[^"]*"|\'[^\']*\')*';
var tagOrComment = new RegExp(
'<(?:'
// Comment body.
+ '!--(?:(?:-*[^->])*--+|-?)'
// Special "raw text" elements whose content should be elided.
+ '|script\\b' + tagBody + '>[\\s\\S]*?</script\\s*'
+ '|style\\b' + tagBody + '>[\\s\\S]*?</style\\s*'
// Regular name
+ '|/?[a-z]'
+ tagBody
+ ')>',
'gi');
function removeTags(html) {
var oldHtml;
do {
oldHtml = html;
html = html.replace(tagOrComment, '');
} while (html !== oldHtml);
return html.replace(/</g, '<');
}
//http://www.w3schools.com/js/js_cookies.asp
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length,c.length);
}
return "";
}
var TARGET_FRAMERATE=25;
var SPEED = 80;
var WALK_WIDTH = null;
possibletexts = ['buy me!', '50% off!', 'only 99.95!', 'while stocks last!', 'click me!', 'you deserve it!', 'put me on!', '99% fat free', 'be yourself!'];
function drawChoice(outfit, position, text){
var c=document.getElementById("chooseCanvas_"+position);
var ctx=c.getContext("2d");
ctx.clear();
for (var key in outfit){
var object = document.getElementById(key+"_"+outfit[key]);
ctx.drawImage(object, 0, 0);
}
ctx.rotate(-Math.PI/2);
ctx.textAlign = "center";
ctx.font="20px Helvetica";
if (getRandom(2) == 0){
ypos = 25;
} else {
ypos = 145;
}
ctx.fillText(possibletexts[getRandom(possibletexts.length)],-85,ypos);
ctx.rotate(Math.PI/2);
}
function drawCharacter(outfit, position){
if (position == 1){
name = protagonist;
} else {
name = "Jed";
}
var charSpan = $();
body = $("#body").attr("src");
var img = $('<img>');
img.attr('src', body);
img.attr('class', 'clothabsolute');
img.css('left', 175*(position-1));
img.appendTo("#characterhtml_"+position);
var charname = $('<span>'+name+'</span>');
charname.attr('class', 'clothabsolute textrotate');
// -18 offset is because for some reason the text rotation means the names look different distances
charname.css('left', 175*(position-1)-28*(2-position));
charname.appendTo("#characterhtml_"+position);
for (var key in outfit){
var object = $("#"+key+"_"+outfit[key]).attr("src");
img = $('<img>');
img.attr('src', object);
img.attr('class', 'clothabsolute');
img.css('left', 175*(position-1));
img.appendTo("#characterhtml_"+position);
}
}
function clearChoice(position){
var c=document.getElementById("chooseCanvas_"+position);
var ctx=c.getContext("2d");
ctx.clear();
}
function clearCharacter(position){
$("#characterhtml_"+position).empty();
}
function equalPeople(person1, person2){
return (person1.hat == person2.hat && person1.shirt == person2.shirt && person1.pants == person2.pants && person1.shoes == person2.shoes);
}
function draw_person(ctx, outfit) {
var body=document.getElementById("body");
ctx.drawImage(body,0,0);
for (var key in outfit){
if (key == "left") continue;
var object = document.getElementById(key+"_"+outfit[key]);
ctx.drawImage(object,0,0);
}
}
// Taken from http://stackoverflow.com/questions/2142535/how-to-clear-the-canvas-for-redrawing
CanvasRenderingContext2D.prototype.clear =
CanvasRenderingContext2D.prototype.clear || function (preserveTransform) {
if (preserveTransform) {
this.save();
this.setTransform(1, 0, 0, 1, 0, 0);
}
this.clearRect(0, 0, this.canvas.width, this.canvas.height);
if (preserveTransform) {
this.restore();
}
}
var lastlevel = 0;
function draw_frame() {
// Do the HTML version
if ($(".walkingCharacter").length == 0){
drawNewPerson();
}
$(".walkingCharacter").each(function(){
offset = $(this).offset();
currentLeft = offset.left;
newLeft = currentLeft - SPEED / TARGET_FRAMERATE
// NOT SURE WHY THIS HACK IS NEEDED TO PASS EXACTLY 400
if (newLeft < 403 && newLeft > 400){
newLeft = 399;
}
if (newLeft < 225){
// Delete if necessary
$(this).remove();
} else {
// Move otherwise
$(this).offset({ top: offset.top, left: newLeft});
}
// Create new character if necessary
if (newLeft < WALK_WIDTH-175 && $(this).hasClass('endCharacter')){
$(this).removeClass('endCharacter');
drawNewPerson();
}
});
// Create character if necessary
}
function drawNewPerson(){
personhtml = $("<div></div>");
personhtml.attr('class', 'walkingCharacter endCharacter');
personhtml.css('left', WALK_WIDTH);
personhtml.appendTo("#walkingscreen");
var new_person = levels[currentLevel].generator();
while (equalPeople(new_person, choices[correct[0]]) || equalPeople(new_person, choices[correct[1]])) {
new_person = levels[currentLevel].generator();
}
var img = $('<img>');
img.attr('src', body);
img.attr('class', 'clothabsolute');
img.appendTo(personhtml);
for (var key in person){
var object = $("#"+key+"_"+person[key]).attr("src");
img = $('<img>');
img.attr('src', object);
img.attr('class', 'clothabsolute');
img.appendTo(personhtml);
}
}
function on_resize_wrapper(ctx, elem) {
// I am a serious closure abuser
return function() {
// resize code
// make the canvas the right size
elem.width = elem.offsetWidth;
elem.height = elem.offsetHeight;
camera.width = elem.width;
camera.height = elem.height;
// make it so that y = 0 is always at the bottom of the screen
camera.top = camera.height / camera.zoom;
}
}
var characters = [-1, -1];
var choices = [];
var correct = [];
nextdressed = 0;
function selectChoice(id){
$("#chooseCanvas_"+id).addClass('selected');
// if either are not dressed
if (characters[0] == -1){
characters[0] = id;
nextdressed = (0+1)%2;
drawCharacter(choices[id], 1);
} else if (characters[1] == -1){
characters[1] = id;
nextdressed = (1+1)%2;
drawCharacter(choices[id], 2);
// Both are dressed. Dress least recently dressed one.
} else {
deselectChoice(characters[nextdressed]);
characters[nextdressed] = id;
drawCharacter(choices[id], (nextdressed+1));
nextdressed = (nextdressed+1)%2;
}
// Red rectangle
var c=document.getElementById("chooseCanvas_"+id);
var ctx=c.getContext("2d");
ctx.beginPath();
ctx.lineWidth="5";
ctx.strokeStyle="red";
ctx.rect(5,5,160,190);
ctx.stroke();
if (characters[0] != -1 && characters[1] != -1){
$("#next").removeClass("ui-state-disabled");
}
}
function deselectChoice(id){
// remove selection
var index = characters.indexOf(id);
if (index > -1) {
characters[index] = -1;
}
$("#chooseCanvas_"+id).removeClass('selected');
// clear choice selection
var c=document.getElementById("chooseCanvas_"+id);
var ctx=c.getContext("2d");
ctx.clear();
drawChoice(choices[id], id);
// redraw characters
for (var x = 0; x < 2; x++){
if (characters[x] != -1){
clearCharacter(x+1);
drawCharacter(choices[characters[x]], x+1);
}
}
for (var x = 0; x < 2; x++){
if (characters[x] == -1){
clearCharacter(x+1);
drawCharacter([], x+1);
}
}
if (characters[0] == -1 || characters[1] == -1)
$("#next").addClass("ui-state-disabled");
}
var frameDrawer;
var currentLevel = 0;
var setupinfo = false;
if (window.location.hash){
var protagonist = removeTags(window.location.hash.replace("#", ""));
} else {
var protagonist = null;
}
var storyTime = true;
var finishedLevel = false;
var currentContainer = "storyscreen";
var hint1timer;
var hint2timer;
function setUpGameScreen(){
choices = [];
correct = [];
characters = [-1, -1];
for (var x=0; x<10; x++){
clearChoice(x);
}
for (var x=1; x<3; x++){
clearCharacter(x);
drawCharacter([], x);
}
level = levels[currentLevel];
correct[0] = (getRandom(10));
correct[1] = (getRandom(10));
while (correct[0] == correct[1]){
correct[1] = getRandom(10);
}
if (correct[0] > correct[1]){
var temp = correct[0];
correct[0] = correct[1];
correct[1] = temp;
}
for (var x=0; x<10; x++){
$("#chooseCanvas_"+x).removeClass('selected');
if (x == correct[0] || x == correct[1]){
newperson = level.generator();
if (x == correct[1]){
while (equalPeople(newperson, choices[correct[0]])){
newperson = level.generator();
}
}
choices.push(newperson);
drawChoice(newperson, x);
} else {
var invalid;
// Check not the same as anyone else
var same = true;
while (same){
same = false;
invalid = level.randgen();
// Make sure cannot enter with this costume
while (level.validator(invalid)){
invalid = level.randgen();
}
// Check for equality with anyone else
choices.forEach(function(person){
if (equalPeople(invalid, person)){
same = true;
}
});
}
choices.push(invalid);
drawChoice(invalid, x);
}
}
$("#walkingscreen").empty();
}
function setUpStoryScreen(){
if (stories[currentLevel] == null){
// Put up correct backdrop
$('#groundfloor').attr('src', $('#'+buildings[9][0]).attr('src'));
$('#windows').attr('src', $('#'+buildings[9][1]).attr('src'));
title = "Chapter "+(currentLevel+1)
story = "<p>You've saved the world, but you're itching to break into some more buildings! Can you crack the code?</p>";
} else {
// Put up correct backdrop
$('#groundfloor').attr('src', $('#'+buildings[currentLevel][0]).attr('src'));
$('#windows').attr('src', $('#'+buildings[currentLevel][1]).attr('src'));
titleAndStory = stories[currentLevel];
title = titleAndStory[0];
story = titleAndStory[1];
}
story = story.replace("%PROT%", protagonist);
$("#backtext").html(title);
$('#storyscreen').html("<h1>"+title+"</h1>"+story);
failure = failures[0].replace("%PROT%", protagonist);
$('#failurescreen').html(failure);
if (successes[currentLevel] == null){
$('#successscreen').html("<h1>Success!</h1><p>You solved the puzzle!</p>");
} else {
success = successes[currentLevel].replace("%PROT%", protagonist);
$('#successscreen').html(success);
}
}
function boot_up_level(){
if (levels[currentLevel] == null){
endTheGame();
}
// Set up info for the level
if (levels[currentLevel].setup){
setupinfo = levels[currentLevel].setup();
}
setUpStoryScreen();
setUpGameScreen();
}
var scenes = {"story":0, "store":1, "hint":2, "failure":3, "success":4};
var sceneIDs = {"name": '#namescreen', "story":'#storyscreen', "store":'#gamescreen', "hint":'#hintscreen', "failure":'#failurescreen', "success":'#successscreen', "endgame":'#endscreen'};
currentScene = null;
chromeFirstRedraw = true;
function transitionScene(nextScene){
switch(currentScene){
case scenes.name:
protagonist = removeTags($('#username').val());
// Check they haven't just entered nothing
if (protagonist == ''){
$("#next").removeClass("ui-state-disabled");
break;
}
if (getCookie(protagonist) != ""){
currentLevel = parseInt(getCookie(protagonist));
}
$('#namescreen').slideUp(function(){
boot_up_level();
transitionSceneTo(nextScene);
framesDrawer = window.setInterval(draw_frame, 1000 / TARGET_FRAMERATE);
});
break;
case scenes.story:
$('#storyscreen').slideUp(function(){
// redraw game screen to fix up Chrome
if (chromeFirstRedraw){
for (var x=0; x<10; x++){
drawChoice(choices[x], x);
}
chromeFirstRedraw = false;
}
$('#back').removeClass("ui-state-disabled");
transitionSceneTo(nextScene);
});
break;
case scenes.store:
// Various events happening from the store: all hide the game screen.
$('#gamescreen').slideUp(function(){
transitionSceneTo(nextScene);
});
break;
case scenes.hint:
$('#hintscreen').slideUp(function(){
transitionSceneTo(nextScene);
});
break;
case scenes.failure:
$('#failurescreen').slideUp(function(){
transitionSceneTo(nextScene);
});
break;
case scenes.success:
// From success can only transition to next story
// Prevent double clicking
$("#next").addClass("ui-state-disabled");
// Progress to next level
currentLevel++;
$('#successscreen').slideUp(function(){
boot_up_level();
transitionSceneTo(nextScene);
});
break;
default:
console.log('default case hit in from scenes');
transitionSceneTo(nextScene);
break;
}
}
function transitionSceneTo(nextScene){
switch(nextScene){
case scenes.name:
$('#nexttext').text('Let\'s play!');
$('#namescreen').slideDown(function(){
$('#next').show();
});
break;
case scenes.story:
// Either finished previous level, or back to reading story
$('#back').addClass("ui-state-disabled");
$('#nexttext').text('Go to the costume store');
$('#storyscreen').slideDown(function(){
$("#next").show();
$("#next").removeClass("ui-state-disabled");
});
break;
case scenes.store:
$('#nexttext').text('Walk in the door');
if (characters[0] == -1 || characters[1] == -1){
$("#next").addClass("ui-state-disabled");
} else {
$("#next").removeClass("ui-state-disabled");
}
// Set up the hint timer
if (!hint1timer){
hint1timer = setTimeout("showHintButton(1)", 15000);
} else {
}
$('#gamescreen').slideDown(function(){
$("#back").removeClass("ui-state-disabled");
});
break;
case scenes.hint:
$('#nexttext').text('Go to the costume store');
$('#hintscreen').slideDown(function(){
$("#next").removeClass("ui-state-disabled");
});
break;
case scenes.failure:
// Player got the costumes wrong
setUpGameScreen();
$('#nexttext').text('Go to the costume store');
$('#failurescreen').slideDown(function(){
$("#next").removeClass("ui-state-disabled");
});
break;
case scenes.success:
// Got the puzzle right
setCookie(protagonist,currentLevel+1,365);
// Clear hint timers
window.clearTimeout(hint1timer);
window.clearTimeout(hint2timer);
hint1timer = 0;
hint2timer = 0;
$('#hint1').hide();
$('#hint2').hide();
// Hide game screen, show success
$('#back').addClass("ui-state-disabled");
if (currentLevel+1 >= levels.length){
$('#successscreen').slideDown(function(){
console.log('current level is '+currentLevel);
console.log(currentLevel+1 >= levels.length);
console.log(levels.length);
endTheGame();
});
} else {
$('#nexttext').text('Next chapter');
$('#successscreen').slideDown(function(){
$("#next").removeClass("ui-state-disabled");
});
}
break;
default:
console.log("Switch case reached the end, something is badly broken!");
break;
}
currentScene = nextScene;
}
function endTheGame(){
if (typeof framesDrawer !== 'undefined'){
clearInterval(framesDrawer);
}
$('#nexttext').text('You won the game!');
$('#successscreen').slideUp(function(){
$('#successscreen').html('<h1>Well done!</h1><p>You\'ve finished all of the levels so far! You are a solid code cracker!</p>');
$('#successscreen').slideDown();
});
$("#next").hide();
}
function showHintButton(number){
$('#hint'+number).slideDown();
}
$(document).ready(function(){
WALK_WIDTH = $('#levelscreen').width();
// Selection code for choices
$('.chooseCanvas').click(function(){
var id = $(this).attr('choice');
if ($(this).hasClass('selected')){
deselectChoice(id);
} else {
selectChoice(id);
}
});
// Selection code for choices
$('.characterSpan').click(function(){
var character = $(this).attr('choice');
deselectChoice(characters[character]);
});
$('#next').click(function(){
// Don't work if disabled
if ($(this).hasClass("ui-state-disabled"))
return true;
// Prevent double clicking
$("#next").addClass("ui-state-disabled");
// Banish the heading for more space
$('#heading').slideUp();
// Success or entered name goes to Story
if (currentScene == scenes.success || currentScene == scenes.name){
transitionScene(scenes.story);
// If in store, check if guesses are correct
} else if (currentScene == scenes.store){
if (characters[0] == -1 || characters[1] == -1){
return 0;
}
if (correct.indexOf(parseInt(characters[0])) != -1 && correct.indexOf(parseInt(characters[1])) != -1){
transitionScene(scenes.success);
} else {
transitionScene(scenes.failure);
}
// Otherwise, go to the store
} else {
transitionScene(scenes.store);
}
});
$('#back').click(function(){
// Don't work if disabled
if ($(this).hasClass("ui-state-disabled"))
return true;
transitionScene(scenes.story);
});
$('#hint1').click(function(){
$('#hintscreen').slideUp(function(){
$('#hintscreen').html(hints[currentLevel][0]);
transitionScene(scenes.hint);
});
// Set up the hint timer
hint2timer = setTimeout("showHintButton(2)", 30000);
});
$('#hint2').click(function(){
$('#hintscreen').slideUp(function(){
$('#hintscreen').html(hints[currentLevel][1]);
});
transitionScene(scenes.hint);
});
// Handle navigations a bit nicer
$(window).unload(function() {
clearInterval(framesDrawer);
});
if (levels[currentLevel].setup){
setupinfo = levels[currentLevel].setup();
}
// Ask for name if it hasn't been provided
if (protagonist == null){
transitionSceneTo(scenes.name);
} else {
transitionSceneTo(scenes.story);
boot_up_level();
framesDrawer = window.setInterval(draw_frame, 1000 / TARGET_FRAMERATE);
}
inSelection = true;
});