Advertisement
Guest User

Untitled

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