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.

168 lines
5.5 KiB

1 year ago
1 year ago
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. Hooks.once("updateToken", (token, updates) => {
  79. const animation = canvas.tokens.get(token.id)?._animation;
  80. if (animation) {
  81. animation.then(() => token?.update(newUpdates));
  82. } else {
  83. token?.update(newUpdates);
  84. }
  85. });
  86. }
  87. }
  88. }
  89. static getStairs() {
  90. let stairs = [];
  91. for (let drawing of canvas.drawings.placeables) {
  92. let { rangeBottom, rangeTop, drawingMode } =
  93. this.getFlagsForObject(drawing);
  94. let isLocked = drawing.document.getFlag(
  95. CONFIG.Levels.MODULE_ID,
  96. "stairLocked"
  97. );
  98. if (
  99. (drawingMode == 2 || drawingMode == 3 || drawingMode == 21 || drawingMode == 22) &&
  100. rangeBottom != -Infinity &&
  101. rangeTop != Infinity &&
  102. !isLocked
  103. ) {
  104. let p = new PIXI.Polygon(adjustPolygonPoints(drawing));
  105. stairs.push({
  106. drawing: drawing,
  107. poly: p,
  108. range: [rangeBottom, rangeTop + 1],
  109. drawingMode: drawingMode,
  110. });
  111. }
  112. }
  113. return stairs;
  114. }
  115. static async renderElevatorDalog(levelsFlag) {
  116. //await new Promise((resolve) => setTimeout(resolve, 1000));
  117. let elevatorFloors = [];
  118. levelsFlag.split("|").forEach((f) => {
  119. elevatorFloors.push(f.split(","));
  120. });
  121. let content = `<div id="levels-elevator">`;
  122. elevatorFloors.forEach((f) => {
  123. content += `<div class="button">
  124. <button id="${f[0]}" class="elevator-level">${f[1]}</button>
  125. </div>`;
  126. });
  127. content += `<hr></div>`;
  128. let dialog = new Dialog({
  129. title: game.i18n.localize("levels.dialog.elevator.title"),
  130. content: content,
  131. buttons: {
  132. close: {
  133. label: game.i18n.localize("levels.yesnodialog.no"),
  134. callback: () => {},
  135. },
  136. },
  137. default: "close",
  138. close: () => {},
  139. });
  140. await dialog._render(true);
  141. let renderedFrom = $("body").find(`div[id="levels-elevator"]`);
  142. for (let btn of $(renderedFrom).find("button")) {
  143. $(btn).on("click", updateElev);
  144. }
  145. function updateElev(event) {
  146. let newElev = parseFloat(event.target.id);
  147. if (newElev || newElev == 0)
  148. canvas.tokens.controlled[0]?.document?.update({ elevation: newElev });
  149. }
  150. }
  151. static getFlagsForObject(drawing) {
  152. return {
  153. rangeBottom: drawing.document.flags?.levels?.rangeBottom ?? -Infinity,
  154. rangeTop: drawing.document.flags?.levels?.rangeTop ?? Infinity,
  155. drawingMode: drawing.document.flags?.levels?.drawingMode ?? 0,
  156. };
  157. }
  158. }