Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ResourceManager = {}
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local TweenService = game:GetService("TweenService")
- local SoundService = game:GetService("SoundService")
- local Lighting = game:GetService("Lighting")
- local Player = game:GetService("Players").LocalPlayer
- local Config = ReplicatedStorage:WaitForChild("Config")
- local PlayerGui = Player:WaitForChild("PlayerGui")
- local CharacterData = require(Config:WaitForChild("Character"))
- local DepthOfField = Lighting:WaitForChild("DepthOfField")
- local Blur = Lighting:WaitForChild("Blur")
- local MainGui = PlayerGui:WaitForChild("MainGui")
- local MainContainer = MainGui:WaitForChild("MainContainer")
- local HealthBar = MainContainer:WaitForChild("Health"):WaitForChild("HealthBar")
- local StaminaBar = MainContainer:WaitForChild("Stamina"):WaitForChild("StaminaBar")
- local REGEN_RATE = 2
- local REGEN_AMOUNT = 1
- local REGEN_DELAY = 15
- local HealthUpdated, StaminaUpdated = nil
- local CanRegen = true
- local LastHurt = nil
- local CachedValues = {
- Health = {
- Previous = CharacterData.Health.Max,
- Effects = {}
- },
- Stamina = {
- Previous = CharacterData.Stamina.Max,
- Effects = {}
- }
- }
- local BlurOn = TweenService:Create(Blur, TweenInfo.new(1), {Size = 10})
- local BlurOff = TweenService:Create(Blur, TweenInfo.new(1.5), {Size = 0})
- local function ResourceEffects(Resource, Toggle)
- local Character = Player.Character
- local ResourceData = CharacterData[Resource]
- local ResourceValues = CachedValues[Resource]
- local EffectData = ResourceData.Effects
- local EffectValues = ResourceValues.Effects
- local CurrentResource = Character:GetAttribute(Resource)
- local MaxResource = ResourceData.Max
- local PercentResource = CurrentResource / MaxResource * 100
- for Percent, Effect in EffectData do
- if PercentResource < Percent and not table.find(EffectValues, Effect) then
- EffectValues[Percent] = Effect
- elseif PercentResource > Percent then
- EffectValues[Percent] = nil
- end
- end
- for Percent, Effect in EffectData do
- print(Effect)
- local Exists = nil
- if EffectValues[Percent] then
- Exists = true
- else
- Exists = false
- end
- ResourceManager[Effect](Exists)
- end
- end
- local function UpdateResourceBar(Resource)
- local ResourceBar = Resource == "Health" and HealthBar
- or Resource == "Stamina" and StaminaBar
- local UIGradient = ResourceBar:FindFirstChildOfClass("UIGradient")
- local Character = Player.Character
- local ResourceData = CharacterData[Resource]
- local ResourceValue = Character:GetAttribute(Resource)
- local MaxOfResource = ResourceData.Max
- local DecimalOfResource = (ResourceValue / MaxOfResource)
- if Resource == "Stamina" then
- --// This is because the stamina bar is upside down, blame past devs.
- DecimalOfResource = -DecimalOfResource
- end
- local TweenBar = TweenService:Create(UIGradient,
- TweenInfo.new(.4), {Offset = Vector2.new(DecimalOfResource, 0)})
- TweenBar:Play()
- end
- local function UpdateResource(Resource)
- local Character = Player.Character
- local Humanoid = Character.Humanoid
- local ResourceData = CharacterData[Resource]
- local CurrentResource = Character:GetAttribute(Resource)
- local PreviousResource = CachedValues[Resource].Previous
- local MaxResource = ResourceData.Max
- if CurrentResource < PreviousResource then
- LastHurt = os.clock()
- task.spawn(function()
- if CanRegen == false then
- return
- end
- CanRegen = false
- local RegenTimer = os.clock()
- repeat task.wait()
- RegenTimer = os.clock()
- until RegenTimer - LastHurt > REGEN_DELAY
- CanRegen = true
- end)
- end
- CachedValues[Resource].Previous = CurrentResource
- UpdateResourceBar(Resource)
- ResourceEffects(Resource)
- if CanRegen == nil or CurrentResource == MaxResource then
- return
- end
- if CanRegen == false then
- repeat task.wait(REGEN_RATE) until CanRegen == true
- end
- CanRegen = nil
- task.spawn(function()
- while Character:GetAttribute(Resource) < MaxResource do task.wait(REGEN_RATE)
- if CanRegen == false then
- break
- end
- local CurrentResource = Character:GetAttribute(Resource)
- local NewResource = CurrentResource + REGEN_AMOUNT
- if NewResource > MaxResource then
- NewResource = MaxResource
- end
- Character:SetAttribute(Resource, NewResource)
- end
- end)
- end
- local function OnCharacterAdded(Character)
- local Humanoid = Character:WaitForChild("Humanoid")
- HealthUpdated = Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
- Character:SetAttribute("Health", Humanoid.Health)
- end)
- Character:GetAttributeChangedSignal("Health"):Connect(function()
- UpdateResource("Health")
- end)
- Character:GetAttributeChangedSignal("Stamina"):Connect(function()
- UpdateResource("Stamina")
- end)
- Character:SetAttribute("Health", Humanoid.Health)
- Humanoid.HealthDisplayType = Enum.HumanoidHealthDisplayType.AlwaysOff
- end
- function ResourceManager.Init()
- if Player.Character then
- OnCharacterAdded(Player.Character)
- end
- Player.CharacterAdded:Connect(OnCharacterAdded)
- end
- function ResourceManager.Tired(Toggle)
- print("Tired")
- local Sound = SoundService:FindFirstChild("Breathing")
- if not Toggle then
- if Sound then
- Sound:Destroy()
- end
- return
- end
- if not Sound then
- Sound = _G.GetSound("Breathing")
- Sound.PlayOnRemove = false
- Sound.Looped = true
- Sound.Volume = .2
- _G.PlaySoundLocally(Sound)
- end
- end
- function ResourceManager.Fatigued(Toggle)
- local Tween = Toggle and BlurOn or BlurOff
- if Blur.Enabled and Toggle then
- return
- end
- Tween:Play()
- if not Toggle then
- Tween.Completed:Wait()
- end
- Blur.Enabled = Toggle
- end
- function ResourceManager.Exhausted(Toggle)
- print(`{Player.Name} now exhausted.`)
- end
- function ResourceManager.Hurt(Toggle)
- local Heartbeat = SoundService:FindFirstChild("Heartbeat")
- if not Toggle then
- if Heartbeat then
- Heartbeat:Destroy()
- end
- return
- end
- if not Heartbeat then
- Heartbeat = _G.GetSound("Heartbeat")
- Heartbeat.PlayOnRemove = false
- Heartbeat.Looped = true
- Heartbeat.Volume = .2
- _G.PlaySoundLocally(Heartbeat)
- end
- Heartbeat.PlaybackSpeed = 1
- end
- function ResourceManager.Damaged(Toggle)
- local Tinnitus = SoundService:FindFirstChild("Tinnitus")
- if not Toggle then
- if Tinnitus then
- Tinnitus:Destroy()
- end
- return
- end
- local Heartbeat = SoundService:FindFirstChild("Heartbeat")
- if not Tinnitus then
- Tinnitus = _G.GetSound("Tinnitus")
- Tinnitus.PlayOnRemove = false
- Tinnitus.Looped = true
- Tinnitus.Volume = .2
- _G.PlaySoundLocally(Tinnitus)
- end
- if Toggle and Heartbeat.PlaybackSpeed < 1.2 then
- Heartbeat.PlaybackSpeed = 1.2
- end
- end
- function ResourceManager.Dying(Toggle)
- local Heartbeat = SoundService:FindFirstChild("Heartbeat")
- if not Toggle then
- return
- end
- print(Heartbeat:GetFullName())
- if Toggle and Heartbeat.PlayBackSpeed < 1.5 then
- Heartbeat.PlaybackSpeed = 1.5
- end
- print(`{Player.Name} now dying.`)
- end
- return ResourceManager
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement