Advertisement
vnx5

zM render process

May 20th, 2019
7,886
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // zursor master render functions
  2. // updated feb 25, 2020
  3.  
  4. zM.dependenciesLoaded |= 0b001000;
  5. let ctx;
  6.  
  7. zM.reqLoad = function() {
  8.     zM.r = {
  9.         fps: 0,
  10.         hudText: function(text, x, y, subLength = 0, color = '#fff') {
  11.             ctx.font = '12px "Nova Flat"';
  12.             ctx.globalAlpha = .5;
  13.             ctx.lineWidth = 3;
  14.             ctx.strokeStyle = '#000';
  15.             ctx.strokeText(text, x-ctx.measureText(text).width*subLength, y);
  16.  
  17.             ctx.globalAlpha = 1;
  18.             ctx.fillStyle = color;
  19.             ctx.fillText(text, x-ctx.measureText(text).width*subLength, y);
  20.         },
  21.         infLvl: 5
  22.     }
  23.     ctx = zM.el.game.getContext('2d');
  24. }
  25.  
  26. zM.reqDo = function() {
  27.     ctx.clearRect(0, 0, 800, 600);
  28.  
  29.     if (isNaN(zM.fSel)) {
  30.         ctx.font = '60px "Nova Flat"';
  31.         ctx.fillStyle = '#000';
  32.         var t = 'No fleet selected';
  33.         ctx.fillText(t, 400 - ctx.measureText(t).width/2, 330);
  34.     } else if (!zM.fleet[zM.fSel]) {
  35.         ctx.font = '60px "Nova Flat"';
  36.         ctx.fillStyle = '#000';
  37.         var t = 'Undefined fleet selected';
  38.         ctx.fillText(t, 400 - ctx.measureText(t).width/2, 330);
  39.     } else if (!zM.fleet[zM.fSel].bots[zM.fleet[zM.fSel].cSel]) { // you can get this if the bot no longer exists (or an invalid bot selected state)
  40.         ctx.font = '60px "Nova Flat"';
  41.         ctx.fillStyle = '#000';
  42.         var t = 'Lost connection to server';
  43.         ctx.fillText(t, 400 - ctx.measureText(t).width/2, 330);
  44.     } else {
  45.         var b = zM.fleet[zM.fSel].bots[zM.fleet[zM.fSel].cSel];
  46.  
  47.         if (b.socket.readyState == WebSocket.CONNECTING) {
  48.             ctx.font = '60px "Nova Flat"';
  49.             ctx.fillStyle = '#000';
  50.             var t = 'Connecting';
  51.             ctx.fillText(t, 400 - ctx.measureText(t).width/2, 330);
  52.         } else { // we don't need to handle closed states, the bot will be gone
  53.             for (var i = 0; i < b.obj.length; ++i) {
  54.                 var o = b.obj[i];
  55.                 switch (o.type) {
  56.                     case 0:
  57.                         ctx.globalAlpha = 1;
  58.                         ctx.font = o.size + 'px "Nova Flat"';
  59.                         ctx.fillStyle = '#000';
  60.                         var x = o.x*2;
  61.                         if (o.isCentered) x -= ctx.measureText(o.content).width/2;
  62.                         ctx.fillText(o.content, x, o.y*2);
  63.                         break;
  64.                     case 1:
  65.                         ctx.globalAlpha = 1;
  66.                         ctx.fillStyle = o.color;
  67.                         ctx.fillRect(o.x*2, o.y*2, o.w*2, o.h*2);
  68.                         ctx.globalAlpha = .2;
  69.                         ctx.strokeStyle = '#000';
  70.                         ctx.lineWidth = 1;
  71.                         ctx.strokeRect(o.x*2+1, o.y*2+1, o.w*2-2, o.h*2-2);
  72.                         break;
  73.                     case 2:
  74.                         ctx.globalAlpha = .2;
  75.                         ctx.fillStyle = o.isBad?'#f00':'#0f0';
  76.                         ctx.fillRect(o.x*2, o.y*2, o.w*2, o.h*2);
  77.                         break;
  78.                     case 3:
  79.                         ctx.globalAlpha = .2;
  80.                         ctx.fillStyle = o.color;
  81.                         ctx.fillRect(o.x*2, o.y*2, o.w*2, o.h*2);
  82.                         ctx.font = '24px "Nova Flat"';
  83.                         ctx.fillStyle = '#000';
  84.                         ctx.globalAlpha = .5;
  85.                         ctx.fillText(o.count, o.x*2 + o.w - ctx.measureText(o.count).width/2, o.y*2+o.h+9);
  86.                         break;
  87.                     case 4:
  88.                         ctx.globalAlpha = 1;
  89.                         ctx.fillStyle = o.color;
  90.                         ctx.fillRect(o.x*2, o.y*2, o.w*2, o.h*2);
  91.  
  92.                         ctx.globalAlpha = .2;
  93.                         ctx.fillStyle = '#000';
  94.                         ctx.fillRect(o.x*2, o.y*2, o.w*2, o.h*2);
  95.  
  96.                         ctx.globalAlpha = 1;
  97.                         ctx.fillStyle = o.color;
  98.                         ctx.fillRect(o.x*2+8, o.y*2+8, o.w*2-16, o.h*2-16);
  99.  
  100.                         var l = Date.now() - o.lastClickAt > 150 ? 8 : 16;
  101.  
  102.                         ctx.globalAlpha = .2;
  103.                         ctx.lineWidth = 1;
  104.                         ctx.strokeStyle = '#000';
  105.                         ctx.beginPath(),
  106.  
  107.                         ctx.moveTo(o.x*2+1, o.y*2+1),
  108.                         ctx.lineTo(o.x*2+o.w*2-2, o.y*2+1),
  109.                         ctx.lineTo(o.x*2+o.w*2-2, o.y*2+o.h*2-2),
  110.                         ctx.lineTo(o.x*2+1, o.y*2+o.h*2-2),
  111.                         ctx.lineTo(o.x*2+1, o.y*2+1);
  112.  
  113.                         ctx.lineTo(o.x*2+1+l, o.y*2+1+l);
  114.  
  115.                         ctx.moveTo(o.x*2+o.w*2-2, o.y*2+1),
  116.                         ctx.lineTo(o.x*2+o.w*2-2-l, o.y*2+1+l);
  117.  
  118.                         ctx.moveTo(o.x*2+o.w*2-2, o.y*2+o.h*2-2);
  119.                         ctx.lineTo(o.x*2+o.w*2-2-l, o.y*2+o.h*2-2-l);
  120.  
  121.                         ctx.moveTo(o.x*2+1, o.y*2+o.h*2-2),
  122.                         ctx.lineTo(o.x*2+1+l, o.y*2+o.h*2-2-l);
  123.  
  124.                         ctx.moveTo(o.x*2+l, o.y*2+l),
  125.                         ctx.lineTo(o.x*2+o.w*2-l, o.y*2+l),
  126.                         ctx.lineTo(o.x*2+o.w*2-l, o.y*2+o.h*2-l),
  127.                         ctx.lineTo(o.x*2+l, o.y*2+o.h*2-l),
  128.                         ctx.lineTo(o.x*2+l, o.y*2+l);
  129.  
  130.                         ctx.stroke();
  131.  
  132.                         ctx.fillStyle = '#000';
  133.                         ctx.globalAlpha = .5;
  134.                         ctx.font = '24px "Nova Flat"';
  135.                         ctx.fillText(o.count, o.x*2 + o.w - ctx.measureText(o.count).width/2, o.y*2+o.h+9);
  136.                 }
  137.             }
  138.  
  139.             ctx.strokeStyle = '#000';
  140.             ctx.lineWidth = 1.5;
  141.             for (var i = 0; i < b.clicks.length; ++i) {
  142.                 if (typeof b.clicks[i] === 'object') {
  143.                     if (Date.now() - b.clicks[i].time > 500) b.clicks.splice(i--, 1);
  144.                     else {
  145.                         ctx.beginPath(),
  146.                         ctx.globalAlpha = .5 - (Date.now() - b.clicks[i].time)/1000;
  147.                         var radius = (Date.now() - b.clicks[i].time)/20;
  148.                         if (radius < 0.1) radius = 0.1 // Anti-crash failsafe
  149.                         ctx.arc(b.clicks[i].x*2, b.clicks[i].y*2, radius, 0, 2*Math.PI);
  150.                         ctx.stroke();
  151.                     }
  152.                 }
  153.             }
  154.  
  155.             ctx.strokeStyle = '#000';
  156.             ctx.lineWidth = 1;
  157.             ctx.globalAlpha = .3;
  158.             for (var i = 0; i < b.drawings.length; ++i) {
  159.                 if (typeof b.drawings[i] === 'object') {
  160.                     if (Date.now() - b.drawings[i].time > 10000) b.drawings.splice(i--, 1);
  161.                     else {
  162.                         ctx.beginPath(),
  163.                         ctx.moveTo(b.drawings[i].x*2, b.drawings[i].y*2);
  164.                         ctx.lineTo(b.drawings[i].x2*2, b.drawings[i].y2*2);
  165.                         ctx.stroke();
  166.                     }
  167.                 }
  168.             }
  169.  
  170.             ctx.globalAlpha = 1;
  171.             ctx.font = '12px "Nova Flat"';
  172.             for (var i = 0; i < b.players.length; ++i) {
  173.                 var img;
  174.                 if (zM.fleet[zM.fSel].ids.indexOf(b.players[i].id) != -1) {
  175.                     for (var j = 0; i < zM.fleet[zM.fSel].bots.length; ++j) {
  176.                         if (zM.fleet[zM.fSel].bots[j]) if (zM.fleet[zM.fSel].bots[j].id == b.players[i].id) break;
  177.                     }
  178.                     if (j < zM.fleet[zM.fSel].bots.length) {
  179.                         if (zM.fleet[zM.fSel].bots[j].helping || zM.fleet[zM.fSel].bots[j].deployed) img = zMIMG.movement.img;
  180.                         else img = zMIMG.local.img;
  181.                     } else img = zMIMG.local.img;
  182.                 } else img = zMIMG.cursor.img;
  183.                 var x = zM.ease(b.players[i].x, b.players[i].ox, b.players[i].lastUpdate)*2;
  184.                 var y = zM.ease(b.players[i].y, b.players[i].oy, b.players[i].lastUpdate)*2;
  185.                 ctx.drawImage(img, x-4, y-5);
  186.                 x += 5;
  187.                 y-= 2;
  188.                 if (x + ctx.measureText(`${b.players[i].id}`).width > 790) x = 790 - ctx.measureText(`${b.players[i].id}`).width;
  189.                 if (y - 12 < 10) y = 22;
  190.                 zM.r.hudText(`${b.players[i].id}`, x, y);
  191.             }
  192.  
  193.             switch (zM.r.infLvl) {
  194.                 case 5:
  195.                     zM.r.hudText(`Received (tp/ps) ${b.packets.receivedTotal} / ${b.packets.receivedPS}`, 790, 22, 1);
  196.                     zM.r.hudText(`Sent (tp/ps) ${b.packets.sentTotal} / ${b.packets.sentPS}`, 790, 36, 1);
  197.                 case 4:
  198.                 case 3:
  199.                 case 2:
  200.                     zM.r.hudText(`Level ${b.level}`, 10, 36);
  201.                 case 1:
  202.                     zM.r.hudText(
  203.                         b.local < 30 ? 'Use shift+click to draw ('+b.local+' > 0)' :
  204.                         b.local < 100 ? 'Too many cursors, drawing is disabled ('+b.local+' > 30)' :
  205.                         b.local < 1010 ? 'Too many cursors, not all cursors are shown ('+b.local+' > 100)' :
  206.                         'Use shift+click to draw ('+b.local+' > 1010)'
  207.                     , 10, 590);
  208.  
  209.                     zM.r.hudText(`${b.online} players online`, 790, 590, 1);
  210.                 case 0:
  211.             }
  212.         }
  213.     }
  214.  
  215.     if (zM.r.infLvl >= 2) zM.r.hudText('FPS ' + zM.r.fps,10,22);
  216.  
  217.     ctx.fillStyle = zM.movement?'#ff0':'#f00';
  218.     ctx.globalAlpha = 0.3;
  219.     ctx.beginPath();
  220.     ctx.moveTo(zM.pos.mouseX+3, zM.pos.mouseY+6);
  221.     ctx.arc(zM.pos.mouseX+3, zM.pos.mouseY+6, 20, 0, 2*Math.PI);
  222.     ctx.fill();
  223.  
  224.     ctx.globalAlpha = 1;
  225.     ctx.drawImage(zMIMG.cursor.img, zM.pos.mouseX-4, zM.pos.mouseY-5);
  226.  
  227.  
  228.     zM.reqAfter();
  229. }
  230.  
  231. zM.reqAfter = function() {
  232.     ++zM.r.fps;
  233.     requestAnimationFrame(zM.reqDo);
  234.     setTimeout(()=>{--zM.r.fps},1000);
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement