gameui.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import gsap from 'gsap'
  2. import '../library/arrayhelpers.js'
  3. const unknownPotion = {"image": "./images/potion_unknown.png", "title": "Unknown", "properties": []}
  4. function displaySelectedIngredients(game, stageData) {
  5. const ingredientSelectedButtons = document.getElementById('brew-selected-container').children
  6. for(let buttonIndex = 0; buttonIndex < ingredientSelectedButtons.length; buttonIndex++) {
  7. ingredientSelectedButtons[buttonIndex].classList.add("disabled")
  8. ingredientSelectedButtons[buttonIndex].getElementsByClassName('disabled-selected-ingredient')[0].style.display = "block"
  9. ingredientSelectedButtons[buttonIndex].getElementsByClassName('active-selected-ingredient')[0].style.display = "none"
  10. }
  11. for(let ingredientIndex = 0; ingredientIndex < stageData.selectedIngredients.length; ingredientIndex++) {
  12. const ingredientName = stageData.selectedIngredients[ingredientIndex]
  13. const ingredientData = stageData.ingredientInfo[ingredientName]
  14. ingredientSelectedButtons[ingredientIndex].classList.remove("disabled")
  15. ingredientSelectedButtons[ingredientIndex].getElementsByClassName('disabled-selected-ingredient')[0].style.display = "none"
  16. const selectedIngredient = ingredientSelectedButtons[ingredientIndex].getElementsByClassName('active-selected-ingredient')[0]
  17. selectedIngredient.style.display = "flex"
  18. selectedIngredient.getElementsByClassName("selected-ingredient-image")[0].getElementsByTagName('img')[0].src = ingredientData.image
  19. selectedIngredient.getElementsByClassName("selected-ingredient-description")[0].getElementsByTagName('h2')[0].innerText = ingredientData.title
  20. selectedIngredient.getElementsByClassName("selected-ingredient-description")[0].getElementsByTagName('p')[0].innerText = ingredientData.description
  21. }
  22. }
  23. function displayBrewPotion(game, stageData) {
  24. const possiblePotions = stageData.potionInfo.filter(potion => stageData.selectedIngredients.equals(potion.ingredients, false))
  25. let potionToBrew = unknownPotion
  26. stageData.potionToBrew = null
  27. if(possiblePotions.length == 1) {
  28. potionToBrew = possiblePotions[0]
  29. stageData.potionToBrew = potionToBrew
  30. }
  31. document.getElementById('potion-icon').innerHTML = ''
  32. let potionIcon = document.createElement('img')
  33. potionIcon.src = potionToBrew.image
  34. document.getElementById('potion-icon').appendChild(potionIcon)
  35. const groupedPotionInventory = stageData.potionInventory.groupCount()
  36. const potionCount = groupedPotionInventory[potionToBrew.name] ?? 0
  37. const potionCountElement = document.getElementById('potion-icon-container').getElementsByClassName("potion-inventory-count")[0]
  38. if(potionCount == 0) {
  39. potionCountElement.style.display = "none"
  40. } else {
  41. potionCountElement.style.display = "block"
  42. potionCountElement.innerHTML = potionCount
  43. }
  44. document.getElementById('potion-name').innerText = potionToBrew.title
  45. document.getElementById('potion-properties-list').innerHTML = ''
  46. potionToBrew.properties.forEach(property => {
  47. let propElement = document.createElement('li')
  48. propElement.innerText = property
  49. document.getElementById('potion-properties-list').appendChild(propElement)
  50. })
  51. }
  52. export function updateIngredients(game, stageData) {
  53. const container = document.getElementById("brew-ingredients-container")
  54. container.innerHTML = ''
  55. const groupedIngredientInventory = stageData.ingredientInventory.groupCount()
  56. for(let ingredientName in stageData.ingredientInfo) {
  57. const info = stageData.ingredientInfo[ingredientName]
  58. const ingredientContainer = document.createElement('div')
  59. ingredientContainer.className = 'ingredient-container'
  60. ingredientContainer.setAttribute('data-ingredient', ingredientName)
  61. const ingredientImage = document.createElement('img')
  62. ingredientImage.src = info.image
  63. ingredientImage.title = info.title
  64. const ingredientCount = document.createElement('div')
  65. ingredientCount.className = 'ingredient-inventory-brew-count'
  66. ingredientCount.innerHTML = groupedIngredientInventory[ingredientName] ?? 0
  67. ingredientContainer.appendChild(ingredientImage)
  68. ingredientContainer.appendChild(ingredientCount)
  69. ingredientContainer.addEventListener('click', (event) => {
  70. event.stopPropagation()
  71. if(stageData.selectedIngredients.length < 2) {
  72. stageData.soundEffects['audio/click1.ogg'].play()
  73. const ingredientName = ingredientContainer.getAttribute('data-ingredient')
  74. stageData.selectedIngredients.push(ingredientName)
  75. displaySelectedIngredients(game, stageData)
  76. }
  77. displayBrewPotion(game, stageData)
  78. })
  79. container.appendChild(ingredientContainer)
  80. }
  81. }
  82. export async function brewUI(game, stageData) {
  83. document.getElementById("brew-container").addEventListener('click', () => {
  84. closeBrewUI(game, stageData)
  85. })
  86. document.getElementById('brew-right-side').addEventListener('click', (event) => {
  87. event.stopPropagation()
  88. })
  89. updateIngredients(game, stageData)
  90. const ingredientSelectedButtons = document.getElementById('brew-selected-container').children
  91. for(let buttonIndex = 0; buttonIndex < ingredientSelectedButtons.length; buttonIndex++) {
  92. const button = ingredientSelectedButtons[buttonIndex]
  93. button.addEventListener("click", (event) => {
  94. event.stopPropagation()
  95. if(button.classList.contains('disabled')) {
  96. return
  97. }
  98. stageData.soundEffects['audio/click1.ogg'].play()
  99. stageData.selectedIngredients.splice(buttonIndex, 1)
  100. displaySelectedIngredients(game, stageData)
  101. displayBrewPotion(game, stageData)
  102. })
  103. }
  104. document.getElementById("start-brew").addEventListener('click', () => {
  105. stageData.beginBrew()
  106. })
  107. }
  108. export async function openBrewUI(game,stageData) {
  109. if(stageData.cauldron.brewMenuOpen) {
  110. return
  111. }
  112. const brewContainer = document.getElementById("brew-container")
  113. brewContainer.style.display = "block"
  114. if(game.canvas.clientWidth >= 1024) {
  115. let currentPosition = stageData.cameraPositions[stageData.currentRoom-1]
  116. gsap.to(game.lookAtFocus, {x: currentPosition.focus.x + 2.5, duration: 0.8})
  117. gsap.to(game.camera.position, {x: currentPosition.camera.x + 2.5, duration: 0.8})
  118. } else {
  119. let currentPosition = stageData.cameraPositions[stageData.currentRoom-1]
  120. gsap.to(game.lookAtFocus, {y: currentPosition.focus.y - 1.5, duration: 0.8})
  121. gsap.to(game.camera.position, {y: currentPosition.camera.y - 1.5, duration: 0.8})
  122. }
  123. updateIngredients(game, stageData)
  124. displaySelectedIngredients(game, stageData)
  125. displayBrewPotion(game, stageData)
  126. stageData.soundEffects['audio/drawKnife2.ogg'].play()
  127. const brewRightSide = document.getElementById("brew-right-side")
  128. gsap.to(brewRightSide, {x: 0, duration: 0.8, onComplete: () => {
  129. stageData.cauldron.brewMenuOpen = true
  130. }})
  131. }
  132. export async function closeBrewUI(game, stageData) {
  133. if(!stageData.cauldron.brewMenuOpen) {
  134. return
  135. }
  136. stageData.cauldron.brewMenuOpen = false
  137. const brewContainer = document.getElementById("brew-container")
  138. if(game.canvas.clientWidth >= 1024) {
  139. let currentPosition = stageData.cameraPositions[stageData.currentRoom-1]
  140. gsap.to(game.lookAtFocus, {x: currentPosition.focus.x, duration: 0.8})
  141. gsap.to(game.camera.position, {x: currentPosition.camera.x, duration: 0.8})
  142. } else {
  143. let currentPosition = stageData.cameraPositions[stageData.currentRoom-1]
  144. gsap.to(game.lookAtFocus, {y: currentPosition.focus.y, duration: 0.8})
  145. gsap.to(game.camera.position, {y: currentPosition.camera.y, duration: 0.8})
  146. }
  147. stageData.soundEffects['audio/drawKnife2.ogg'].play()
  148. const brewRightSide = document.getElementById("brew-right-side")
  149. gsap.to(brewRightSide, {x: 1024, duration: 0.8, onComplete: () => {
  150. brewContainer.style.display = "none"
  151. }})
  152. }