Advertisement
CapsAdmin

Untitled

Apr 21st, 2013
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.38 KB | None | 0 0
  1. if _G.rope then
  2.     for k,v in pairs(rope.ActiveRopes) do
  3.         for k,v in pairs(v.points) do
  4.             if v.type == 2 then
  5.                 v.obj:SetDieTime(0)
  6.             else
  7.                 SafeRemoveEntity(v.ent)
  8.             end
  9.         end
  10.     end
  11. end
  12.  
  13. rope = {}
  14.  
  15. rope.ActiveRopes = {}
  16.  
  17. local tempvec = Vector()
  18. local tempvecn = Vector()
  19. local sqrt = math.sqrt
  20.  
  21. local x,y,z = 0,0,0
  22. local ox,oy,oz = 0,0,0
  23. local len = 0
  24.  
  25. local THINKTIME = 0
  26. local DELTA = 1
  27. local SPEED = 100
  28.  
  29. local function calc(a, b)  
  30.     ---local offset = a.obj:GetPos() - b.obj:GetPos()
  31.     --local len = offset:Length()
  32.    
  33.     local apos = a.obj:GetPos()
  34.     local bpos = b.obj:GetPos()
  35.    
  36.     local avel = a.obj:GetVelocity()
  37.     local bvel = b.obj:GetVelocity()
  38.    
  39.     ox = apos.x - bpos.x
  40.     oy = apos.y - bpos.y
  41.     oz = apos.z - bpos.z
  42.     len = (ox*ox) + (oy*oy) + (oz*oz)
  43.    
  44.     --force = (a.obj:GetVelocity() - b.obj:GetVelocity()) * -200
  45.     x = (avel.x - bvel.x) * -100
  46.     y = (avel.y - bvel.y) * -100
  47.     z = (avel.z - bvel.z) * -100
  48.  
  49.     if len ~= 0 then
  50.         --force = force + offset:GetNormalized() * (len) * (-7000)
  51.         len = sqrt(len)
  52.        
  53.         x = x + ((ox / len) * (len) * (-10000) * DELTA)
  54.         y = y + ((oy / len) * (len) * (-10000) * DELTA)
  55.         z = z + ((oz / len) * (len) * (-10000) * DELTA)
  56.     end
  57.  
  58.    
  59.     if a.type == 1 then
  60.         x = (x * 0.1 / 100)
  61.         y = (y * 0.1 / 100)
  62.         z = (z * 0.1 / 100)
  63.        
  64.         tempvec.x = x
  65.         tempvec.y = y
  66.         tempvec.z = z
  67.        
  68.         tempvecn.x = -x
  69.         tempvecn.y = -y
  70.         tempvecn.z = -z
  71.        
  72.         a.obj:ApplyForceCenter(tempvec)
  73.         b.obj:ApplyForceCenter(tempvecn) -- does unm create a new vector?????
  74.     elseif a.type == 2 then
  75.         x = (x * 0.1 / 50)
  76.         y = (y * 0.1 / 50)
  77.         z = (z * 0.1 / 50)
  78.        
  79.         tempvec.x = avel.x + x
  80.         tempvec.y = avel.y + y
  81.         tempvec.z = avel.z + z
  82.        
  83.         tempvecn.x = bvel.x - x
  84.         tempvecn.y = bvel.y - y
  85.         tempvecn.z = bvel.z - z
  86.        
  87.         a.obj:SetVelocity(tempvec)
  88.         b.obj:SetVelocity(tempvecn)
  89.     end
  90. end
  91.  
  92. local a
  93. local b
  94. local vec
  95. local T = 0
  96. local last
  97.  
  98. function rope.Think()
  99.    
  100.     DELTA = math.Clamp(FrameTime() * 30, 0, 0.4)
  101.    
  102.     --local T = SysTime()
  103.     --last = last or T
  104.    
  105.     --if last < T then
  106.        
  107.         for key, rope in pairs(rope.ActiveRopes) do                    
  108.             for i = 2, #rope.points do
  109.                 calc(rope.points[i - 1], rope.points[i])
  110.             end
  111.         end
  112.                
  113.         --DELTA = (T-last) * SPEED
  114.         --last = T + THINKTIME
  115.     --end
  116.    
  117.     for key, rope in pairs(rope.ActiveRopes) do                    
  118.         for i, var in pairs(rope.attachments) do
  119.             local point = rope.points[i]
  120.            
  121.             if point then
  122.                 point.obj:SetPos(type(var) == "function" and var() or var)
  123.                 point.obj:SetVelocity(vector_origin)
  124.             end
  125.         end
  126.     end
  127. end
  128.  
  129. local color_white = color_white
  130. local sprite_mat = Material("particle/particle_sphere")
  131.  
  132. function rope.Draw()
  133.     for key, rope in pairs(rope.ActiveRopes) do
  134.         local count = #rope.points
  135.            
  136.         render.SetMaterial(rope.mat or sprite_mat)
  137.                        
  138.         render.StartBeam(count)    
  139.             for i = 1, count do                        
  140.                 render.AddBeam(rope.points[i].obj:GetPos(), 1, (i/count), color_white)    
  141.             end
  142.         render.EndBeam()
  143.                
  144.         render.SetMaterial(sprite_mat)
  145.     end
  146. end
  147.  
  148. function rope.Create(points, attachments, mat, ent_type)
  149.     ent_type = ent_type or 2
  150.     points = points or 128
  151.     mat = mat or Material("trails/laser")
  152.     attachments = attachments or {[1] = function() return LocalPlayer():EyePos() end, [#points] = function() return Vector(0,0,0) end}
  153.    
  154.     local data = {}
  155.    
  156.     data.mat = mat
  157.     data.points = {}
  158.     data.attachments = attachments
  159.    
  160.     local pos = select(2, next(attachments))
  161.     pos = type(pos) == "function" and pos() or pos
  162.    
  163.     for i = 1, points do
  164.         local obj
  165.        
  166.         if ent_type == 2 then
  167.             rope.emitter = rope.emitter or ParticleEmitter(Vector())
  168.             ent = rope.emitter:Add("particle/snow", pos)
  169.             ent:SetVelocity(VectorRand())
  170.             ent:SetDieTime(1000)
  171.             ent:SetLifeTime(0)
  172.             ent:SetStartSize(0)
  173.             ent:SetEndSize(0)
  174.             ent:SetGravity(physenv.GetGravity())
  175.             ent:SetCollide(true)
  176.             ent:SetAirResistance(100)
  177.         elseif ent_type == 1 then
  178.             ent = ents.CreateClientProp()
  179.             ent:SetCollisionGroup(COLLISION_GROUP_INTERACTIVE_DEBRIS)
  180.             ent:Spawn()
  181.             ent:PhysicsInitSphere(1)
  182.            
  183.            
  184.             local phys = ent:GetPhysicsObject()
  185.             phys:SetMass(1)
  186.             phys:SetMaterial("jeeptire")
  187.             phys:SetDamping(0.2, 0)
  188.             phys:EnableGravity(true)
  189.             ent:SetVelocity(VectorRand())
  190.            
  191.             ent = phys
  192.         end
  193.        
  194.         ent:SetPos(pos)
  195.        
  196.         data.points[i] = {
  197.             obj = ent,
  198.             type = ent_type
  199.         }
  200.     end
  201.    
  202.     data.id = table.insert(rope.ActiveRopes, data)
  203.     data.Remove = function()
  204.         table.remove(rope.ActiveRopes, data.id)
  205.     end
  206.    
  207.     return data
  208. end
  209.  
  210. timer.Create("rope", 0, 0, rope.Think)
  211.  
  212. -- hacky optimization
  213. -- allows only the last draw call
  214.  
  215. local function setup_suppress()
  216.     local last_framenumber = 0
  217.     local current_frame = 0
  218.     local current_frame_count = 0
  219.    
  220.     return function()
  221.         local frame_number = FrameNumber()
  222.        
  223.         if frame_number == last_framenumber then
  224.             current_frame = current_frame + 1
  225.         else
  226.             last_framenumber = frame_number
  227.                        
  228.             if current_frame_count ~= current_frame then
  229.                 current_frame_count = current_frame
  230.             end
  231.            
  232.             current_frame = 1
  233.         end
  234.                
  235.         return current_frame < current_frame_count
  236.     end
  237. end
  238. -- hacky optimization
  239.  
  240. local uh = setup_suppress()
  241.  
  242. hook.Add("PostDrawTranslucentRenderables", "rope", function()
  243.     if uh() then return end
  244.     rope.Think()
  245.     rope.Draw()
  246. end)
  247.  
  248.  
  249. local chess = chess
  250. rope.Create(64, {[1] = function() return me:EyePos() end, [64] = function() return chess:EyePos() end}, nil, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement