Advertisement
DayDun

GlobeSim

Jun 3rd, 2018
2,826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var ws = new WebSocket("ws://daydun.com:4444");
  2. var id = -1;
  3. var entity;
  4. ws.addEventListener("open", function() {
  5.     console.log("[GlobeSim] WebSocket Open");
  6.     if (localStorage.globesimCode) {
  7.         ws.send(JSON.stringify({
  8.             type: "login",
  9.             code: localStorage.globesimCode
  10.         }));
  11.     } else {
  12.         OWOP.windowSys.addWindow(loginWindow);
  13.     }
  14. });
  15. ws.addEventListener("message", function(event) {
  16.     var msg = JSON.parse(event.data);
  17.     switch(msg.type) {
  18.         case "login":
  19.             if (msg.success) {
  20.                 localStorage.globesimCode = msg.entity.code;
  21.                 id = msg.id;
  22.                 entity = msg.entity;
  23.             } else {
  24.                 OWOP.windowSys.addWindow(loginWindow);
  25.             }
  26.             break;
  27.     }
  28. });
  29. ws.addEventListener("close", function() {
  30.     console.log("[GlobeSim] WebSocket Close");
  31. });
  32.  
  33. function darken(color) {
  34.     let r = color >> 16 & 0xff;
  35.     let g = color >> 8  & 0xff;
  36.     let b = color >> 0  & 0xff;
  37.  
  38.     let darken = 50;
  39.  
  40.     if ((r + g + b) / 3 < 100) {
  41.         darken = -50;
  42.     }
  43.     r = Math.min(Math.max(r - darken, 0), 255);
  44.     g = Math.min(Math.max(g - darken, 0), 255);
  45.     b = Math.min(Math.max(b - darken, 0), 255);
  46.  
  47.     return (r << 16) | (g << 8) | (b << 0);
  48. }
  49.  
  50. function placeLand(x, y, remove) {
  51.     ws.send(JSON.stringify({
  52.         type: "placeLand",
  53.         x: x,
  54.         y: y,
  55.         remove: remove
  56.     }));
  57.     var c = [
  58.         entity.color >> 16 & 0xff,
  59.         entity.color >> 8  & 0xff,
  60.         entity.color >> 0  & 0xff
  61.     ];
  62.     [
  63.         [0, 0],
  64.         [1, 0], [0, 1], [-1, 0], [0, -1]
  65.     ].forEach(function(i) {
  66.         //OWOP.world.setPixel(x + i[0], y + i[1], c);
  67.     });
  68.    
  69. }
  70.  
  71. OWOP.tool.addToolObject(new OWOP.tool.class("Territory", OWOP.cursors.cursor, OWOP.fx.player.RECT_SELECT_ALIGNED(1), OWOP.RANK.USER, function (tool) {
  72.     var lastX, lastY;
  73.     tool.setEvent("mousedown mousemove", function (mouse, event) {
  74.         var remove = mouse.buttons === 2;
  75.         switch (mouse.buttons) {
  76.             case 1:
  77.             case 2:
  78.                 if (!lastX || !lastY) {
  79.                     lastX = mouse.tileX;
  80.                     lastY = mouse.tileY;
  81.                 }
  82.                 OWOP.util.line(lastX, lastY, mouse.tileX, mouse.tileY, 1, function (x, y) {
  83.                     placeLand(x, y, remove);
  84.                 });
  85.                 lastX = mouse.tileX;
  86.                 lastY = mouse.tileY;
  87.                 break;
  88.         }
  89.         return 3;
  90.     });
  91.     tool.setEvent("mouseup", function (mouse) {
  92.         lastX = null;
  93.         lastY = null;
  94.     });
  95. }));
  96.  
  97. var loginWindow = new OWOP.windowSys.class.window("Login", {closeable: true}, function(wdow) {
  98.     wdow.container.style.width = "120px";
  99.     wdow.container.style.textAlign = "center";
  100.     var register = document.createElement("button");
  101.     register.innerHTML = "Register";
  102.     register.style.width = "120px";
  103.     register.style.display = "block";
  104.     register.addEventListener("click", function() {
  105.         wdow.container.removeChild(register);
  106.         wdow.container.removeChild(login);
  107.         var info = document.createElement("span");
  108.         info.style.color = "#fff";
  109.         info.innerHTML = "Choose a color for your land.";
  110.         wdow.container.appendChild(info);
  111.         var color = document.createElement("input");
  112.         color.type = "color";
  113.         color.style.height = "32px";
  114.         color.style.marginTop = "8px";
  115.         color.addEventListener("input", function(event) {
  116.             OWOP.windowSys.delWindow(loginWindow);
  117.             ws.send(JSON.stringify({
  118.                 type: "register",
  119.                 color: parseInt(event.target.value.slice(1), 16)
  120.             }));
  121.         });
  122.         wdow.container.appendChild(color);
  123.     });
  124.     wdow.container.appendChild(register);
  125.     var login = document.createElement("button");
  126.     login.innerHTML = "Login";
  127.     login.style.width = "120px";
  128.     wdow.container.appendChild(login);
  129. }).move(window.innerWidth - 195, 32);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement