CapsAdmin

Untitled

Aug 2nd, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.37 KB | None | 0 0
  1. local print = epoe.Print
  2.  
  3. instrument = {} local s = instrument
  4.  
  5. instrument.DefaultSound = "synth/12_5_pwm_440.wav"
  6.  
  7. instrument.ValidKeys =
  8. {
  9.     IN_ATTACK,
  10.     IN_ATTACK2,
  11.     IN_WALK,
  12.     IN_SPEED,
  13.     IN_USE,
  14. }
  15.  
  16. -- sample meta
  17. do
  18.     local META = {}
  19.     META.__index = META
  20.    
  21.     function META:Initialize(ply)
  22.         self.Player = ply
  23.        
  24.         for i, path in pairs(self.Samples) do
  25.             self:SetSample(i, path)
  26.         end
  27.     end
  28.    
  29.     function META:CanPlay()
  30.         local wep = self.Player:GetActiveWeapon()
  31.         if wep:IsWeapon() and wep:GetClass() == "none" then
  32.             return true
  33.         end
  34.        
  35.         return false
  36.     end
  37.    
  38.     function META:GetPos()
  39.         --local pos = self.Player:GetBonePosition(self.Player:LookupBone("ValveBiped.Bip01_Head1"))
  40.         return self.Player:EyePos()
  41.     end
  42.    
  43.     function META:GetAngles()
  44.         return self.Player:EyeAngles()
  45.     end
  46.    
  47.     function META:IsPlaying() -- hm
  48.         for _, on in pairs(self.Keys) do
  49.             if on then
  50.                 return true
  51.             end
  52.         end
  53.        
  54.         return false
  55.     end
  56.    
  57.     function META:SetSample(i, path)
  58.         self.CSP[i] = CreateSound(self.Player, path or s.DefaultSound)
  59.         self.CSP[i]:SetSoundLevel(100)
  60.     end
  61.    
  62.     function META:ChangeVolume(i, num)     
  63.         if self.CSP[i] then
  64.             self.CSP[i]:ChangeVolume(self.Volume)
  65.         end
  66.     end
  67.    
  68.     function META:ChangePitch(i, num)      
  69.         if self.CSP[i] then            
  70.             self.CSP[i]:ChangePitch(self.Pitch)
  71.         end
  72.     end
  73.    
  74.     function META:SetPitch(num) -- ???
  75.         num = num or 1
  76.        
  77.         if self:IsKeyDown(IN_WALK) then
  78.             num = num - 1
  79.         end
  80.        
  81.         if self:IsKeyDown(IN_SPEED) then
  82.             num = num - 2
  83.         end
  84.        
  85.         self.Pitch = math.Clamp(math.floor(100 * 2 ^ num), 1, 255)
  86.                
  87.         for i in pairs(self.Samples) do
  88.             self:ChangePitch(i, self.Pitch)
  89.         end
  90.     end
  91.    
  92.     function META:SetVolume(num)       
  93.         self.Volume = math.Clamp(num or self.Volume, 0.0001, 1)
  94.        
  95.         for i in pairs(self.Samples) do
  96.             self:ChangeVolume(i, self.Volume)
  97.         end
  98.     end
  99.    
  100.     function META:Start(i) 
  101.         if not self:CanPlay() then return end
  102.        
  103.         if self.CSP[i] then
  104.             self.CSP[i]:PlayEx(self.Volume, self.Pitch)
  105.         end
  106.     end
  107.  
  108.     function META:Stop(i)
  109.         if self.CSP[i] then
  110.             self.CSP[i]:Stop()
  111.         end
  112.     end
  113.    
  114.     function META:IsKeyDown(key)
  115.         return self.Keys[key] == true
  116.     end
  117.    
  118.     function META:OnKeyEvent(key, press)
  119.         if key == IN_ATTACK then
  120.             if press then
  121.                 self:Start(1)
  122.             else
  123.                 self:Stop(1)
  124.             end
  125.         end
  126.        
  127.         if key == IN_ATTACK2 then
  128.             if press then
  129.                 self:Start(2)
  130.             else
  131.                 self:Stop(2)
  132.             end
  133.         end
  134.        
  135.         LocalPlayer():SetDSP(22)
  136.     end
  137.    
  138.     function META:Think()
  139.         if not self:CanPlay() then
  140.             self:Stop(1)
  141.             self:Stop(2)
  142.         return end
  143.    
  144.         local ang = self:GetAngles()
  145.        
  146.         if self:IsKeyDown(IN_USE) then
  147.             if self.using then
  148.                 self:SetVolume(math.abs(ang.y - self.using) / 20)
  149.             else
  150.                 self.using = ang.y
  151.             end
  152.         else
  153.             self.using = false
  154.             self:SetVolume(1)
  155.         end
  156.    
  157.         self:SetPitch(-ang.p/89)
  158.        
  159.         if self:IsKeyDown(IN_ATTACK) or self:IsKeyDown(IN_ATTACK2) then
  160.             self:MakeParticle()
  161.         end
  162.     end
  163.    
  164.     local emitter = CLIENT and ParticleEmitter(Vector()) or nil
  165.    
  166.     function META:MakeParticle()
  167.         local pitch = self.Pitch
  168.        
  169.         if self:IsKeyDown(IN_ATTACK2) then
  170.             pitch = math.Clamp(pitch * 2, 0, 200)
  171.         end
  172.        
  173.         local particle = emitter:Add("particle/particle_glow_04_additive", self:GetPos() + (self:GetAngles():Forward() * 30))
  174.         if particle then
  175.             local col = HSVToColor(pitch*1.4, self.Volume, 1)
  176.             particle:SetColor(col.r, col.g, col.b, self.Volume)
  177.            
  178.             particle:SetVelocity(VectorRand() * self.Volume )
  179.  
  180.             particle:SetDieTime(40)
  181.             particle:SetLifeTime(0)
  182.            
  183.             local size = (-pitch + 255) / 100
  184.                        
  185.             particle:SetAngles(AngleRand())
  186.             particle:SetStartSize(size)
  187.             particle:SetEndSize(size)
  188.            
  189.             particle:SetStartAlpha(255*self.Volume)
  190.             particle:SetEndAlpha(0)
  191.  
  192.             --particle:SetRollDelta(math.Rand(-1,1)*20)
  193.             particle:SetAirResistance(130)
  194.         end
  195.     end
  196.    
  197.     function META:Draw()
  198.    
  199.     end
  200.    
  201.     instrument.SamplerMeta = META
  202. end
  203.  
  204. do -- player meta
  205.     local PLAYER = FindMetaTable("Player")
  206.    
  207.     function PLAYER:GetSampler()
  208.         return self.sampler
  209.     end
  210. end
  211.  
  212. local hack = {}
  213.  
  214. function instrument.KeyEvent(ply, key, press)
  215.     --WHAT
  216.     local id = ply:UniqueID() .. key
  217.     if hack[id] == press then return end
  218.     hack[id] = press
  219.     --WHAT
  220.    
  221.     local sampler = ply:GetSampler()
  222.    
  223.     if sampler and sampler.OnKeyEvent and ply == sampler.Player then
  224.        
  225.         sampler.Keys[key] = press
  226.        
  227.         return sampler:OnKeyEvent(key, press)
  228.     end
  229. end
  230.  
  231. function instrument.Think()
  232.     for key, ply in pairs(player.GetAll()) do
  233.         local sampler = ply:GetSampler()
  234.  
  235.         if sampler and sampler.Think then
  236.             sampler:Think()
  237.         end
  238.     end
  239. end
  240.  
  241. hook.Add("Think", "instrument_think", instrument.Think)
  242.  
  243. function instrument.Draw()
  244.     for key, ply in pairs(player.GetAll()) do
  245.         local sampler = ply:GetSampler()
  246.  
  247.         if sampler and sampler.Draw then
  248.             sampler:Draw()
  249.         end
  250.     end
  251. end
  252.  
  253. hook.Add("PostDrawOpaqueRenderables", "instrument_draw", instrument.Draw)
  254.  
  255. local function send(ply, key, press)       
  256.     local rp = RecipientFilter()
  257.     rp:AddAllPlayers()
  258.     rp:RemovePlayer(ply)
  259.    
  260.     umsg.Start("instrument_keyevent", rp)
  261.         umsg.Entity(ply)
  262.         umsg.Long(key) -- or short?
  263.         umsg.Bool(press)
  264.     umsg.End()
  265. end
  266.  
  267. hook.Add("KeyPress", "instrument_keypress", function(ply, key)
  268.     if s.IsValidKey(key) then
  269.         if SERVER then
  270.             send(ply, key, true)
  271.         end
  272.        
  273.         if CLIENT then
  274.             instrument.KeyEvent(ply, key, true)
  275.         end
  276.     end
  277. end)
  278.  
  279. hook.Add("KeyRelease", "instrument_keyrelease", function(ply, key) 
  280.     if s.IsValidKey(key) then
  281.         if SERVER then
  282.             send(ply, key, false)
  283.         end
  284.        
  285.         if CLIENT then
  286.             instrument.KeyEvent(ply, key, false)
  287.         end
  288.     end
  289. end)
  290.  
  291. if CLIENT then
  292.     usermessage.Hook("instrument_keyevent", function(umr)
  293.         local ply = umr:ReadEntity()
  294.         local key = umr:ReadLong()
  295.         local press = umr:ReadBool()
  296.        
  297.         if ply:IsPlayer() then
  298.             instrument.KeyEvent(ply, key, press)
  299.         end
  300.     end)       
  301. end
  302.  
  303. function instrument.New(ply)
  304.     local sampler = setmetatable({}, instrument.SamplerMeta)
  305.     ply.sampler = sampler
  306.    
  307.     sampler.Player = NULL
  308.    
  309.     sampler.Pitch = 100
  310.     sampler.Volume = 1
  311.    
  312.     sampler.Keys = {}
  313.     sampler.CSP = {}
  314.    
  315.     sampler.Samples =
  316.     {
  317.         "synth/sine_440.wav",
  318.         "synth/sine_880.wav",
  319.     }
  320.    
  321.     sampler:Initialize(ply)
  322.    
  323.     return sampler
  324. end
  325.  
  326. function instrument.IsValidKey(key)
  327.     return table.HasValue(s.ValidKeys, key)
  328. end
  329.  
  330. if CLIENT then
  331.     for key, ply in pairs(player.GetAll()) do
  332.         instrument.New(ply)
  333.     end
  334. end
Advertisement
Add Comment
Please, Sign In to add comment