Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- AddCSLuaFile()
- wowozela = {}
- wowozela.ValidNotes =
- {
- IN_ATTACK,
- IN_ATTACK2,
- }
- wowozela.ValidKeys =
- {
- IN_ATTACK,
- IN_ATTACK2,
- IN_WALK,
- IN_SPEED,
- IN_USE,
- }
- for key, val in pairs(wowozela.ValidNotes) do
- concommand.Add("wowozela_select_" .. key, function(ply, _, args)
- local wep = ply:GetActiveWeapon()
- if wep:IsValid() and wep:GetClass() == "wowozela" then
- local key, val = tonumber(args[1]) or 1, tonumber(args[2]) or 0
- wep.dt[key] = val
- end
- end)
- end
- wowozela.Samples = {}
- for _, file_name in pairs(file.Find("sound/wowozela/samples/*.wav", "GAME")) do
- table.insert(wowozela.Samples, "wowozela/samples/" .. file_name)
- if SERVER then
- resource.AddFile("sound/wowozela/samples/" .. file_name)
- end
- end
- table.sort(wowozela.Samples, function(a,b) return a < b end)
- function wowozela.New(ply)
- local sampler = setmetatable({}, wowozela.SamplerMeta)
- ply.sampler = sampler
- sampler.Player = NULL
- sampler.Pitch = 100
- sampler.Volume = 1
- sampler.Keys = {}
- sampler.CSP = {}
- sampler:Initialize(ply)
- return sampler
- end
- function wowozela.IsValidKey(key)
- return table.HasValue(wowozela.ValidKeys, key)
- end
- function wowozela.IsValidNote(key)
- return table.HasValue(wowozela.ValidNotes, key)
- end
- do -- swep meta
- local SWEP = {Primary = {}, Secondary = {}}
- SWEP.Base = "weapon_base"
- SWEP.Author = ""
- SWEP.Contact = ""
- SWEP.Purpose = ""
- SWEP.Instructions = ""
- SWEP.PrintName = "Wowozela"
- SWEP.SlotPos = 1
- SWEP.Slot = 1
- SWEP.Spawnable = false
- SWEP.AdminSpawnable = true
- SWEP.AutoSwitchTo = true
- SWEP.AutoSwitchFrom = true
- SWEP.HoldType = "normal"
- SWEP.Primary.ClipSize = -1
- SWEP.Primary.DefaultClip = -1
- SWEP.Primary.Automatic = false
- SWEP.Primary.Ammo = "none"
- SWEP.Secondary.ClipSize = -1
- SWEP.Secondary.DefaultClip = -1
- SWEP.Secondary.Automatic = false
- SWEP.Secondary.Ammo = "none"
- SWEP.DrawAmmo = false
- SWEP.DrawCrosshair = true
- SWEP.ViewModel = "models/weapons/v_hands.mdl"
- SWEP.WorldModel = "models/weapons/w_bugbait.mdl"
- SWEP.DrawWeaponInfoBox = true
- function SWEP:SetupDataTables()
- for i, key in ipairs(wowozela.ValidNotes) do
- self:DTVar("Int", i, key)
- end
- end
- function SWEP:PrintWeaponInfo() end
- function SWEP:DrawWeaponSelection() end
- function SWEP:DrawWorldModel() return true end
- function SWEP:CanPrimaryAttack() return false end
- function SWEP:CanSecondaryAttack() return false end
- function SWEP:ShouldDropOnDie() return false end
- function SWEP:Reload() return false end
- function SWEP:Initialize()
- self:SetWeaponHoldType("normal")
- end
- if SERVER then
- function SWEP:OnDrop()
- self:Remove()
- end
- end
- function SWEP:Deploy()
- self.Think = self._Think
- return true
- end
- function SWEP:Holster()
- if not self.Owner:KeyDown(IN_RELOAD) then
- return true
- end
- return false
- end
- function SWEP:OnKeyEvent(key, press)
- if press and wowozela.IsValidNote(key) and self.Owner:KeyDown(IN_RELOAD) then
- self.dt[key] = math.Clamp((self.dt[key] + 1)%#wowozela.Samples, 1, #wowozela.Samples)
- wowozela.BroadcastKeyEvent(self.Owner, key, press, true)
- wowozela.BroadcastKeyEvent(self.Owner, key, press, false)
- end
- end
- function SWEP:_Think()
- if self.Owner and self.Owner:IsValid() and self.Owner:GetViewModel():IsValid() then
- self.Owner:GetViewModel():SetNoDraw(true)
- self.Think = nil
- end
- end
- function SWEP:GetViewModelPosition(pos, ang)
- pos.x = 35575
- pos.y = 35575
- pos.z = 35575
- return pos, ang
- end
- if CLIENT then
- local size = 80
- local count = 4
- surface.CreateFont(
- "WowozelaFont",
- {
- font = "Roboto Bk",
- size = size,
- weight = 1000,
- }
- )
- local names = {}
- function SWEP:DrawHUD()
- local in1 = self.dt and self.dt[IN_ATTACK] ~= 0 and self.dt[IN_ATTACK] or 1
- local in2 = self.dt and self.dt[IN_ATTACK2] ~= 0 and self.dt[IN_ATTACK2] or 1
- local total = #wowozela.Samples
- if self.Owner:KeyDown(IN_RELOAD) then
- for i = -count, count do
- in1 = math.max((in1+i)%total, 1)
- in2 = math.max((in2+i)%total, 1)
- if not names[in1] then
- names[in1] = wowozela.Samples[in1]:match("^wowozela/samples/(.+)%.wav")
- end
- if not names[in2] then
- names[in2] = wowozela.Samples[in2]:match("^wowozela/samples/(.+)%.wav")
- end
- --local alpha = -math.abs(i / count) + 1
- alpha = 255 --alpha * 255
- local col = HSVToColor((in1/total)*360, 1, 1)
- col.a = alpha
- draw.SimpleText(
- names[in1],
- "WowozelaFont",
- 16,
- (i + count) * size,
- col,
- TEXT_ALIGN_LEFT,
- TEXT_ALIGN_CENTER
- )
- local col = HSVToColor((in2/total)*360, 1, 1)
- col.a = alpha
- draw.SimpleText(
- names[in2],
- "WowozelaFont",
- ScrW() - 16,
- (i + count) * size,
- col,
- TEXT_ALIGN_RIGHT,
- TEXT_ALIGN_CENTER
- )
- end
- else
- if not names[in1] then
- names[in1] = wowozela.Samples[in1]:match("^wowozela/samples/(.+)%.wav")
- end
- if not names[in2] then
- names[in2] = wowozela.Samples[in2]:match("^wowozela/samples/(.+)%.wav")
- end
- draw.SimpleText(
- names[in1],
- "WowozelaFont",
- 16,
- ScrH() - ScrH()/2,
- HSVToColor((in1/total)*360, 1, 1),
- TEXT_ALIGN_LEFT,
- TEXT_ALIGN_CENTER
- )
- draw.SimpleText(
- names[in2],
- "WowozelaFont",
- ScrW() - 16,
- ScrH() - ScrH()/2,
- HSVToColor((in2/total)*360, 1, 1),
- TEXT_ALIGN_RIGHT,
- TEXT_ALIGN_CENTER
- )
- end
- end
- end
- weapons.Register(SWEP, "wowozela", true)
- end
- do -- sample meta
- local META = {}
- META.__index = META
- META.Weapon = NULL
- function META:Initialize(ply)
- self.Player = ply
- for i, path in pairs(wowozela.Samples) do
- self:SetSample(i, path)
- end
- self.IDs = {}
- end
- function META:GetSampleIndex(key)
- if wowozela.IsValidNote(key) then
- local wep = self.Player:GetActiveWeapon()
- if wep:IsWeapon() and wep:GetClass() == "wowozela" then
- return math.Clamp(wep.dt[key], 1, #wowozela.Samples)
- end
- end
- end
- function META:CanPlay()
- local wep = self.Player:GetActiveWeapon()
- if wep:IsWeapon() and wep:GetClass() == "wowozela" then
- self.Weapon = wep
- return true
- end
- return false
- end
- function META:GetPos()
- if self.Player == LocalPlayer() and not self.Player:ShouldDrawLocalPlayer() then
- return self.Player:EyePos()
- end
- return self.Player:GetBonePosition(self.Player:LookupBone("ValveBiped.Bip01_Head1"))
- end
- function META:GetAngles()
- local ang = self.Player:GetAimVector():Angle()
- ang.p = math.NormalizeAngle(ang.p)
- ang.y = math.NormalizeAngle(ang.y)
- ang.r = 0
- return ang
- 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 wowozela.DefaultSound)
- self.CSP[i]:SetSoundLevel(100)
- end
- function META:ChangeVolume(i, num)
- if self.CSP[i] then
- self.CSP[i]:ChangeVolume(self.Volume, 0)
- end
- end
- function META:ChangePitch(i, num)
- if self.CSP[i] then
- self.CSP[i]:ChangePitch(self.Pitch, 0)
- 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(wowozela.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(wowozela.Samples) do
- self:ChangeVolume(i, self.Volume)
- end
- end
- function META:Start(i, id)
- if not self:CanPlay() then return end
- if self.CSP[i] then
- if id then
- local snd = self.IDs[id]
- if snd then
- snd:Stop()
- end
- snd = self.CSP[i]
- snd:PlayEx(self.Volume, self.Pitch)
- self.IDs[id] = snd
- else
- self.CSP[i]:PlayEx(self.Volume, self.Pitch)
- end
- end
- end
- function META:Stop(i, id)
- if self.CSP[i] then
- if id then
- local snd = self.IDs[id]
- if snd then
- snd:Stop()
- end
- self.IDs[id] = self.CSP[i]
- else
- self.CSP[i]:Stop()
- end
- end
- end
- function META:IsKeyDown(key)
- return self.Keys[key] == true
- end
- function META:OnKeyEvent(key, press)
- local id = self:GetSampleIndex(key)
- if id then
- if press then
- self:Start(id, key)
- self:SetVolume(1)
- else
- self:Stop(id, key)
- end
- end
- end
- function META:Think()
- if not self:CanPlay() then
- for _, csp in pairs(self.CSP) do
- csp:Stop()
- end
- 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
- function META:MakeParticle()
- local pitch = self.Pitch
- emitter = emitter or ParticleEmitter(Vector())
- local scale = self.Player:GetModelScale()
- local forward = self:GetAngles():Forward()
- local particle = emitter:Add("particle/particle_glow_04_additive", self:GetPos() + forward * 10 * scale)
- if particle then
- local col = HSVToColor(pitch*2.55, self.Volume, 1)
- particle:SetColor(col.r, col.g, col.b, self.Volume)
- particle:SetVelocity(self.Volume * self:GetAngles():Forward() * 500 * scale)
- particle:SetDieTime(20)
- particle:SetLifeTime(0)
- local size = ((-pitch + 255) / 250) + 1
- particle:SetAngles(AngleRand())
- particle:SetStartSize(math.max(size*2*scale, 1))
- particle:SetEndSize(0)
- particle:SetStartAlpha(255*self.Volume)
- particle:SetEndAlpha(0)
- --particle:SetRollDelta(math.Rand(-1,1)*20)
- particle:SetAirResistance(500)
- --particle:SetGravity(Vector(math.Rand(-1,1), math.Rand(-1,1), math.Rand(-1, 1)) * 8 )
- end
- end
- wowozela.SamplerMeta = META
- end
- do -- player meta
- local PLAYER = FindMetaTable("Player")
- function PLAYER:GetSampler()
- return self.sampler
- end
- end
- do -- hooks
- local hack = {}
- function wowozela.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 wowozela.Think()
- for key, ply in pairs(player.GetAll()) do
- local sampler = ply:GetSampler()
- if not sampler then sampler = wowozela.New(ply) end
- if sampler and sampler.Think then
- sampler:Think()
- end
- end
- end
- hook.Add("Think", "wowozela_think", wowozela.Think)
- function wowozela.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", "wowozela_draw", wowozela.Draw)
- function wowozela.BroadcastKeyEvent(ply, key, press, filter)
- local rp = RecipientFilter()
- rp:AddAllPlayers()
- if not filter then
- rp:RemovePlayer(ply)
- end
- umsg.Start("wowozela_keyevent", rp)
- umsg.Entity(ply)
- umsg.Long(key) -- or short?
- umsg.Bool(press)
- umsg.End()
- end
- hook.Add("KeyPress", "wowozela_keypress", function(ply, key)
- local wep = ply:GetActiveWeapon()
- if wep:IsValid() and wep:GetClass() == "wowozela" and wowozela.IsValidKey(key) then
- if SERVER then
- wowozela.BroadcastKeyEvent(ply, key, true)
- wep:OnKeyEvent(key, true)
- end
- if CLIENT then
- wowozela.KeyEvent(ply, key, true)
- end
- end
- end)
- hook.Add("KeyRelease", "wowozela_keyrelease", function(ply, key)
- local wep = ply:GetActiveWeapon()
- if wep:IsValid() and wep:GetClass() == "wowozela" and wowozela.IsValidKey(key) then
- if SERVER then
- wowozela.BroadcastKeyEvent(ply, key, false)
- wep:OnKeyEvent(key, false)
- end
- if CLIENT then
- wowozela.KeyEvent(ply, key, false)
- end
- end
- end)
- if CLIENT then
- usermessage.Hook("wowozela_keyevent", function(umr)
- local ply = umr:ReadEntity()
- local key = umr:ReadLong()
- local press = umr:ReadBool()
- if ply:IsPlayer() then
- wowozela.KeyEvent(ply, key, press)
- end
- end)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment