Advertisement
DayDun

Road tool

Jul 31st, 2018
3,414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. OWOP.tool.addToolObject(new OWOP.tool.class("Road", OWOP.cursors.paste, OWOP.fx.player.NONE, OWOP.RANK.ADMIN, function (tool) {
  2.     var canvas = document.createElement("canvas");
  3.     canvas.width = canvas.height = 0;
  4.    
  5.     var xPos = null;
  6.     var yPos = null;
  7.     var progress = 0;
  8.    
  9.     var width = 1;
  10.     var height = 1;
  11.     var speed = 30;
  12.    
  13.     var optWin = new OWOP.windowSys.class.window("Road Tool", {}, function(wdow) {
  14.         var style = document.createElement("style");
  15.         style.innerHTML = "label {color:#fff;display:block;}";
  16.         wdow.container.appendChild(style);
  17.        
  18.         var widthInput = document.createElement("input");
  19.         widthInput.id = "road-width";
  20.         var heightInput = document.createElement("input");
  21.         heightInput.id = "road-height";
  22.         var speedInput = document.createElement("input");
  23.         speedInput.id = "road-speed";
  24.         widthInput.type = heightInput.type = speedInput.type = "number";
  25.         widthInput.addEventListener("input", function() {
  26.             width = parseInt(this.value) || 0;
  27.         });
  28.         heightInput.addEventListener("input", function() {
  29.             height = parseInt(this.value) || 0;
  30.         });
  31.         speedInput.addEventListener("input", function() {
  32.             speed = parseFloat(this.value) || 0;
  33.         });
  34.        
  35.         var widthLabel = document.createElement("label");
  36.         var heightLabel = document.createElement("label");
  37.         var speedLabel = document.createElement("label");
  38.         widthLabel.for = widthInput.id;
  39.         heightLabel.for = heightInput.id;
  40.         speedLabel.for = speedInput.id;
  41.         widthLabel.innerHTML = "Width: ";
  42.         heightLabel.innerHTML = "Height: ";
  43.         speedLabel.innerHTML = "Speed: ";
  44.         widthLabel.appendChild(widthInput);
  45.         heightLabel.appendChild(heightInput);
  46.         speedLabel.appendChild(speedInput);
  47.        
  48.         wdow.container.appendChild(widthLabel);
  49.         wdow.container.appendChild(heightLabel);
  50.         wdow.container.appendChild(speedLabel);
  51.        
  52.         var button = document.createElement("button");
  53.         button.innerHTML = "Lay";
  54.         var running = false;
  55.         button.addEventListener("click", function() {
  56.             if (xPos === null || yPos === null) {
  57.                 return;
  58.             }
  59.            
  60.             if (!running) {
  61.                 tool.setEvent("tick", tick);
  62.                 button.innerHTML = "Cancel";
  63.             } else {
  64.                 tool.setEvent("tick", null);
  65.                 button.innerHTML = "Lay";
  66.                 progress = 0;
  67.                 ticks = 0;
  68.             }
  69.             running = !running;
  70.         });
  71.         wdow.container.appendChild(button);
  72.     }).move(window.innerWidth - 240, 32);
  73.    
  74.     tool.setFxRenderer(function (fx, ctx, time) {
  75.         if (!fx.extra.isLocalPlayer || !(canvas.width && canvas.height)) {
  76.             return;
  77.         }
  78.        
  79.         var z = OWOP.camera.zoom;
  80.         var x = xPos !== null ? xPos : Math.floor(fx.extra.player.tileX / 16);
  81.         var y = yPos !== null ? yPos : Math.floor(fx.extra.player.tileY / 16);
  82.         var fxx = x * 16 - OWOP.camera.x;
  83.         var fxy = y * 16 - OWOP.camera.y;
  84.        
  85.         var pattern = ctx.createPattern(canvas, "repeat");
  86.        
  87.         ctx.globalAlpha = 0.5 + Math.sin(time / 500) / 4;
  88.         ctx.strokeStyle = "#000000";
  89.         ctx.scale(z, z);
  90.         ctx.rect(fxx, fxy, canvas.width * width, canvas.height * height);
  91.         ctx.fillStyle = pattern;
  92.         ctx.fill();
  93.         ctx.scale(1 / z, 1 / z);
  94.         ctx.globalAlpha = 0.8;
  95.         ctx.strokeRect(fxx * z, fxy * z, canvas.width * width * z, canvas.height * height * z);
  96.         return 0;
  97.     });
  98.    
  99.     var ticks = 0;
  100.     function tick() {
  101.         ticks += speed / 30;
  102.        
  103.         while (ticks >= 1) {
  104.             ticks--;
  105.            
  106.             var chunkX = Math.floor(progress / (canvas.height / 16 * height));
  107.             var chunkY = (progress % (canvas.height / 16 * height));
  108.            
  109.             var imgX = chunkX % (canvas.width / 16);
  110.             var imgY = chunkY % (canvas.height / 16);
  111.            
  112.             var data = new Uint32Array(16 * 16);
  113.             var imgData = canvas.getContext("2d").getImageData(imgX * 16, imgY * 16, 16, 16).data;
  114.             for (var i=0, j=0; i<16*16; i++, j+=4) {
  115.                 data[i] = imgData[j + 2] << 16 | imgData[j + 1] << 8 | imgData[j];
  116.             }
  117.            
  118.             OWOP.net.protocol.setChunk(xPos + chunkX, yPos + chunkY, data);
  119.            
  120.             progress++;
  121.            
  122.             if (progress == (canvas.height / 16 * height * canvas.width / 16 * width)) {
  123.                 ticks = 0;
  124.                 progress = 0;
  125.                 tool.setEvent("tick", null);
  126.             }
  127.         }
  128.     }
  129.  
  130.     tool.setEvent("mousedown mousemove", function (mouse) {
  131.         if (mouse.buttons & 3 && progress === 0) {
  132.             xPos = Math.floor(mouse.tileX / 16);
  133.             yPos = Math.floor(mouse.tileY / 16);
  134.         }
  135.     });
  136.  
  137.     tool.setEvent("select", function() {
  138.         OWOP.windowSys.addWindow(optWin);
  139.        
  140.         var input = document.createElement("input");
  141.         input.type = "file";
  142.         input.accept = "image/*";
  143.         input.addEventListener("change", function(event) {
  144.             if (!input.files || !input.files[0]) {
  145.                 return;
  146.             }
  147.            
  148.             var reader = new FileReader();
  149.             reader.addEventListener("load", function(e) {
  150.                 var image = new Image();
  151.                 image.addEventListener("load", function() {
  152.                     if (canvas.width % 16 || canvas.width % 16) {
  153.                         throw "Image size needs to be multiples of 16";
  154.                     }
  155.                    
  156.                     canvas.width = image.width;
  157.                     canvas.height = image.height;
  158.                     canvas.getContext("2d").drawImage(image, 0, 0);
  159.                     console.log("Loaded image");
  160.                 });
  161.                 image.src = reader.result;
  162.             });
  163.             reader.readAsDataURL(input.files[0]);
  164.         });
  165.         input.click();
  166.     });
  167.    
  168.     tool.setEvent("deselect", function() {
  169.         OWOP.windowSys.delWindow(optWin);
  170.     });
  171. }));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement