gameui.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 = {}
  36. stageData.potionInventory.forEach(potionName => {
  37. if (!groupedPotionInventory[potionName]) {
  38. groupedPotionInventory[potionName] = 1
  39. } else {
  40. groupedPotionInventory[potionName]++
  41. }
  42. })
  43. const potionCount = groupedPotionInventory[potionToBrew.name] ?? 0
  44. const potionCountElement = document.getElementById('potion-icon-container').getElementsByClassName("potion-inventory-count")[0]
  45. if(potionCount == 0) {
  46. potionCountElement.style.display = "none"
  47. } else {
  48. potionCountElement.style.display = "block"
  49. potionCountElement.innerHTML = potionCount
  50. }
  51. document.getElementById('potion-name').innerText = potionToBrew.title
  52. document.getElementById('potion-properties-list').innerHTML = ''
  53. potionToBrew.properties.forEach(property => {
  54. let propElement = document.createElement('li')
  55. propElement.innerText = property
  56. document.getElementById('potion-properties-list').appendChild(propElement)
  57. })
  58. }
  59. export async function brewUI(game, stageData) {
  60. document.getElementById("brew-container").addEventListener('click', () => {
  61. closeBrewUI(game, stageData)
  62. })
  63. document.getElementById('brew-right-side').addEventListener('click', (event) => {
  64. event.stopPropagation()
  65. })
  66. const ingredientSelectedButtons = document.getElementById('brew-selected-container').children
  67. const ingredientButtons = document.getElementById('brew-ingredients-container').children
  68. for(const button of ingredientButtons) {
  69. button.addEventListener("click", (event) => {
  70. event.stopPropagation()
  71. if(stageData.selectedIngredients.length < 2) {
  72. stageData.soundEffects['audio/click1.ogg'].play()
  73. const ingredientName = button.getAttribute('data-ingredient')
  74. stageData.selectedIngredients.push(ingredientName)
  75. displaySelectedIngredients(game, stageData)
  76. }
  77. displayBrewPotion(game, stageData)
  78. })
  79. }
  80. for(let buttonIndex = 0; buttonIndex < ingredientSelectedButtons.length; buttonIndex++) {
  81. const button = ingredientSelectedButtons[buttonIndex]
  82. button.addEventListener("click", (event) => {
  83. event.stopPropagation()
  84. if(button.classList.contains('disabled')) {
  85. return
  86. }
  87. stageData.soundEffects['audio/click1.ogg'].play()
  88. stageData.selectedIngredients.splice(buttonIndex, 1)
  89. displaySelectedIngredients(game, stageData)
  90. displayBrewPotion(game, stageData)
  91. })
  92. }
  93. document.getElementById("start-brew").addEventListener('click', () => {
  94. stageData.beginBrew()
  95. })
  96. }
  97. export async function openBrewUI(game,stageData) {
  98. if(stageData.cauldron.brewMenuOpen) {
  99. return
  100. }
  101. const brewContainer = document.getElementById("brew-container")
  102. brewContainer.style.display = "block"
  103. if(game.canvas.clientWidth >= 1024) {
  104. let currentPosition = stageData.cameraPositions[stageData.currentRoom-1]
  105. gsap.to(game.lookAtFocus, {x: currentPosition.focus.x + 2.5, duration: 0.8})
  106. gsap.to(game.camera.position, {x: currentPosition.camera.x + 2.5, duration: 0.8})
  107. } else {
  108. let currentPosition = stageData.cameraPositions[stageData.currentRoom-1]
  109. gsap.to(game.lookAtFocus, {y: currentPosition.focus.y - 1.5, duration: 0.8})
  110. gsap.to(game.camera.position, {y: currentPosition.camera.y - 1.5, duration: 0.8})
  111. }
  112. displaySelectedIngredients(game, stageData)
  113. displayBrewPotion(game, stageData)
  114. const brewRightSide = document.getElementById("brew-right-side")
  115. gsap.to(brewRightSide, {x: 0, duration: 0.8, onComplete: () => {
  116. stageData.cauldron.brewMenuOpen = true
  117. }})
  118. }
  119. export async function closeBrewUI(game, stageData) {
  120. if(!stageData.cauldron.brewMenuOpen) {
  121. return
  122. }
  123. stageData.cauldron.brewMenuOpen = false
  124. const brewContainer = document.getElementById("brew-container")
  125. if(game.canvas.clientWidth >= 1024) {
  126. let currentPosition = stageData.cameraPositions[stageData.currentRoom-1]
  127. gsap.to(game.lookAtFocus, {x: currentPosition.focus.x, duration: 0.8})
  128. gsap.to(game.camera.position, {x: currentPosition.camera.x, duration: 0.8})
  129. } else {
  130. let currentPosition = stageData.cameraPositions[stageData.currentRoom-1]
  131. gsap.to(game.lookAtFocus, {y: currentPosition.focus.y, duration: 0.8})
  132. gsap.to(game.camera.position, {y: currentPosition.camera.y, duration: 0.8})
  133. }
  134. const brewRightSide = document.getElementById("brew-right-side")
  135. gsap.to(brewRightSide, {x: 1024, duration: 0.8, onComplete: () => {
  136. brewContainer.style.display = "none"
  137. }})
  138. }