Guest User

Browncoats mod Ghosts

a guest
Oct 13th, 2014
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.36 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Browncoat's TagPro Mod (Ghost version)
  3. // @version 1.1.2
  4. // @description Range of configurable mods for TagPro
  5. // @include http://tagpro-*.koalabeast.com:*
  6. // @include http://tangent.jukejuice.com:*
  7. // @include http://*.newcompte.fr:*
  8. // @author Browncoat (edited by Lysozyme)
  9. // ==/UserScript==
  10.  
  11. /**
  12. * Define a few namespaces to prevent false IDE warnings
  13. * @namespace tagpro
  14. * @namespace tagpro.tiles
  15. * @namespace tagpro.viewPort
  16. * @namespace player.degree
  17. * @namespace player.dead
  18. * @namespace player.flag
  19. */
  20.  
  21. // Main tagpro function
  22. tagpro.ready(function () {
  23.  
  24. if (tagpro.events.drawPlayer)
  25. return;
  26.  
  27. var SpinType = {
  28. OFF: 0,
  29. WHOLE_BALL: 1,
  30. OVERLAY: 2
  31. };
  32.  
  33. // -------------- Options --------------
  34.  
  35. // Ball drop shadows
  36. var showDropShadows = false;
  37.  
  38. // Ball shine effect
  39. var showBallShine = false;
  40.  
  41. // Image to show over balls, without spinning. Default is a shine effect. Image should be 38x38 pixels
  42. var ballShineUrl = "http://i.imgur.com/YsZccQv.png";
  43.  
  44. // Show flair in the center of balls and spin it
  45. var centerFlair = false;
  46.  
  47. /**
  48. * How to handle the ball spinning.
  49. * SpinType.OFF - no spinning
  50. * SpinType.WHOLE_BALL - spin the entire ball as it is on your texture pack
  51. * SpinType.OVERLAY - spin the specified overlay image url.
  52. */
  53. var spinType = SpinType.OFF;
  54.  
  55. // 80x40 image with red overlay on the left side and blue on the right http://i.imgur.com/hjeRNWH.png is default
  56. var spinOverlayUrl = "http://i.imgur.com/KtwiVqW.png";
  57.  
  58. // set to 2 for twice the particles! Use at own risk
  59. var particleFactor = 0.5;
  60.  
  61. // Show particles (who would disable this)
  62. var showParticles = true;
  63.  
  64. // ------------ End Options ------------
  65.  
  66. var particles = [];
  67. var drawDropShadowCallbacks = [];
  68. var drawPlayerCallbacks = [];
  69. var drawPlayerInfoCallbacks = [];
  70. var offset = {
  71. x: 0,
  72. y: 0
  73. };
  74. var viewBox = {
  75. left: 0,
  76. top: 0,
  77. right: 0,
  78. bottom: 0,
  79. leftClip: 0,
  80. topClip: 0,
  81. rightClip: 0,
  82. bottomClip: 0
  83. };
  84.  
  85. function addAsset(id, url) {
  86. var asset = document.createElement("img");
  87. asset.src = url;
  88. asset.id = id;
  89. asset.className = "asset";
  90. $("#assets").append(asset.outerHTML);
  91. }
  92.  
  93. addAsset("shine", ballShineUrl);
  94. addAsset("spinOverlay", spinOverlayUrl);
  95.  
  96. // Our custom draw function.
  97. var superUiDraw = tagpro.ui.draw;
  98. tagpro.ui.draw = function(context) {
  99. updateViewBox(context);
  100. doAll(drawDropShadowCallbacks);
  101. drawParticles(context, Layer.UNDER_PLAYERS);
  102. doAll(drawPlayerCallbacks);
  103. drawParticles(context, Layer.OVER_PLAYERS);
  104. doAll(drawPlayerInfoCallbacks);
  105. superUiDraw(context);
  106. };
  107.  
  108. function updateViewBox(context) {
  109. var e = 20;
  110. var canvas = $("canvas");
  111. var a = {
  112. x: canvas.attr("width")/2,
  113. y: canvas.attr("height")/2
  114. };
  115. viewBox.left = Math.round(tagpro.viewPort.source.x / tagpro.zoom) * tagpro.zoom - a.x * tagpro.zoom + e;
  116. viewBox.top = Math.round(tagpro.viewPort.source.y / tagpro.zoom) * tagpro.zoom - a.y * tagpro.zoom + e;
  117. viewBox.right = Math.round(tagpro.viewPort.source.x / tagpro.zoom) * tagpro.zoom + a.x * tagpro.zoom + e;
  118. viewBox.bottom = Math.round(tagpro.viewPort.source.y / tagpro.zoom) * tagpro.zoom + a.y * tagpro.zoom + e;
  119. viewBox.leftClip = 0; viewBox.topClip = 0; viewBox.rightClip = 0; viewBox.bottomClip = 0;
  120. viewBox.left < 0 && (viewBox.leftClip = -viewBox.left);
  121. viewBox.top < 0 && (viewBox.topClip = -viewBox.top);
  122. viewBox.right > context.canvas.width && (viewBox.rightClip = viewBox.right - context.canvas.width);
  123. viewBox.bottom > context.canvas.height && (viewBox.bottomClip = viewBox.bottom - context.canvas.height);
  124. }
  125.  
  126. function doAll(callbacks) {
  127. while (callbacks.length > 0) {
  128. (callbacks.shift())();
  129. }
  130. }
  131.  
  132. function drawParticles(context, layer) {
  133. for (var i = 0; i < particles.length; i++) {
  134. var particle = particles[i];
  135. particle.draw(context, offset, layer);
  136. if (particle.shouldBeRemoved()) {
  137. particles.splice(i, 1);
  138. i--;
  139. }
  140. }
  141. }
  142.  
  143. tagpro.events.register({
  144. drawPlayer: function (player, context, drawPos, TILESIZE) {
  145.  
  146. if (tagpro.viewPort.source == player) {
  147. offset.x = drawPos.x - player.x;
  148. offset.y = drawPos.y - player.y;
  149. }
  150.  
  151. drawDropShadowCallbacks.push(function() {
  152. drawDropShadow(context, drawPos);
  153. });
  154.  
  155. drawPlayerCallbacks.push(function () {
  156. drawPlayer(player, context, drawPos, TILESIZE);
  157. });
  158.  
  159. drawPlayerInfoCallbacks.push(function () {
  160. drawPlayerInfo(context, player);
  161. });
  162.  
  163. addParticles(player);
  164. }
  165. });
  166.  
  167. function addParticles(player) {
  168. var centerX = player.x + 20;
  169. var centerY = player.y + 20;
  170.  
  171. if (player.team === 1) {
  172. addParticle(new Particle(centerX, centerY, 18, "65, 22, 27", 500, 0, 0.1, Layer.UNDER_PLAYERS), 1);
  173. var radius = Math.floor(random(3, 7));
  174. addParticle(new Particle(centerX + random(-20, 20), centerY + random(-20, 20), radius, "65, 22, 27", 1000, {x: 0, y: 0}, 0.4));
  175. } else {
  176. addParticle(new Particle(centerX, centerY, 18, "27, 22, 65", 500, 0, 0.1, Layer.UNDER_PLAYERS), 1);
  177. var radius = Math.floor(random(3, 7));
  178. addParticle(new Particle(centerX + random(-20, 20), centerY + random(-20, 20), radius, "27, 22, 65", 1000, {x: 0, y: 0}, 0.4));
  179. }
  180.  
  181.  
  182. if (player.tagpro) {
  183. addParticle(new Particle(centerX + random(-20, 20), centerY + random(-20, 20), 2, "0, 255, 0", 500));
  184. }
  185.  
  186. if (player.bomb) {
  187. // Spark
  188. addParticle(new Particle(centerX + random(-20, 20), centerY + random(-20, 20), 1, "255, 255, 0", 200));
  189.  
  190. //Smoke
  191. var radius = Math.floor(random(3, 7));
  192. addParticle(new Particle(centerX + random(-20, 20), centerY + random(-20, 20), radius, "52, 52, 52", 1000, {x: 0, y: 0}, 0.4));
  193. }
  194.  
  195. if (player.grip) {
  196. if (player.team === 1) {
  197. addParticle(new Particle(centerX, centerY, 18, "213, 134, 203", 500, 0, 0.1, Layer.UNDER_PLAYERS), 1);
  198. } else {
  199. addParticle(new Particle(centerX, centerY, 18, "213, 134, 203", 500, 0, 0.1, Layer.UNDER_PLAYERS), 1);
  200. }
  201. addParticle(new Particle(centerX + random(-20, 20), centerY + random(-20, 20), 1, "255, 255, 255", 250));
  202. }
  203. }
  204.  
  205. function addParticle(particle, overrideFactor) {
  206. if (showParticles) {
  207. var total = 1;
  208. if (overrideFactor) {
  209. total = overrideFactor * total;
  210. } else {
  211. total = total * particleFactor;
  212. }
  213. for (var i = 0; i < total; i++) {
  214. particles.push(particle);
  215. }
  216. }
  217. }
  218.  
  219. function transformPosition(p) {
  220. return {
  221. x: Math.round(p.x * (1 / tagpro.zoom)) - Math.round(viewBox.left * (1 / tagpro.zoom)),
  222. y: Math.round(p.y * (1 / tagpro.zoom)) - Math.round(viewBox.top * (1 / tagpro.zoom))
  223. }
  224. }
  225.  
  226. function drawDropShadow(context, drawPos) {
  227. if (showDropShadows) {
  228. context.save();
  229. context.beginPath();
  230. context.arc(drawPos.x + 20 * (1 / tagpro.zoom), drawPos.y + 20 * (1 / tagpro.zoom), 18 * (1 / tagpro.zoom), 0, Math.PI * 2, true);
  231. context.fillStyle = "rgba(0, 0, 0, 1)";
  232. context.shadowColor = 'rgba(0, 0, 0, 0.5)';
  233. context.shadowBlur = 10;
  234. context.shadowOffsetX = -5;
  235. context.shadowOffsetY = 5;
  236. context.fill();
  237. context.closePath();
  238. context.restore();
  239. }
  240. }
  241.  
  242. function drawPlayer(player, context, drawPos, TILESIZE) {
  243. context.save();
  244.  
  245. var rotateCanvas = function () {
  246. context.translate(drawPos.x + (TILESIZE / 2) * (1 / tagpro.zoom), drawPos.y + (TILESIZE / 2) * (1 / tagpro.zoom));
  247. context.rotate(player.angle);
  248. context.translate(-drawPos.x - (TILESIZE / 2) * (1 / tagpro.zoom), -drawPos.y - (TILESIZE / 2) * (1 / tagpro.zoom));
  249. };
  250.  
  251. if (spinType == SpinType.WHOLE_BALL) {
  252. rotateCanvas();
  253. }
  254.  
  255. tagpro.tiles.drawWithZoom(context, player.team == 1 ? "redball" : "blueball", drawPos);
  256.  
  257. // flair in center
  258. var e = $("#flair").get(0);
  259. if (centerFlair && player.flair && tagpro.zoom <= 1) {
  260. context.drawImage(e, player.flair.x * 16, player.flair.y * 16, 16, 16, drawPos.x + 12 * (1 / tagpro.zoom), drawPos.y + 13 * (1 / tagpro.zoom), 16, 16)
  261. }
  262.  
  263. if (player.bomb && Math.round(Math.random() * 4) == 1) {
  264. context.fillStyle = "rgba(255, 255, 0, .50)";
  265. context.beginPath();
  266. context.arc(drawPos.x + TILESIZE / 2 * (1 / tagpro.zoom), drawPos.y + TILESIZE / 2 * (1 / tagpro.zoom), 19 * (1 / tagpro.zoom), 0, Math.PI * 2, true);
  267. context.closePath();
  268. context.fill();
  269. }
  270.  
  271. if (player.tagpro) {
  272. context.strokeStyle = "#00FF00";
  273. context.fillStyle = "rgba(0, 255, 0, .25)";
  274. context.lineWidth = 2 * (1 / tagpro.zoom);
  275. context.beginPath();
  276. context.arc(drawPos.x + TILESIZE / 2 * (1 / tagpro.zoom), drawPos.y + TILESIZE / 2 * (1 / tagpro.zoom), 19 * (1 / tagpro.zoom), 0, Math.PI * 2, true);
  277. context.closePath();
  278. if (!player.bomb) {
  279. context.fill();
  280. }
  281. context.stroke();
  282. }
  283.  
  284. if (spinType == SpinType.OVERLAY) {
  285. rotateCanvas();
  286. var spinOverlay = $("#spinOverlay").get(0);
  287. var p = (player.team == 1) ? 0 : 1;
  288. context.drawImage(spinOverlay, 40 * p, 0, 40, 40, drawPos.x, drawPos.y, 40 / tagpro.zoom, 40 / tagpro.zoom);
  289. }
  290.  
  291. context.restore();
  292.  
  293. //shine
  294. if (showBallShine) {
  295. var s = $("#shine").get(0);
  296. context.drawImage(s, drawPos.x + 1, drawPos.y + 1, 40 / tagpro.zoom, 40 / tagpro.zoom);
  297. }
  298.  
  299. // Flair in default position
  300. if (!centerFlair && player.flair && tagpro.zoom <= 1) {
  301. context.drawImage(e, player.flair.x * 16, player.flair.y * 16, 16, 16, drawPos.x + 12, drawPos.y - 17, 16, 16);
  302. }
  303.  
  304. }
  305.  
  306. // Username, flags and degrees
  307. var flagNames = ["redflag", "blueflag", "yellowflag"];
  308. function drawPlayerInfo(context, player) {
  309. var p = transformPosition(player);
  310. if (!player.dead && player.draw) {
  311.  
  312. // Flag
  313. if (player.flag) {
  314. var flag = "bc_" + flagNames[player.flag - 1];
  315. tagpro.tiles.drawWithZoom(context, flag, {
  316. x: p.x + Math.round(13 * (1 / tagpro.zoom)),
  317. y: p.y - Math.round(32 * (1 / tagpro.zoom))
  318. });
  319. }
  320.  
  321. // Degrees
  322. if (tagpro.zoom <= 1.5 && player.browncoatDegreeCache) {
  323. context.drawImage(
  324. player.browncoatDegreeCache.canvas,
  325. p.x + 20 * (1 / tagpro.zoom),
  326. p.y - Math.round(8 * (1 / tagpro.zoom))
  327. );
  328. }
  329.  
  330. // Name
  331. if (player.name && tagpro.zoom <= 4 && player.browncoatCache) {
  332. context.drawImage(
  333. player.browncoatCache.canvas,
  334. p.x + Math.round(18 * (1 / tagpro.zoom)),
  335. p.y - 20 * (1 / tagpro.zoom)
  336. );
  337. }
  338.  
  339. }
  340. }
  341.  
  342. // Add effects on bomb explosion
  343. onSocketEvent("bomb", function (e) {
  344. // e = top left corner of tile
  345. for (var i = 0; i < 3; i++) {
  346.  
  347. // Black fragments
  348. for (var j = 0; j < 3; j++) {
  349. var speed = {
  350. x: random(-7, 7),
  351. y: random(-7, 7)
  352. };
  353. addParticle(new Particle(e.x + 20, e.y + 20, 2, "0, 0, 0", 500, speed));
  354. }
  355.  
  356. // Grey smoke
  357. var radius = Math.random() * 5 + 10;
  358. var x = e.x + random(0, 40);
  359. var y = e.y + random(0, 40);
  360. addParticle(new Particle(x, y, radius, "51, 51, 51", 1000));
  361.  
  362. // White flash
  363. x = e.x + random(0, 40);
  364. y = e.y + random(0, 40);
  365. addParticle(new Particle(x, y, 10, "255, 255, 255", 200));
  366. }
  367. });
  368.  
  369. // Add effects on player pop
  370. onSocketEvent("splat", function (player) {
  371. for (var i = 0; i < 30; i++) {
  372. var rgb = player.t == 1 ? "255, 0, 0" : "0, 0, 255";
  373. var speed = {
  374. x: random(-4, 4),
  375. y: random(-4, 4)
  376. };
  377. var x = player.x + 20 + speed.x * random(0, 20);
  378. var y = player.y + 20 + speed.y * random(0, 20);
  379. addParticle(new Particle(x, y, 1, rgb, 300, speed));
  380. }
  381. });
  382.  
  383. function onSocketEvent(event, handler) {
  384. tagpro.socket.on(event, function(param) {
  385. try {
  386. handler(param);
  387. } catch (err) {
  388. console.log("Browncoat's mod error handling event '" + event + "': " + err);
  389. }
  390. });
  391. }
  392.  
  393.  
  394. // -------------------------- OVERRIDES --------------------------
  395.  
  396.  
  397. // Don't draw the speed/grip icon over players unless particles are disabled
  398. // Don't draw flags - done in drawPlayerInfo()
  399. var superDrawWithZoom = tagpro.tiles.drawWithZoom;
  400. tagpro.tiles.drawWithZoom = function (context, type, position, r, i, s) {
  401. if ((!showParticles || type != "grip") && type != "yellowflag" && type != "redflag" && type != "blueflag") {
  402. if (type.substr(0, 3) == "bc_") {
  403. type = type.substr(3, type.length);
  404. }
  405. superDrawWithZoom(context, type, position, r, i, s);
  406. }
  407. };
  408.  
  409. tagpro.flair.draw = function (context, drawPos, flair) {
  410. // Do nothing - flair is draw in the drawPlayerInfo() function
  411. // t.drawImage(e, r.x * 16, r.y * 16, 16, 16, n.x + 12, n.y - 17, 16, 16)
  412. };
  413.  
  414. tagpro.ui.createPlayerCache = function(player) {
  415. player.cache = {
  416. canvas: $("<canvas></canvas>").attr("width", 140).attr("height", 35).attr("visible", false).get(0)
  417. };
  418. player.degreeCache = {
  419. canvas: $("<canvas></canvas>").attr("width", 45).attr("height", 35).attr("visible", false).get(0)
  420. };
  421. player.cache.context = player.cache.canvas.getContext("2d");
  422. player.degreeCache.context = player.degreeCache.canvas.getContext("2d");
  423.  
  424. player.browncoatCache = {
  425. canvas: $("<canvas></canvas>").attr("width", 140).attr("height", 35).get(0)
  426. };
  427. player.browncoatDegreeCache = {
  428. canvas: $("<canvas></canvas>").attr("width", 45).attr("height", 35).get(0)
  429. };
  430. player.browncoatCache.context = player.browncoatCache.canvas.getContext("2d");
  431. player.browncoatDegreeCache.context = player.browncoatDegreeCache.canvas.getContext("2d");
  432. player.cache.update = function () {
  433. // Hide default caches and create new caches to draw on to in drawPlayerInfo()
  434. // Couldn't figure out how to override default draw name/flair so we hide default and create new canvases
  435. player.browncoatCache.context.clearRect(0, 0, 140, 35);
  436. player.browncoatDegreeCache.context.clearRect(0, 0, 45, 35);
  437. var nameColor = player.auth ? "#BFFF00" : "#ffffff";
  438. if (tagpro.settings.ui.names) {
  439. tagpro.prettyText(player.name, 15, 15, nameColor, false, false, player.browncoatCache.context);
  440. }
  441. if (player.degree && tagpro.settings.ui.degrees) {
  442. tagpro.prettyText(player.degree + "°", 20, 15, "#ffffff", false, false, player.browncoatDegreeCache.context);
  443. }
  444. };
  445. player.cache.update();
  446. };
  447.  
  448. function random(min, max) {
  449. return Math.random() * (max - min) + min;
  450. }
  451.  
  452. var Layer = {
  453. UNDER_PLAYERS: 1,
  454. OVER_PLAYERS: 2
  455. };
  456.  
  457. /**
  458. *
  459. * Particle class
  460. *
  461. * @constructor
  462. * @param x
  463. * @param y
  464. * @param radius
  465. * @param {String} rgb should be in the form "255, 255, 255"
  466. * @param lifeTime
  467. * @param [speed]
  468. * @param [alpha]
  469. * @param {int} [layer]
  470. */
  471. var Particle = function (x, y, radius, rgb, lifeTime, speed, alpha, layer) {
  472. speed = speed || {x: 0, y: 0};
  473. alpha = alpha || 1;
  474. layer = layer || Layer.OVER_PLAYERS;
  475. this.x = x;
  476. this.y = y;
  477. this.radius = radius;
  478. this.rgb = rgb;
  479. this.alpha = alpha;
  480. this.initialAlpha = alpha;
  481. this.remove = false;
  482. this.lifeTime = lifeTime;
  483. this.timer = 0;
  484. this.speed = speed;
  485. this.layer = layer;
  486. };
  487.  
  488. Particle.prototype.draw = function (context, offset, layer) {
  489. if (!this.remove && this.layer == layer) {
  490. this.x += this.speed.x;
  491. this.y += this.speed.y;
  492. context.save();
  493. context.fillStyle = "rgba(" + this.rgb + ", " + this.alpha + ")";
  494. context.beginPath();
  495. context.arc(this.x + offset.x, this.y + offset.y, this.radius * (1 / tagpro.zoom), 0, Math.PI * 2, true);
  496. context.closePath();
  497. context.fill();
  498. context.restore();
  499.  
  500. this.timer += 1000 / 60;
  501. this.alpha = (1 - (this.timer / this.lifeTime)) * this.initialAlpha;
  502. if (this.alpha < 0.001 || this.timer >= this.lifeTime) {
  503. this.remove = true;
  504. }
  505. }
  506. };
  507.  
  508. Particle.prototype.shouldBeRemoved = function () {
  509. return this.remove;
  510. };
  511.  
  512. });
Advertisement
Add Comment
Please, Sign In to add comment