Guest User

GMod energy class

a guest
Aug 17th, 2016
109
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- This method replaces the suit strength system for running,
  2. -- underwater breathing, and anything else the user might use it for.
  3.  
  4. Energy = {};
  5.  
  6. Energy.COST_TO_RUN = 2;
  7. Energy.COST_TO_BREATHE = 4;
  8.  
  9. Energy.RegisteredUses = { "run" };
  10.  
  11. local function min(num1, num2)
  12.     if num1 < num2 then return num1 else return num2 end
  13. end
  14.  
  15. if SERVER then
  16.  
  17.     function Energy:InitEnergy(ply)
  18.         local dat = PlayerData_Get(ply:SteamID())
  19.        
  20.         // ply.Energy = dat['energy'];
  21.         ply.MaxEnergy = dat['maxenergy'];
  22.         ply.EnergyRegen = dat['energyregen'];
  23.         Energy:BeginRegen(ply, ply.EnergyRegen, nil);
  24.        
  25.     end
  26.  
  27.     function Energy:AddEnergy(ply, amount)
  28.         ply.Energy = min(ply.Energy + amount, ply.MaxEnergy)
  29.         Energy:SendEnergy(ply);
  30.     end
  31.  
  32.     function Energy:TakeEnergy(ply, amount)
  33.         if (amount > ply.Energy) then return false end
  34.         ply.Energy = ply.Energy - amount;
  35.         return true
  36.     end
  37.  
  38.     function Energy:BeginTimedDrain(ply, name, amount, callback)
  39.         if ply == nil or ply.Energy == nil then return false end
  40.         if ply.Energy < amount then return false end;
  41.         ply.Energy = ply.Energy - amount;
  42.         Energy:SendEnergy(ply);
  43.         Energy:EndRegen(ply, name);
  44.         timer.Create( "energyremove-" .. name .. "-" .. ply:SteamID(), 1, 0, function() Energy:DrainCall(ply, name, amount, callback); end )
  45.         return true;
  46.     end
  47.  
  48.     function Energy:DrainCall(ply, name, amount, callback)
  49.         if ply.Energy < amount then
  50.             Energy:EndTimedDrain(ply, name);
  51.             if callback != nil then
  52.                 callback(ply, name);
  53.             end
  54.             return;
  55.         end
  56.        
  57.         ply.Energy = ply.Energy - amount;
  58.        
  59.         Energy:SendEnergy(ply);
  60.        
  61.     end
  62.  
  63.     function Energy:EndTimedDrain(ply, name)
  64.         timer.Remove( "energyremove-" .. name .. "-" .. ply:SteamID());
  65.        
  66.         if (Energy:CheckDrain(ply)) then return end;
  67.         Energy:BeginRegen(ply, ply.EnergyRegen, nil);
  68.     end
  69.    
  70.     function Energy:CheckDrain(ply)
  71.         for k,v in pairs(Energy.RegisteredUses) do
  72.             if timer.IsTimer("energyremove-" .. v .. "-" .. ply:SteamID()) then
  73.                 return true
  74.             end
  75.         end
  76.         return false
  77.     end
  78.    
  79.     function Energy:Regenerate(ply)
  80.         if Energy:CheckDrain(ply) then return end;
  81.         Energy:BeginRegen(ply, ply.EnergyRegen, nil)
  82.     end
  83.    
  84.     function Energy:BeginRegen(ply, amount, callback)
  85.         if ply == nil or ply.Energy >= ply.MaxEnergy then return end
  86.         timer.Create( "energyregen-" .. ply:SteamID(), 1, 0, function() Energy:RegenCall(ply, amount, callback) end);
  87.     end
  88.    
  89.     function Energy:RegenCall(ply, amount, callback)
  90.         if ply.Energy >= ply.MaxEnergy then
  91.             Energy:EndRegen(ply)
  92.             if callback != nil then
  93.                 callback(ply);
  94.             end
  95.             return;
  96.         end
  97.         ply.Energy = min(ply.Energy + amount, ply.MaxEnergy);
  98.         Energy:SendEnergy(ply);
  99.     end
  100.    
  101.     function Energy:EndRegen(ply)
  102.         timer.Remove( "energyregen-" .. ply:SteamID());
  103.     end
  104.    
  105.     function Energy:SendEnergy(ply)
  106.         umsg.Start("energyupdate", ply)
  107.             umsg.Long(ply.Energy);
  108.             umsg.Long(ply.MaxEnergy);
  109.         umsg.End();
  110.     end
  111.  
  112.  
  113.     -- Default functions to run and breathe
  114.    
  115.     function Energy:CheckKeys(player, key)
  116.         if !player:Alive() then return end
  117.         if key == IN_SPEED then
  118.             Energy:DoRun(player);
  119.         else
  120.             return nil;
  121.         end
  122.         return 1;
  123.     end
  124.    
  125.     function Energy:CheckEndKeys(player, key)
  126.         if !GAMEMODE:InRound() or !player:Alive() then return end
  127.         if key == IN_SPEED then
  128.             Energy:DoEndRun(player);
  129.         else
  130.             return nil;
  131.         end
  132.         return 1;
  133.     end
  134.    
  135.     function Energy:DoRun(ply)
  136.         if not Energy:BeginTimedDrain(ply, "run", Energy.COST_TO_RUN, function() Energy:DoEndRun(ply) end) then return end;
  137.         ply:SetWalkSpeed(ply.RunSpeed or GAMEMODE.DefaultRunSpeed); // default 500
  138.         ply:SetRunSpeed(ply.RunSpeed or GAMEMODE.DefaultRunSpeed);
  139.     end
  140.     hook.Add("KeyPress", "runkeypress", function(player, key) Energy:CheckKeys(player, key); end);
  141.  
  142.     function Energy:DoEndRun(ply)
  143.         Energy:EndTimedDrain(ply, "run");
  144.         ply:SetWalkSpeed(ply.WalkSpeed or GAMEMODE.DefaultWalkSpeed); // default 250
  145.         ply:SetRunSpeed(ply.WalkSpeed or GAMEMODE.DefaultWalkSpeed);
  146.     end -- return values
  147.     hook.Add("KeyRelease", "runkeyrelease", function(player, key) Energy:CheckEndKeys(player, key); end);
  148.  
  149. elseif CLIENT then  ---------------------------------------------------------------------------
  150.    
  151.     function Energy:GetEnergy(data)
  152.         PlayerData.Energy = data:ReadLong();
  153.         PlayerData.MaxEnergy = data:ReadLong();
  154.     end
  155.     usermessage.Hook("energyupdate", function(data) Energy:GetEnergy(data) end);
  156. end
RAW Paste Data