marketui.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. descriptionContainer.appendChild(title)
  49. descriptionContainer.appendChild(ingredientPrice)
  50. descriptionContainer.appendChild(callToAction)
  51. const inCartContainer = document.createElement('div')
  52. inCartContainer.className = 'ingredient-in-cart-container'
  53. const inCartAmount = document.createElement('div')
  54. inCartAmount.className = 'ingredient-in-cart-amount'
  55. inCartAmount.innerHTML = 0
  56. inCartContainer.appendChild(inCartAmount)
  57. sellContainer.appendChild(iconContainer)
  58. iconContainer.addEventListener('click', () => {
  59. const ingredientToAdd = sellContainer.getAttribute('data-ingredient')
  60. const infoToAdd = stageData.ingredientInfo[ingredientToAdd]
  61. if(infoToAdd.cost + cartTotal > stageData.currency) {
  62. stageData.soundEffects['audio/witch_cackle1.ogg'].play()
  63. return
  64. }
  65. stageData.soundEffects['audio/click1.ogg'].play()
  66. stageData.cartItems.push(ingredientToAdd)
  67. inCartAmount.innerHTML = stageData.cartItems.filter(item => item == ingredientName).length
  68. updateCartTotal(game, stageData)
  69. })
  70. inCartContainer.addEventListener('click', () => {
  71. const ingredientToRemove = sellContainer.getAttribute('data-ingredient')
  72. if(stageData.cartItems.indexOf(ingredientToRemove) != -1) {
  73. stageData.soundEffects['audio/click1.ogg'].play()
  74. stageData.cartItems.splice(stageData.cartItems.indexOf(ingredientToRemove), 1)
  75. inCartAmount.innerHTML = stageData.cartItems.filter(item => item == ingredientName).length
  76. updateCartTotal(game, stageData)
  77. }
  78. })
  79. sellContainer.appendChild(descriptionContainer)
  80. sellContainer.appendChild(inCartContainer)
  81. ingredientList.appendChild(sellContainer)
  82. }
  83. updateCartTotal(game, stageData)
  84. }
  85. export function updateCartTotal(game, stageData) {
  86. cartTotal = 0
  87. stageData.cartItems.forEach((ingredientName) => {
  88. const info = stageData.ingredientInfo[ingredientName]
  89. cartTotal += info.cost
  90. })
  91. document.getElementById("total-amount-value").innerHTML = cartTotal
  92. }
  93. export function openMarketUI(game, stageData) {
  94. if(stageData.sellingSkeleton.marketMenuOpen) {
  95. return
  96. }
  97. const marketContainer = document.getElementById("market-container")
  98. marketContainer.style.display = "block"
  99. populateMarketIngredients(game, stageData)
  100. stageData.soundEffects['audio/drawKnife2.ogg'].play()
  101. const marketPanel = document.getElementById("market-panel")
  102. gsap.to(marketPanel, {
  103. x: 0, duration: 0.8, onComplete: () => {
  104. stageData.sellingSkeleton.marketMenuOpen = true
  105. }
  106. })
  107. }
  108. export function closeMarketUI(game, stageData) {
  109. if(!stageData.sellingSkeleton.marketMenuOpen) {
  110. return
  111. }
  112. stageData.sellingSkeleton.marketMenuOpen = false
  113. stageData.cartItems = []
  114. stageData.soundEffects['audio/drawKnife2.ogg'].play()
  115. const marketPanel = document.getElementById("market-panel")
  116. gsap.to(marketPanel, {
  117. x: 1024, duration: 0.8, onComplete: () => {
  118. const marketContainer = document.getElementById("market-container")
  119. marketContainer.style.display = "none"
  120. }
  121. })
  122. }