Advertisement
Guest User

amulet.lua

a guest
Sep 19th, 2020
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 12.07 KB | None | 0 0
  1. local assets=
  2. {
  3.     Asset("ANIM", "anim/amulets.zip"),
  4.     Asset("ANIM", "anim/torso_amulets.zip"),
  5. }
  6.  
  7. --[[ Each amulet has a seperate onequip and onunequip function so we can also
  8. add and remove event listeners, or start/stop update functions here. ]]
  9.  
  10. ---RED
  11. local function healowner(inst, owner)
  12.     if (owner.components.health and owner.components.health:IsHurt())
  13.     and (owner.components.hunger and owner.components.hunger.current > 5 )then
  14.         owner.components.health:DoDelta(TUNING.REDAMULET_CONVERSION,false,"redamulet")
  15.         owner.components.hunger:DoDelta(-TUNING.REDAMULET_CONVERSION)
  16.         inst.components.finiteuses:Use(1)
  17.     end
  18. end
  19.  
  20. local function onequip_red(inst, owner)
  21.     owner.AnimState:OverrideSymbol("swap_body", "torso_amulets", "redamulet")
  22.     inst.task = inst:DoPeriodicTask(30, function() healowner(inst, owner) end)
  23. end
  24.  
  25. local function onunequip_red(inst, owner)
  26.     owner.AnimState:ClearOverrideSymbol("swap_body")
  27.     if inst.task then inst.task:Cancel() inst.task = nil end
  28. end
  29.  
  30. ---BLUE
  31. local function onequip_blue(inst, owner)
  32.     owner.AnimState:OverrideSymbol("swap_body", "torso_amulets", "blueamulet")
  33.  
  34.     inst.freezefn = function(attacked, data)
  35.         if data and data.attacker and data.attacker.components.freezable then
  36.             data.attacker.components.freezable:AddColdness(0.67)
  37.             data.attacker.components.freezable:SpawnShatterFX()
  38.             inst.components.fueled:DoDelta(-(inst.components.fueled.maxfuel * 0.03))
  39.         end
  40.     end
  41.  
  42.     inst:ListenForEvent("attacked", inst.freezefn, owner)
  43.  
  44.     if inst.components.fueled then
  45.         inst.components.fueled:StartConsuming()        
  46.     end
  47.  
  48. end
  49.  
  50. local function onunequip_blue(inst, owner)
  51.     owner.AnimState:ClearOverrideSymbol("swap_body")
  52.  
  53.     inst:RemoveEventCallback("attacked", inst.freezefn, owner)
  54.  
  55.     if inst.components.fueled then
  56.         inst.components.fueled:StopConsuming()        
  57.     end
  58. end
  59.  
  60. ---PURPLE
  61. local function induceinsanity(val, owner)
  62.     if owner.components.sanity then
  63.         owner.components.sanity.inducedinsanity = val
  64.     end
  65.     if owner.components.sanitymonsterspawner then
  66.         --Ensure the popchangetimer fully ticks over by running max tick time twice.
  67.         owner.components.sanitymonsterspawner:UpdateMonsters(20)
  68.         owner.components.sanitymonsterspawner:UpdateMonsters(20)
  69.     end
  70.  
  71.     local pt = owner:GetPosition()
  72.     local ents = TheSim:FindEntities(pt.x,pt.y,pt.z, 100, nil, nil, {'rabbit', 'manrabbit'})
  73.  
  74.     for k,v in pairs(ents) do
  75.         if v.CheckTransformState ~= nil then
  76.             v.CheckTransformState(v)
  77.         end
  78.     end
  79.  
  80. end
  81.  
  82. local function onequip_purple(inst, owner)
  83.     owner.AnimState:OverrideSymbol("swap_body", "torso_amulets", "purpleamulet")
  84.     if inst.components.fueled then
  85.         inst.components.fueled:StartConsuming()        
  86.     end
  87.     induceinsanity(true, owner)
  88. end
  89.  
  90. local function onunequip_purple(inst, owner)
  91.     owner.AnimState:ClearOverrideSymbol("swap_body")
  92.     if inst.components.fueled then
  93.         inst.components.fueled:StopConsuming()        
  94.     end
  95.     induceinsanity(nil, owner)
  96. end
  97.  
  98. ---GREEN
  99.  
  100. local function onequip_green(inst, owner)
  101.     owner.AnimState:OverrideSymbol("swap_body", "torso_amulets", "greenamulet")
  102.     owner.components.builder.ingredientmod = TUNING.GREENAMULET_INGREDIENTMOD
  103.     inst.onitembuild = function()
  104.         inst.components.finiteuses:Use(1)
  105.     end
  106.     inst:ListenForEvent("consumeingredients", inst.onitembuild, owner)
  107.  
  108. end
  109.  
  110. local function onunequip_green(inst, owner)
  111.     owner.AnimState:ClearOverrideSymbol("swap_body")
  112.     owner.components.builder.ingredientmod = 1
  113.     inst:RemoveEventCallback("consumeingredients", inst.onitembuild, owner)
  114. end
  115.  
  116. ---ORANGE
  117. local function SpawnEffect(inst)
  118.     local pt = inst:GetPosition()
  119.     local fx = SpawnPrefab("small_puff")
  120.     fx.Transform:SetPosition(pt.x, pt.y, pt.z)
  121.     fx.Transform:SetScale(0.5,0.5,0.5)
  122. end
  123.  
  124. local function getitem(player, amulet, item)
  125.     --Amulet will only ever pick up items one at a time. Even from stacks.
  126.     SpawnEffect(item)
  127.     amulet.components.finiteuses:Use(1)
  128.    
  129.     if item.components.stackable then
  130.         item = item.components.stackable:Get()
  131.     end
  132.    
  133.     if item.components.trap and item.components.trap:IsSprung() then
  134.         item.components.trap:Harvest(player)
  135.         return
  136.     end
  137.    
  138.     player.components.inventory:GiveItem(item)
  139. end
  140.  
  141. local function pickup(inst, owner)
  142.     local pt = owner:GetPosition()
  143.     local ents = TheSim:FindEntities(pt.x, pt.y, pt.z, TUNING.ORANGEAMULET_RANGE)
  144.  
  145.     for k,v in pairs(ents) do
  146.         if v.components.inventoryitem and v.components.inventoryitem.canbepickedup and v.components.inventoryitem.cangoincontainer and not
  147.             v.components.inventoryitem:IsHeld() then
  148.  
  149.             if not owner.components.inventory:IsFull() then
  150.                 --Your inventory isn't full, you can pick something up.
  151.                 getitem(owner, inst, v)
  152.                 return
  153.  
  154.             elseif v.components.stackable then
  155.                 --Your inventory is full, but the item you're trying to pick up stacks. Check for an exsisting stack.
  156.                 --An acceptable stack should: Be of the same item type, not be full already and not be in the "active item" slot of inventory.
  157.                 local stack = owner.components.inventory:FindItem(function(item) return (item.prefab == v.prefab and not item.components.stackable:IsFull()
  158.                     and item ~= owner.components.inventory.activeitem) end)
  159.                 if stack then
  160.                     getitem(owner, inst, v)
  161.                     return
  162.                 end
  163.             end
  164.         end
  165.     end
  166.    
  167. end
  168.  
  169. local function onequip_orange(inst, owner)
  170.     owner.AnimState:OverrideSymbol("swap_body", "torso_amulets", "orangeamulet")
  171.     inst.task = inst:DoPeriodicTask(TUNING.ORANGEAMULET_ICD, function() pickup(inst, owner) end)
  172. end
  173.  
  174. local function onunequip_orange(inst, owner)
  175.     owner.AnimState:ClearOverrideSymbol("swap_body")
  176.     if inst.task then inst.task:Cancel() inst.task = nil end
  177. end
  178.  
  179. ---YELLOW
  180. local function onequip_yellow(inst, owner)
  181.     owner.AnimState:OverrideSymbol("swap_body", "torso_amulets", "yellowamulet")
  182.  
  183.     if inst.components.fueled then
  184.         inst.components.fueled:StartConsuming()        
  185.     end
  186.  
  187.     inst.Light:Enable(true)
  188.  
  189.     owner.AnimState:SetBloomEffectHandle( "shaders/anim.ksh" )
  190.  
  191. end
  192.  
  193. local function onunequip_yellow(inst, owner)
  194.     owner.AnimState:ClearBloomEffectHandle()
  195.     owner.AnimState:ClearOverrideSymbol("swap_body")
  196.     if inst.components.fueled then
  197.         inst.components.fueled:StopConsuming()        
  198.     end
  199.  
  200.     inst.Light:Enable(false)
  201. end
  202.  
  203. ---COMMON FUNCTIONS
  204.  
  205. local function onfinished(inst)
  206.     inst:Remove()
  207. end
  208.  
  209. local function unimplementeditem(inst)
  210.     local player = GetPlayer()
  211.     player.components.talker:Say(GetString(player.prefab, "ANNOUNCE_UNIMPLEMENTED"))
  212.     if player.components.health.currenthealth > 1 then
  213.         player.components.health:DoDelta(-player.components.health.currenthealth * 0.5)
  214.     end
  215.  
  216.     if inst.components.useableitem then
  217.         inst.components.useableitem:StopUsingItem()
  218.     end
  219. end
  220.  
  221. local function commonfn()
  222.     local inst = CreateEntity()
  223.    
  224.     inst.entity:AddTransform()
  225.     inst.entity:AddAnimState()
  226.     MakeInventoryPhysics(inst)  
  227.  
  228.     inst.AnimState:SetBank("amulets")
  229.     inst.AnimState:SetBuild("amulets")
  230.    
  231.     inst:AddComponent("inspectable")
  232.    
  233.     inst:AddComponent("equippable")
  234.     inst.components.equippable.equipslot = EQUIPSLOTS.BODY
  235.    
  236.    
  237.     inst:AddComponent("dapperness")
  238.     inst.components.dapperness.dapperness = TUNING.DAPPERNESS_SMALL    
  239.    
  240.    
  241.     inst:AddComponent("inventoryitem")
  242.     inst.components.inventoryitem.foleysound = "dontstarve/movement/foley/jewlery"
  243.    
  244.     return inst
  245. end
  246.  
  247. local function red(inst)
  248.     local inst = commonfn(inst)
  249.         inst.AnimState:PlayAnimation("redamulet")
  250.         inst.components.inventoryitem.keepondeath = true
  251.         inst.components.equippable:SetOnEquip( onequip_red )
  252.         inst.components.equippable:SetOnUnequip( onunequip_red )
  253.         inst:AddComponent("finiteuses")
  254.         inst.components.finiteuses:SetOnFinished( onfinished )
  255.         inst.components.finiteuses:SetMaxUses(TUNING.REDAMULET_USES)
  256.         inst.components.finiteuses:SetUses(TUNING.REDAMULET_USES)
  257.     return inst
  258. end
  259.  
  260. local function blue(inst)
  261.     local inst = commonfn(inst)
  262.         inst.AnimState:PlayAnimation("blueamulet")
  263.         inst.components.equippable:SetOnEquip( onequip_blue )
  264.         inst.components.equippable:SetOnUnequip( onunequip_blue )
  265.         inst:AddComponent("heater")
  266.         inst.components.heater.iscooler = true
  267.         inst.components.heater.equippedheat = TUNING.BLUEGEM_COOLER
  268.  
  269.         inst:AddComponent("fueled")
  270.         inst.components.fueled.fueltype = "MAGIC"
  271.         inst.components.fueled:InitializeFuelLevel(TUNING.BLUEAMULET_FUEL)
  272.         inst.components.fueled:SetDepletedFn(onfinished)
  273.     return inst
  274. end
  275.  
  276. local function purple(inst)
  277.     local inst = commonfn(inst)
  278.  
  279.         inst:AddComponent("fueled")
  280.         inst.components.fueled.fueltype = "MAGIC"
  281.         inst.components.fueled:InitializeFuelLevel(TUNING.PURPLEAMULET_FUEL)
  282.         inst.components.fueled:SetDepletedFn(onfinished)
  283.  
  284.         inst.AnimState:PlayAnimation("purpleamulet")
  285.         inst.components.equippable:SetOnEquip( onequip_purple )
  286.         inst.components.equippable:SetOnUnequip( onunequip_purple )
  287.  
  288.         inst.components.dapperness.dapperness = -TUNING.DAPPERNESS_MED    
  289.  
  290.     return inst
  291. end
  292.  
  293. local function green(inst)
  294.     local inst = commonfn(inst)
  295.         inst.AnimState:PlayAnimation("greenamulet")
  296.         inst.components.equippable:SetOnEquip( onequip_green )
  297.         inst.components.equippable:SetOnUnequip( onunequip_green )
  298.  
  299.         inst:AddComponent("finiteuses")
  300.         inst.components.finiteuses:SetOnFinished( onfinished )
  301.         inst.components.finiteuses:SetMaxUses(TUNING.GREENAMULET_USES)
  302.         inst.components.finiteuses:SetUses(TUNING.GREENAMULET_USES)
  303.  
  304.     return inst
  305. end
  306.  
  307. local function orange(inst)
  308.     local inst = commonfn(inst)
  309.         inst.AnimState:PlayAnimation("orangeamulet")
  310.         -- inst.components.inspectable.nameoverride = "unimplemented"
  311.         -- inst:AddComponent("useableitem")
  312.         -- inst.components.useableitem:SetOnUseFn(unimplementeditem)
  313.         inst.components.equippable:SetOnEquip( onequip_orange )
  314.         inst.components.equippable:SetOnUnequip( onunequip_orange )
  315.  
  316.         inst:AddComponent("finiteuses")
  317.         inst.components.finiteuses:SetOnFinished( onfinished )
  318.         inst.components.finiteuses:SetMaxUses(TUNING.ORANGEAMULET_USES)
  319.         inst.components.finiteuses:SetUses(TUNING.ORANGEAMULET_USES)
  320.  
  321.     return inst
  322. end
  323.  
  324. local function yellow(inst)
  325.     local inst = commonfn(inst)
  326.         inst.AnimState:PlayAnimation("yellowamulet")
  327.  
  328.         inst.components.equippable:SetOnEquip( onequip_yellow )
  329.         inst.components.equippable:SetOnUnequip( onunequip_yellow )
  330.         inst.components.equippable.walkspeedmult = 1.2
  331.         inst.components.inventoryitem:SetOnDroppedFn(function(inst) inst.Light:Enable(false) end)
  332.         inst.entity:AddLight()
  333.         inst.Light:Enable(false)
  334.         inst.Light:SetRadius(2)
  335.         inst.Light:SetFalloff(0.7)
  336.         inst.Light:SetIntensity(.65)
  337.         inst.Light:SetColour(223/255, 208/255, 69/255)
  338.  
  339.         inst:AddComponent("fueled")
  340.         inst.components.fueled.accepting = true
  341.         inst.components.fueled.fueltype = "NIGHTMARE"
  342.         inst.components.fueled:InitializeFuelLevel(TUNING.YELLOWAMULET_FUEL)
  343.         inst.components.fueled:SetDepletedFn(onfinished)
  344.     return inst
  345. end
  346.  
  347.  
  348. return Prefab( "common/inventory/amulet", red, assets),
  349. Prefab("common/inventory/blueamulet", blue, assets),
  350. Prefab("common/inventory/purpleamulet", purple, assets),
  351. Prefab("common/inventory/orangeamulet", orange, assets),
  352. Prefab("common/inventory/greenamulet", green, assets),
  353. Prefab("common/inventory/yellowamulet", yellow, assets)
  354.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement