PlayerCraftingHud.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. require('TemplateLoader');
  2. class PlayerCraftingHud {
  3. constructor() {
  4. this.templateLoader = new TemplateLoader();
  5. this.containerId = "crafting-container";
  6. }
  7. async attach(parent) {
  8. let template = await this.templateLoader.get('crafting');
  9. let container = document.getElementById(this.containerId);
  10. container.innerHTML = template;
  11. this.recipes = document.getElementById('crafting--recipes');
  12. this.hide();
  13. }
  14. init() {
  15. this.buildRecipes();
  16. }
  17. buildRecipes() {
  18. this.recipes.innerHTML = "";
  19. game.crafting.getAllRecipes().forEach((recipe) => {
  20. let recipeRow = document.createElement("div");
  21. let recipeButton = document.createElement('button');
  22. let ingredients = recipe.ingredients.reduce((list, item) => {
  23. return `${list} ${item.amount} ${item.name}`;
  24. }, "");
  25. let results = recipe.results.reduce((list, item) => {
  26. return `${list} ${item.amount} ${item.name} `;
  27. }, "");
  28. recipeButton.innerHTML = `${ingredients} -> ${results}`;
  29. recipeButton.setAttribute('data-recipe-id', recipe.id);
  30. recipeButton.addEventListener('click', (event) => this.craftRecipe(event));
  31. recipeRow.appendChild(recipeButton);
  32. this.recipes.appendChild(recipeRow);
  33. });
  34. }
  35. craftRecipe(event) {
  36. game.craftRecipe(event.target.getAttribute('data-recipe-id'));
  37. }
  38. hide() {
  39. document.getElementById(this.containerId).style.opacity = "0";
  40. document.getElementById(this.containerId).style.visibility = "hidden";
  41. }
  42. show() {
  43. document.getElementById(this.containerId).style.opacity = "1";
  44. document.getElementById(this.containerId).style.visibility = "visible";
  45. }
  46. }