All user data for FoundryVTT. Includes worlds, systems, modules, and any asset in the "foundryuserdata" directory. Does NOT include the FoundryVTT installation itself.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
5.6 KiB

1 year ago
  1. import { adjustPolygonPoints } from "../helpers.js";
  2. export class DrawingHandler {
  3. static isDrawingVisible(drawing) {
  4. const currentElevation = CONFIG.Levels.currentToken?.losHeight
  5. const rangeBottom = drawing.document.flags.levels?.rangeBottom ?? -Infinity;
  6. if(currentElevation === undefined) return true;
  7. const isVisible = rangeBottom <= currentElevation;
  8. return isVisible;
  9. }
  10. static executeStairs(updates, token) {
  11. if ("x" in updates || "y" in updates) {
  12. let stairs = this.getStairs();
  13. let tokenX = updates.x || token.x;
  14. let tokenY = updates.y || token.y;
  15. let newUpdates;
  16. let tokenElev = updates.elevation || token.elevation;
  17. let gridSize = canvas.scene.dimensions.size;
  18. let newTokenCenter = {
  19. x: tokenX + (gridSize * token.width) / 2,
  20. y: tokenY + (gridSize * token.height) / 2,
  21. };
  22. let inStair;
  23. for (let stair of stairs) {
  24. if (stair.poly.contains(newTokenCenter.x, newTokenCenter.y)) {
  25. if (token.inStair == stair.drawing.id) {
  26. inStair = stair.drawing.id;
  27. } else {
  28. if (stair.drawingMode == 2) {
  29. if (tokenElev <= stair.range[1] && tokenElev >= stair.range[0]) {
  30. if (tokenElev == stair.range[1]) {
  31. inStair = stair.drawing.id;
  32. newUpdates = { elevation: stair.range[0] };
  33. }
  34. if (tokenElev == stair.range[0]) {
  35. inStair = stair.drawing.id;
  36. newUpdates = { elevation: stair.range[1] };
  37. }
  38. }
  39. } else if (stair.drawingMode == 21) {
  40. if (tokenElev <= stair.range[1] && tokenElev >= stair.range[0]) {
  41. if (tokenElev == stair.range[1]) {
  42. inStair = stair.drawing.id;
  43. newUpdates = { elevation: stair.range[0] };
  44. }
  45. }
  46. } else if (stair.drawingMode == 22) {
  47. if (tokenElev <= stair.range[1] && tokenElev >= stair.range[0]) {
  48. if (tokenElev == stair.range[0]) {
  49. inStair = stair.drawing.id;
  50. newUpdates = { elevation: stair.range[1] };
  51. }
  52. }
  53. } else if (stair.drawingMode == 3) {
  54. if (tokenElev <= stair.range[1] && tokenElev >= stair.range[0]) {
  55. CONFIG.Levels.handlers.DrawingHandler.renderElevatorDalog(
  56. stair.drawing.document.getFlag(
  57. CONFIG.Levels.MODULE_ID,
  58. "elevatorFloors"
  59. ),
  60. token
  61. );
  62. inStair = stair.drawing.id;
  63. }
  64. }
  65. }
  66. } else {
  67. inStair = inStair || false;
  68. }
  69. }
  70. token.inStair = inStair;
  71. if (!inStair) {
  72. $("#levels-elevator")
  73. .closest(".app")
  74. .find(`a[class="header-button close"]`)
  75. .click();
  76. }
  77. if (newUpdates) {
  78. const s = canvas.dimensions.size;
  79. const oldToken = canvas.tokens.get(token.id);
  80. const updateX = updates.x || oldToken.x;
  81. const updateY = updates.y || oldToken.y;
  82. const dist = Math.sqrt(
  83. Math.pow(oldToken.x - updateX, 2) + Math.pow(oldToken.y - updateY, 2)
  84. );
  85. const speed = s * 10;
  86. const duration = (dist * 1000) / speed;
  87. setTimeout(function () {
  88. token?.update(newUpdates);
  89. }, duration);
  90. }
  91. }
  92. }
  93. static getStairs() {
  94. let stairs = [];
  95. for (let drawing of canvas.drawings.placeables) {
  96. let { rangeBottom, rangeTop, drawingMode } =
  97. this.getFlagsForObject(drawing);
  98. let isLocked = drawing.document.getFlag(
  99. CONFIG.Levels.MODULE_ID,
  100. "stairLocked"
  101. );
  102. if (
  103. (drawingMode == 2 || drawingMode == 3 || drawingMode == 21 || drawingMode == 22) &&
  104. rangeBottom != -Infinity &&
  105. rangeTop != Infinity &&
  106. !isLocked
  107. ) {
  108. let p = new PIXI.Polygon(adjustPolygonPoints(drawing));
  109. stairs.push({
  110. drawing: drawing,
  111. poly: p,
  112. range: [rangeBottom, rangeTop + 1],
  113. drawingMode: drawingMode,
  114. });
  115. }
  116. }
  117. return stairs;
  118. }
  119. static async renderElevatorDalog(levelsFlag) {
  120. let elevatorFloors = [];
  121. levelsFlag.split("|").forEach((f) => {
  122. elevatorFloors.push(f.split(","));
  123. });
  124. let content = `<div id="levels-elevator">`;
  125. elevatorFloors.forEach((f) => {
  126. content += `<div class="button">
  127. <button id="${f[0]}" class="elevator-level">${f[1]}</button>
  128. </div>`;
  129. });
  130. content += `<hr></div>`;
  131. let dialog = new Dialog({
  132. title: game.i18n.localize("levels.dialog.elevator.title"),
  133. content: content,
  134. buttons: {
  135. close: {
  136. label: game.i18n.localize("levels.yesnodialog.no"),
  137. callback: () => {},
  138. },
  139. },
  140. default: "close",
  141. close: () => {},
  142. });
  143. await dialog._render(true);
  144. let renderedFrom = $("body").find(`div[id="levels-elevator"]`);
  145. for (let btn of $(renderedFrom).find("button")) {
  146. $(btn).on("click", updateElev);
  147. }
  148. function updateElev(event) {
  149. let newElev = parseFloat(event.target.id);
  150. if (newElev || newElev == 0)
  151. canvas.tokens.controlled[0]?.document?.update({ elevation: newElev });
  152. }
  153. }
  154. static getFlagsForObject(drawing) {
  155. return {
  156. rangeBottom: drawing.document.flags?.levels?.rangeBottom ?? -Infinity,
  157. rangeTop: drawing.document.flags?.levels?.rangeTop ?? Infinity,
  158. drawingMode: drawing.document.flags?.levels?.drawingMode ?? 0,
  159. };
  160. }
  161. }