Agar Movement Keys

Use arrow keys or ESDF to move.

  1. // ==UserScript==
  2. // @name Agar Movement Keys
  3. // @description Use arrow keys or ESDF to move.
  4. // @version 0.2
  5. // @match http://agar.io/
  6. // @grant none
  7. // @namespace https://greasyfork.org/users/12022
  8. // ==/UserScript==
  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. // Closure:
  13. var handleKeys = (function() {
  14. // KeyCodes and an object for storing key states.
  15. var keys = [37, 38, 39, 40, 83, 69, 70, 68];
  16. var key = {left: "keyup", up: "keyup", right: "keyup", down: "keyup"};
  17. // Stop movement and reset center when window loses focus or is resized.
  18. ["blur", "resize"].forEach(function(listener) {
  19. window.addEventListener(listener, function() {
  20. key.left = key.up = key.right = key.down = "keyup";
  21. endPoint = {clientX: innerWidth / 2, clientY: innerHeight / 2};
  22. canvas.onmousedown(endPoint);
  23. }, false);
  24. });
  25. // The actual handleKeys function.
  26. return function(event, keyState) {
  27. // Stop if keydown is repeating.
  28. if (event.repeat && keyState === "keydown") return;
  29. // Iterate through keycodes.
  30. for (var i = 0; i < keys.length; i++) {
  31. // If keycode doesn't match, skip it.
  32. if (event.which !== keys[i]) continue;
  33. // Get axis based on key index, odd = y, even = x. Store directions (axis.dir) to be evaluated next.
  34. var axis = (i % 2) ? {dir: ["up", "down"], value: "clientY"} : {dir: ["left", "right"], value: "clientX"};
  35. // Get direction based on index and axis. If divisible by 4, direction must be up or left (depending on the axis).
  36. var direction = ((i % 4) === 0 || ((i - 1) % 4) === 0) ? axis.dir[0] : axis.dir[1];
  37. // If key state is already set, return.
  38. if (key[direction] === keyState) return;
  39. // Else, set it.
  40. key[direction] = keyState;
  41. // Positive or negative value based on key state.
  42. var point = (keyState === "keydown") ? 1000 : -1000;
  43. // Invert value if direction is left or up.
  44. point = (direction === "left" || direction === "up") ? -point : point;
  45. // Add point to fake mouse position property.
  46. endPoint[axis.value] += point;
  47. // Return true to send the movement.
  48. return true;
  49. }
  50. };
  51. })();
  52. // Send all key events to handleKeys.
  53. ["keydown", "keyup"].forEach(function(keyState) {
  54. window.addEventListener(keyState, function(event) {
  55. // Send movement if handleKeys returns true.
  56. if (handleKeys(event, keyState)) canvas.onmousedown(endPoint);
  57. }, false);
  58. });
  59. // Stop the default mouse move behavior.
  60. (function nullMouseMove(startTime) {
  61. if (Date.now() - startTime > 5000) return;
  62. if (!canvas.onmousemove) return setTimeout(nullMouseMove, 0, startTime);
  63. canvas.onmousemove = null;
  64. })(Date.now());