shopui.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import gsap from "gsap"
  2. import { addValueToSave, playBottleClink, removeValueFromSave, updatePotionShelfDisplay } from "../game.js"
  3. export function shopTutorialPrompt(stageData) {
  4. if(stageData.shopTutorial1.alreadySeen) {
  5. return
  6. }
  7. stageData.shopInactivityHandle = setTimeout(() => {
  8. gsap.to(stageData.shopTutorial1.material, {duration: 1.5, opacity: 1, onStart: () => {
  9. stageData.shopTutorial1.castShadow = true
  10. }})
  11. }, 5000)
  12. }
  13. export function potionTutorialPrompt(stageData) {
  14. if(stageData.shopTutorial2.alreadySeen) {
  15. return
  16. }
  17. stageData.sellInactivityHandle = setTimeout(() => {
  18. gsap.to(stageData.shopTutorial2.material, {duration: 1.5, opacity: 1, onStart: () => {
  19. stageData.shopTutorial2.castShadow = true
  20. }})
  21. }, 5000)
  22. }
  23. export async function shopUI(game, stageData) {
  24. document.getElementById("shop-container").addEventListener('click', () => {
  25. closeShopUI(game, stageData)
  26. })
  27. document.getElementById('shop-panel').addEventListener('click', (event) => {
  28. event.stopPropagation()
  29. })
  30. document.getElementById('start-selling').addEventListener('click', (event) => {
  31. stageData.beginSell()
  32. })
  33. }
  34. function displayPotionInventory(game, stageData) {
  35. const container = document.getElementById("shop-potions-container")
  36. container.innerHTML = ''
  37. const groupedPotionInventory = {}
  38. stageData.potionInventory.forEach(potionName => {
  39. if (!groupedPotionInventory[potionName]) {
  40. groupedPotionInventory[potionName] = 1
  41. } else {
  42. groupedPotionInventory[potionName]++
  43. }
  44. })
  45. Object.keys(groupedPotionInventory).forEach((potionName) => {
  46. const potionToDisplay = stageData.potionInfo.find(potion => potion.name == potionName)
  47. const potionContainer = document.createElement('div')
  48. potionContainer.className = "potion-container"
  49. potionContainer.title = potionToDisplay.title
  50. potionContainer.setAttribute('data-potion', potionToDisplay.name)
  51. const potionImage = document.createElement('img')
  52. potionImage.src = potionToDisplay.image
  53. potionImage.title = potionToDisplay.title
  54. const potionCount = document.createElement('div')
  55. potionCount.className = 'potion-inventory-count'
  56. potionCount.innerHTML = `${groupedPotionInventory[potionName]}`
  57. potionContainer.appendChild(potionImage)
  58. potionContainer.appendChild(potionCount)
  59. container.appendChild(potionContainer)
  60. })
  61. const potionButtons = document.getElementById('shop-potions-container').children
  62. for (const button of potionButtons) {
  63. button.addEventListener("click", (event) => {
  64. event.stopPropagation()
  65. if (stageData.potionStocked.length < 6) {
  66. stageData.soundEffects['audio/click1.ogg'].play()
  67. const potionName = button.getAttribute('data-potion')
  68. addValueToSave(stageData.potionStocked, 'potion-stocked', potionName)
  69. removeValueFromSave(stageData.potionInventory, 'potion-inventory', potionName)
  70. stageData.potionInventory.sort()
  71. stageData.potionStocked.sort()
  72. //playBottleClink()
  73. }
  74. displayPotionInventory(game, stageData)
  75. displayStockedPotions(game, stageData)
  76. })
  77. }
  78. }
  79. function displayStockedPotions(game, stageData) {
  80. const container = document.getElementById('shop-shelves-container')
  81. container.innerHTML = ''
  82. let bottleCount = 0
  83. for (let i = 0; i < 6; i++) {
  84. if (bottleCount % 2 == 0) {
  85. const shelf = document.createElement('div')
  86. shelf.className = 'shop-shelf'
  87. container.appendChild(shelf)
  88. }
  89. const currentShelf = container.getElementsByClassName('shop-shelf')[Math.floor(bottleCount / 2)]
  90. bottleCount++
  91. const potionName = stageData.potionStocked[i]
  92. if (!potionName) {
  93. const potionContainer = document.createElement('div')
  94. potionContainer.className = "stocked-potion-container disabled-stocked-potion"
  95. currentShelf.appendChild(potionContainer)
  96. continue
  97. }
  98. const potionToDisplay = stageData.potionInfo.find(potion => potion.name == potionName)
  99. const potionContainer = document.createElement('div')
  100. potionContainer.className = "stocked-potion-container"
  101. potionContainer.title = potionToDisplay.title
  102. potionContainer.setAttribute('data-potion', potionToDisplay.name)
  103. const potionImage = document.createElement('img')
  104. potionImage.src = potionToDisplay.image
  105. potionImage.title = potionToDisplay.title
  106. potionContainer.appendChild(potionImage)
  107. currentShelf.appendChild(potionContainer)
  108. }
  109. const potionStockedButtons = document.getElementById('shop-shelves-container').getElementsByClassName('stocked-potion-container')
  110. for (const button of potionStockedButtons) {
  111. if (button.classList.contains('disabled-stocked-potion')) {
  112. continue
  113. }
  114. button.addEventListener("click", (event) => {
  115. event.stopPropagation()
  116. if (stageData.potionStocked.length > 0) {
  117. stageData.soundEffects['audio/click1.ogg'].play()
  118. const potionName = button.getAttribute('data-potion')
  119. removeValueFromSave(stageData.potionStocked, 'potion-stocked', potionName)
  120. addValueToSave(stageData.potionInventory, 'potion-inventory', potionName)
  121. stageData.potionInventory.sort()
  122. stageData.potionStocked.sort()
  123. //playBottleClink()
  124. }
  125. displayPotionInventory(game, stageData)
  126. displayStockedPotions(game, stageData)
  127. })
  128. }
  129. updatePotionShelfDisplay()
  130. }
  131. export async function openShopUI(game, stageData) {
  132. const shopContainer = document.getElementById("shop-container")
  133. shopContainer.style.display = "block"
  134. displayPotionInventory(game, stageData)
  135. displayStockedPotions(game, stageData)
  136. const shopPanel = document.getElementById("shop-panel")
  137. gsap.to(shopPanel, {
  138. x: 0, duration: 0.8, onComplete: () => {
  139. stageData.chest.isStocking = true
  140. }
  141. })
  142. }
  143. export async function closeShopUI(game, stageData) {
  144. stageData.chest.isStocking = false
  145. const shopPanel = document.getElementById("shop-panel")
  146. gsap.to(shopPanel, {
  147. x: 1024, duration: 0.8, onComplete: () => {
  148. const shopContainer = document.getElementById("shop-container")
  149. shopContainer.style.display = "none"
  150. }
  151. })
  152. }