Advertisement
Starly124

Untitled

Feb 11th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.15 KB | None | 0 0
  1. local PopupDialogScreen = require "screens/popupdialog"
  2.  
  3. local assets=
  4. {
  5. Asset("ANIM", "anim/cave_entrance.zip"),
  6. Asset("ANIM", "anim/ruins_entrance.zip"),
  7.  
  8. }
  9.  
  10. local prefabs =
  11. {
  12. "bat",
  13. "exitcavelight"
  14. }
  15.  
  16. local function GetVerb(inst)
  17. return STRINGS.ACTIONS.ACTIVATE.SPELUNK
  18. end
  19.  
  20. local function ReturnChildren(inst)
  21. for k,child in pairs(inst.components.childspawner.childrenoutside) do
  22. if child.components.homeseeker then
  23. child.components.homeseeker:GoHome()
  24. end
  25. child:PushEvent("gohome")
  26. end
  27. end
  28.  
  29. local function OnActivate(inst)
  30.  
  31. if not IsGamePurchased() then return end
  32.  
  33. ProfileStatsSet("cave_entrance_used", true)
  34.  
  35. SetPause(true)
  36.  
  37. local function go_spelunking()
  38. SaveGameIndex:GetSaveFollowers(GetPlayer())
  39.  
  40. local function onsaved()
  41. SetPause(false)
  42. StartNextInstance({reset_action=RESET_ACTION.LOAD_SLOT, save_slot = SaveGameIndex:GetCurrentSaveSlot()}, true)
  43. end
  44.  
  45. local function doenter()
  46. local level = 1
  47. if GetWorld().prefab == "cave" then
  48. level = (GetWorld().topology.level_number or 1 ) + 1
  49. end
  50. SaveGameIndex:SaveCurrent(function() SaveGameIndex:EnterCave(onsaved,nil, inst.cavenum, level) end, "descend", inst.cavenum)
  51. end
  52.  
  53. if not inst.cavenum then
  54. -- We need to make sure we only ever have one cave underground
  55. -- this is because caves are verticle and dont have sub caves
  56. if GetWorld().prefab == "cave" then
  57. inst.cavenum = SaveGameIndex:GetCurrentCaveNum()
  58. doenter()
  59. else
  60. inst.cavenum = SaveGameIndex:GetNumCaves() + 1
  61. SaveGameIndex:AddCave(nil, doenter)
  62. end
  63. else
  64. doenter()
  65. end
  66. end
  67. GetPlayer().HUD:Hide()
  68.  
  69. TheFrontEnd:Fade(false, 2, function()
  70. go_spelunking()
  71. end)
  72. end
  73.  
  74. local function MakeRuins(inst)
  75. inst.AnimState:SetBank("ruins_entrance")
  76. inst.AnimState:SetBuild("ruins_entrance")
  77.  
  78. if inst.components.lootdropper then
  79. inst.components.lootdropper:SetLoot({"thulecite", "thulecite_pieces", "thulecite_pieces"})
  80. end
  81.  
  82. inst.MiniMapEntity:SetIcon("ruins_closed.png")
  83.  
  84. end
  85.  
  86. local function Open(inst)
  87.  
  88. inst.startspawningfn = function()
  89. inst.components.childspawner:StopRegen()
  90. inst.components.childspawner:StartSpawning()
  91. end
  92.  
  93. inst.stopspawningfn = function()
  94. inst.components.childspawner:StartRegen()
  95. inst.components.childspawner:StopSpawning()
  96. ReturnChildren(inst)
  97. end
  98. inst.components.childspawner:StopSpawning()
  99. inst:ListenForEvent("dusktime", inst.startspawningfn, GetWorld())
  100. inst:ListenForEvent("daytime", inst.stopspawningfn, GetWorld())
  101.  
  102.  
  103. inst.AnimState:PlayAnimation("idle_open", true)
  104. inst:RemoveComponent("workable")
  105.  
  106. inst.open = true
  107.  
  108. inst.name = STRINGS.NAMES.CAVE_ENTRANCE_OPEN
  109. if SaveGameIndex:GetCurrentMode() == "cave" then
  110. inst.name = STRINGS.NAMES.CAVE_ENTRANCE_OPEN_CAVE
  111. end
  112. inst:RemoveComponent("lootdropper")
  113.  
  114. inst.MiniMapEntity:SetIcon("cave_open.png")
  115.  
  116. --inst:AddTag("NOCLICK")
  117. inst:DoTaskInTime(2, function()
  118.  
  119. if IsGamePurchased() then
  120. inst:AddComponent("activatable")
  121. inst.components.activatable.OnActivate = OnActivate
  122. inst.components.activatable.inactive = true
  123. inst.components.activatable.getverb = GetVerb
  124. inst.components.activatable.quickaction = true
  125. end
  126.  
  127. end)
  128.  
  129. end
  130.  
  131. local function OnWork(inst, worker, workleft)
  132. local pt = Point(inst.Transform:GetWorldPosition())
  133. if workleft <= 0 then
  134. inst.SoundEmitter:PlaySound("dontstarve/wilson/rock_break")
  135. inst.components.lootdropper:DropLoot(pt)
  136. ProfileStatsSet("cave_entrance_opened", true)
  137. Open(inst)
  138. else
  139. if workleft < TUNING.ROCKS_MINE*(1/3) then
  140. inst.AnimState:PlayAnimation("low")
  141. elseif workleft < TUNING.ROCKS_MINE*(2/3) then
  142. inst.AnimState:PlayAnimation("med")
  143. else
  144. inst.AnimState:PlayAnimation("idle_closed")
  145. end
  146. end
  147. end
  148.  
  149.  
  150. local function Close(inst)
  151.  
  152. if inst.open then
  153. inst:RemoveEventCallback("daytime", inst.stopspawningfn)
  154. inst:RemoveEventCallback("dusktime", inst.startspawningfn)
  155. end
  156. inst:RemoveComponent("activatable")
  157. inst.AnimState:PlayAnimation("idle_closed", true)
  158.  
  159. inst:AddComponent("workable")
  160. inst.components.workable:SetWorkAction(ACTIONS.MINE)
  161. inst.components.workable:SetWorkLeft(TUNING.ROCKS_MINE)
  162. inst.components.workable:SetOnWorkCallback(OnWork)
  163. inst:AddComponent("lootdropper")
  164. inst.components.lootdropper:SetLoot({"rocks", "rocks", "flint", "flint", "flint"})
  165.  
  166. inst.name = STRINGS.NAMES.CAVE_ENTRANCE_CLOSED
  167. if SaveGameIndex:GetCurrentMode() == "cave" then
  168. inst.name = STRINGS.NAMES.CAVE_ENTRANCE_CLOSED_CAVE
  169. end
  170.  
  171. inst.open = false
  172. end
  173.  
  174.  
  175. local function onsave(inst, data)
  176. data.cavenum = inst.cavenum
  177. data.open = inst.open
  178. end
  179.  
  180. local function onload(inst, data)
  181. inst.cavenum = data and data.cavenum
  182.  
  183. if GetWorld():IsCave() then
  184. MakeRuins(inst)
  185. end
  186.  
  187. if data and data.open then
  188. Open(inst)
  189. end
  190. end
  191.  
  192. local function GetStatus(inst)
  193. if inst.open then
  194. return "OPEN"
  195. end
  196. end
  197.  
  198. local function fn(Sim)
  199. local inst = CreateEntity()
  200. local trans = inst.entity:AddTransform()
  201. local anim = inst.entity:AddAnimState()
  202. inst.entity:AddSoundEmitter()
  203. MakeObstaclePhysics(inst, 1)
  204. local minimap = inst.entity:AddMiniMapEntity()
  205. minimap:SetIcon("cave_closed.png")
  206. anim:SetBank("cave_entrance")
  207. anim:SetBuild("cave_entrance")
  208.  
  209. inst:AddComponent("inspectable")
  210. inst.components.inspectable:RecordViews()
  211. inst.components.inspectable.getstatus = GetStatus
  212.  
  213. inst:AddComponent( "childspawner" )
  214. inst.components.childspawner:SetRegenPeriod(60)
  215. inst.components.childspawner:SetSpawnPeriod(.1)
  216. inst.components.childspawner:SetMaxChildren(6)
  217. inst.components.childspawner.childname = "bat"
  218.  
  219. Open(inst)
  220. inst.OnSave = onsave
  221. inst.OnLoad = onload
  222.  
  223. --this is a hack to make sure these don't show up in adventure mode
  224. if SaveGameIndex:GetCurrentMode() == "adventure" then
  225. inst:DoTaskInTime(0, function() inst:Remove() end)
  226. end
  227.  
  228. return inst
  229. end
  230.  
  231. return Prefab( "common/cave_entrance", fn, assets, prefabs),
  232. MakePlacer( "common/cave_entrance_placer", "cave_entrance", "cave_entrance", "idle_open" )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement