JoyStick.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. class JoyStick {
  2. constructor(e) {
  3. E(this, "_zone", document.createElement("div"));
  4. E(this, "_joystick", null);
  5. E(this, "_room");
  6. this._room = e
  7. }
  8. init(e) {
  9. const {interval: t=33, triggerDistance: r=25, style: n={
  10. left: 0,
  11. bottom: 0
  12. }} = e
  13. , o = (u,c)=>{
  14. this._room.actionsHandler.joystick({
  15. degree: Math.floor(u),
  16. level: Math.floor(c / 5)
  17. })
  18. }
  19. , a = this._zone;
  20. document.body.appendChild(a),
  21. a.style.position = "absolute",
  22. a.style.width = "200px",
  23. a.style.height = "200px",
  24. a.style.left = String(n.left),
  25. a.style.bottom = String(n.bottom),
  26. a.style.zIndex = "999",
  27. a.style.userSelect = "none",
  28. a.style.webkitUserSelect = "none",
  29. this._joystick = nipplejs.create({
  30. zone: a,
  31. mode: "static",
  32. position: {
  33. left: "50%",
  34. top: "50%"
  35. },
  36. color: "white"
  37. });
  38. let s, l;
  39. return this._joystick.on("move", (u,c)=>{
  40. s = c
  41. }
  42. ),
  43. this._joystick.on("start", ()=>{
  44. l = window.setInterval(()=>{
  45. s && s.distance > r && o && o(s.angle.degree, s.distance)
  46. }
  47. , t)
  48. }
  49. ),
  50. this._joystick.on("end", ()=>{
  51. l && window.clearInterval(l),
  52. l = void 0
  53. }
  54. ),
  55. this._joystick
  56. }
  57. show() {
  58. if (!this._joystick)
  59. throw new Error("joystick is not created");
  60. this._zone.style.display = "block"
  61. }
  62. hide() {
  63. if (!this._joystick)
  64. throw new Error("joystick is not created");
  65. this._zone.style.display = "none"
  66. }
  67. }