Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.89 KB | None | 0 0
  1. local me = Ply"oh"
  2.  
  3. local ENT = {}
  4.  
  5. do -- ENT
  6.  
  7.     ENT.Type = "anim"
  8.     ENT.Base = "base_entity"
  9.  
  10.     ENT.Bottom = Vector(-0.53724020719528,-0.059773083776236,-34.8798828125)
  11.     ENT.ClassName = "player_physics"
  12.  
  13.     function ENT:GetBottom()
  14.         return self:LocalToWorld(self.Bottom)
  15.     end
  16.  
  17.     function ENT:GetEyePos()
  18.         return self:GetBottom() + self:GetUp() * 64
  19.     end
  20.  
  21.     function ENT:HasPlayer()
  22.         return self:GetOwner():IsPlayer()
  23.     end
  24.  
  25.     function ENT:GetPlayer()
  26.         return self:GetOwner():IsPlayer() and self:GetOwner() or NULL
  27.     end
  28.  
  29.     if CLIENT then
  30.  
  31.         function ENT:Draw()
  32.             render.CullMode(1)
  33.                 render.SetBlend(0.9)   
  34.                     render.SetColorModulation(0.2, 0.2, 0.2)
  35.                         self:DrawModel()
  36.                     render.SetColorModulation(1, 1, 1)
  37.                 render.SetBlend(1)
  38.             render.CullMode(0)
  39.         end
  40.        
  41.     else
  42.        
  43.         function ENT:Initialize()
  44.             self:SetModel("models/props_wasteland/controlroom_filecabinet002a.mdl")
  45.        
  46.             self:SetMoveType(MOVETYPE_VPHYSICS)
  47.             self:PhysicsInit(SOLID_VPHYSICS)
  48.            
  49.             self:SetMaterial("models/debug/debugwhite")
  50.            
  51.             self:StartMotionController()
  52.            
  53.             self:NextThink(CurTime()+2)
  54.         end
  55.  
  56.         function ENT:SetPlayer(ply)
  57.             self:RemovePlayer()
  58.            
  59.             ply:SetViewOffset(vector_origin)
  60.             ply:SetViewOffsetDucked(vector_origin + Vector(0,0,1)) -- view offset will fix player parent rotation
  61.        
  62.             self:SetPos(ply:GetPos())
  63.        
  64.             ply:SetMoveType(MOVETYPE_NOCLIP)
  65.            
  66.             ply:SetParent(self)
  67.                        
  68.             ply:SetNotSolid(true)
  69.             self:SetOwner(ply)
  70.             ply:SetOwner(self)
  71.                        
  72.             ply.__player_physics_request_duck = true
  73.            
  74.             self:PhysWake()
  75.         end
  76.            
  77.         function ENT:OnRemove()
  78.             local ply = self:GetPlayer()
  79.            
  80.             if ply:IsPlayer() then
  81.                 ply:SetParent()
  82.                 ply:SetOwner()
  83.                            
  84.                 ply:SetMoveType(MOVETYPE_WALK)
  85.  
  86.                 ply.__player_physics_request_duck = true
  87.                
  88.                 ply:SetViewOffset(Vector(0,0,64))
  89.                 ply:SetViewOffsetDucked(Vector(0,0,28))
  90.             end
  91.         end
  92.        
  93.         ENT.RemovePlayer = ENT.OnRemove
  94.  
  95.         function ENT:PhysicsSimulate(phys, delta)
  96.             local ply = self:GetPlayer()
  97.            
  98.             if ply:IsPlayer() then
  99.                 hook.Call("PPhys", nil, ply, self, phys)
  100.             end
  101.         end
  102.        
  103.         function ENT:Think()
  104.             if not self:HasPlayer() then self:Remove() return end
  105.            
  106.             if self:GetPlayer():GetPos():Distance(self:GetPos()) > 200 then
  107.                 self:GetPlayer():SetPos(self:GetEyePos())
  108.             end
  109.            
  110.             self:NextThink(CurTime()+2)
  111.             return true
  112.         end
  113.     end
  114.  
  115.     scripted_ents.Register(ENT, ENT.ClassName, true)
  116. end
  117.  
  118. do -- meta 
  119.     do -- player
  120.  
  121.         ENT.OldMetaFunctions = ENT.OldMetaFunctions or {
  122.             entity = {},
  123.             player = {},
  124.         }
  125.        
  126.         local oldmeta = ENT.OldMetaFunctions
  127.  
  128.         local player_meta = FindMetaTable("Player")
  129.         local entity_meta = FindMetaTable("Entity")
  130.  
  131.         function player_meta:HasPlayerPhysics()
  132.             return self:GetParent().ClassName == "player_physics"
  133.         end
  134.  
  135.         function player_meta:GetPlayerPhysics()
  136.             return self:HasPlayerPhysics() and self:GetParent() or NULL
  137.         end
  138.        
  139.         oldmeta.player.GetPlayerTrace = oldmeta.player.GetPlayerTrace or util.GetPlayerTrace
  140.             function util.GetPlayerTrace(ply, dir, ...)
  141.                 if ply and ply:IsPlayer() and ply:HasPlayerPhysics() then
  142.                     dir = dir or ply:GetAimVector()
  143.  
  144.                     local trace = {}
  145.                    
  146.                     trace.start = ply:EyePos()
  147.                     trace.endpos = trace.start + (dir * 4096 * 4)
  148.                     trace.filter = {ply, ply:GetPlayerPhysics()}
  149.                    
  150.                     return trace
  151.                 end
  152.                 return oldmeta.player.GetPlayerTrace(ply, dir, ...)
  153.             end    
  154.        
  155.         if SERVER then
  156.             function player_meta:SetPlayerPhysics(bool)        
  157.                 if bool and not self:HasPlayerPhysics() then
  158.                     local entity = ents.Create(ENT.ClassName)
  159.                     entity:Spawn()
  160.                     entity:SetPlayer(self)
  161.                     return entity
  162.                 elseif bool == false and self:HasPlayerPhysics() then
  163.                     self:GetPlayerPhysics():Remove()                   
  164.                 end
  165.             end
  166.  
  167.             oldmeta.entity.SetPos = oldmeta.entity.SetPos or entity_meta.SetPos
  168.                 function entity_meta:SetPos(command, ...)
  169.                     if self and not (self:IsPlayer() and self:HasPlayerPhysics()) then
  170.                         return oldmeta.entity.SetPos(self, command, ...)
  171.                     end
  172.                 end
  173.  
  174.             oldmeta.player.Give = oldmeta.player.Give or player_meta.Give
  175.                 function player_meta:Give(class, ...)
  176.                     if self and type(class) == "string" and self:HasPlayerPhysics() then
  177.  
  178.                         local player_phys = self:GetPlayerPhysics()
  179.  
  180.                         self:SetParent()
  181.  
  182.                         local ent = self:Give(class, ...)
  183.  
  184.                         self:SetParent(player_phys)
  185.  
  186.                         return ent
  187.                     end
  188.                     return oldmeta.player.Give(self, class, ...)
  189.                 end
  190.         end
  191.     end
  192. end
  193.  
  194. do -- hooks
  195.  
  196.     hook.Add("ShouldCollide", "player_physics", function(a, b)
  197.         if a:IsPlayer() and a:GetPlayerPhysics() == b or b:IsPlayer() and b:GetPlayerPhysics() == a then
  198.             return false
  199.         end
  200.     end)
  201.  
  202.     hook.Add("Move", "player_physics", function(ply, ucmd)
  203.                
  204.         if ply.__player_physics_request_duck then
  205.             ucmd:SetVelocity(vector_origin)
  206.             ply.__player_physics_request_duck = nil
  207.         end
  208.    
  209.         if ply:HasPlayerPhysics() then
  210.             return true
  211.         end
  212.     end)
  213.  
  214.     local function Disallow(ply, ent)
  215.         if ply:GetPlayerPhysics() == ent then
  216.             return false
  217.         end
  218.     end
  219.    
  220.     hook.Add("GravGunPickupAllowed", "player_physics", Disallow)
  221.     hook.Add("GravGunPunt", "player_physics", Disallow)
  222.     hook.Add("PhysgunPickup", "player_physics", Disallow)
  223.    
  224.     if CLIENT then
  225.    
  226.         local ply = LocalPlayer()
  227.    
  228.         hook.Add("CreateMove", "player_physics", function(ucmd)
  229.             ply = ply:IsPlayer() and ply or LocalPlayer()
  230.            
  231.             if ply:HasPlayerPhysics() then
  232.                 ucmd:SetButtons(ucmd:GetButtons() | IN_DUCK)
  233.             end
  234.         end)
  235.  
  236.         hook.Add("UpdateAnimation", "player_physics", function(ply)
  237.             if ply:HasPlayerPhysics() then
  238.                 local self = ply:GetPlayerPhysics()
  239.                 if ply == LocalPlayer() and not ply:ShouldDrawLocalPlayer() then return end
  240.                
  241.                 ply:SetPos(self:GetBottom())
  242.                 ply:SetRenderOrigin(ply:GetPos())
  243.                 ply:SetAngles(self:GetAngles())
  244.                 ply:SetRenderAngles(ply:GetAngles())
  245.                 ply:SetupBones()               
  246.                
  247.                 hook.Call("PPhys", nil, ply, self, NULL)
  248.             end
  249.         end)
  250.        
  251.         hook.Add("CalcView", "player_physics", function(ply,pos,ang,fov)
  252.             if LocalPlayer():HasPlayerPhysics() then
  253.                
  254.                 local new_pos, new_ang =  hook.Call("PPhys", nil, ply, ply:GetPlayerPhysics(), NULL)
  255.                
  256.                 if new_pos or new_ang then
  257.                     pos = new_pos or pos
  258.                     pos = new_ang or ang
  259.                    
  260.                     return GAMEMODE:CalcView(ply,pos,ang,fov)
  261.                 end
  262.             end
  263.         end)
  264.        
  265.     else
  266.         local me = nero.GetPlayer"oh"
  267.         me:SetPlayerPhysics(false)
  268.         timer.Simple(0.2, function()
  269.         local ent = me:SetPlayerPhysics(true)
  270.         undo.Create("player_physics")
  271.             undo.SetPlayer(me)
  272.             undo.AddEntity(ent)
  273.         undo.Finish()
  274.         end)
  275.     end
  276. end
  277.  
  278. do -- example
  279.     hook.Add("PPhys", 0, function(ply, ent, phys)
  280.         if CLIENT then
  281.             --prediction
  282.         end
  283.        
  284.         if SERVER then
  285.             phys:EnableGravity(false)
  286.            
  287.             local ang = ply:EyeAngles()
  288.             phys:AddAngleVelocity(ang * 0.12)
  289.            
  290.             phys:AddAngleVelocity(phys:GetAngleVelocity() * - 0.1)
  291.            
  292.         end
  293.     end)
  294. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement