-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
1 lines (1 loc) · 12.5 KB
/
game.js
1
function removeTags(e){var t;do{t=e;e=e.replace(tagOrComment,"")}while(e!==t);return e.replace(/</g,"<")}function setCookie(e,t,n){var r=new Date;r.setTime(r.getTime()+n*24*60*60*1e3);var i="expires="+r.toGMTString();document.cookie=e+"="+t+"; "+i}function getCookie(e){var t=e+"=";var n=document.cookie.split(";");for(var r=0;r<n.length;r++){var i=n[r];while(i.charAt(0)==" ")i=i.substring(1);if(i.indexOf(t)!=-1)return i.substring(t.length,i.length)}return""}function drawChoice(e,t){var n=document.getElementById("chooseCanvas_"+t);var r=n.getContext("2d");r.clear();for(var i in e){var s=document.getElementById(i+"_"+e[i]);r.drawImage(s,0,0)}r.rotate(-Math.PI/2);r.textAlign="center";r.font="20px Helvetica";if(getRandom(2)==0){ypos=25}else{ypos=145}r.fillText(possibletexts[getRandom(possibletexts.length)],-85,ypos);r.rotate(Math.PI/2)}function drawCharacter(e,t){if(t==1){name=protagonist}else{name="Jed"}var n=$();body=$("#body").attr("src");var r=$("<img>");r.attr("src",body);r.attr("class","clothabsolute");r.css("left",175*(t-1));r.appendTo("#characterhtml_"+t);var i=$("<span>"+name+"</span>");i.attr("class","clothabsolute textrotate");i.css("left",175*(t-1)-28*(2-t));i.appendTo("#characterhtml_"+t);for(var s in e){var o=$("#"+s+"_"+e[s]).attr("src");r=$("<img>");r.attr("src",o);r.attr("class","clothabsolute");r.css("left",175*(t-1));r.appendTo("#characterhtml_"+t)}}function clearChoice(e){var t=document.getElementById("chooseCanvas_"+e);var n=t.getContext("2d");n.clear()}function clearCharacter(e){$("#characterhtml_"+e).empty()}function equalPeople(e,t){return e.hat==t.hat&&e.shirt==t.shirt&&e.pants==t.pants&&e.shoes==t.shoes}function draw_person(e,t){var n=document.getElementById("body");e.drawImage(n,0,0);for(var r in t){if(r=="left")continue;var i=document.getElementById(r+"_"+t[r]);e.drawImage(i,0,0)}}function draw_frame(){if($(".walkingCharacter").length==0){walkNewPerson()}$(".walkingCharacter").each(function(){offset=$(this).offset();currentLeft=offset.left;newLeft=currentLeft-SPEED/TARGET_FRAMERATE;if(newLeft<403&&newLeft>400){newLeft=399}if(newLeft<225){$(this).remove()}else{$(this).offset({top:offset.top,left:newLeft})}if(newLeft<WALK_WIDTH-175&&$(this).hasClass("endCharacter")){$(this).removeClass("endCharacter");walkNewPerson()}})}function walkNewPerson(){if(levels[currentLevel].partners!=null){var e=levels[currentLevel].generator();while(equalPeople(e[0],choices[correct[0]])||equalPeople(e[0],choices[correct[1]])||equalPeople(e[1],choices[correct[0]])||equalPeople(e[1],choices[correct[1]])){e=levels[currentLevel].generator()}drawNewPerson(e[0],false,0);drawNewPerson(e[1],true,100)}else{var t=levels[currentLevel].generator();while(equalPeople(t,choices[correct[0]])||equalPeople(t,choices[correct[1]])){t=levels[currentLevel].generator()}drawNewPerson(t,true,0)}}function drawNewPerson(e,t,n){personhtml=$("<div></div>");personhtml.attr("class","walkingCharacter "+(t?"endCharacter":""));personhtml.css("left",WALK_WIDTH+n);personhtml.appendTo("#walkingscreen");var r=$("<img>");r.attr("src",body);r.attr("class","clothabsolute");r.appendTo(personhtml);for(var i in e){var s=$("#"+i+"_"+e[i]).attr("src");r=$("<img>");r.attr("src",s);r.attr("class","clothabsolute");r.appendTo(personhtml)}}function selectChoice(e){$("#chooseCanvas_"+e).addClass("selected");if(characters[0]==-1){characters[0]=e;nextdressed=(0+1)%2;drawCharacter(choices[e],1)}else if(characters[1]==-1){characters[1]=e;nextdressed=(1+1)%2;drawCharacter(choices[e],2)}else{deselectChoice(characters[nextdressed]);characters[nextdressed]=e;drawCharacter(choices[e],nextdressed+1);nextdressed=(nextdressed+1)%2}var t=document.getElementById("chooseCanvas_"+e);var n=t.getContext("2d");n.beginPath();n.lineWidth="5";n.strokeStyle="red";n.rect(5,5,160,190);n.stroke();if(characters[0]!=-1&&characters[1]!=-1){$("#next").removeClass("ui-state-disabled")}}function deselectChoice(e){var t=characters.indexOf(e);if(t>-1){characters[t]=-1}$("#chooseCanvas_"+e).removeClass("selected");var n=document.getElementById("chooseCanvas_"+e);var r=n.getContext("2d");r.clear();drawChoice(choices[e],e);for(var i=0;i<2;i++){if(characters[i]!=-1){clearCharacter(i+1);drawCharacter(choices[characters[i]],i+1)}}for(var i=0;i<2;i++){if(characters[i]==-1){clearCharacter(i+1);drawCharacter([],i+1)}}if(characters[0]==-1||characters[1]==-1)$("#next").addClass("ui-state-disabled")}function setUpGameScreen(){choices=[];correct=[];characters=[-1,-1];for(var e=0;e<10;e++){clearChoice(e)}for(var e=1;e<3;e++){clearCharacter(e);drawCharacter([],e)}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 t=correct[0];correct[0]=correct[1];correct[1]=t}if(level.partners!=null){partners=level.generator()}for(var e=0;e<10;e++){$("#chooseCanvas_"+e).removeClass("selected");if(e==correct[0]||e==correct[1]){if(level.partners!=null){console.log("doing partners number "+correct.indexOf(e)+" at position "+e);newperson=partners[correct.indexOf(e)]}else{newperson=level.generator();if(e==correct[1]){while(equalPeople(newperson,choices[correct[0]])){newperson=level.generator()}}}choices.push(newperson);drawChoice(newperson,e)}else{var n;var r=true;while(r){r=false;n=level.randgen();if(level.partners==null){while(level.validator(n)){n=level.randgen()}}choices.forEach(function(e){if(equalPeople(n,e)){r=true}})}choices.push(n);drawChoice(n,e)}}$("#walkingscreen").empty()}function setUpStoryScreen(){if(stories[currentLevel]==null){$("#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{$("#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()}if(levels[currentLevel].setup){setupinfo=levels[currentLevel].setup()}setUpStoryScreen();setUpGameScreen()}function transitionScene(e){switch(currentScene){case scenes.name:protagonist=removeTags($("#username").val());if(protagonist==""){$("#next").removeClass("ui-state-disabled");break}if(getCookie(protagonist)!=""){currentLevel=parseInt(getCookie(protagonist))}$("#namescreen").slideUp(function(){boot_up_level();transitionSceneTo(e);framesDrawer=window.setInterval(draw_frame,1e3/TARGET_FRAMERATE)});break;case scenes.story:$("#storyscreen").slideUp(function(){if(chromeFirstRedraw){for(var t=0;t<10;t++){drawChoice(choices[t],t)}chromeFirstRedraw=false}$("#back").removeClass("ui-state-disabled");transitionSceneTo(e)});break;case scenes.store:$("#gamescreen").slideUp(function(){transitionSceneTo(e)});break;case scenes.hint:$("#hintscreen").slideUp(function(){transitionSceneTo(e)});break;case scenes.failure:$("#failurescreen").slideUp(function(){transitionSceneTo(e)});break;case scenes.success:$("#next").addClass("ui-state-disabled");currentLevel++;$("#successscreen").slideUp(function(){boot_up_level();transitionSceneTo(e)});break;default:console.log("default case hit in from scenes");transitionSceneTo(e);break}}function transitionSceneTo(e){switch(e){case scenes.name:$("#nexttext").text("Let's play!");$("#namescreen").slideDown(function(){$("#next").show()});break;case scenes.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")}if(!hint1timer){hint1timer=setTimeout("showHintButton(1)",15e3)}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:setUpGameScreen();$("#nexttext").text("Go to the costume store");$("#failurescreen").slideDown(function(){$("#next").removeClass("ui-state-disabled")});break;case scenes.success:setCookie(protagonist,currentLevel+1,365);window.clearTimeout(hint1timer);window.clearTimeout(hint2timer);hint1timer=0;hint2timer=0;$("#hint1").hide();$("#hint2").hide();$("#back").addClass("ui-state-disabled");if(currentLevel+1>=levels.length){$("#successscreen").slideDown(function(){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=e}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(e){$("#hint"+e).slideDown()}var tagBody="(?:[^\"'>]|\"[^\"]*\"|'[^']*')*";var tagOrComment=new RegExp("<(?:"+"!--(?:(?:-*[^->])*--+|-?)"+"|script\\b"+tagBody+">[\\s\\S]*?</script\\s*"+"|style\\b"+tagBody+">[\\s\\S]*?</style\\s*"+"|/?[a-z]"+tagBody+")>","gi");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!"];CanvasRenderingContext2D.prototype.clear=CanvasRenderingContext2D.prototype.clear||function(e){if(e){this.save();this.setTransform(1,0,0,1,0,0)}this.clearRect(0,0,this.canvas.width,this.canvas.height);if(e){this.restore()}};var lastlevel=0;var characters=[-1,-1];var choices=[];var correct=[];nextdressed=0;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;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;$(document).ready(function(){WALK_WIDTH=$("#levelscreen").width();$(".chooseCanvas").click(function(){var e=$(this).attr("choice");if($(this).hasClass("selected")){deselectChoice(e)}else{selectChoice(e)}});$(".characterSpan").click(function(){var e=$(this).attr("choice");deselectChoice(characters[e])});$("#next").click(function(){if($(this).hasClass("ui-state-disabled"))return true;$("#next").addClass("ui-state-disabled");$("#heading").slideUp();if(currentScene==scenes.success||currentScene==scenes.name){transitionScene(scenes.story)}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 if(levels[currentLevel].partners&&levels[currentLevel].validator(choices[characters[0]],choices[characters[1]])){transitionScene(scenes.success)}else{transitionScene(scenes.failure)}}else{transitionScene(scenes.store)}});$("#back").click(function(){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)});hint2timer=setTimeout("showHintButton(2)",3e4)});$("#hint2").click(function(){$("#hintscreen").slideUp(function(){$("#hintscreen").html(hints[currentLevel][1])});transitionScene(scenes.hint)});$(window).unload(function(){clearInterval(framesDrawer)});if(levels[currentLevel].setup){setupinfo=levels[currentLevel].setup()}if(protagonist==null){transitionSceneTo(scenes.name)}else{transitionSceneTo(scenes.story);boot_up_level();framesDrawer=window.setInterval(draw_frame,1e3/TARGET_FRAMERATE)}inSelection=true})