Advertisement
Gudu0

UMT Utility JS

May 5th, 2025 (edited)
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | Source Code | 0 0
  1. import machineData from './machine_data.js';
  2. import oreData from './ore_data.js'; // Add this if not already imported
  3.  
  4. const variables = setupVariables();
  5.  
  6. function setupVariables() {
  7. let selectedImage = 'Black_Square32.jpg';
  8. let globalRotationCount = 0;
  9. let machineIdCounter = 1;
  10. let currentHoverCell = null;
  11. let isMouseDown = false;
  12. let lastClicked = '';
  13.  
  14. let showPreviewtoggle = true; // Default to showing preview
  15. let allowPlacementOver = true; // Default to allowing placement over machines
  16.  
  17. showPreviewtoggle = localStorage.getItem('showPreview') === 'false' ? false : true;
  18. allowPlacementOver = localStorage.getItem('allowOverlap') === 'false' ? false : true;
  19.  
  20. return {
  21. selectedImage,
  22. globalRotationCount,
  23. machineIdCounter,
  24. currentHoverCell,
  25. isMouseDown,
  26. showPreviewtoggle,
  27. allowPlacementOver,
  28. lastClicked
  29. };
  30. }
  31.  
  32. function calculateTotalCost() {
  33. const allTiles = document.querySelectorAll('.tile');
  34. const seenMachines = new Set();
  35. let totalCost = 0;
  36.  
  37. const ignoredImages = ['Black_Square32.jpg', 'Red_Square32.jpg', 'White_Square32.jpg', 'Input.png']; // Add names of images to ignore
  38.  
  39. for (const tile of allTiles) {
  40. const id = tile.dataset.machineId;
  41. const img = tile.style.backgroundImage;
  42. if (id && !seenMachines.has(id)) {
  43. const match = img.match(/Images\/(.+?)['")]/);
  44. if (match) {
  45. const imageName = match[1];
  46. const machine = machineData[imageName];
  47. if (machine && machine.cost) {
  48. totalCost += machine.cost;
  49. } else if (!ignoredImages.includes(imageName)) {
  50. console.warn(`No cost defined for machine: ${imageName}`);
  51. }
  52. seenMachines.add(id);
  53. }
  54. }
  55. }
  56.  
  57. document.getElementById('costDisplay').textContent = `Total Factory Cost: $${totalCost}`;
  58. }
  59.  
  60. // Filtering functionality
  61. document.querySelectorAll('#filter-buttons button').forEach(button => {
  62. button.addEventListener('click', () => {
  63. const filter = button.dataset.filter;
  64.  
  65. document.querySelectorAll('#image-selector img').forEach(img => {
  66. if (filter === 'all' || img.dataset.category === filter) {
  67. img.style.display = '';
  68. } else {
  69. img.style.display = 'none';
  70. }
  71. });
  72.  
  73. // Optional: highlight the active filter button
  74. document.querySelectorAll('#filter-buttons button').forEach(b => b.classList.remove('active-filter'));
  75. button.classList.add('active-filter');
  76. });
  77. });
  78.  
  79. function generateUniqueMachineId() {
  80. let id = `machine-${variables.machineIdCounter++}`;
  81. // Check if the ID already exists
  82. while (document.querySelector(`[data-machine-id="${id}"]`)) {
  83. id = `machine-${variables.machineIdCounter++}`; // Increment and try again if the ID exists
  84. }
  85. return id;
  86. }
  87.  
  88. function handleImageSize(img) {
  89. if (img.naturalWidth === 32 && img.naturalHeight === 32) {
  90. img.classList.add('small-img');
  91. } else {
  92. img.classList.add('large-img');
  93. img.classList.remove('small-img'); // Just in case it was wrongly applied
  94. }
  95. }
  96.  
  97. export { calculateTotalCost, setupVariables, generateUniqueMachineId, handleImageSize };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement