Guest User

TagPro Smooth Spectator Cam

a guest
Feb 13th, 2015
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name          TagPro Smooth Spectator Cam
  3. // @version       1.1
  4. // @author        browncoat
  5. // @description   Smoothly pan the camera between players when spectating
  6. // @include       http://tagpro-*.koalabeast.com:*
  7. // @include       http://tangent.jukejuice.com*
  8. // @include       http://*.newcompte.fr*
  9. // ==/UserScript==
  10.  
  11. tagpro.ready(function () {
  12.  
  13.     // OPTIONS
  14.  
  15.     // true: camera will 'smooth' whether you are playing or spectating
  16.     // false: camera will 'smooth' only when spectating
  17.     var smoothAsPlayerAsWell = false;
  18.  
  19.     // "linear": camera will pan at a constant speed
  20.     // "ease_out": camera will smoothly slow down as it reaches its target (like a car coming to a stop)
  21.     // "elastic": camera will bounce slightly back and forth until settling on the target (like a spring)
  22.     var followType = "ease_out";
  23.  
  24.     // Between 0 and 1. Closer to 1 means the camera will reach the target quicker.
  25.     var cameraSpeed = 0.3;
  26.  
  27.     // END OPTIONS
  28.  
  29.  
  30.     var tr = tagpro.renderer;
  31.     var state = "snap";
  32.     var initialSet = false;
  33.     var targetX = 0;
  34.     var targetY = 0;
  35.     var cameraX = 0;
  36.     var cameraY = 0;
  37.     var accX = 0;
  38.     var accY = 0;
  39.     var linearCameraSpeed = cameraSpeed * 100;
  40.     var snapDistance = 10;
  41.     var startEaseThreshold = 80;
  42.     var FollowType = {EASE_OUT: "ease_out", LINEAR: "linear", ELASTIC: "elastic"};
  43.  
  44.     // Override this to store the desired or 'target' position for the camera to follow
  45.     var centerContainerToPoint = tr.centerContainerToPoint;
  46.     tr.centerContainerToPoint = function (x, y) {
  47.         if (!initialSet) {
  48.             initialSet = true;
  49.             cameraX = targetX;
  50.             cameraY = targetY;
  51.         }
  52.         targetX = x;
  53.         targetY = y;
  54.  
  55.         if (state == "snap") {
  56.             var xd = targetX - cameraX;
  57.             var yd = targetY - cameraY;
  58.             var distance = Math.sqrt(xd * xd + yd * yd);
  59.             if (distance > startEaseThreshold) {
  60.                 state = "easing";
  61.                 console.log("Start ease");
  62.             }
  63.         }
  64.     };
  65.  
  66.     function checkSnap() {
  67.         // If the cam is close enough just snap to the target
  68.         if (Math.abs(targetX - cameraX) <= snapDistance && Math.abs(targetY - cameraY) <= snapDistance) {
  69.             cameraX = targetX;
  70.             cameraY = targetY;
  71.             state = "snap";
  72.             console.log("Ease stop");
  73.         }
  74.     }
  75.  
  76.     // Override to run the smoothing function every render frame, after all other graphics have been updated
  77.     var updateGraphics = tr.updateGraphics;
  78.     tr.updateGraphics = function () {
  79.         updateGraphics();
  80.  
  81.         if (smoothAsPlayerAsWell || tagpro.spectator) {
  82.             if (state == "snap") {
  83.                 cameraX = targetX;
  84.                 cameraY = targetY;
  85.             } else if (state == "easing") {
  86.  
  87.                 // Easing function
  88.                 if (followType == FollowType.LINEAR) {
  89.                     cameraX += Math.sign(targetX - cameraX) * linearCameraSpeed;
  90.                     cameraY += Math.sign(targetY - cameraY) * linearCameraSpeed;
  91.  
  92.                     checkSnap();
  93.                 } else if (followType == FollowType.EASE_OUT) {
  94.                     cameraX += (targetX - cameraX) * cameraSpeed;
  95.                     cameraY += (targetY - cameraY) * cameraSpeed;
  96.  
  97.                     checkSnap();
  98.                 } else if (followType == FollowType.ELASTIC) {
  99.                     accX += (targetX - cameraX) * cameraSpeed;
  100.                     accY += (targetY - cameraY) * cameraSpeed;
  101.                     accX *= 0.8;
  102.                     accY *= 0.8;
  103.                     cameraX += accX;
  104.                     cameraY += accY;
  105.  
  106.                     // If the cam is close enough just snap to the target
  107.                     var lowDistance = Math.abs(targetX - cameraX) <= snapDistance && Math.abs(targetY - cameraY) <= snapDistance;
  108.                     var lowAcc = Math.abs(accX) <= snapDistance && Math.abs(accY) <= snapDistance;
  109.                     if (lowDistance && lowAcc) {
  110.                         cameraX = targetX;
  111.                         cameraY = targetY;
  112.                         state = "snap";
  113.                     }
  114.                 }
  115.             }
  116.         } else {
  117.             // Don't smooth if not spectating
  118.             cameraX = targetX;
  119.             cameraY = targetY;
  120.         }
  121.  
  122.         // Run default tr.centerContainerToPoint - actually sets the camera position
  123. //        centerContainerToPoint(cameraX, cameraY);
  124.         var viewport = $('#viewport');
  125.         var vpWidth = viewport.outerWidth();
  126.         var vpHeight = viewport.outerHeight();
  127.         var resizeScaleFactor = (vpHeight / tr.canvas_height).toFixed(2);
  128.         tr.gameContainer.x = Math.round(vpWidth / 2 - (cameraX / tagpro.zoom * resizeScaleFactor));
  129.         tr.gameContainer.y = Math.round(vpHeight / 2 - (cameraY / tagpro.zoom * resizeScaleFactor));
  130.  
  131.     };
  132.  
  133. });
Advertisement
Add Comment
Please, Sign In to add comment