Advertisement
HK47

Broom

Nov 21st, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.56 KB | None | 0 0
  1. /***
  2. *   Made by kekc vn
  3. *
  4. *   Use, modify this source code as you wish
  5. ***/
  6.  
  7. AddCSLuaFile()
  8.  
  9. ENT.Type = "anim"
  10. ENT.Base = "base_anim"
  11. ENT.Spawnable = true
  12. ENT.PrintName = "Broom"
  13. ENT.Category = "Harry Potter"
  14.  
  15. ENT.Wait = 0 // wait before next use
  16. ENT.Speed = 1500 // default speed
  17. //ENT.Parts = Color(255, 255, 0)
  18. ENT.CameraRotate = false // disabling mouse rotate
  19.  
  20. function ENT:Draw()
  21.     if SERVER then return end
  22.    
  23.     self:DrawModel()
  24.    
  25.     if self:GetNWBool("broom_diactivated") and GetConVar("brooms_drawendparts"):GetBool() then
  26.         local ef = EffectData()
  27.         ef:SetOrigin(self:GetPos() - self:GetForward() * 50)
  28.         ef:SetAngles(self:GetAngles())
  29.         util.Effect("effect_sm_kekc", ef)
  30.     end
  31. end
  32.  
  33. function ENT:OnTakeDamage(dmg)
  34.     if SERVER then self:TakePhysicsDamage(dmg) end
  35. end
  36.  
  37. function ENT:Initialize()
  38.     if CLIENT then return end
  39.  
  40.     self:SetModel("models/props_junk/harpoon002a.mdl")
  41.     self:SetMoveType(MOVETYPE_VPHYSICS)
  42.     self:SetSolid(SOLID_VPHYSICS)
  43.     self:PhysicsInit(SOLID_VPHYSICS)
  44.    
  45.     local phys = self:GetPhysicsObject()
  46.    
  47.     if phys:IsValid() then
  48.         phys:SetMass(200)
  49.     end
  50.    
  51.     self:StartMotionController()
  52. end
  53.  
  54. function ENT:Use(activator, caller)
  55.     if CLIENT then return end
  56.  
  57.     if not activator:IsValid() or not activator:IsPlayer() then return end
  58.     if self:GetNWBool("broom_diactivated") then return end
  59.     if activator:GetNWEntity("broom_active"):IsValid() then return end
  60.  
  61.     if CurTime() < self.Wait then return end
  62.    
  63.     self:SetDriver(activator)
  64.     self.Wait = CurTime() + 1
  65. end
  66.  
  67. function ENT:OnRemove()
  68.     self:SetDriver(NULL)
  69.     self:StopMotionController()
  70. end
  71.  
  72. function ENT:SpawnFunction(ply, tr, name)
  73.     if not tr.Hit then return end
  74.  
  75.     local pos = tr.HitPos + tr.HitNormal * 32
  76.    
  77.     local ent = ents.Create(name)
  78.     ent:SetPos(pos)
  79.     ent:SetAngles(Angle(0, 0, 180))
  80.     ent:Spawn()
  81.     ent:Activate()
  82.    
  83.     ent.Class = self.PrintName // set class
  84.    
  85.     if IsValid(ent:GetPhysicsObject()) then ent:GetPhysicsObject():Wake() else return NULL end
  86.    
  87.     return ent
  88. end
  89.  
  90. function ENT:Diactivate(time)
  91.     if CLIENT then return end
  92.  
  93.     self:StopMotionController()
  94.     self:SetNWBool("broom_diactivated", true)
  95.    
  96.     timer.Create("start_motion_contr_kekc" .. self:EntIndex(), time, 1, function()
  97.         if self:IsValid() then self:StartMotionController() self:SetNWBool("broom_diactivated", false) end
  98.     end)
  99. end
  100.  
  101. function ENT:GetDriver() return self:GetOwner() end
  102.  
  103. function ENT:SetDriver(ply)
  104.     if CLIENT then return end
  105.  
  106.     local driver = self:GetDriver()
  107.    
  108.     if driver:IsValid() then
  109.         if not ply:IsValid() then
  110.             driver:SetNWEntity("broom_active", NULL)
  111.             driver:DrawWorldModel(true)
  112.             driver:DrawViewModel(true)
  113.             driver:SetNoDraw(false)
  114.             driver:SetMoveType(MOVETYPE_WALK)
  115.            
  116.             if self.UsedWeapon then
  117.                 if driver:HasWeapon("weapon_crowbar") then driver:StripWeapon("weapon_crowbar") end
  118.                 self.UsedWeapon = false
  119.             end
  120.            
  121.             if self.PlyOldWep and driver:HasWeapon(self.PlyOldWep) then
  122.                 driver:SelectWeapon(self.PlyOldWep)
  123.                 self.PlyOldWep = nil
  124.             end
  125.            
  126.             driver:SetPos(driver:GetPos() + Vector(0, 0, 60)) // fixing stuck in walls
  127.             if self:GetPhysicsObject():IsValid() then self:GetPhysicsObject():SetVelocity(self:GetForward() * 500) end
  128.            
  129.             // Removing ghost and hitentity
  130.             if self.Ghost and self.Ghost:IsValid() then self.Ghost:Remove() end
  131.         else
  132.             return
  133.         end
  134.     end
  135.  
  136.     /*******************
  137.         Setup some shit
  138.         Ugly animation
  139.         Saving data
  140.     *******************/
  141.     if ply:IsValid() then
  142.         local weapon = ply:GetActiveWeapon()
  143.         if weapon:IsValid() then self.PlyOldWep = weapon:GetClass() end
  144.        
  145.         if not ply:HasWeapon("weapon_crowbar") then
  146.             ply:Give("weapon_crowbar")
  147.             self.UsedWeapon = true
  148.         end
  149.        
  150.         ply:SelectWeapon("weapon_crowbar")
  151.    
  152.         ply:SetNWEntity("broom_active", self)
  153.         ply:DrawWorldModel(false)
  154.         ply:DrawViewModel(false)
  155.         ply:SetNoDraw(true)
  156.         ply:SetMoveType(MOVETYPE_NOCLIP)
  157.        
  158.         if self.Ghost and self.Ghost:IsValid() then self.Ghost:Remove() end
  159.  
  160.         local ang = self:GetAngles()
  161.         ang:RotateAroundAxis(ang:Forward(), 180)
  162.         ply:SetEyeAngles(Angle(ang.p, ang.y, 0))
  163.         ang:RotateAroundAxis(ang:Right(), -60)
  164.        
  165.         self.Ghost = ents.Create("kekc_ghost_ent")
  166.         self.Ghost:SetPos(self:LocalToWorld(Vector(-20, 0, -5)))
  167.         self.Ghost:SetAngles(ang)
  168.         self.Ghost:SetParent(self)
  169.         self.Ghost:SetModel(ply:GetModel())
  170.         self.Ghost:Spawn()
  171.         self.Ghost:SetNWEntity("broom_driver", ply)
  172.         self.Ghost:SetSolid(SOLID_NONE)
  173.    
  174.         /**********************************
  175.             Ugly animation goes here
  176.             Do not kill me for this shit
  177.         **********************************/
  178.         self.Ghost:ManipulateBoneAngles(Entity(1):LookupBone("ValveBiped.Bip01_Head1"), Angle(0, 30, 0))   
  179.         self.Ghost:ManipulateBoneAngles(Entity(1):LookupBone("ValveBiped.Bip01_R_Calf"), Angle(0, 30, 0))
  180.         self.Ghost:ManipulateBoneAngles(Entity(1):LookupBone("ValveBiped.Bip01_R_Thigh"), Angle(10, 30, 0))
  181.         self.Ghost:ManipulateBoneAngles(Entity(1):LookupBone("ValveBiped.Bip01_L_Calf"), Angle(0, 30, 0))
  182.         self.Ghost:ManipulateBoneAngles(Entity(1):LookupBone("ValveBiped.Bip01_L_Thigh"), Angle(-10, 30, 0))
  183.         self.Ghost:ManipulateBoneAngles(Entity(1):LookupBone("ValveBiped.Bip01_R_Hand"), Angle(-30, -30, 40))
  184.         self.Ghost:ManipulateBoneAngles(Entity(1):LookupBone("ValveBiped.Bip01_R_Forearm"), Angle(0, 50, 0))
  185.         self.Ghost:ManipulateBoneAngles(Entity(1):LookupBone("ValveBiped.Bip01_R_Clavicle"), Angle(50, 20, 100))
  186.         self.Ghost:ManipulateBoneAngles(Entity(1):LookupBone("ValveBiped.Bip01_L_Hand"), Angle(30, -30, -40))
  187.         self.Ghost:ManipulateBoneAngles(Entity(1):LookupBone("ValveBiped.Bip01_L_Forearm"), Angle(0,  50,0))
  188.         self.Ghost:ManipulateBoneAngles(Entity(1):LookupBone("ValveBiped.Bip01_L_Clavicle"), Angle(-50, 0, -100))
  189.         self.Ghost:SetSequence(ply:LookupSequence("sit"))
  190.        
  191.         self:SetNWEntity("broom_ghost", self.Ghost)
  192.     end
  193.  
  194.     self:SetNWEntity("broom_driver", ply)
  195.     self:SetOwner(ply)
  196. end
  197.  
  198. /******************
  199.     Stuff
  200. ******************/
  201. function ENT:Think()
  202.     if CLIENT then return end
  203.    
  204.     local phys = self:GetPhysicsObject()
  205.     if phys:IsValid() then phys:Wake() end
  206.    
  207.     local driver = self:GetDriver()
  208.    
  209.     if driver:IsValid() then
  210.         driver:DrawWorldModel(false)
  211.         driver:DrawViewModel(false)
  212.         driver:SetNoDraw(true)
  213.        
  214.         local weapon = driver:GetActiveWeapon()
  215.        
  216.         if weapon:IsValid() then
  217.             weapon:SetNextPrimaryFire(CurTime() + 1.5)
  218.             weapon:SetNextSecondaryFire(CurTime() + 1.5)
  219.         end
  220.     end
  221.    
  222.     self:NextThink(CurTime())
  223.     return true
  224. end
  225.  
  226. /*******************
  227.     Now we can fly!
  228. *******************/
  229. ENT.K = 1
  230.  
  231. function ENT:PhysicsSimulate(phys, deltatime)
  232.     if CLIENT then return end
  233.    
  234.     local angf = Vector(0, 0, 0)
  235.     local vecf = Vector(0, 0, 0)
  236.     local damping = -phys:GetAngleVelocity() * 5
  237.     local ang1 = phys:GetAngles()
  238.    
  239.     local driver = self:GetDriver()
  240.    
  241.     if driver:IsValid() then
  242.         if self:WaterLevel() >= 3 then
  243.             self:SetDriver(NULL)
  244.             vecf = vecf + VectorRand() * 3000 + vector_up * 5000
  245.             angf = angf + vecf
  246.             driver:SetVelocity(VectorRand() * 200)
  247.         end
  248.    
  249.         local ang2 = driver:EyeAngles()
  250.        
  251.         local angy = math.NormalizeAngle(ang1.y - ang2.y)
  252.         local angp = math.NormalizeAngle(ang1.p - ang2.p)
  253.         local angr = math.NormalizeAngle(ang1.r + angy * 5)
  254.        
  255.         // disabling mouse rotate
  256.         if self.CameraRotate then angy = 0 angp = 0 angr = math.NormalizeAngle(ang1.r - 0) / 20 end
  257.        
  258.         // fix some shit with angles
  259.         //if ang1.p < -60 then angp = -15 elseif ang1.p > 60 then angp = 15 end
  260.  
  261.         angf = angf + Vector(angr * deltatime * 50, angp * 15, angy * 25)
  262.  
  263.         /***
  264.             Controls
  265.         ***/
  266.         local max = GetConVar("brooms_speed_max") // maximum speed
  267.         if max then max = max:GetInt() else max = 2 end
  268.        
  269.         if driver:KeyDown(IN_SPEED) then
  270.             if self.K < max then self.K = self.K + 1 end
  271.         else
  272.             if self.K > 1 then self.K = self.K - 1 end
  273.         end
  274.        
  275.         if driver:KeyDown(IN_JUMP) then
  276.             vecf = vecf + vector_up * (self.Speed * 0.06) * (self.K * 5)
  277.         end
  278.            
  279.         if driver:KeyDown(IN_DUCK) then
  280.             vecf = vecf - vector_up * (self.Speed * 0.06) * (self.K * 5)
  281.         end
  282.        
  283.         if driver:KeyDown(IN_FORWARD) then
  284.             vecf = vecf + phys:GetAngles():Forward() * self.Speed * self.K
  285.         elseif driver:KeyDown(IN_BACK) then
  286.             vecf = vecf - phys:GetAngles():Forward() * (self.Speed * 0.5) * self.K
  287.         else       
  288.             if driver:KeyDown(IN_MOVERIGHT) then
  289.                 vecf = vecf - phys:GetAngles():Right() * (self.Speed * 0.6) * self.K
  290.                 //angf = angf + Vector(100, 0, 0)
  291.             end
  292.            
  293.             if driver:KeyDown(IN_MOVELEFT) then
  294.                 vecf = vecf + phys:GetAngles():Right() * (self.Speed * 0.6) * self.K
  295.                 //angf = angf - Vector(100, 0, 0)
  296.             end
  297.         end
  298.     else
  299.         local angp = math.NormalizeAngle(ang1.p - 0)
  300.         local angr = math.NormalizeAngle(ang1.r - 0) * deltatime
  301.        
  302.         angf = angf + Vector(angr * 155, angp * 35, 0)
  303.     end
  304.    
  305.     vecf = vecf + (-phys:GetVelocity() * 1.5) + vector_up * (586.5 + math.sin(CurTime() * 2) * 3)
  306.     angf = angf + damping * deltatime * 350 * (self.K * 0.25)
  307.    
  308.     return angf, vecf, SIM_GLOBAL_ACCELERATION
  309. end
  310.  
  311. function ENT:PhysicsCollide(data, phys)
  312.     if CLIENT then return end
  313.  
  314.     if data.DeltaTime < 0.6 then return end
  315.    
  316.     local driver = self:GetDriver()
  317.    
  318.     if driver and driver:IsValid() and data.Speed > 250 then
  319.         driver:TakeDamage(5)
  320.         driver:EmitSound("physics/body/body_medium_break" .. math.random(2, 4) .. ".wav")
  321.        
  322.         if not data.HitEntity:IsWorld() and data.Speed > 900 then
  323.             if self:IsValid() then
  324.                 self:SetDriver(NULL)
  325.                 driver:TakeDamage(15)
  326.                 driver:EmitSound("physics/body/body_medium_break" .. math.random(2, 4) .. ".wav")
  327.                
  328.                 driver:SetVelocity((VectorRand() + vector_up * 600) * 800)
  329.                
  330.                 self:GetPhysicsObject():SetVelocity(VectorRand() * 1000)
  331.                 self:GetPhysicsObject():AddAngleVelocity(VectorRand() * 2000)
  332.                
  333.                 self:Diactivate(5)
  334.             end
  335.         end
  336.     end
  337. end
  338.  
  339.  
  340. /***********************
  341.     Stuff
  342.     Hooks
  343.     Rotating
  344.     First/Third person
  345. ***********************/
  346. hook.Add("KeyPress", "broom_KeyPress", function(ply, key)
  347.     if CLIENT then return end
  348.  
  349.     local ent = ply:GetNWEntity("broom_active")
  350.     if not ent:IsValid() then return end
  351.  
  352.     if key == IN_ATTACK2 then
  353.         ent.CameraRotate = !ent.CameraRotate
  354.        
  355.         local text
  356.         if ent.CameraRotate then text = "disabled" else text = "enabled" end
  357.        
  358.         ply:ChatPrint("Camera rotating: " .. text)
  359.     end
  360.    
  361.     if key == IN_USE and CurTime() > ent.Wait then
  362.         ent:SetDriver(NULL)
  363.         ent.Wait = CurTime() + 1
  364.     end
  365. end)
  366.  
  367. hook.Add("ShouldDrawLocalPlayer", "broom_drawply", function()
  368.     local ent = LocalPlayer():GetNWEntity("broom_active")
  369.  
  370.     if ent and ent:IsValid() then
  371.         /* Drawing player if he is not in first person mode */
  372.  
  373.         local gh = ent:GetNWEntity("broom_ghost")
  374.         if gh and gh:IsValid() then gh:SetNoDraw(false) end
  375.        
  376.         return false
  377.     end
  378. end)
  379.  
  380. hook.Add("CalcView", "broom_calcview", function(ply, pos, ang, fov)
  381.     local ent = ply:GetNWEntity("broom_active")
  382.  
  383.     if not ent:IsValid() then return end
  384.     if ply:InVehicle() or not ply:Alive() or ply:GetViewEntity() != ply then return end
  385.    
  386.     local dist = GetConVar("brooms_camdist")
  387.     if dist then dist = dist:GetInt() else dist = 200 end
  388.  
  389.     local dir = ply:GetAngles():Forward()
  390.     local pos = ent:GetPos() + Vector(0, 0, 70) - dir * dist
  391.     local ang = dir:Angle()
  392.    
  393.     // First person mode
  394.     if GetConVar("brooms_enablefp") and GetConVar("brooms_enablefp"):GetBool() then
  395.         local gh = ent:GetNWEntity("broom_ghost")
  396.         if gh and gh:IsValid() then gh:SetNoDraw(true) end
  397.        
  398.         if not gh then return end
  399.         if not gh:IsValid() then return end
  400.    
  401.         local bone = gh:LookupBone("ValveBiped.Bip01_Head1")
  402.         bone = gh:GetBonePosition(bone) + gh:GetForward() * 2 - gh:GetUp() * 10
  403.        
  404.         local localang = gh:GetAngles()
  405.         localang:RotateAroundAxis(localang:Right(), 60)
  406.         localang.y = ang.y
  407.         if GetConVar("brooms_fullmousectrl") and GetConVar("brooms_fullmousectrl"):GetBool() then localang.p = ang.p end
  408.        
  409.         if bone then ang = localang pos = bone end
  410.     end
  411.  
  412.     local tr = util.TraceHull {
  413.         start = ent:GetPos(),
  414.         endpos = pos,
  415.         filter = { ent, ply, ent:GetNWEntity("broom_active") },
  416.         mask = MASK_NPCWORLDSTATIC,
  417.         mins = Vector(-4, -4, -4),
  418.         maxs = Vector(4, 4, 4)
  419.     }
  420.  
  421.     local view = {
  422.         origin = tr.HitPos,
  423.         angles = ang,
  424.         fov = fov,
  425.     }
  426.  
  427.     return view
  428. end)
  429.  
  430. hook.Add("Move", "create_move_broom", function(ply, mv)
  431.     local ent = ply:GetNWEntity("broom_active")
  432.     if not ent:IsValid() then return end
  433.     if not ply:Alive() then ent:SetDriver(NULL) end
  434.    
  435.     mv:SetOrigin(ent:GetPos() - Vector(0, 0, 60)) // fixing bugs with view
  436.    
  437.     return true
  438. end)
  439.  
  440. /***************
  441.     Client stuff
  442. ***************/
  443. if CLIENT then
  444.     /*surface.CreateFont("broom_font1", {
  445.         font = "Arial",
  446.         size = 40,
  447.         weight = 0,
  448.         blursize = 0,
  449.         scanlines = 0,
  450.         antialias = true,
  451.         underline = false,
  452.         italic = false,
  453.         strikeout = false,
  454.         symbol = false,
  455.         rotary = false,
  456.         shadow = false,
  457.         additive = false,
  458.         outline = false,
  459.     })*/
  460.    
  461.     hook.Add("HUDPaint", "broom_drawhud", function()
  462.         local ent = LocalPlayer():GetNWEntity("broom_active")
  463.         if not ent:IsValid() then return end
  464.        
  465.         if GetConVar("brooms_drawcircle") and GetConVar("brooms_drawcircle"):GetBool() then
  466.             local angp = math.NormalizeAngle(ent:GetAngles().p - LocalPlayer():EyeAngles().p)
  467.             local n = 2 + (angp * 0.01)
  468.            
  469.             surface.DrawCircle(ScrW() / 2, ScrH() / n, 10, Color(0, 255, 0))
  470.         end
  471.     end)
  472.    
  473.     hook.Add("PlayerBindPress", "broom_bindpress", function(ply, bind, pressed)
  474.         local ent = ply:GetNWEntity("broom_active")
  475.         if not ent:IsValid() then return end
  476.        
  477.         local tools = {
  478.             "phys_swap",
  479.             "slot",
  480.             "invnext",
  481.             "invprev",
  482.             "lastinv",
  483.             "gmod_tool",
  484.             "gmod_toolmode"
  485.         }
  486.        
  487.         for k, v in pairs(tools) do
  488.             if bind:find(v) then return true end
  489.         end
  490.     end)
  491. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement