movement.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. module.exports = class Movement {
  2. constructor() {
  3. }
  4. move(player, input) {
  5. switch(input.action) {
  6. case "move":
  7. this.handleMoveDirection(player, input);
  8. break;
  9. }
  10. core.sendUpdateToAll(player);
  11. return true;
  12. }
  13. handleMoveDirection(player, input) {
  14. switch(input.direction) {
  15. case "up":
  16. this.handleUp(player, input);
  17. break;
  18. case "down":
  19. this.handleDown(player, input);
  20. break;
  21. case "left":
  22. this.handleLeft(player, input);
  23. break;
  24. case "right":
  25. this.handleRight(player, input);
  26. break;
  27. }
  28. }
  29. handleUp(player, input) {
  30. if(input.hasOwnProperty("start")) {
  31. player.velocity.y = -1;
  32. } else {
  33. player.velocity.y = 0;
  34. }
  35. }
  36. handleDown(player, input) {
  37. if(input.hasOwnProperty("start")) {
  38. player.velocity.y = 1;
  39. } else {
  40. player.velocity.y = 0;
  41. }
  42. }
  43. handleLeft(player, input) {
  44. if(input.hasOwnProperty("start")) {
  45. player.velocity.x = -1;
  46. } else {
  47. player.velocity.x = 0;
  48. }
  49. }
  50. handleRight(player, input) {
  51. if(input.hasOwnProperty("start")) {
  52. player.sector.x = 1;
  53. } else {
  54. player.velocity.x = 0;
  55. }
  56. }
  57. target(player, input) {
  58. player.moveToTarget = true;
  59. player.targetDestination = input.position;
  60. var angle = Math.atan2(input.position.y - player.position.y, input.position.x - player.position.x);
  61. player.targetAngle = angle;
  62. if(input.position.x > player.position.x) {
  63. player.velocity.x = player.maxSpeed * Math.cos(angle);
  64. }
  65. if(input.position.x < player.position.x) {
  66. player.velocity.x = player.maxSpeed * Math.cos(angle);
  67. }
  68. if(input.position.y > player.position.y) {
  69. player.velocity.y = player.maxSpeed * Math.sin(angle);
  70. }
  71. if(input.position.y < player.position.y) {
  72. player.velocity.y = player.maxSpeed * Math.sin(angle);
  73. }
  74. core.sendUpdateToAll(player);
  75. return true;
  76. }
  77. getErrorMessage() {
  78. return "Error in Movement Command";
  79. }
  80. };