Advertisement
FusionLord

gms_plant.lua

Aug 18th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.28 KB | None | 0 0
  1. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ gms_base_entity.lua
  2.  
  3.  
  4. AddCSLuaFile();
  5.  
  6. ENT.Type = "anim";
  7. ENT.Base = "base_gmodentity";
  8.  
  9. ENT.PrintName = "GMS Base Entity";
  10. ENT.Author = "Stranded Team";
  11. ENT.Spawnable = false;
  12.  
  13. ENT.Model = "models/props/de_inferno/ClayOven.mdl";
  14.  
  15. function ENT:Initialize()
  16.     if ( self.Color ) then
  17.         self:SetColor( self.Color );
  18.     end
  19.     self:SetModel( self.Model );
  20.     self:SetMoveType( MOVETYPE_VPHYSICS );
  21.     self:SetSolid( SOLID_VPHYSICS );
  22.  
  23.     if SERVER then
  24.         self:PhysicsInit( SOLID_VPHYSICS );
  25.         if ( self.DoWake ) then
  26.             self:PhysWake();
  27.         end
  28.         if ( self.DoFreeze ) then
  29.             self:Freeze();
  30.         end
  31.     end
  32.  
  33.     local phys = self:GetPhysicsObject();
  34.     if ( IsValid( phys ) ) then
  35.         phys:RecheckCollisionFilter();
  36.     end
  37.  
  38.     self:OnInitialize();
  39. end
  40.  
  41. function ENT:OnInitialize()
  42. end
  43.  
  44. if ( CLIENT ) then return end
  45.  
  46. function ENT:Use( ply )
  47.     if ( !ply:KeyPressed( IN_USE ) ) then return; end
  48.     self:OnUse( ply );
  49. end
  50.  
  51. function ENT:Wake()
  52.     local phys = self:GetPhysicsObject();
  53.     if ( IsValid( phys ) ) then
  54.         phys:Wake();
  55.     end
  56. end
  57.  
  58. function ENT:Freeze()
  59.     local phys = self:GetPhysicsObject();
  60.     if ( IsValid( phys ) ) then
  61.         phys:EnableMotion( false );
  62.     end
  63. end
  64.  
  65. function ENT:OnTakeDamage( dmg )
  66.     self:TakePhysicsDamage( dmg );
  67. end
  68.  
  69. function ENT:OnUse( ply )
  70. end
  71.  
  72.  
  73. $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ gms_plant.lua
  74.  
  75. AddCSLuaFile()
  76.  
  77. ENT.Type        = "anim";
  78. ENT.Base        = "gms_base_entity";
  79. ENT.PrintName   = "Plant";
  80.  
  81. ENT.Model       = "models/weapons/w_bugbait.mdl";
  82. ENT.Color       = Color(0, 255, 0, 255);
  83. ENT.DoWake      = true;
  84. ENT.DoFreeze    = true;
  85.  
  86. concommand.Add("gms_plant",
  87.     function ( ply, cmd, args )
  88.         if not IsValid(ply) and ply:IsAdmin() and cmd ~= "gms_plant" then return end;
  89.         net.Start("gms_plant");
  90.             net.WriteTable({ pos = ply:GetEyeTrace().HitPos,  plant = args[1] });
  91.         net.SendToServer();
  92.     end,
  93.     function ( cmd, args )
  94.         args = string.Trim( args ); -- Remove any spaces before or after.
  95.         args = string.lower( args );
  96.  
  97.         local tbl = {};
  98.  
  99.         for k, v in pairs( GMS.Plants ) do
  100.             local plant = k;
  101.             if string.find(string.lower( plant ), args ) then
  102.                 plant = "\"" .. plant .. "\""; -- We put quotes around it in case players have spaces in their names.
  103.                 plant = "gms_plant " .. plant; -- We also need to put the cmd before for it to work properly.
  104.  
  105.                 table.insert( tbl, plant );
  106.             else
  107.                 print("args not found" .. k .. " " .. args);
  108.             end
  109.         end
  110.  
  111.         return tbl;
  112.     end,
  113. "Plants a plant.");
  114.  
  115. if CLIENT then
  116.     return;
  117. end
  118.  
  119. util.AddNetworkString("gms_plant");
  120.  
  121. net.Receive("gms_plant", function(len, ply)
  122.     local data = net.ReadTable();
  123.     local plant = ents.Create("gms_plant");
  124.     plant:SetPos(data.pos);
  125.     plant:SetUp(GMS.Plants[data.plant], ply);
  126.     plant:Spawn();
  127. end);
  128.  
  129. function ENT:OnInitialize()
  130.     self:SetRenderMode(RENDERMODE_TRANSALPHA);
  131. end
  132.  
  133. function ENT:SetUp(plant, planter)
  134.     self.plant = plant;
  135.     if planter then
  136.         if plant.name ~= "tree" then
  137.             planter:SetNWInt("plants", planter:GetNWInt("plants") + 1);
  138.         end
  139.         self:SetOwner(planter);
  140.     end
  141.     timer.Simple(plant.gestation, function() self:Grow() end);
  142. end
  143.  
  144. function ENT:CreateFruit()
  145.     local plant = self.plant;
  146.     local fruit = ents.Create("prop_physics");
  147.     fruit:SetAngles( Angle(0, math.random(0, 360), 0) );
  148.     fruit:SetModel(self.plant.fruit);
  149.     local x, y, z = plant.fruitSpread[1], plant.fruitSpread[2], plant.fruitSpread[3];
  150.     local pos = self.plant.fruitAnchor + Vector(math.random(x*-1, x), math.random(y*-1, y), math.random(z*-1, z));
  151.     fruit:SetPos(self:GetPos() + pos);
  152.     fruit:Spawn();
  153.     fruit:SetParent(self);
  154.     fruit:SetOwner(self:GetOwner());
  155.  
  156.     fruit.PhysgunDisabled = true;
  157.  
  158.     function fruit:Use(activator, caller)
  159.         self:Use(activator, caller);
  160.     end
  161. end
  162.  
  163. function ENT:Grow()
  164.     local owner = self:GetOwner();
  165.    
  166.     self.Color = Color(255, 255, 255, 255);
  167.     self.Model = self.plant.base;
  168.     self:Initialize();
  169.  
  170.     local num = 5;
  171.     if IsValid(owner) then
  172.         num = num + math.random(owner:GetSkills()["farming"] or 0);
  173.     end
  174.     for i = 1, num, 1 do
  175.         self:CreateFruit();
  176.         UTIL:Log("Added Fruit! " .. i .. " of " .. num);
  177.     end
  178. end
  179.  
  180. function ENT:Use(activator, caller)
  181.     if IsValid(caller) and caller:IsPlayer() then
  182.         caller:Eat(self.plant);
  183.         self:GetChildren()[math.random(#self:GetChildren())]:Remove();
  184.     end
  185. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement