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
HTML5breakout - editor.js

editor.js

Caricato da: GN
Scarica il programma completo

  1. var w, h, level, bonuses, id;
  2. function editor(i){
  3.         id = i;
  4.         if(localStorage.getItem("lv" + id) == null){
  5.                 var wOK = false; var hOK = false;
  6.                 while (!wOK){
  7.                         w = prompt("Enter width (blocks) (please enter a valid integer between 2 and 50):", "10");
  8.                         if ((!isNaN(w)) && w >1 && w<51){wOK = true;}
  9.                 }
  10.                 while (!hOK){
  11.                         h = prompt("Enter height (blocks) (please enter a valid integer between 2 and 25):", "5");
  12.                         if ((!isNaN(h)) && h>1 && h<26){hOK = true;}
  13.                 }
  14.                 level = new Array(h);
  15.                 bonuses = new Array(h);
  16.                 for(var i = 0; i < h; i++){
  17.                         level[i] = new Array(w);
  18.                         bonuses[i] = new Array(w);
  19.                         for(var j = 0; j < w; j++){
  20.                                 level[i][j] = 1;
  21.                                 bonuses[i][j] = 0;
  22.                         }
  23.                 }
  24.         }else{
  25.                 level = JSON.parse(localStorage.getItem("lv" + i));
  26.                 bonuses = JSON.parse(localStorage.getItem("lv" + i + "b"));
  27.         }
  28.         refresh();
  29. }
  30. function refresh(){
  31.         var xString = '<select id="x1" onchange="javascript:editorSelectionChanged()">';
  32.         for (var x = 0; x < w; x++){
  33.                 xString += '<option value="' + x + '">' + x + '</option>';
  34.         }
  35.         xString += '</select>';
  36.         var yString = '<select id="y1" onchange="javascript:editorSelectionChanged()">';
  37.         for (var y = 0; y < h; y++){
  38.                 yString += '<option value="' + y + '">' + y + '</option>';
  39.         }
  40.         yString += '</select>';
  41.         var brickString = '<select id="brick"><option value="1">Brick 1</option><option value="2">Brick 2</option><option value="3">Brick 3</option><option value="4">Brick 4</option><option value="5">TNT brick</option></select>';
  42.         var bonusString = '<select id="bonus"><option value="0">No bonus</option><option value="1">100 points</option><option value="2">200 points</option><option value="3">500 points</option><option value="4">Bar +</option><option value="5">Bar -</option><option value="6">+1Life</option><option value="7">-1Life</option><option value="8">DoubleScore</option></select>';
  43.         var html = '<br><h1>Level editor: ' + id + '</h1><p><button onclick="javascript:editorSave()">Save and exit</button><button onclick="javascript:menu()">Exit without saving</button></p><p>Select blocks from ' + xString + ',' + yString + ' to ' + xString.replace('x1','x2') + ',' + yString.replace('y1','y2') + ' and change to' + brickString + bonusString + '<button onclick="javascript:editorOK()">Go</button></p><table border="1">';
  44.         for (var i = -1; i < h; i++){
  45.                 html += '<tr>';
  46.                 for (var j = -1; j < w; j++){
  47.                         var s = "";
  48.                         if ((i == -1) && (j != -1)){s = j;}
  49.                         else if ((j == -1) && (i != -1)){s = i;}
  50.                         else if ((i != -1) && (j != -1)){
  51.                                 var assetId = level[i][j];
  52.                                 if(assetId == 5){assetId = "tnt";}
  53.                                 var b = ""
  54.                                 try{b = document.getElementById('bonus').options[bonuses[i][j]].text;}catch(e){b = "No bonus";}
  55.                                 s = '<img id="' + i + '_' + j + '" src="assets/graphics/sprites/brick/' + assetId + '.png" />' + b;
  56.                         }
  57.                         html += '<td>' + s + '</td>';
  58.                 }
  59.                 html += '</tr>';
  60.         }
  61.         html += '</table>';
  62.         document.getElementById("content").innerHTML = html;
  63. }
  64. function editorSelectionChanged(){
  65.         var x1 = document.getElementById('x1').value;
  66.         var x2 = document.getElementById('x2').value;
  67.         var y1 = document.getElementById('y1').value;
  68.         var y2 = document.getElementById('y2').value;
  69.         for(var i = 0; i < h; i++){
  70.                 for(var j = 0; j < w; j++){
  71.                         if ((i >= y1) && (i <= y2) && (j >= x1) && (j <= x2)){document.getElementById(i + '_' + j).setAttribute('class', 'editorSelected');}
  72.                         else {document.getElementById(i + '_' + j).setAttribute('class', '');}
  73.                 }
  74.         }
  75. }
  76. function editorOK(){
  77.         var x1 = document.getElementById('x1').value;
  78.         var x2 = document.getElementById('x2').value;
  79.         var y1 = document.getElementById('y1').value;
  80.         var y2 = document.getElementById('y2').value;
  81.         var brick = document.getElementById('brick').value;
  82.         var bonus = document.getElementById('bonus').value;
  83.         for(var i = 0; i < h; i++){
  84.                 for(var j = 0; j < w; j++){
  85.                         if ((i >= y1) && (i <= y2) && (j >= x1) && (j <= x2)){
  86.                                 level[i][j] = brick;
  87.                                 bonuses[i][j] = bonus;
  88.                         }
  89.                 }
  90.         }
  91.         refresh();
  92. }
  93. function editorSave(){
  94.         localStorage.setItem("lv" + id, JSON.stringify(level));
  95.         localStorage.setItem("lv" + id + "b", JSON.stringify(bonuses));
  96.         menu();
  97. }