Advertisement
Guest User

Untitled

a guest
May 6th, 2015
19,436
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Agar Movement Keys
  3. // @description  Use arrow keys or WASD to move. E to eject mass. T to toggle movement with mouse.
  4. // @version      0.3
  5. // @match        http://agar.io/
  6. // @grant        none
  7. // ==/UserScript==
  8.  
  9. // Get canvas and create an object with (fake) mouse position properties.
  10. var canvas = document.getElementById("canvas");
  11. var endPoint = {clientX: innerWidth / 2, clientY: innerHeight / 2};
  12. var holdMoveEvent = null;
  13.  
  14. // Closure:
  15. var handleKeys = (function() {
  16.  
  17.     // KeyCodes and an object for storing key states.
  18.     var keys = [37, 38, 39, 40, 65, 87, 68, 83];
  19.     var key = {left: "keyup", up: "keyup", right: "keyup", down: "keyup"};
  20.  
  21.     // Stop movement and reset center when window loses focus or is resized.
  22.     ["blur", "resize"].forEach(function(listener) {
  23.         window.addEventListener(listener, function() {
  24.             key.left = key.up = key.right = key.down = "keyup";
  25.             endPoint = {clientX: innerWidth / 2, clientY: innerHeight / 2};
  26.             canvas.onmousedown(endPoint);
  27.         }, false);
  28.     });
  29.  
  30.     // The actual handleKeys function.
  31.     return function(event, keyState) {
  32.  
  33.         // Stop if keydown is repeating.
  34.         if (event.repeat && keyState === "keydown") return;
  35.        
  36.         // Thanks to /u/Apostolique for the toggle!
  37.         if (event.which === 84 && keyState === "keydown") {
  38.             if (canvas.onmousemove === null) {
  39.                 canvas.onmousemove = holdMoveEvent;
  40.             } else {
  41.                 canvas.onmousemove = null;
  42.             }
  43.             return;
  44.         }
  45.        
  46.         if (canvas.onmousemove === holdMoveEvent) return;
  47.  
  48.         // Iterate through keycodes.
  49.         for (var i = 0; i < keys.length; i++) {
  50.  
  51.             // If keycode doesn't match, skip it.
  52.             if (event.which !== keys[i]) continue;
  53.  
  54.             // Get axis based on key index, odd = y, even = x. Store directions (axis.dir) to be evaluated next.
  55.             var axis = (i % 2) ? {dir: ["up", "down"], value: "clientY"} : {dir: ["left", "right"], value: "clientX"};
  56.  
  57.             // Get direction based on index and axis. If divisible by 4, direction must be up or left (depending on the axis).
  58.             var direction = ((i % 4) === 0 || ((i - 1) % 4) === 0) ? axis.dir[0] : axis.dir[1];
  59.  
  60.             // If key state is already set, return.
  61.             if (key[direction] === keyState) return;
  62.  
  63.             // Else, set it.
  64.             key[direction] = keyState;
  65.  
  66.             // Positive or negative value based on key state.
  67.             var point = (keyState === "keydown") ? 500 : -500;
  68.  
  69.             // Invert value if direction is left or up.
  70.             point = (direction === "left" || direction === "up") ? -point : point;
  71.  
  72.             // Return true to send the movement.
  73.             return [axis.value, point];
  74.         }
  75.     };
  76. })();
  77.  
  78. // Send all key events to handleKeys.
  79. ["keydown", "keyup"].forEach(function(keyState) {
  80.     window.addEventListener(keyState, function(event) {
  81.         if (!canvas.onmousedown) return;
  82.         var data = handleKeys(event, keyState);
  83.         if (data && keyState === "keyup") {
  84.             var fraction = data[1] / 10;
  85.             for (var i = 0; i < 2; i++) {
  86.                 endPoint[data[0]] += (fraction * (i ? 1 : 9));
  87.                 canvas.onmousedown(endPoint);
  88.             }
  89.         } else if (data && keyState === "keydown") {
  90.             endPoint[data[0]] += data[1];
  91.             canvas.onmousedown(endPoint);
  92.         }
  93.         if (event.which === 87) event.stopPropagation();
  94.         if (event.which === 69) {
  95.             window["on" + keyState]({keyCode: 87});
  96.         }
  97.     }, true);
  98. });
  99.  
  100. // Overwrites default behavior.
  101. (function overwrite(startTime) {
  102.     if (Date.now() - startTime > 5000) return;
  103.     if (!canvas.onmousemove) return setTimeout(overwrite, 100, startTime);
  104.     holdMoveEvent = canvas.onmousemove;
  105.     canvas.onmousemove = null;
  106. })(Date.now());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement