Advertisement
Arbybear

TagPro Team Tile Pattern

Jan 17th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          TagPro Team Tile Pattern
  3. // @namespace     http://www.reddit.com/u/snaps_/
  4. // @description   Tints every other floor tile a slightly different shade.
  5. // @include       http://tagpro-*.koalabeast.com:*
  6. // @include       http://tangent.jukejuice.com:*
  7. // @include       http://*.newcompte.fr:*
  8. // @license       GPL version 3 or any later version; http://www.gnu.org/copyleft/gpl.html
  9. // @author        snaps, Arbybear
  10. // @version       0.1.0
  11. // ==/UserScript==
  12.  
  13. /* User-Defined Variables */
  14.  
  15. // The color to tint the floor tiles if TINT_TEAM is false.
  16. var TINT_COLOR = "#663300";
  17.  
  18. // The color to tint the floor tiles if TINT_TEAM is true.
  19. var TINT_RED = "#FF0000";
  20. var TINT_BLUE = "#0000FF";
  21.  
  22. // Whether to use team colors or not.
  23. var TINT_TEAM = true;
  24.  
  25. // How much to tint the floor tiles. Should be a number from 0 to 1.
  26. // In practice try values from 0.05 to 1 in increments of .2 to start,
  27. // then smaller amounts to hone in on what you like.
  28. var OPACITY = 0.05;
  29.  
  30. /* End of User-Defined Variables */
  31.  
  32.  
  33. /**
  34.  * Executes `fn` when the relevant parts of the `tagpro` object have
  35.  * been initialized.
  36.  * @param {Function} fn - The function to execute.
  37.  */
  38. function waitForInitialized(fn) {
  39.   if (!tagpro || !tagpro.tiles || !tagpro.tiles.draw || !tagpro.renderer) {
  40.     setTimeout(function() {
  41.       waitForInitialized(fn);
  42.     }, 10);
  43.   } else {
  44.     // Only override if we load early enough.
  45.     if (!tagpro.renderer.layers.backgroundDrawn) {
  46.       fn();
  47.     }
  48.   }
  49. }
  50.  
  51. waitForInitialized(function() {
  52.   var stdDraw = tagpro.tiles.draw;
  53.   // ids of the tiles we're interested in changing.
  54.   var floorTiles = [2, 11, 12, 17, 18];
  55.   var prefix = "__tinted__";
  56.  
  57.   /**
  58.    * Set tint of a given canvas element.
  59.    * @param {HTMLElement} image - Canvas element holding the image to tint.
  60.    * @param {string} [color="#000000"] - Color string to tint the tiles, like "#FF0000".
  61.    * @param {number} [opacity="0.01"] - How much to preserve the original image.
  62.    */
  63.   var setTint = function(image, color, opacity) {
  64.     // Adapted from: http://stackoverflow.com/a/4231508/1698058
  65.     var tint = document.createElement("canvas");
  66.     tint.width = image.width;
  67.     tint.height = image.height;
  68.  
  69.     var tintCtx = tint.getContext("2d");
  70.     tintCtx.fillStyle = color || "#000000";
  71.     tintCtx.fillRect(0, 0, tint.width, tint.height);
  72.     tintCtx.globalCompositeOperation = "destination-atop";
  73.     tintCtx.drawImage(image, 0, 0);
  74.  
  75.     var imageCtx = image.getContext("2d");
  76.     imageCtx.globalAlpha = opacity || 0.01;
  77.     imageCtx.drawImage(tint, 0, 0);
  78.   }
  79.  
  80.   /**
  81.    * Creates the tinted texture for the tile of the given id and sets
  82.    * the relevant values such that the returned value will function in
  83.    * the original `tagpro.tiles.draw` function.
  84.    * @param {(number|string)} tileId - The original id of the tile to set information for.
  85.    * @return {string} - The new id to use for the tile.
  86.    */
  87.   var setTintedTexture = function(tileId) {
  88.     var originalTileId = tileId;
  89.     tileId = prefix + originalTileId;
  90.     if (!tagpro.tiles[tileId] || !PIXI.TextureCache[tileId]) {
  91.       var tile = tagpro.tiles[originalTileId];
  92.       tagpro.tiles[tileId] = tile;
  93.  
  94.       var spread = tile.spread || 0;
  95.       var elt = document.createElement("canvas");
  96.       elt.width = tile.size || 40;
  97.       elt.height = tile.size || 40;
  98.  
  99.       var ctx = elt.getContext("2d");
  100.       var sx = tile.x * 40 - spread;
  101.       var sy = tile.y * 40 - spread;
  102.       var size = (tile.size || 40) + spread * 2;
  103.       ctx.drawImage(tagpro.tiles.image, sx, sy, size, size, 0, 0, size, size);
  104.        
  105.       if(TINT_TEAM) {
  106.         if(player.team == 1) {
  107.           setTint(elt, TINT_RED, OPACITY);
  108.         }else {
  109.           setTint(elt, TINT_BLUE, OPACITY);
  110.         }
  111.       }else {
  112.         setTint(elt, TINT_COLOR, OPACITY);
  113.       }
  114.        
  115.       PIXI.TextureCache[tileId] = PIXI.Texture.fromCanvas(elt);
  116.     }
  117.     return tileId;
  118.   }
  119.  
  120.   // Override `tagpro.tiles.draw`.
  121.   tagpro.tiles.draw = function() {
  122.     // Only make changes when drawing background tiles.
  123.     if (tagpro.tiles.draw.caller === tagpro.tiles.drawLayers) {
  124.       var loc = arguments[2];
  125.      
  126.       var floorTile = floorTiles.indexOf(arguments[1]) !== -1;
  127.       if (loc && !(typeof arguments[1] == "object") && floorTile) {
  128.         var arrayLoc = {
  129.           x: Math.floor(loc.x / 40),
  130.           y: Math.floor(loc.y / 40)
  131.         };
  132.         if ((arrayLoc.x % 2 == 0 && arrayLoc.y % 2 == 0) ||
  133.             (arrayLoc.x % 2 == 1 && arrayLoc.y % 2 == 1)) {
  134.           arguments[1] = setTintedTexture(arguments[1]);
  135.         }
  136.       }
  137.     }
  138.     return stdDraw.apply(null, arguments);
  139.   }
  140. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement