Advertisement
CapsAdmin

Untitled

Mar 6th, 2013
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.87 KB | None | 0 0
  1. rope = {}
  2.  
  3. rope.ActiveRopes = {}
  4.  
  5. local A = Vector()
  6. local damping = -0.05
  7. local speed = 1
  8. local gravity = {x=0,y=0,z=0}
  9.  
  10. local temp_vec = Vector() -- temp
  11. local point_contents = util.PointContents
  12. local band = bit.band
  13. local CONTENTS_SOLID = CONTENTS_SOLID
  14.  
  15. local function add_vel(a, vx, vy, vz, ft)              
  16.     a.vel.x = a.vel.x + (vx * ft)
  17.     a.vel.y = a.vel.y + (vy * ft)
  18.     a.vel.z = a.vel.z + (vz * ft)  
  19. end
  20.  
  21. local function check_collision(a, ft)
  22.     temp_vec.x = a.pos.x + (a.vel.x) * ft
  23.     temp_vec.y = a.pos.y + (a.vel.y) * ft
  24.     temp_vec.z = a.pos.z + (a.vel.z) * ft
  25.    
  26.     local content = point_contents(temp_vec)
  27.    
  28.     if band(content, CONTENTS_SOLID) == CONTENTS_SOLID then    
  29.         a.vel.x = a.vel.x * 0.25
  30.         a.vel.y = a.vel.y * 0.25
  31.         a.vel.z = -a.vel.z * 0.25
  32.        
  33.         return true
  34.     end
  35. end
  36.  
  37. local function calc(a, b, ft)
  38.     if not a.first then
  39.         add_vel(a, math.Rand(-1,1)*10, math.Rand(-1,1)*10, math.Rand(-1,1)*10, ft)
  40.         a.first = true
  41.     end
  42.    
  43.     add_vel(
  44.         a,
  45.        
  46.         (b.pos.x - a.pos.x) * speed,
  47.         (b.pos.y - a.pos.y) * speed,
  48.         (b.pos.z - a.pos.z) * speed,
  49.        
  50.         ft
  51.     )
  52.    
  53.     --add_vel(a, gravity.x,  gravity.y,  gravity.z, ft)
  54.            
  55.     if not check_collision(a, ft) then
  56.         a.pos.x = a.pos.x + (a.vel.x) * ft
  57.         a.pos.y = a.pos.y + (a.vel.y) * ft
  58.         a.pos.z = a.pos.z + (a.vel.z) * ft
  59.     end
  60.    
  61.     a.vel.x = a.vel.x + a.vel.x * damping
  62.     a.vel.y = a.vel.y + a.vel.y * damping
  63.     a.vel.z = a.vel.z + a.vel.z * damping
  64. end
  65.  
  66. function rope.Think()
  67.     A = Entity(483):GetPos()
  68.     local ft = RealFrameTime() * 10
  69.    
  70.     for key, rope in pairs(rope.ActiveRopes) do
  71.         local points = rope.points
  72.                
  73.         for i = 2, #points do
  74.             calc(points[i - 1], points[i], ft)
  75.             calc(points[i], points[i - 1], ft)
  76.         end
  77.        
  78.         for i, var in pairs(rope.attachments) do
  79.             local point = rope.points[i]
  80.            
  81.             if point then
  82.                 if type(var) == "function" then
  83.                     local val = var()
  84.                     point.pos.x = val.x
  85.                     point.pos.y = val.y
  86.                     point.pos.z = val.z
  87.                 else
  88.                     local val = var
  89.                     point.pos.x = val.x
  90.                     point.pos.y = val.y
  91.                     point.pos.z = val.z
  92.                 end
  93.                
  94.                 point.vel.x = 0
  95.                 point.vel.y = 0
  96.                 point.vel.z = 0
  97.             end
  98.         end
  99.     end
  100. end
  101.  
  102. local color_white = color_white
  103. local sprite_mat = Material("particle/particle_sphere")
  104.  
  105. function rope.Draw()
  106.     for key, rope in pairs(rope.ActiveRopes) do
  107.         local count = #rope.points
  108.            
  109.         render.SetMaterial(rope.mat)
  110.                        
  111.         render.StartBeam(count)    
  112.             for i = 1, count do
  113.                 local v = rope.points[i].pos
  114.            
  115.                 temp_vec.x = v.x
  116.                 temp_vec.y = v.y
  117.                 temp_vec.z = v.z
  118.                
  119.                 render.AddBeam(temp_vec, 20, i/count, color_white)    
  120.             end
  121.         render.EndBeam()
  122.                
  123.         render.SetMaterial(sprite_mat)
  124.        
  125.         for i = 1, count do
  126.             local v = rope.points[i].pos
  127.        
  128.             temp_vec.x = v.x
  129.             temp_vec.y = v.y
  130.             temp_vec.z = v.z
  131.            
  132.             render.DrawSprite(temp_vec, 20, 20, color_white)
  133.         end
  134.     end
  135. end
  136.  
  137. function rope.Create(points, attachments, mat)
  138.     points = points or 128
  139.     mat = mat or Material("cable/rope")
  140.     attachments = attachments or {[1] = function() return LocalPlayer():EyePos() end, [#points] = function() return Vector(0,0,0) end}
  141.    
  142.     local data = {}
  143.    
  144.     data.mat = mat
  145.     data.points = {}
  146.     data.attachments = attachments
  147.  
  148.     local pos = select(2, next(attachments))
  149.     pos = type(pos) == "function" and pos() or pos or VectorRand()
  150.        
  151.     for i = 1, points do
  152.         data.points[i] = {
  153.             pos =
  154.             {
  155.                 x = pos.x,
  156.                 y = pos.y,
  157.                 z = pos.z,
  158.             },
  159.            
  160.             vel = {x=math.Rand(-1,1),y=math.Rand(-1,1),z=math.Rand(-1,1)}
  161.         }
  162.     end
  163.    
  164.     data.id = table.insert(rope.ActiveRopes, data)
  165.     data.Remove = function()
  166.         table.remove(rope.ActiveRopes, data.id)
  167.     end
  168.    
  169.     return data
  170. end
  171.  
  172. hook.Add("Think", "rope", rope.Think)
  173. hook.Add("PostDrawTranslucentRenderables", "rope", rope.Draw)
  174.  
  175. rope.Create(128, {[1] = function() return Entity(483):GetPos() end, [128] = function() return Entity(485):GetPos() end})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement