Classic Mouse

Restores the mouse from TankTrouble Classic back into TankTrouble Online

  1. // ==UserScript==
  2. // @name Classic Mouse
  3. // @author commander
  4. // @description Restores the mouse from TankTrouble Classic back into TankTrouble Online
  5. // @namespace https://github.com/asger-finding/tanktrouble-userscripts
  6. // @version 0.0.1
  7. // @license GPL-3.0
  8. // @match https://tanktrouble.com/*
  9. // @match https://beta.tanktrouble.com/*
  10. // @exclude *://classic.tanktrouble.com/
  11. // @run-at document-end
  12. // @grant none
  13. // @noframes
  14. // ==/UserScript==
  15.  
  16. /* eslint-disable new-cap */
  17. /* eslint-disable complexity */
  18.  
  19. UIConstants.CLASSIC_MOUSE_INPUT = {
  20. ROTATION_DEAD_ANGLE: 0.1,
  21. POSITION_DEAD_DISTANCE: 180
  22. };
  23.  
  24. MouseInputManager.method('update', function() {
  25. this._super();
  26.  
  27. const game = GameManager.getGame();
  28. if (game) {
  29. let forwardState = false;
  30. const backState = false;
  31. const leftState = [];
  32. const rightState = [];
  33. let fireState = false;
  34.  
  35. const gameBounds = game.scale.bounds;
  36. const gameScale = game.scale.scaleFactor;
  37. this.mouseX = (MouseInputManager.mousePageX - gameBounds.x) * gameScale.x;
  38. this.mouseY = (MouseInputManager.mousePageY - gameBounds.y) * gameScale.y;
  39. if (game.input.enabled && MouseInputManager.mouseActivated) {
  40. if (game.state.getCurrentState().getTankSprite) {
  41. const tankSprite = game.state.getCurrentState().getTankSprite(this.playerId);
  42. if (tankSprite) {
  43. const relativeToTank = tankSprite.toLocal(new Phaser.Point(this.mouseX, this.mouseY));
  44. const angle = Phaser.Math.angleBetween(0, 0, relativeToTank.x, relativeToTank.y);
  45. const distance = Math.abs((angle / Math.PI) + 0.5);
  46.  
  47. const rotationSpeedMultiplier = distance > 0.1
  48. ? Math.max(Math.min(distance * 12, 4), 1)
  49. : 1;
  50.  
  51. leftState.push(angle + (Math.PI * 0.5) < -UIConstants.CLASSIC_MOUSE_INPUT.ROTATION_DEAD_ANGLE, rotationSpeedMultiplier);
  52. rightState.push(angle + (Math.PI * 0.5) > UIConstants.CLASSIC_MOUSE_INPUT.ROTATION_DEAD_ANGLE, rotationSpeedMultiplier);
  53.  
  54. forwardState = relativeToTank.getMagnitude() > UIConstants.CLASSIC_MOUSE_INPUT.POSITION_DEAD_DISTANCE / UIConstants.GAME_ASSET_SCALE;
  55.  
  56. fireState ||= KeyboardInputManager.leftMouseDown || game.input.mousePointer.leftButton.isDown;
  57. }
  58. }
  59. }
  60. let stateChanged = false;
  61. stateChanged ||= this.storedStates.forward !== forwardState;
  62. stateChanged ||= this.storedStates.fire !== fireState;
  63. stateChanged ||= !leftState.every((el, i) => this.storedStates.left[i] === el);
  64. stateChanged ||= !rightState.every((el, i) => this.storedStates.right[i] === el);
  65.  
  66. const gameController = GameManager.getGameController();
  67. if (stateChanged && gameController) {
  68. const inputState = InputState.withState(this.playerId, forwardState, backState, leftState, rightState, fireState);
  69. gameController.setInputState(inputState);
  70. }
  71.  
  72. this.storedStates.forward = forwardState;
  73. this.storedStates.back = backState;
  74. this.storedStates.left = leftState;
  75. this.storedStates.right = rightState;
  76. this.storedStates.fire = fireState;
  77. }
  78. });
  79.  
  80. Tank.method('setTankState', function(tankState) {
  81. this.playerId = tankState.getPlayerId();
  82. this.x = tankState.getX();
  83. this.y = tankState.getY();
  84. this.forward = tankState.getForward();
  85. this.back = tankState.getBack();
  86. this.rotation = tankState.getRotation();
  87. this.fireDown = tankState.getFireDown();
  88. this.locked = tankState.getLocked();
  89.  
  90. const left = tankState.getLeft();
  91. const right = tankState.getRight();
  92. [this.left, this.rotationSpeedMultiplier] = Array.isArray(left) ? left : [left, 1.0];
  93. [this.right, this.rotationSpeedMultiplier] = Array.isArray(right) ? right : [right, 1.0];
  94.  
  95. if (this.b2dbody) {
  96. this.b2dbody.SetPosition(Box2D.Common.Math.b2Vec2.Make(this.x, this.y));
  97. this.b2dbody.SetAngle(this.rotation);
  98.  
  99. this.update();
  100. }
  101. });
  102.  
  103. Tank.method('update', function() {
  104. this.x = this.b2dbody.GetPosition().x;
  105. this.y = this.b2dbody.GetPosition().y;
  106. this.rotation = this.b2dbody.GetAngle();
  107. if (this.locked) {
  108. this.b2dbody.SetLinearVelocity(Box2D.Common.Math.b2Vec2.Make(0.0, 0.0));
  109. this.b2dbody.SetAngularVelocity(0.0);
  110. } else {
  111. this._computeSpeed();
  112. this._computeRotationSpeed();
  113. const speedX = Math.sin(this.rotation) * this.speed;
  114. const speedY = -Math.cos(this.rotation) * this.speed;
  115. this.b2dbody.SetLinearVelocity(Box2D.Common.Math.b2Vec2.Make(speedX, speedY));
  116. this.b2dbody.SetAngularVelocity(this.rotationSpeed * (this.rotationSpeedMultiplier || 1));
  117. }
  118. });

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址