colorful rat Ratfactor.com > Dave's Repos

hoot

Silly HTML game building engine
git clone http://ratfactor.com/repos/hoot/hoot.git

hoot/hoot.js

Download raw file: hoot.js

1 2 3 /* 4 5 TODO: runtime needs for the "location" var to mean the current location 6 so that the engine can always display a clickable with the location name 7 as a clickable title in the game display 8 9 ALSO: allow location to be set to something invalid (like NOWHERE) which 10 will not display the location at all 11 12 13 */ 14 15 16 17 function rungame(tree, outputarea){ 18 var gamevars = []; 19 var upper = false; 20 21 // the first element in the tree will be array of the rest of the tree 22 tree = tree[0]; 23 24 outputarea.empty(); 25 runfunc('start'); 26 27 function runfunc(funcname){ 28 thefunc = findfunc(funcname); 29 if(!thefunc){ alert("Sorry, your sorry can't continue because there is no \""+funcname+"\" thing defined."); return; } 30 // console.log("running function "+thefunc[1][1]); 31 expressions(thefunc, 2); 32 } 33 34 function findfunc(funcname){ 35 for(s in tree){ 36 if(tree[s][1][1] == funcname){ 37 return tree[s]; 38 } 39 } 40 return null; 41 } 42 43 function expression(expr){ 44 switch(expr[0]){ 45 case 'assign': 46 // console.log("assigning "+expr[1][1]+" to "+expr[2][1]); 47 gamevars[expr[1][1]] = expr[2][1]; 48 49 for(v in gamevars){ 50 // console.log(gamevars[v]); 51 } 52 53 break; 54 case 'print': print(expr); break; 55 case 'ifseq': ifsequence(expr); break; 56 case 'runme': runfunc(expr[1][1]); break; 57 case 'decr': gamevars[expr[1][1]]--; break; 58 case 'incr': gamevars[expr[1][1]]++; break; 59 case 'elseseq': break; // do nothing, this will run if needed 60 default: alert("Sorry, '"+expr[0]+"' not (yet) supported!"); 61 } 62 } 63 64 function expressions(list, startpos){ 65 // run multiple expressions from a list, starting from startpos 66 for(var e=startpos; e<list.length; e++){ 67 expression(list[e]); 68 } 69 } 70 71 function ifsequence(tree){ 72 var val1 = tree[1][1]; 73 var test_opr = tree[2][1]; 74 var val2 = tree[3][1]; 75 76 // console.log("Test: "+val1+" "+test_opr+" "+val2); 77 78 if(tree[1][0] == "name"){ val1=gamevars[val1]; } 79 if(tree[3][0] == "name"){ val2=gamevars[val2]; } 80 81 // console.log("translates to: "+val1+" "+test_opr+" "+val2); 82 83 84 if( (test_opr == "equals" && val1 == val2) || 85 (test_opr == "less than" && val1 < val2) || 86 (test_opr == "greater than" && val1 > val2) || 87 (test_opr == "doesn't equal" && val1 != val2) ){ 88 // console.log("true"); 89 expressions(tree,4); 90 } 91 else{ 92 // console.log("false"); 93 // see if there's an else to run 94 for(e in tree){ 95 if(tree[e][0] == "elseseq"){ 96 expressions(tree[e],1); 97 } 98 } 99 } 100 } 101 102 function print(printables){ 103 for(var p = 1; p<printables.length; p++){ 104 type = printables[p][0]; // the type of printable item 105 body = printables[p][1]; // the body of the printable 106 // console.log("printing "+type+": "+body); 107 switch(type){ 108 case 'string' : printstr(body); break; 109 case 'strname' : printname(body); break; 110 case 'upname' : upper = true; printname(body); break; 111 case 'link' : printlink(body); break; 112 case 'break' : printbreak(); break; 113 default: alert("Sorry, printing '"+type+"' not (yet) supported."); 114 } 115 } 116 } 117 118 function printname(name){ 119 if(name in gamevars){ 120 printstr(gamevars[name]); 121 return; 122 } 123 if(findfunc(name)){ 124 runfunc(name); 125 return; 126 } 127 alert("Sorry, couldn't find anything called '"+name+"' to display."); 128 } 129 130 function printstr(str){ 131 str = String(str); 132 if(upper){ 133 upper = false; // we've done it, don't do it again until requested 134 printstr(str.charAt(0).toUpperCase()+str.slice(1)); 135 return; 136 } 137 outputarea.append("<span>"+str+"</span>"); 138 } 139 140 function printlink(funcname){ 141 outputarea.append($("<a href=\"#\">"+body+"</a>").click(function(){ 142 outputarea.empty(); 143 runfunc(funcname); 144 return false; 145 })); 146 } 147 148 function printbreak(){ 149 outputarea.append("<hr>"); 150 } 151 }