Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
CSAL - csal.js

csal.js

Caricato da: ZioCrocifisso
Scarica il programma completo

  1. //Creato da ZioCrocifisso
  2.  
  3. var game_canvas, context, hmax, vmax, container, mouse_rotation = 0, moves;
  4. var rotated_level = -1, sectheight, sectwidth, prev_mouse_rotation, config;
  5. var gradient1, gradient2, gradient_back, remote = false, req, id;
  6. var is_firefox = navigator.userAgent.indexOf("Firefox") != -1, menu;
  7.  
  8. function game_initialize() {}
  9. function game_resize() {}
  10. function game_draw_sector(h, v) {}
  11. function game_rotate(v, amount) {}
  12. function game_reverse(h) {}
  13. function game_reverse_single(h) {}
  14. function game_redraw() {}
  15. function game_generate(hmax, vmax) {}
  16. function game_end() {}
  17.  
  18. function atan2(y, x) {}
  19.  
  20. function game_remote_start(hm, vm, nick) {}
  21. function game_remote_post(data, block) {}
  22. function game_check_end() {}
  23. function game_remote_end(closed) {}
  24. function game_remote_highs() {}
  25.  
  26. function game_start() {
  27.         moves = [];
  28.  
  29.         /*
  30.         if (is_firefox) {
  31.                 atans = [];
  32.  
  33.                 for (var x = 0; x < game_canvas.width; x++) {
  34.                         atans.push([]);
  35.                         for (var y = 0; y < game_canvas.height; y++) {
  36.                                 atans[x].push(atan2(y, x));
  37.                         }
  38.                 }
  39.         }
  40.         */
  41.  
  42.         window.onresize = game_resize;
  43.  
  44.         game_canvas.onmousedown = function(e) {
  45.                 var x = e.clientX - game_canvas.offsetLeft;
  46.                 var y = e.clientY - game_canvas.offsetTop;
  47.  
  48.                 rotated_level = Math.floor(Math.sqrt(
  49.                                         Math.pow(x - game_canvas.width / 2, 2) +
  50.                                         Math.pow(y - game_canvas.height / 2, 2)
  51.                                 ) / sectheight);
  52.  
  53.                 prev_mouse_rotation = 0;
  54.                 game_canvas.onmousemove(e);
  55.                 prev_mouse_rotation = mouse_rotation;
  56.                 mouse_rotation = 0;
  57.  
  58.                 if (rotated_level == 0) {
  59.                         game_reverse(Math.round((prev_mouse_rotation + Math.PI * 2
  60.                                 - sectwidth / 2) / (2 * Math.PI) * hmax) % hmax);
  61.                         rotated_level = -1;
  62.                 }
  63.  
  64.                 game_redraw();
  65.         };
  66.  
  67.         game_canvas.onmousemove = function(e) {
  68.                 var x = e.clientX - game_canvas.offsetLeft;
  69.                 var y = e.clientY - game_canvas.offsetTop;
  70.  
  71.                 if (rotated_level >= 0) {
  72.                         mouse_rotation = -(atan2(
  73.                                                 (game_canvas.height / 2 - y),
  74.                                                 (x - game_canvas.width / 2)
  75.                                         )) - prev_mouse_rotation;
  76.  
  77.                         game_redraw();
  78.                 }
  79.         };
  80.  
  81.         game_canvas.onmouseup = function() {
  82.                 var rotated_levels = Math.round(mouse_rotation / (2 * Math.PI)
  83.                                                 * hmax);
  84.                 if (rotated_level >= 0) {
  85.                         game_rotate(rotated_level, rotated_levels);
  86.                 }
  87.  
  88.                 rotated_level = -1;
  89.                 mouse_rotation = 0;
  90.                 game_redraw();
  91.  
  92.                 game_check_end();
  93.         }
  94.  
  95.         window.onbeforeunload = function() {
  96.                 game_remote_end(true);
  97.                 return null;
  98.         }
  99.  
  100.         game_resize();
  101. }
  102.  
  103. function game_redraw() {
  104.         context.beginPath();
  105.         context.rect(0, 0, game_canvas.width, game_canvas.height);
  106.         context.fillStyle = gradient_back;
  107.         context.fill();
  108.  
  109.         for (var h = 0; h < hmax; h++) {
  110.                 for (var v = 0; v < vmax; v++) {
  111.                         game_draw_sector(h, v);
  112.                 }
  113.         }
  114. }
  115.  
  116. function game_draw_sector(h, v) {
  117.         var addrot = (rotated_level == v) ? mouse_rotation : 0;
  118.         var mul = (config[v][h] == 2) + 1;
  119.  
  120.         if (h > 0) {
  121.                 if (config[v][h - 1] == 2) {
  122.                         return;
  123.                 }
  124.         }
  125.  
  126.         context.beginPath();
  127.  
  128.         if (v > 0) {
  129.                 for (i = 0; i <= 1; i++) {
  130.                         context.arc(
  131.                                 game_canvas.width / 2,
  132.                                 game_canvas.height / 2,
  133.                                 (v + i) * sectheight,
  134.                                 (sectwidth) * (h + i * mul) + addrot,
  135.                                 (sectwidth) * (h + !i * mul) + addrot,
  136.                                 i
  137.                         );
  138.                         context.lineTo(
  139.                                 game_canvas.width / 2 +
  140.                                 sectheight * (v + !i) * Math.cos(sectwidth * (h + !i * mul) + addrot),
  141.                                 game_canvas.height / 2 +
  142.                                 sectheight * (v + !i) * Math.sin(sectwidth * (h + !i * mul) + addrot)
  143.                         );
  144.                 }
  145.         } else {
  146.                 context.moveTo(game_canvas.width / 2, game_canvas.height / 2);
  147.                 context.lineTo(
  148.                         game_canvas.width / 2 +
  149.                         sectheight * Math.cos(sectwidth * h),
  150.                         game_canvas.height / 2 +
  151.                         sectheight * Math.sin(sectwidth * h)
  152.                 );
  153.  
  154.                 context.arc(
  155.                         game_canvas.width / 2,
  156.                         game_canvas.height / 2,
  157.                         sectheight,
  158.                         sectwidth * h,
  159.                         sectwidth * (h + mul)
  160.                 );
  161.                 context.lineTo(game_canvas.width / 2, game_canvas.height / 2);
  162.         }
  163.  
  164.         context.closePath();
  165.         context.lineWidth = 2;
  166.  
  167.         if (mul == 2)
  168.                 h++;
  169.  
  170.         if (config[v][h] == 1)
  171.                 context.fillStyle = gradient1;
  172.         else
  173.                 context.fillStyle = gradient2;
  174.  
  175.         context.fill();
  176.         context.stroke();
  177. }
  178.  
  179. function game_rotate(v, amount) {
  180.         var len = config[v].length;
  181.  
  182.         if (amount < 0) {
  183.                 amount += hmax;
  184.         }
  185.  
  186.         if (config[v][0] == 2 && amount % 2 != 0)
  187.                 return;
  188.  
  189.         config[v] = config[v].slice(
  190.                                         len - amount,
  191.                                         len
  192.                                 ).concat(
  193.                                         config[v].slice(
  194.                                                 0,
  195.                                                 len - amount
  196.                                         )
  197.                                 );
  198.         if (remote) {
  199.                 moves.push({
  200.                                 "type": "rot",
  201.                                 "v": v,
  202.                                 "a": amount
  203.                 });
  204.         }
  205. }
  206.  
  207. function game_reverse(h) {
  208.         var ch = h;
  209.  
  210.         if (config[0][h] == 2) {
  211.                 ch = (h + 1) % hmax;
  212.                 game_reverse_single(ch);
  213.         } else if (h >= 1) {
  214.                 if (config[0][h - 1] == 2) {
  215.                         game_reverse_single(h - 1);
  216.                 }
  217.         }
  218.  
  219.         config[0][ch] = !config[0][ch];
  220.  
  221.         game_reverse_single(h);
  222.  
  223.         if (remote) {
  224.                 moves.push({
  225.                                 "type": "rev",
  226.                                 "h": h
  227.                 });
  228.         }
  229. }
  230.  
  231. function game_reverse_single(h) {
  232.         var ch;
  233.  
  234.         for (var v = 1; v < vmax; v++) {
  235.                 if (config[v][h] == 2)
  236.                         ch = (h + 1) % hmax;
  237.                 else
  238.                         ch = h;
  239.  
  240.                 config[v][ch] = !config[v][ch];
  241.         }
  242. }
  243.  
  244. var _qPI = Math.PI / 4;
  245.  
  246. function atan2(y, x) {
  247.         /*
  248.         if (x < atans.length) {
  249.                 if (y < atans[x].length) {
  250.                         return atans[x][y];
  251.                 }
  252.         }
  253.         */
  254.  
  255.         if (is_firefox) {
  256.                 var ay = Math.abs(y);
  257.                 var xay = x + ay;
  258.                 var angle;
  259.  
  260.                 if (x >= 0) {
  261.                         angle = _qPI - _qPI * ((x - ay) / xay);
  262.                 } else {
  263.                         angle = _qPI * 3 - (xay / (ay - x));
  264.                 }
  265.  
  266.                 if (y < 0) {
  267.                         return -angle;
  268.                 } else {
  269.                         return angle;
  270.                 }
  271.         } else {
  272.                 return Math.atan2(y, x);
  273.         }
  274. }
  275.  
  276. function game_generate(hm, vm) {
  277.         hmax = hm;
  278.         vmax = vm;
  279.         config = [];
  280.  
  281.         for (v = 0; v < vm; v++) {
  282.                 var row = [];
  283.  
  284.                 for (h = 0; h < hm; h++) {
  285.                         if (v == 0 && h % 2 == 0 && hmax % 2 == 0) {
  286.                                 row.push(2);
  287.                         } else {
  288.                                 row.push(1);
  289.                         }
  290.                 }
  291.  
  292.                 config.push(row);
  293.         }
  294.  
  295.         for (var i = 0; i < 200; i++) {
  296.                 if (Math.random() >= 0.4) {
  297.                         game_rotate(Math.floor(Math.random() * (vmax - 1)) + 1,
  298.                                         Math.floor(Math.random() * (hmax - 1)));
  299.                 } else {
  300.                         game_reverse(Math.floor(Math.random() * hmax));
  301.                 }
  302.         }
  303. }
  304.  
  305. function game_remote_post(data, block) {
  306.         var str = "", xhr;
  307.  
  308.         for (key in data) {
  309.                 str += key + "=" + data[key] + "&";
  310.         }
  311.  
  312.         str = str.substr(0, str.length - 1);
  313.  
  314.         xhr = new XMLHttpRequest();
  315.         xhr.open("POST", "csal.php", !block);
  316.         xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  317.         xhr.send(str);
  318.  
  319.         return xhr;
  320. }
  321.  
  322. function game_remote_start(hm, vm, nick) {
  323.         req = game_remote_post({
  324.                                         "action": "start",
  325.                                         "hmax": hm,
  326.                                         "vmax": vm,
  327.                                         "nick": nick
  328.                                 });
  329.  
  330.         hmax = hm;
  331.         vmax = vm;
  332.  
  333.         req.onreadystatechange = function() {
  334.                 var jsonresp;
  335.  
  336.                 if (req.readyState == 4) {
  337.                         var jsonvalid;
  338.                         try {
  339.                                 jsonresp = JSON.parse(req.responseText);
  340.                                 jsonvalid = true;
  341.                         } catch (e) {
  342.                                 jsonvalid = false;
  343.                         }
  344.  
  345.                         if (req.status == 200 && jsonvalid) {
  346.                                 config = jsonresp.config;
  347.                                 id = jsonresp.id;
  348.                                 remote = true;
  349.                                 game_start();
  350.                         } else {
  351.                                 alert("Impossibile creare una partita remota: \n" +
  352.                                         req.responseText + "\nGiocherai localmente.");
  353.                                 remote = false;
  354.                                 game_generate(hmax, vmax);
  355.                                 game_start();
  356.                         }
  357.                 }
  358.         }
  359. }
  360.  
  361. function game_check_end() {
  362.         for (var h = 0; h < hmax; h++) {
  363.                 for (v = 0; v < vmax; v++) {
  364.                         if (config[v][h] == 0) {
  365.                                 return;
  366.                         }
  367.                 }
  368.         }
  369.  
  370.         if (remote)
  371.                 game_remote_end();
  372.         else
  373.                 game_end();
  374. }
  375.  
  376. function game_remote_end(closed) {
  377.         req = game_remote_post({
  378.                                         "action": "end",
  379.                                         "id": id,
  380.                                         "moves": JSON.stringify(moves)
  381.                                 }, closed);
  382.  
  383.         if (!closed) {
  384.                 req.onreadystatechange = function() {
  385.                         if (req.readyState == 4) {
  386.                                 if (req.status == 200) {
  387.                                         alert(req.responseText);
  388.                                 }
  389.                                 game_end();
  390.                         }
  391.                 }
  392.         }
  393. }
  394.  
  395. function game_end() {
  396.         remote = false;
  397.        
  398.         game_canvas.onmouseup = null;
  399.         game_canvas.onmousedown = null;
  400.         game_canvas.onmousemove = null;
  401. }
  402.  
  403. function game_initialize() {
  404.         menu = document.getElementById("menu");
  405.         game_canvas = document.getElementById("game");
  406.         context = game_canvas.getContext("2d");
  407.         container = document.getElementById("container");
  408.  
  409.         if (localStorage.getItem('nickname') != null) {
  410.                 document.getElementById("inick").value = localStorage.getItem('nickname');
  411.         }
  412.  
  413.         document.getElementById("iremote").onclick = function() {
  414.                 if (document.getElementById("iremote").checked) {
  415.                         document.getElementById("inick").disabled = "";
  416.                 } else {
  417.                         document.getElementById("inick").disabled = "disabled";
  418.                 }
  419.         }
  420.  
  421.         document.getElementById("isubmit").onclick = function() {
  422.                 var hm, vm;
  423.  
  424.                 hm = parseInt(document.getElementById("ihmax").value);
  425.                 vm = parseInt(document.getElementById("ivmax").value);
  426.  
  427.                 if (remote) {
  428.                         game_remote_end(true);
  429.                 }
  430.  
  431.        
  432.                 localStorage.setItem('nickname', document.getElementById("inick").value);
  433.  
  434.                 if (document.getElementById("iremote").checked) {
  435.                         game_remote_start(hm, vm, document.getElementById("inick").value);
  436.                 } else {
  437.                         remote = false;
  438.                         game_generate(hm, vm);
  439.                         game_start();
  440.                 }
  441.         }
  442.  
  443.         document.getElementById("ihighs").onclick = function() {
  444.                 var highs = document.getElementById("highs");
  445.        
  446.                 if (highs.style.display == "none") {
  447.                         highs.style.display = "block";
  448.                 } else {
  449.                         highs.style.display = "none";
  450.                 }
  451.  
  452.                 game_remote_highs();
  453.                 game_resize();
  454.         }
  455. }
  456.  
  457. function game_remote_highs() {
  458.         document.getElementById("highs").innerText = "Caricamento...";
  459.         req = game_remote_post({ "action": "high" });
  460.  
  461.         req.onreadystatechange = function () {
  462.                 if (req.readyState == 4) {
  463.                         var text = "";
  464.  
  465.                         if (req.status == 200) {
  466.                                 var highs = JSON.parse(req.responseText);
  467.  
  468.                                 highs.sort(function(a, b) {
  469.                                         return b.score - a.score;
  470.                                 });
  471.  
  472.                                 for (i in highs) {
  473.                                         text += "<p><b>" + (parseInt(i) + 1) + ".</b> " +
  474.                                                 highs[i].nick + " " + highs[i].score + "</p>";
  475.                                 }
  476.                         } else {
  477.                                 text = "Caricamento fallito: " + req.responseText;
  478.                         }
  479.  
  480.                         document.getElementById("highs").innerHTML = text;
  481.                 }
  482.         }
  483. }
  484.  
  485. function game_resize() {
  486.         game_canvas.width = container.getClientRects()[0].width;
  487.         game_canvas.height = container.getClientRects()[0].height
  488.                                 - menu.getClientRects()[0].height;
  489.         sectheight = (Math.min(game_canvas.height, game_canvas.width) / (vmax + 1)) / 2;
  490.         sectwidth = Math.PI * 2 / hmax;
  491.  
  492.         gradient1 = context.createRadialGradient(
  493.                                                 game_canvas.width / 2,
  494.                                                 game_canvas.height / 2,
  495.                                                 0,
  496.                                                 game_canvas.width / 2,
  497.                                                 game_canvas.height / 2,
  498.                                                 sectheight * vmax
  499.                                         );
  500.         gradient1.addColorStop(0, "#55555F");
  501.         gradient1.addColorStop(1, "#222222");
  502.  
  503.         gradient2 = context.createRadialGradient(
  504.                                                 game_canvas.width / 2,
  505.                                                 game_canvas.height / 2,
  506.                                                 0,
  507.                                                 game_canvas.width / 2,
  508.                                                 game_canvas.height / 2,
  509.                                                 sectheight * vmax
  510.                                         );
  511.         gradient2.addColorStop(0, "#FFFFFF");
  512.         gradient2.addColorStop(1, "#AAAAAA");
  513.  
  514.         gradient_back = context.createLinearGradient(
  515.                                                 0, 0, 0,
  516.                                                 game_canvas.height);
  517.         gradient_back.addColorStop(0, "#818181");
  518.         gradient_back.addColorStop(1, "#3F3F3F");
  519.  
  520.         game_redraw();
  521. }