StageSystem.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. -- sets up stages created by the obby system
  2. -- handles respawning
  3. local Players = game:GetService("Players")
  4. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  5. local CollectionService = game:GetService("CollectionService")
  6. local remote = ReplicatedStorage:WaitForChild("remote")
  7. local lib = ReplicatedStorage:WaitForChild("lib")
  8. local common = ReplicatedStorage:WaitForChild("common")
  9. local client = game:GetService("StarterPlayer"):WaitForChild("StarterPlayerScripts")
  10. local Signal = require(lib:WaitForChild("Signal"))
  11. local RECS = require(lib:WaitForChild("RECS"))
  12. local Components = require(common:WaitForChild("Components"))
  13. local hitIsYou = require(client:WaitForChild("hitIsYou"))
  14. local StageSystem = RECS.System:extend("StageSystem")
  15. local RESPAWN_TIME = 2/3
  16. local localPlayer = Players.LocalPlayer
  17. local e_RequestCharacterLoad = remote:WaitForChild("RequestCharacterLoad")
  18. function StageSystem:newStage(stage, stageinstance)
  19. stage.stageCompleted = Signal.new()
  20. stage.characterDied = nil
  21. stage.characterAdded = localPlayer.CharacterAdded:connect(function(character)
  22. print(("Character [%s] spawned"):format(character.Name))
  23. local humanoid = character:WaitForChild("Humanoid")
  24. local root = character:WaitForChild("HumanoidRootPart")
  25. -- attach the relavant character components
  26. CollectionService:AddTag(character,"PlayerHitbox")
  27. CollectionService:AddTag(character,"CastShadow")
  28. CollectionService:AddTag(root,"ForceReciever")
  29. local died = false
  30. -- oh noes, respawn the character!
  31. stage.characterDied = humanoid.Died:connect(function()
  32. if not died then
  33. died = true
  34. self:characterDied(stage)
  35. end
  36. end)
  37. end)
  38. -- add starts
  39. for startinstance, start in self.core:components(Components.Start) do
  40. if startinstance:IsDescendantOf(stageinstance) then
  41. table.insert(stage.starts,start)
  42. end
  43. end
  44. -- add checkpoints
  45. for checkpointinstance, checkpoint in self.core:components(Components.Checkpoint) do
  46. if checkpointinstance:IsDescendantOf(stageinstance) then
  47. table.insert(stage.checkpoints,checkpoint)
  48. end
  49. end
  50. -- add goals
  51. for goalinstance, goal in self.core:components(Components.Goal) do
  52. if goalinstance:IsDescendantOf(stageinstance) then
  53. table.insert(stage.goals,goal)
  54. local goalConnection
  55. goalConnection = goalinstance.Touched:connect(function(hit)
  56. if hitIsYou(hit) then
  57. local character = hit.Parent
  58. local humanoid = character:WaitForChild("Humanoid")
  59. if humanoid.Health > 0 then
  60. character:Destroy()
  61. goalConnection:Disconnect()
  62. stage.characterDied:Disconnect()
  63. stage.characterAdded:Disconnect()
  64. stage.stageCompleted:fire()
  65. end
  66. end
  67. end)
  68. end
  69. end
  70. self:loadCharacter(stage)
  71. end
  72. function StageSystem:loadCharacter(stage)
  73. local starts = stage.starts
  74. local startToUse = starts[math.random(#starts)]
  75. local startInstance = startToUse.instance
  76. local spawnPos = (startInstance.CFrame * CFrame.new(0,6,0)).p
  77. print("Attempting to respawn...")
  78. e_RequestCharacterLoad:FireServer(spawnPos)
  79. end
  80. function StageSystem:characterDied(stage)
  81. wait(RESPAWN_TIME)
  82. StageSystem:loadCharacter(stage)
  83. end
  84. function StageSystem:init()
  85. for instance, stage in self.core:components(Components.Stage) do
  86. self:newStage(stage,instance)
  87. end
  88. self.maid.componentAdded =
  89. self.core:getComponentAddedSignal(Components.Stage):Connect(function(stage, stageinstance)
  90. print(("Stage [%s] created"):format(stageinstance:GetFullName()))
  91. self:newStage(stage, stageinstance)
  92. end)
  93. self.core:getComponentRemovingSignal(Components.Stage):Connect(
  94. function(stage, instance)
  95. stage.characterAdded:disconnect()
  96. end)
  97. end
  98. function StageSystem:step()
  99. -- it seems i have to do this, though this system just attaches playerstats to the player
  100. end
  101. StageSystem.stepperDefinition = RECS.interval(1, {
  102. StageSystem
  103. })
  104. return StageSystem