Advertisement
Guest User

Untitled

a guest
Apr 15th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.70 KB | None | 0 0
  1. // ==UserScript==
  2. // @name TagPro Telestrator
  3. // @version 2.1.0
  4. // @description Use a telestrator while spectating TagPro!
  5. // @include http://tagpro-*.koalabeast.com:*
  6. // @include http://tangent.jukejuice.com:*
  7. // @include http://maptest*.newcompte.fr:*
  8. // @author BBQchicken
  9. // @namespace reddit.com/u/Splanky222
  10. // @downloadURL https://gist.github.com/JonathanDCohen/50aea7b437c64b728199/raw/tagpro_telestrator_2_0.user.js
  11. // ==/UserScript==
  12.  
  13. tagpro.ready(function init() {
  14. if (!tagpro.playerId || !tagpro.renderer.layers.background) {
  15. return setTimeout(init, 200);
  16. }
  17. if (!tagpro.spectator) {
  18. return false;
  19. }
  20.  
  21. var traceLayer = tagpro.renderer.layers.midground;
  22. var drawingLayer = tagpro.renderer.layers.foreground;
  23.  
  24. // ---------- OPTIONS ---------- \\
  25.  
  26. var kickClick = false;
  27. var traceLength = Infinity;
  28. var blinkTime = 250;
  29.  
  30. var circleColor = 0xFF6600;
  31. var pathColor = 0xFFFF00;
  32. var arrowColor = 0xC800FA;
  33.  
  34. var autoTrace = false;
  35. var traceDelay = 2000;
  36.  
  37. // ---------- PATH -------------- \\
  38.  
  39. var dashOn = true;
  40.  
  41. function Path(start, color, alpha, dashed, layer) {
  42. this.points = [start, start, start];
  43. this.color = color;
  44. this.alpha = alpha || 0.6;
  45. this.dashed = dashed;
  46. this.graphics = new PIXI.Graphics();
  47. this.layer = layer || drawingLayer;
  48. this.layer.addChild(this.graphics);
  49. }
  50.  
  51. Path.prototype.update = function(point) {
  52. var points = this.points;
  53. this.graphics.lineStyle(10, this.color, this.alpha);
  54. var from = points.shift();
  55. points.push(point);
  56. this.graphics.moveTo(from.x, from.y);
  57. // this.graphics.quadraticCurveTo(points[0].x, points[0].y, points[1].x, points[1].y);
  58. // this.graphics.bezierCurveTo(points[0].x, points[0].y, points[1].x, points[1].y, points[2].x, points[2].y);
  59. (!this.dashed || dashOn) && this.graphics.lineTo(points[0].x, points[0].y);
  60.  
  61. }
  62.  
  63. Path.prototype.clear = function(delayMS) {
  64. var delay = delayMS || 0;
  65. var graphics = this.graphics;
  66. var layer = this.layer;
  67. setTimeout(function() {
  68. graphics.clear();
  69. layer.removeChild(graphics);
  70. }, delayMS);
  71. }
  72.  
  73.  
  74. // ---------- TRACE -------------- \\
  75.  
  76. function Trace(player, auto) {
  77. this.player = player;
  78. this.path = new Path({x: player.x + 20, y: player.y + 20}, player.team === 1 ? 0xFF0000 : 0x0000FF, 0.4, true, traceLayer);
  79. this.active = true;
  80. this.flaccid = auto;
  81. var thisThing = this; //because javascript scope rules
  82. this.flaccid && setTimeout(function() { thisThing.flaccid = false; }, 3000);
  83. }
  84.  
  85. Trace.prototype.update = function() {
  86. this.active && this.path.update({x: this.player.x + 20, y: this.player.y + 20});
  87. };
  88.  
  89. Trace.prototype.clear = function(delayMS) {
  90. this.path.clear(this.flaccid ? 0 : delayMS);
  91. };
  92.  
  93. Trace.prototype.stop = function() {
  94. this.active = false;
  95. }
  96.  
  97. // ---------- CIRCLE -------------- \\
  98.  
  99. function Circle(center, color) {
  100. this.radius = 0;
  101. this.color = color || 0;
  102. this.center = center;
  103. this.graphics = new PIXI.Graphics();
  104. drawingLayer.addChild(this.graphics);
  105. }
  106.  
  107. Circle.prototype.update = function(point) {
  108. this.radius = Math.sqrt(Math.pow((point.x - this.center.x), 2) + Math.pow(point.y - this.center.y, 2));
  109. this.graphics.clear();
  110. this.graphics.lineStyle(10, this.color, 0.6);
  111. this.graphics.drawCircle(this.center.x, this.center.y, this.radius);
  112. }
  113.  
  114. Circle.prototype.clear = function() {
  115. this.graphics.clear();
  116. drawingLayer.removeChild(this.graphics);
  117. }
  118.  
  119. // ---------- ARROW -------------- \\
  120.  
  121. function Arrow(start, color) {
  122. console.log("Arrow constructor start");
  123. this.start = new PIXI.Point(start.x, start.y);
  124. this.wingLength = 45;
  125. this.headAngle = .4;
  126. this.leftWing = this.start.clone();
  127. this.rightWing = this.start.clone();
  128. this.angle = 0;
  129. this.color = color || 0;
  130.  
  131. console.log("Arrow Graphics creation");
  132. this.graphics = new PIXI.Graphics();
  133. drawingLayer.addChild(this.graphics);
  134. console.log("Arrow constructor end");
  135. }
  136.  
  137. Arrow.prototype.rotateHead = function(end) {
  138. console.log("Arrow rotate right wing");
  139. var phiRight = this.angle + this.headAngle;
  140. this.rightWing.x = end.x - this.wingLength * Math.cos(phiRight);
  141. this.rightWing.y = end.y - this.wingLength * Math.sin(phiRight);
  142.  
  143. console.log("Arrow rotate left wing");
  144. var phiLeft = this.angle - this.headAngle;
  145. this.leftWing.x = end.x - this.wingLength * Math.cos(phiLeft);
  146. this.leftWing.y = end.y - this.wingLength * Math.sin(phiLeft);
  147. }
  148.  
  149. Arrow.prototype.draw = function(end) {
  150. console.log("Arrow draw begin");
  151. this.graphics.clear();
  152. this.graphics.lineStyle(10, this.color, .6);
  153. this.graphics.moveTo(this.start.x, this.start.y);
  154. this.graphics.lineTo(end.x, end.y);
  155. this.graphics.moveTo(this.rightWing.x, this.rightWing.y);
  156. this.graphics.lineTo(end.x, end.y);
  157. this.graphics.lineTo(this.leftWing.x, this.leftWing.y);
  158. console.log("Arrow draw end");
  159. }
  160.  
  161. Arrow.prototype.update = function(end) {
  162. console.log("Arrow update begin");
  163. //this.end = end;
  164. this.angle = Math.atan2(end.y - this.start.y, end.x - this.start.x);
  165. this.rotateHead(end);
  166. this.draw(end);
  167. console.log("Arrow update end");
  168. }
  169.  
  170. Arrow.prototype.clear = function() {
  171. this.graphics.clear();
  172. drawingLayer.removeChild(this.graphics);
  173. }
  174.  
  175.  
  176. // ---------- LOGIC -------------- \\
  177.  
  178. var current = null, drawing = false, drawings = [], traces = {};
  179. var shift = false, alt = false;
  180. var stage = tagpro.renderer.stage;
  181.  
  182. function addUniqueTrace(player, auto) {
  183. console.log('trace construction attempted');
  184. if (!traces[player.id]) {
  185. traces[player.id] = new Trace(player, auto || false);
  186. console.log('trace construction succeeded')
  187. }
  188. }
  189.  
  190. function removeTrace(id) {
  191. if (!traces[id]) {
  192. return false;
  193. }
  194. traces[id].stop();
  195. traces[id].clear(traceDelay);
  196. setTimeout(function() { delete traces[id]; }, traceDelay);
  197. }
  198.  
  199. tagpro.socket.on('p', function (event) {
  200. var events = (tagpro.serverHost.search("map") !== -1) ? event.u : event;
  201. for (var idx in events) {
  202. var e = events[idx];
  203. if ((typeof(e.flag) !== 'undefined') && e.flag) {
  204. autoTrace && addUniqueTrace(tagpro.players[e.id], true);
  205. } else if((typeof(e['s-pops']) !== 'undefined') || (typeof(e['s-captures']) !== 'undefined')) {
  206. removeTrace(e.id);
  207. }
  208. }
  209. });
  210.  
  211. tpKick = tagpro.kick.player;
  212. tagpro.kick.player = function (player) {
  213. console.log("kick.player called");
  214. var shiftAlt = (alt && shift);
  215. if (kickClick || !(tagpro.spectator || shiftAlt)) {
  216. tpKick(player);
  217. }
  218. if (shiftAlt) {
  219. console.log("trace added");
  220. addUniqueTrace(player, false);
  221. }
  222. }
  223.  
  224. $(document).on("keydown keyup", function (event) {
  225. shift = event.shiftKey;
  226. alt = event.altKey;
  227. });
  228.  
  229. $(document).dblclick(function(event) {
  230. window.getSelection().removeAllRanges();
  231.  
  232. for (var i in drawings) {
  233. drawings[i].clear();
  234. }
  235. drawings = [];
  236.  
  237. if (!event.shiftKey) {
  238. return false;
  239. }
  240.  
  241. for (var i in traces) {
  242. traces[i] && traces[i].clear();
  243. }
  244. traces = {};
  245. });
  246.  
  247. //thanks to ProfessorTag
  248. function canvasMousePosition(e) {
  249. var tr = tagpro.renderer;
  250. var resizeScaleFactor = tr.options.disableViewportScaling ? 1 : (tr.vpHeight / tr.canvas_height).toFixed(2),
  251. scale = (tagpro.zoom / resizeScaleFactor),
  252. x1 = tr.gameContainer.x * scale,
  253. x2 = x1 - tr.vpWidth * scale,
  254. x = - Math.round((x1 + x2) / 2 - (e.x - innerWidth / 2) * scale),
  255. y1 = tr.gameContainer.y * scale,
  256. y2 = y1 - tr.vpHeight * scale,
  257. y = - Math.round((y1 + y2) / 2 - (e.y - innerHeight / 2) * scale);
  258. return {x: x, y: y};
  259. }
  260.  
  261. window.onmousedown = function (click) {
  262. drawing = true;
  263. }
  264.  
  265. window.onmousemove = function(click) {
  266. if (!drawing) {
  267. return false;
  268. } else if (current) {
  269. current.update(canvasMousePosition(click));
  270. } else {
  271. if (shift && alt) {
  272. drawing = false;
  273. } else if (shift) {
  274. current = new Arrow(canvasMousePosition(click), arrowColor);
  275. } else if (alt) {
  276. current = new Circle(canvasMousePosition(click), circleColor);
  277. } else {
  278. current = new Path(canvasMousePosition(click), pathColor);
  279. }
  280. }
  281. }
  282.  
  283. window.onmouseup = function(click) {
  284. console.log("mouseup");
  285. drawing = false;
  286. current && drawings.push(current);
  287. current = null;
  288. }
  289.  
  290. function drawAll() {
  291. for (var idx in traces) {
  292. trace = traces[idx];
  293. trace && trace.update();
  294. }
  295. requestAnimFrame(drawAll);
  296. }
  297.  
  298. requestAnimFrame(drawAll);
  299.  
  300. setInterval(function() {
  301. dashOn = !dashOn;
  302. }, blinkTime);
  303. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement