Bloble.io NoobScript V3 GameUtils Fragment

A fragment of code from NoobScript V3 - The FPS and resolution setter.

目前為 2018-01-02 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Bloble.io NoobScript V3 GameUtils Fragment
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description A fragment of code from NoobScript V3 - The FPS and resolution setter.
  6. // @author NoobishHacker
  7. // @match http://bloble.io/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. window.UIList = window.UIList || [];
  12. window.initFuncs = window.initFuncs || [];
  13. window.statusItems = window.statusItems || [];
  14. // Resolution and FPS chooser.
  15. var resolution = 1;
  16. var rate = 0;
  17.  
  18. window.removeEventListener("mousemove", gameInput);
  19.  
  20. window.gameInput = function (a) {
  21. a.preventDefault();
  22. a.stopPropagation();
  23. mouseX = a.clientX * resolution;
  24. mouseY = a.clientY * resolution;
  25. }
  26. window.addEventListener("mousemove", gameInput, false);
  27. window.removeEventListener("resize", resize);
  28. window.resize = function (n) {
  29. screenWidth = window.innerWidth * resolution;
  30. screenHeight = window.innerHeight * resolution;
  31. scaleFillNative = MathMAX(screenWidth / maxScreenWidth, screenHeight / maxScreenHeight);
  32. if (n !== true) {
  33. mainCanvas.width = screenWidth;
  34. mainCanvas.height = screenHeight;
  35. mainCanvas.style.width = (screenWidth / resolution) + "px";
  36. mainCanvas.style.height = (screenHeight / resolution) + "px";
  37. }
  38.  
  39. mainContext.setTransform(scaleFillNative, 0, 0, scaleFillNative, Math.floor((screenWidth - maxScreenWidth * scaleFillNative) / 2), Math.floor((screenHeight - maxScreenHeight * scaleFillNative) / 2));
  40. player || renderBackground()
  41. }
  42. window.addEventListener("resize", resize);
  43. window.statusItems.push({
  44. order: 3,
  45. value: function () {
  46. return document.getElementById('fps').textContent;
  47. }
  48. }, {
  49. order: 4,
  50. name: 'Resolution',
  51. value: function () {
  52. return document.getElementById('res').textContent;
  53. }
  54. });
  55. window.UIList.push({
  56. level: 3,
  57. x: 4,
  58. html: '<div id="res" onclick=setRes()>Normal Res</div>'
  59. }, {
  60. level: 3,
  61. x: 5,
  62. html: '<div id="fps" onclick=setFPS()>Unlim FPS</div>'
  63. })
  64. window.setRes = function () {
  65. var el = document.getElementById('res');
  66. if (resolution === 2) {
  67. resolution = .5;
  68. el.textContent = 'Low Res';
  69. } else if (resolution === .5) {
  70. resolution = .8;
  71. el.textContent = 'Med Res';
  72. } else if (resolution === .8) {
  73. resolution = 1;
  74. el.textContent = 'Normal Res';
  75. } else if (resolution === 1) {
  76. resolution = 1.5;
  77. el.textContent = 'High Res';
  78. } else {
  79. resolution = 2;
  80. el.textContent = 'Retina Res';
  81. }
  82. unitSprites = {};
  83. resize();
  84. window.statusBar();
  85. }
  86. window.setFPS = function () {
  87. var el = document.getElementById('fps');
  88. if (rate === 0) {
  89. el.textContent = '45 FPS'
  90. rate = 22;
  91. } else if (rate === 22) {
  92. el.textContent = '40 FPS'
  93. rate = 25;
  94. } else if (rate === 25) {
  95. el.textContent = '35 FPS'
  96. rate = 28;
  97. } else if (rate === 28) {
  98. el.textContent = '30 FPS';
  99. rate = 33;
  100. } else if (rate === 33) {
  101. el.textContent = '25 FPS';
  102. rate = 40;
  103. } else if (rate === 40) {
  104. el.textContent = '20 FPS';
  105. rate = 50;
  106. } else {
  107. el.textContent = 'Unlim FPS';
  108. rate = 0;
  109. }
  110. window.statusBar();
  111. }
  112.  
  113. window.callUpdate = function () {
  114. requestAnimFrame(callUpdate);
  115. currentTime = window.performance.now();
  116. var a = currentTime - then;
  117. if (a >= rate) {
  118. then = currentTime;
  119. updateGameLoop(a);
  120. }
  121. updateMenuLoop(a)
  122. }
  123.  
  124. window.makeUI = function () {
  125. if (window.hasMadeUI) return;
  126. window.hasMadeUI = true;
  127. window.statusItems.sort(function (a, b) {
  128. return a.order - b.order;
  129. })
  130. var levels = [];
  131. window.UIList.forEach((item) => {
  132. if (!levels[item.level]) levels[item.level] = [];
  133. levels[item.level].push(item)
  134. })
  135.  
  136. levels = levels.filter((a) => {
  137. if (a) {
  138. a.sort(function (a, b) {
  139. return a.x - b.x;
  140. })
  141. return true;
  142. } else {
  143. return false;
  144. }
  145. })
  146.  
  147. var headAppend = document.getElementsByTagName("head")[0],
  148. style = document.createElement("div");
  149.  
  150. var toast = document.createElement('div');
  151. toast.id = "snackbar";
  152. var css = document.createElement('div');
  153.  
  154. css.innerHTML = '<style>\n\
  155. #snackbar {\n\
  156. visibility: hidden;\n\
  157. min-width: 250px;\n\
  158. margin-left: -125px;\n\
  159. background-color: rgba(40, 40, 40, 0.5);\n\
  160. color: #fff;\n\
  161. text-align: center;\n\
  162. border-radius: 4px;\n\
  163. padding: 10px;\n\
  164. font-family: "regularF";\n\
  165. font-size: 20px;\n\
  166. position: fixed;\n\
  167. z-index: 100;\n\
  168. left: 50%;\n\
  169. top: 30px;\n\
  170. }\n\
  171. #snackbar.show {\n\
  172. visibility: visible;\n\
  173. -webkit-animation: fadein 0.5s;\n\
  174. animation: fadein 0.5s;\n\
  175. }\n\
  176. #snackbar.hide {\n\
  177. visibility: visible;\n\
  178. -webkit-animation: fadeout 0.5s;\n\
  179. animation: fadeout 0.5s;\n\
  180. }\n\
  181. @-webkit-keyframes fadein {\n\
  182. from {top: 0; opacity: 0;}\n\
  183. to {top: 30px; opacity: 1;}\n\
  184. }\n\
  185. @keyframes fadein {\n\
  186. from {top: 0; opacity: 0;}\n\
  187. to {top: 30px; opacity: 1;}\n\
  188. }\n\
  189. @-webkit-keyframes fadeout {\n\
  190. from {top: 30px; opacity: 1;}\n\
  191. to {top: 0; opacity: 0;}\n\
  192. }\n\
  193. @keyframes fadeout {\n\
  194. from {top: 30px; opacity: 1;}\n\
  195. to {top: 0; opacity: 0;}\n\
  196. }\n\
  197. </style>'
  198. var height = levels.length * (14 + 19) + (levels.length - 1) * 7 + 15;
  199. style.innerHTML = "<style>\n\
  200. #noobscriptUI, #noobscriptUI > div > div {\n\
  201. background-color:rgba(40,40,40,.5);\n\
  202. margin-left: 3px;\n\
  203. border-radius:4px;\n\
  204. pointer-events:all\n\
  205. }\n\
  206. #noobscriptUI {\n\
  207. top: -" + (height + 12) + "px;\n\
  208. transition: 1s;\n\
  209. margin-left:10px;\n\
  210. position:absolute;\n\
  211. padding-left:24px;\n\
  212. margin-top:9px;\n\
  213. padding-top:15px;\n\
  214. width:580px;\n\
  215. height: " + height + "px;\n\
  216. font-family:arial;\n\
  217. left:25%\n\
  218. }\n\
  219. #noobscriptUI:hover{\n\
  220. top:0px\n\
  221. }\n\
  222. #noobscriptUI > div > div {\n\
  223. color:#fff;\n\
  224. padding:7px;\n\
  225. height:19px;\n\
  226. display:inline-block;\n\
  227. cursor:pointer;\n\
  228. font-size:15px\n\
  229. }\n\
  230. </style>"
  231.  
  232. headAppend.appendChild(style);
  233. headAppend.appendChild(css);
  234.  
  235.  
  236. var contAppend = document.getElementById("gameUiContainer"),
  237. menuA = document.createElement("div");
  238.  
  239. var code = ['<div id="noobscriptUI">\n'];
  240.  
  241. levels.forEach((items, i) => {
  242. code.push(i === 0 ? ' <div>\n' : ' <div style="margin-top:7px;">\n');
  243. items.forEach((el) => {
  244. code.push(' ' + el.html + '\n');
  245. })
  246. code.push(' </div>\n');
  247. })
  248. code.push(' <div id="confinfo" style="margin-top:4px; color: white; text-align: center; font-size: 10px; white-space:pre"></div>')
  249. code.push('</div>');
  250.  
  251. menuA.innerHTML = code.join("");
  252. contAppend.insertBefore(menuA, contAppend.firstChild)
  253. contAppend.appendChild(toast)
  254. var toastTimeout = false;
  255. window.showToast = function (msg) {
  256. toast.textContent = msg;
  257.  
  258. if (toastTimeout) clearTimeout(toastTimeout);
  259. else toast.className = "show";
  260. toastTimeout = setTimeout(function () {
  261. toast.className = 'hide'
  262. setTimeout(function () {
  263. toast.className = '';
  264. }, 400);
  265. toastTimeout = false;
  266. }, 3000);
  267. }
  268. window.statusBar = function () {
  269. var el = document.getElementById('confinfo');
  270. var text = [];
  271.  
  272. window.statusItems.forEach((item, i) => {
  273. if (i !== 0) text.push(' ');
  274. if (item.name) text.push(item.name + ': ');
  275. text.push(item.value());
  276. })
  277.  
  278. el.textContent = text.join('');
  279. }
  280. window.statusBar();
  281.  
  282. window.initFuncs.forEach((func) => {
  283. func();
  284. })
  285. }
  286. setTimeout(() => {
  287. window.makeUI();
  288. }, 1000)