Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local print = epoe.Print
- instrument = {} local s = instrument
- instrument.DefaultSound = "synth/12_5_pwm_440.wav"
- instrument.ValidKeys =
- {
- IN_ATTACK,
- IN_ATTACK2,
- IN_WALK,
- IN_SPEED,
- IN_USE,
- }
- -- sample meta
- do
- local META = {}
- META.__index = META
- function META:Initialize(ply)
- self.Player = ply
- for i, path in pairs(self.Samples) do
- self:SetSample(i, path)
- end
- end
- function META:CanPlay()
- local wep = self.Player:GetActiveWeapon()
- if wep:IsWeapon() and wep:GetClass() == "none" then
- return true
- end
- return false
- end
- function META:GetPos()
- --local pos = self.Player:GetBonePosition(self.Player:LookupBone("ValveBiped.Bip01_Head1"))
- return self.Player:EyePos()
- end
- function META:GetAngles()
- return self.Player:EyeAngles()
- end
- function META:IsPlaying() -- hm
- for _, on in pairs(self.Keys) do
- if on then
- return true
- end
- end
- return false
- end
- function META:SetSample(i, path)
- self.CSP[i] = CreateSound(self.Player, path or s.DefaultSound)
- self.CSP[i]:SetSoundLevel(100)
- end
- function META:ChangeVolume(i, num)
- if self.CSP[i] then
- self.CSP[i]:ChangeVolume(self.Volume)
- end
- end
- function META:ChangePitch(i, num)
- if self.CSP[i] then
- self.CSP[i]:ChangePitch(self.Pitch)
- end
- end
- function META:SetPitch(num) -- ???
- num = num or 1
- if self:IsKeyDown(IN_WALK) then
- num = num - 1
- end
- if self:IsKeyDown(IN_SPEED) then
- num = num - 2
- end
- self.Pitch = math.Clamp(math.floor(100 * 2 ^ num), 1, 255)
- for i in pairs(self.Samples) do
- self:ChangePitch(i, self.Pitch)
- end
- end
- function META:SetVolume(num)
- self.Volume = math.Clamp(num or self.Volume, 0.0001, 1)
- for i in pairs(self.Samples) do
- self:ChangeVolume(i, self.Volume)
- end
- end
- function META:Start(i)
- if not self:CanPlay() then return end
- if self.CSP[i] then
- self.CSP[i]:PlayEx(self.Volume, self.Pitch)
- end
- end
- function META:Stop(i)
- if self.CSP[i] then
- self.CSP[i]:Stop()
- end
- end
- function META:IsKeyDown(key)
- return self.Keys[key] == true
- end
- function META:OnKeyEvent(key, press)
- if key == IN_ATTACK then
- if press then
- self:Start(1)
- else
- self:Stop(1)
- end
- end
- if key == IN_ATTACK2 then
- if press then
- self:Start(2)
- else
- self:Stop(2)
- end
- end
- LocalPlayer():SetDSP(22)
- end
- function META:Think()
- if not self:CanPlay() then
- self:Stop(1)
- self:Stop(2)
- return end
- local ang = self:GetAngles()
- if self:IsKeyDown(IN_USE) then
- if self.using then
- self:SetVolume(math.abs(ang.y - self.using) / 20)
- else
- self.using = ang.y
- end
- else
- self.using = false
- self:SetVolume(1)
- end
- self:SetPitch(-ang.p/89)
- if self:IsKeyDown(IN_ATTACK) or self:IsKeyDown(IN_ATTACK2) then
- self:MakeParticle()
- end
- end
- local emitter = CLIENT and ParticleEmitter(Vector()) or nil
- function META:MakeParticle()
- local pitch = self.Pitch
- if self:IsKeyDown(IN_ATTACK2) then
- pitch = math.Clamp(pitch * 2, 0, 200)
- end
- local particle = emitter:Add("particle/particle_glow_04_additive", self:GetPos() + (self:GetAngles():Forward() * 30))
- if particle then
- local col = HSVToColor(pitch*1.4, self.Volume, 1)
- particle:SetColor(col.r, col.g, col.b, self.Volume)
- particle:SetVelocity(VectorRand() * self.Volume )
- particle:SetDieTime(40)
- particle:SetLifeTime(0)
- local size = (-pitch + 255) / 100
- particle:SetAngles(AngleRand())
- particle:SetStartSize(size)
- particle:SetEndSize(size)
- particle:SetStartAlpha(255*self.Volume)
- particle:SetEndAlpha(0)
- --particle:SetRollDelta(math.Rand(-1,1)*20)
- particle:SetAirResistance(130)
- end
- end
- function META:Draw()
- end
- instrument.SamplerMeta = META
- end
- do -- player meta
- local PLAYER = FindMetaTable("Player")
- function PLAYER:GetSampler()
- return self.sampler
- end
- end
- local hack = {}
- function instrument.KeyEvent(ply, key, press)
- --WHAT
- local id = ply:UniqueID() .. key
- if hack[id] == press then return end
- hack[id] = press
- --WHAT
- local sampler = ply:GetSampler()
- if sampler and sampler.OnKeyEvent and ply == sampler.Player then
- sampler.Keys[key] = press
- return sampler:OnKeyEvent(key, press)
- end
- end
- function instrument.Think()
- for key, ply in pairs(player.GetAll()) do
- local sampler = ply:GetSampler()
- if sampler and sampler.Think then
- sampler:Think()
- end
- end
- end
- hook.Add("Think", "instrument_think", instrument.Think)
- function instrument.Draw()
- for key, ply in pairs(player.GetAll()) do
- local sampler = ply:GetSampler()
- if sampler and sampler.Draw then
- sampler:Draw()
- end
- end
- end
- hook.Add("PostDrawOpaqueRenderables", "instrument_draw", instrument.Draw)
- local function send(ply, key, press)
- local rp = RecipientFilter()
- rp:AddAllPlayers()
- rp:RemovePlayer(ply)
- umsg.Start("instrument_keyevent", rp)
- umsg.Entity(ply)
- umsg.Long(key) -- or short?
- umsg.Bool(press)
- umsg.End()
- end
- hook.Add("KeyPress", "instrument_keypress", function(ply, key)
- if s.IsValidKey(key) then
- if SERVER then
- send(ply, key, true)
- end
- if CLIENT then
- instrument.KeyEvent(ply, key, true)
- end
- end
- end)
- hook.Add("KeyRelease", "instrument_keyrelease", function(ply, key)
- if s.IsValidKey(key) then
- if SERVER then
- send(ply, key, false)
- end
- if CLIENT then
- instrument.KeyEvent(ply, key, false)
- end
- end
- end)
- if CLIENT then
- usermessage.Hook("instrument_keyevent", function(umr)
- local ply = umr:ReadEntity()
- local key = umr:ReadLong()
- local press = umr:ReadBool()
- if ply:IsPlayer() then
- instrument.KeyEvent(ply, key, press)
- end
- end)
- end
- function instrument.New(ply)
- local sampler = setmetatable({}, instrument.SamplerMeta)
- ply.sampler = sampler
- sampler.Player = NULL
- sampler.Pitch = 100
- sampler.Volume = 1
- sampler.Keys = {}
- sampler.CSP = {}
- sampler.Samples =
- {
- "synth/sine_440.wav",
- "synth/sine_880.wav",
- }
- sampler:Initialize(ply)
- return sampler
- end
- function instrument.IsValidKey(key)
- return table.HasValue(s.ValidKeys, key)
- end
- if CLIENT then
- for key, ply in pairs(player.GetAll()) do
- instrument.New(ply)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment