marketui.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import gsap from "gsap"
  2. export function marketUI(game, stageData) {
  3. document.getElementById("market-container").addEventListener('click', () => {
  4. closeMarketUI(game, stageData)
  5. })
  6. document.getElementById('market-panel').addEventListener('click', (event) => {
  7. event.stopPropagation()
  8. })
  9. document.getElementById('buy-ingredients').addEventListener('click', (event) => {
  10. stageData.buyIngredients()
  11. })
  12. }
  13. let cartTotal = 0
  14. export function populateMarketIngredients(game, stageData) {
  15. const ingredientList = document.getElementById("ingredients-list")
  16. ingredientList.innerHTML = ''
  17. const groupedIngredientCount = stageData.ingredientInventory.groupCount()
  18. for(let ingredientName in stageData.ingredientInfo) {
  19. const info = stageData.ingredientInfo[ingredientName]
  20. const sellContainer = document.createElement('div')
  21. sellContainer.className = "ingredient-sell-container"
  22. sellContainer.setAttribute('data-ingredient', ingredientName)
  23. const iconContainer = document.createElement('div')
  24. iconContainer.className = "ingredient-icon-container"
  25. const ingredientIcon = document.createElement('img')
  26. ingredientIcon.src = info.image
  27. const inventoryCount = document.createElement('div')
  28. inventoryCount.className = 'ingredient-inventory-count'
  29. inventoryCount.innerHTML = groupedIngredientCount[ingredientName] ?? 0
  30. iconContainer.appendChild(ingredientIcon)
  31. iconContainer.appendChild(inventoryCount)
  32. const descriptionContainer = document.createElement('div')
  33. descriptionContainer.className = 'ingredient-description'
  34. const title = document.createElement('h2')
  35. title.innerHTML = info.title
  36. const ingredientPrice = document.createElement('div')
  37. ingredientPrice.className = "ingredient-price"
  38. const coinImage = document.createElement('img')
  39. coinImage.src = "./images/coin.svg"
  40. const priceValue = document.createElement('div')
  41. priceValue.className = 'ingredient-price-value'
  42. priceValue.innerHTML = info.cost
  43. ingredientPrice.appendChild(coinImage)
  44. ingredientPrice.appendChild(priceValue)
  45. const callToAction = document.createElement('div')
  46. callToAction.className = 'call-to-action'
  47. callToAction.innerHTML = "Click to add to order"
  48. callToAction.addEventListener('click', event => {
  49. const ingredientToAdd = sellContainer.getAttribute('data-ingredient')
  50. const infoToAdd = stageData.ingredientInfo[ingredientToAdd]
  51. if(infoToAdd.cost + cartTotal > stageData.currency) {
  52. stageData.soundEffects['audio/witch_cackle1.ogg'].play()
  53. return
  54. }
  55. stageData.soundEffects['audio/click1.ogg'].play()
  56. stageData.cartItems.push(ingredientToAdd)
  57. inCartAmount.innerHTML = stageData.cartItems.filter(item => item == ingredientName).length
  58. updateCartTotal(game, stageData)
  59. })
  60. descriptionContainer.appendChild(title)
  61. descriptionContainer.appendChild(ingredientPrice)
  62. descriptionContainer.appendChild(callToAction)
  63. const inCartContainer = document.createElement('div')
  64. inCartContainer.className = 'ingredient-in-cart-container'
  65. const inCartAmount = document.createElement('div')
  66. inCartAmount.className = 'ingredient-in-cart-amount'
  67. inCartAmount.innerHTML = 0
  68. inCartContainer.appendChild(inCartAmount)
  69. const addIngredient = document.createElement('button')
  70. addIngredient.className = "add-ingredient-button"
  71. addIngredient.innerHTML = "+"
  72. addIngredient.addEventListener('click', () => {
  73. const ingredientToAdd = sellContainer.getAttribute('data-ingredient')
  74. const infoToAdd = stageData.ingredientInfo[ingredientToAdd]
  75. if(infoToAdd.cost + cartTotal > stageData.currency) {
  76. stageData.soundEffects['audio/witch_cackle1.ogg'].play()
  77. return
  78. }
  79. stageData.soundEffects['audio/click1.ogg'].play()
  80. stageData.cartItems.push(ingredientToAdd)
  81. inCartAmount.innerHTML = stageData.cartItems.filter(item => item == ingredientName).length
  82. updateCartTotal(game, stageData)
  83. })
  84. const removeIngredient = document.createElement('button')
  85. removeIngredient.className = "remove-ingredient-button"
  86. removeIngredient.innerHTML = "-"
  87. removeIngredient.addEventListener('click', () => {
  88. const ingredientToRemove = sellContainer.getAttribute('data-ingredient')
  89. if(stageData.cartItems.indexOf(ingredientToRemove) != -1) {
  90. stageData.soundEffects['audio/click1.ogg'].play()
  91. stageData.cartItems.splice(stageData.cartItems.indexOf(ingredientToRemove), 1)
  92. inCartAmount.innerHTML = stageData.cartItems.filter(item => item == ingredientName).length
  93. updateCartTotal(game, stageData)
  94. }
  95. })
  96. iconContainer.addEventListener('click', () => {
  97. const ingredientToAdd = sellContainer.getAttribute('data-ingredient')
  98. const infoToAdd = stageData.ingredientInfo[ingredientToAdd]
  99. if(infoToAdd.cost + cartTotal > stageData.currency) {
  100. stageData.soundEffects['audio/witch_cackle1.ogg'].play()
  101. return
  102. }
  103. stageData.soundEffects['audio/click1.ogg'].play()
  104. stageData.cartItems.push(ingredientToAdd)
  105. inCartAmount.innerHTML = stageData.cartItems.filter(item => item == ingredientName).length
  106. updateCartTotal(game, stageData)
  107. })
  108. inCartContainer.addEventListener('click', () => {
  109. const ingredientToRemove = sellContainer.getAttribute('data-ingredient')
  110. if(stageData.cartItems.indexOf(ingredientToRemove) != -1) {
  111. stageData.soundEffects['audio/click1.ogg'].play()
  112. stageData.cartItems.splice(stageData.cartItems.indexOf(ingredientToRemove), 1)
  113. inCartAmount.innerHTML = stageData.cartItems.filter(item => item == ingredientName).length
  114. updateCartTotal(game, stageData)
  115. }
  116. })
  117. sellContainer.appendChild(iconContainer)
  118. sellContainer.appendChild(descriptionContainer)
  119. sellContainer.appendChild(addIngredient)
  120. sellContainer.appendChild(inCartContainer)
  121. sellContainer.appendChild(removeIngredient)
  122. ingredientList.appendChild(sellContainer)
  123. }
  124. updateCartTotal(game, stageData)
  125. }
  126. export function updateCartTotal(game, stageData) {
  127. cartTotal = 0
  128. stageData.cartItems.forEach((ingredientName) => {
  129. const info = stageData.ingredientInfo[ingredientName]
  130. cartTotal += info.cost
  131. })
  132. document.getElementById("total-amount-value").innerHTML = cartTotal
  133. }
  134. export function openMarketUI(game, stageData) {
  135. if(stageData.sellingSkeleton.marketMenuOpen) {
  136. return
  137. }
  138. const marketContainer = document.getElementById("market-container")
  139. marketContainer.style.display = "block"
  140. populateMarketIngredients(game, stageData)
  141. stageData.soundEffects['audio/drawKnife2.ogg'].play()
  142. const marketPanel = document.getElementById("market-panel")
  143. gsap.to(marketPanel, {
  144. x: 0, duration: 0.8, onComplete: () => {
  145. stageData.sellingSkeleton.marketMenuOpen = true
  146. }
  147. })
  148. }
  149. export function closeMarketUI(game, stageData) {
  150. if(!stageData.sellingSkeleton.marketMenuOpen) {
  151. return
  152. }
  153. stageData.sellingSkeleton.marketMenuOpen = false
  154. stageData.cartItems = []
  155. stageData.soundEffects['audio/drawKnife2.ogg'].play()
  156. const marketPanel = document.getElementById("market-panel")
  157. gsap.to(marketPanel, {
  158. x: 1024, duration: 0.8, onComplete: () => {
  159. const marketContainer = document.getElementById("market-container")
  160. marketContainer.style.display = "none"
  161. }
  162. })
  163. }