Advertisement
Guest User

text adventure js code

a guest
Mar 22nd, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* This function takes a list of DOM elements as well as a
  2.  * localStorage key. It retrieves thee value N from localStorage and
  3.  * hides all but the Nth element from the list of elements. This is
  4.  * used to display different things based on game state.
  5.  */
  6.  
  7. function createSwitch(elements, lskey) {
  8.     if (elements.length < 2) return -1;
  9.     var i, lsval = localStorage.getItem(lskey);
  10.  
  11.     // make sure true/false switches aren't bungled
  12.     if (lsval === false) lsval = 0;
  13.     if (lsval === true) lsval = 1;
  14.  
  15.     for (i = 0; i < elements.length; i++) {
  16.         if ((lsval === null && i == 0) || lsval == i) {
  17.             elements[i].style.display = "";
  18.         } else {
  19.             elements[i].style.display = "none";
  20.         }
  21.     }
  22. }
  23.  
  24. var updateInterval = 5; // ms
  25. function morphColor(c1, c2, time, updatefunc, finish) {
  26.     if (time <= updateInterval) {
  27.         updatefunc(c2);
  28.         return -1;
  29.     }
  30.  
  31.     var r = c1[0], g = c1[1], b = c1[2];
  32.     var R = c2[0], G = c2[1], B = c2[2];
  33.  
  34.     var dr = R-r, dg = G-g, db = B-b;
  35.  
  36.     var steps = time / updateInterval;
  37.  
  38.     var rstep = dr / steps;
  39.     var gstep = dg / steps;
  40.     var bstep = db / steps;
  41.  
  42.     var i = 0;
  43.     var inter = window.setInterval(function() {
  44.         r += rstep;
  45.         g += gstep;
  46.         b += bstep;
  47.  
  48.         updatefunc([parseInt(r,10),parseInt(g,10),parseInt(b,10)]);
  49.  
  50.         if (++i >= steps) {
  51.             clearInterval(inter);
  52.             if (finish) finish();
  53.         }
  54.  
  55.     }, updateInterval);
  56.  
  57.     return inter;
  58. }
  59.  
  60. function setbg(el) {
  61.     return function(c) {
  62.         el.style.backgroundColor = "rgb("+c[0]+","+c[1]+","+c[2]+")";
  63.     };
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement