Advertisement
Guest User

randomblueprint.lua

a guest
Feb 25th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. function init()
  2. self.duplicateChance = config.getParameter("allowDuplicateChance")
  3. self.recipes = config.getParameter("recipes")
  4. self.swingTime = config.getParameter("swingTime")
  5. activeItem.setArmAngle(-math.pi / 2)
  6. end
  7.  
  8. function update(dt, fireMode, shiftHeld)
  9. updateAim()
  10.  
  11. if not self.swingTimer and fireMode == "primary" and player then
  12. self.swingTimer = self.swingTime
  13. end
  14.  
  15. if self.swingTimer then
  16. self.swingTimer = math.max(0, self.swingTimer - dt)
  17.  
  18. activeItem.setArmAngle((-math.pi / 2) * (self.swingTimer / self.swingTime))
  19.  
  20. if self.swingTimer == 0 then
  21. learnBlueprint()
  22. end
  23. end
  24. end
  25.  
  26. function learnBlueprint()
  27. local itemName = chooseRecipe(self.recipes)
  28. local i = 0
  29. if player.blueprintKnown(itemName) and math.random() > self.duplicateChance then
  30. while player.blueprintKnown(itemName) and i < 20 do
  31. itemName = chooseRecipe(self.recipes)
  32. i = i + 1
  33. end
  34. end
  35.  
  36. if player.blueprintKnown(itemName) then
  37. player.giveItem(itemName .. "-recipe")
  38. else
  39. player.giveBlueprint(itemName)
  40. end
  41.  
  42. animator.playSound("learnBlueprint")
  43.  
  44. item.consume(1)
  45. end
  46.  
  47. function chooseRecipe(recipeOrRecipes)
  48. if type(recipeOrRecipes) == "table" then
  49. local choice = recipeOrRecipes[math.random(1, #recipeOrRecipes)]
  50. return chooseRecipe(choice)
  51. else
  52. return recipeOrRecipes
  53. end
  54. end
  55.  
  56. function updateAim()
  57. self.aimAngle, self.aimDirection = activeItem.aimAngleAndDirection(0, activeItem.ownerAimPosition())
  58. activeItem.setFacingDirection(self.aimDirection)
  59. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement