shopui.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 = stageData.potionInventory.groupCount()
  38. Object.keys(groupedPotionInventory).forEach((potionName) => {
  39. const potionToDisplay = stageData.potionInfo.find(potion => potion.name == potionName)
  40. const potionContainer = document.createElement('div')
  41. potionContainer.className = "potion-container"
  42. potionContainer.title = potionToDisplay.title
  43. potionContainer.setAttribute('data-potion', potionToDisplay.name)
  44. const potionImage = document.createElement('img')
  45. potionImage.src = potionToDisplay.image
  46. potionImage.title = potionToDisplay.title
  47. const potionCount = document.createElement('div')
  48. potionCount.className = 'potion-inventory-count'
  49. potionCount.innerHTML = `${groupedPotionInventory[potionName] ?? 0}`
  50. potionContainer.appendChild(potionImage)
  51. potionContainer.appendChild(potionCount)
  52. container.appendChild(potionContainer)
  53. })
  54. const potionButtons = document.getElementById('shop-potions-container').children
  55. for (const button of potionButtons) {
  56. button.addEventListener("click", (event) => {
  57. event.stopPropagation()
  58. if (stageData.potionStocked.length < 6) {
  59. stageData.soundEffects['audio/click1.ogg'].play()
  60. const potionName = button.getAttribute('data-potion')
  61. addValueToSave(stageData.potionStocked, 'potion-stocked', potionName)
  62. removeValueFromSave(stageData.potionInventory, 'potion-inventory', potionName)
  63. stageData.potionInventory.sort()
  64. stageData.potionStocked.sort()
  65. //playBottleClink()
  66. }
  67. displayPotionInventory(game, stageData)
  68. displayStockedPotions(game, stageData)
  69. })
  70. }
  71. }
  72. function displayStockedPotions(game, stageData) {
  73. const container = document.getElementById('shop-shelves-container')
  74. container.innerHTML = ''
  75. let bottleCount = 0
  76. for (let i = 0; i < 6; i++) {
  77. if (bottleCount % 2 == 0) {
  78. const shelf = document.createElement('div')
  79. shelf.className = 'shop-shelf'
  80. container.appendChild(shelf)
  81. }
  82. const currentShelf = container.getElementsByClassName('shop-shelf')[Math.floor(bottleCount / 2)]
  83. bottleCount++
  84. const potionName = stageData.potionStocked[i]
  85. if (!potionName) {
  86. const potionContainer = document.createElement('div')
  87. potionContainer.className = "stocked-potion-container disabled-stocked-potion"
  88. currentShelf.appendChild(potionContainer)
  89. continue
  90. }
  91. const potionToDisplay = stageData.potionInfo.find(potion => potion.name == potionName)
  92. const potionContainer = document.createElement('div')
  93. potionContainer.className = "stocked-potion-container"
  94. potionContainer.title = potionToDisplay.title
  95. potionContainer.setAttribute('data-potion', potionToDisplay.name)
  96. const potionImage = document.createElement('img')
  97. potionImage.src = potionToDisplay.image
  98. potionImage.title = potionToDisplay.title
  99. potionContainer.appendChild(potionImage)
  100. currentShelf.appendChild(potionContainer)
  101. }
  102. const potionStockedButtons = document.getElementById('shop-shelves-container').getElementsByClassName('stocked-potion-container')
  103. for (const button of potionStockedButtons) {
  104. if (button.classList.contains('disabled-stocked-potion')) {
  105. continue
  106. }
  107. button.addEventListener("click", (event) => {
  108. event.stopPropagation()
  109. if (stageData.potionStocked.length > 0) {
  110. stageData.soundEffects['audio/click1.ogg'].play()
  111. const potionName = button.getAttribute('data-potion')
  112. removeValueFromSave(stageData.potionStocked, 'potion-stocked', potionName)
  113. addValueToSave(stageData.potionInventory, 'potion-inventory', potionName)
  114. stageData.potionInventory.sort()
  115. stageData.potionStocked.sort()
  116. //playBottleClink()
  117. }
  118. displayPotionInventory(game, stageData)
  119. displayStockedPotions(game, stageData)
  120. })
  121. }
  122. updatePotionShelfDisplay()
  123. }
  124. export async function openShopUI(game, stageData) {
  125. const shopContainer = document.getElementById("shop-container")
  126. shopContainer.style.display = "block"
  127. displayPotionInventory(game, stageData)
  128. displayStockedPotions(game, stageData)
  129. stageData.soundEffects['audio/drawKnife2.ogg'].play()
  130. const shopPanel = document.getElementById("shop-panel")
  131. gsap.to(shopPanel, {
  132. x: 0, duration: 0.8, onComplete: () => {
  133. stageData.chest.isStocking = true
  134. }
  135. })
  136. }
  137. export async function closeShopUI(game, stageData) {
  138. stageData.chest.isStocking = false
  139. stageData.soundEffects['audio/drawKnife2.ogg'].play()
  140. const shopPanel = document.getElementById("shop-panel")
  141. gsap.to(shopPanel, {
  142. x: 1024, duration: 0.8, onComplete: () => {
  143. const shopContainer = document.getElementById("shop-container")
  144. shopContainer.style.display = "none"
  145. }
  146. })
  147. }