Untitled conversation
- Userlua
- Assistant
if CLIENT then
-------------------------------------------------------------------
-- FirstPersonView Module
-- Предоставляет точную первую личную камеру, расположенную на голове игрока.
-------------------------------------------------------------------
-- Определение таблицы FirstPersonView
local FirstPersonView = {
-- Конфигурационная таблица с значениями по умолчанию. Эти значения могут быть переопределены через
-- консольные переменные (ConVars) для динамической настройки.
config = {
headBoneNames = {
"ValveBiped.Bip01_Head1", -- Приоритетный список названий костей головы
"ValveBiped.Bip01_Neck1",
"ValveBiped.Bip01_EyeLeft",
"ValveBiped.Bip01_EyeRight",
"ValveBiped.Bip01_Mouth"
},
cameraOffset = Vector(0, 0, 0), -- Смещение камеры по умолчанию (изменяется через ConVar)
drawLocalPlayer = true, -- Отображать модель локального игрока в первом лице
spectatorTeam = nil, -- Идентификатор команды наблюдателей (устанавливается при инициализации)
notifyOnError = true, -- Уведомлять игрока об ошибках
cameraCollision = true, -- Включить детекцию коллизий камеры
maxCameraDistance = 60, -- Максимальное расстояние камеры для предотвращения клиппинга
hullSize = 2, -- Размер для TraceHull (определяет полусзмер коробки)
},
-- Таблица для кеширования индексов костей головы для локального игрока
headBoneIndices = {},
-- Флаг для однократной инициализации
initialized = false,
-- Ограничитель частоты уведомлений об ошибках для предотвращения спама
errorRateLimiter = {
lastErrorTime = 0,
cooldown = 5, -- секунды
},
-- Таблица для хранения оригинальных масштабов костей
originalBoneScales = {},
-- Таблица для хранения идентификаторов callback для ConVar
convarCallbacks = {},
}
---------------------------------------------------
-- Утилиты
---------------------------------------------------
-- Функция для логирования сообщений с префиксом
local function Log(message, isError)
if isError then
if FirstPersonView.config.notifyOnError then chat.AddText(Color(255, 0, 0), "[FirstPersonView] " .. message) end
print("[FirstPersonView] " .. message)
else
print("[FirstPersonView] " .. message)
end
end
-- Функция для проверки и обновления ограничителя скорости уведомлений об ошибках
local function CanNotifyError()
local currentTime = CurTime()
if currentTime - FirstPersonView.errorRateLimiter.lastErrorTime >= FirstPersonView.errorRateLimiter.cooldown then
FirstPersonView.errorRateLimiter.lastErrorTime = currentTime
return true
end
return false
end
---------------------------------------------------
-- Загрузка и управление конфигурацией
---------------------------------------------------
-- Функция для загрузки и применения смещения камеры из ConVar
local function LoadCameraOffset()
local offsetX = GetConVar("fvp_camera_offset_x"):GetFloat()
local offsetY = GetConVar("fvp_camera_offset_y"):GetFloat()
local offsetZ = GetConVar("fvp_camera_offset_z"):GetFloat()
FirstPersonView.config.cameraOffset = Vector(offsetX, offsetY, offsetZ)
end
-- Функция для загрузки названий костей головы из ConVar (разделенные запятыми)
local function LoadHeadBoneNames()
local boneNamesString = GetConVar("fvp_head_bone_names"):GetString()
local boneNamesRaw = string.Split(boneNamesString, ",")
-- Обрезка пробелов и обеспечение уникальности с использованием временной таблицы
local uniqueBoneSet = {}
local uniqueBoneList = {}
for _, name in ipairs(boneNamesRaw) do
local trimmed = string.Trim(name)
if trimmed ~= "" and not uniqueBoneSet[trimmed] then
table.insert(uniqueBoneList, trimmed)
uniqueBoneSet[trimmed] = true
end
end
FirstPersonView.config.headBoneNames = uniqueBoneList
end
-- Функция для загрузки команды наблюдателей из ConVar
local function LoadSpectatorTeam()
-- Попытка безопасно получить TEAM_SPECTATOR
local spectatorTeam = GetConVar("fvp_spectator_team"):GetInt()
if spectatorTeam <= 0 then
-- Обратный план на случай некорректного значения
spectatorTeam = TEAM_SPECTATOR or 8
end
FirstPersonView.config.spectatorTeam = spectatorTeam
end
-- Функция для инициализации ConVar для динамической конфигурации
local function InitializeConVars()
-- Создание ConVar с уникальными идентификаторами для последующего удаления
FirstPersonView.convarCallbacks.fvp_camera_offset_x = CreateClientConVar("fvp_camera_offset_x", "0", true, false, "FirstPersonView: Смещение камеры X (вперед/назад)")
FirstPersonView.convarCallbacks.fvp_camera_offset_y = CreateClientConVar("fvp_camera_offset_y", "0", true, false, "FirstPersonView: Смещение камеры Y (влево/вправо)")
FirstPersonView.convarCallbacks.fvp_camera_offset_z = CreateClientConVar("fvp_camera_offset_z", "0", true, false, "FirstPersonView: Смещение камеры Z (вверх/вниз)")
FirstPersonView.convarCallbacks.fvp_head_bone_names = CreateClientConVar("fvp_head_bone_names", "ValveBiped.Bip01_Head1,ValveBiped.Bip01_Neck1,ValveBiped.Bip01_EyeLeft,ValveBiped.Bip01_EyeRight,ValveBiped.Bip01_Mouth", true, false, "FirstPersonView: Список названий костей головы, разделенных запятыми")
FirstPersonView.convarCallbacks.fvp_spectator_team = CreateClientConVar("fvp_spectator_team", tostring(FirstPersonView.config.spectatorTeam or (TEAM_SPECTATOR or 8)), true, false, "FirstPersonView: Идентификатор команды наблюдателей")
end
-- Функция для обновления конфигурации из ConVar
local function RefreshConfig()
LoadCameraOffset()
LoadHeadBoneNames()
LoadSpectatorTeam()
end
-- Функция для установки хуков на изменение ConVar
local function SetupConVarChangeHooks()
-- Функция для добавления callback и сохранения его идентификатора
local function AddConVarCallback(cvarName, callbackFunc, identifier)
local callback = function(convar, oldValue, newValue) callbackFunc() end
cvars.AddChangeCallback(cvarName, callback, identifier)
-- Сохранение идентификатора для последующего удаления
FirstPersonView.convarCallbacks[identifier] = callback
end
-- Добавление хуков на изменения ConVar
AddConVarCallback("fvp_camera_offset_x", LoadCameraOffset, "FirstPersonView_CameraOffsetX")
AddConVarCallback("fvp_camera_offset_y", LoadCameraOffset, "FirstPersonView_CameraOffsetY")
AddConVarCallback("fvp_camera_offset_z", LoadCameraOffset, "FirstPersonView_CameraOffsetZ")
AddConVarCallback("fvp_head_bone_names", LoadHeadBoneNames, "FirstPersonView_HeadBoneNames")
AddConVarCallback("fvp_spectator_team", LoadSpectatorTeam, "FirstPersonView_SpectatorTeam")
end
---------------------------------------------------
-- Кеширование костей
---------------------------------------------------
-- Функция для кеширования индексов костей головы для локального игрока
function FirstPersonView:CacheHeadBones()
local ply = LocalPlayer()
if not IsValid(ply) then
if CanNotifyError() then Log("Локальный игрок не валиден. Невозможно кешировать кости головы.", true) end
return
end
local model = ply:GetModel()
if not model or model == "" then
if CanNotifyError() then Log("Модель локального игрока не валидна. Невозможно кешировать кости головы.", true) end
return
end
-- Очистка существующих кешированных костей
table.Empty(self.headBoneIndices)
-- Проход по списку возможных названий костей головы и кеширование найденных костей
for _, boneName in ipairs(self.config.headBoneNames) do
local boneIndex = ply:LookupBone(boneName)
if boneIndex then
table.insert(self.headBoneIndices, boneIndex)
-- Логирование кеширования костей (раскомментировать для уменьшения спама в консоли)
-- Log("Кеширована кость '" .. boneName .. "' (Индекс: " .. tostring(boneIndex) .. ") для модели '" .. model .. "'.", false)
end
end
-- Если не найдены кости головы, вывод предупреждения с ограничением частоты уведомлений
if #self.headBoneIndices == 0 and CanNotifyError() then Log("Предупреждение: Не найдены валидные кости головы для модели '" .. model .. "'. Ожидаемые кости: " .. table.concat(self.config.headBoneNames, ", "), true) end
end
---------------------------------------------------
-- Позиционирование камеры
---------------------------------------------------
-- Функция для получения позиции головы локального игрока
function FirstPersonView:GetHeadPosition()
local ply = LocalPlayer()
if not IsValid(ply) then return nil end
if #self.headBoneIndices == 0 then
-- Попытка кешировать кости головы, если они ещё не кешированы
self:CacheHeadBones()
if #self.headBoneIndices == 0 then
-- Обратный план на позицию глаз, если кости головы не найдены
Log("Используется позиция глаз в качестве обратного плана для камеры.", true)
return ply:EyePos()
end
end
-- Использование позиции первой найденной валидной кости головы
local boneIndex = self.headBoneIndices[1]
local bonePos = ply:GetBonePosition(boneIndex) -- Удалили неиспользуемую переменную boneAng
if bonePos then
return bonePos
else
Log("Не удалось получить позицию для кости головы с индексом " .. tostring(boneIndex) .. ". Используется позиция глаз.", true)
return ply:EyePos()
end
end
-- Переменная для хранения текущего вида камеры
local currentView = {
origin = Vector(0, 0, 0),
angles = Angle(0, 0, 0),
fov = 0,
drawviewer = false,
}
-- Хук для вычисления и установки вида камеры
function FirstPersonView:CalcView(ply, pos, angles, fov)
if not IsValid(ply) then return end
if ply:Team() == self.config.spectatorTeam then return end
local headPos = self:GetHeadPosition()
if not headPos then return end
-- Использование реальных углов обзора игрока для выравнивания камеры с его видом
local playerAngles = ply:EyeAngles()
-- Вычисление желаемой позиции камеры на основе позиции головы и смещения камеры
local desiredCameraPos = headPos + playerAngles:Forward() * self.config.cameraOffset.x + playerAngles:Right() * self.config.cameraOffset.y + playerAngles:Up() * self.config.cameraOffset.z
-- Реализация обнаружения коллизий камеры для предотвращения клиппинга
if self.config.cameraCollision then
local trace = {}
trace.start = headPos
trace.endpos = desiredCameraPos
trace.filter = ply
trace.mask = MASK_SOLID
trace.min = Vector(-self.config.hullSize, -self.config.hullSize, -self.config.hullSize)
trace.max = Vector(self.config.hullSize, self.config.hullSize, self.config.hullSize)
local tr = util.TraceHull(trace)
if tr.Hit then
local distance = tr.HitPos:Distance(headPos)
if distance < self.config.maxCameraDistance then
desiredCameraPos = tr.HitPos
else
-- Ограничение дистанции камеры
desiredCameraPos = headPos + playerAngles:Forward() * self.config.maxCameraDistance + playerAngles:Right() * self.config.cameraOffset.y + playerAngles:Up() * self.config.cameraOffset.z
end
end
end
-- Установка позиции камеры и углов без сглаживания
currentView.origin = desiredCameraPos
currentView.angles = playerAngles
currentView.fov = fov
currentView.drawviewer = self.config.drawLocalPlayer
return currentView
end
---------------------------------------------------
-- Отображение модели локального игрока
---------------------------------------------------
-- Функция для определения, следует ли рисовать локального игрока
function FirstPersonView:ShouldDrawLocalPlayer()
local ply = LocalPlayer()
if not IsValid(ply) then return true end
-- Не отображать модель игрока, если он находится в режиме наблюдателя
if ply:Team() == self.config.spectatorTeam then return false end
return self.config.drawLocalPlayer
end
---------------------------------------------------
-- Манипуляция костями для скрытия головы
---------------------------------------------------
-- Функция для скрытия костей головы игрока
function FirstPersonView:HideHeadBones()
local ply = LocalPlayer()
if not IsValid(ply) then return end
if #self.headBoneIndices == 0 then return end
for _, boneIndex in ipairs(self.headBoneIndices) do
-- Сохранение оригинального масштаба, если он ещё не сохранён
if not self.originalBoneScales[boneIndex] then
local originalScale = ply:GetManipulateBoneScale(boneIndex) or Vector(1, 1, 1)
self.originalBoneScales[boneIndex] = originalScale
end
-- Скрытие кости путем установки её масштаба в ноль
ply:ManipulateBoneScale(boneIndex, Vector(0, 0, 0))
end
end
-- Функция для восстановления оригинальных масштабов костей игрока
function FirstPersonView:RestoreHeadBones()
local ply = LocalPlayer()
if not IsValid(ply) then return end
if #self.headBoneIndices == 0 then return end
for _, boneIndex in ipairs(self.headBoneIndices) do
if self.originalBoneScales[boneIndex] then
ply:ManipulateBoneScale(boneIndex, self.originalBoneScales[boneIndex])
self.originalBoneScales[boneIndex] = nil
else
-- Если оригинальный масштаб не сохранён, устанавливаем стандартный
ply:ManipulateBoneScale(boneIndex, Vector(1, 1, 1))
end
end
end
-- Хук для обработки предварительного рисования модели игрока
function FirstPersonView:PrePlayerDraw(ply)
if ply ~= LocalPlayer() then return end
self:HideHeadBones()
end
-- Хук для обработки пост-рисования модели игрока
function FirstPersonView:PostPlayerDraw(ply)
if ply ~= LocalPlayer() then return end
self:RestoreHeadBones()
end
---------------------------------------------------
-- Обработчики событий
---------------------------------------------------
-- Хук для обработки смены модели игрока
function FirstPersonView:OnPlayerSetModel(ply)
if ply ~= LocalPlayer() then return end
self:CacheHeadBones()
end
-- Хук для обработки событий спавна игрока
function FirstPersonView:OnPlayerSpawn(ply)
if ply ~= LocalPlayer() then return end
-- Использование безопасного таймера для задержки кеширования до полной загрузки модели
timer.Simple(0.5, function() if IsValid(ply) then self:CacheHeadBones() end end)
end
---------------------------------------------------
-- Инициализация и очистка
---------------------------------------------------
-- Функция для очистки всех хуков и кешированных данных
function FirstPersonView:RemoveHooks()
hook.Remove("CalcView", "FirstPersonView_CalcView")
hook.Remove("ShouldDrawLocalPlayer", "FirstPersonView_ShouldDrawLocalPlayer")
hook.Remove("SetModel", "FirstPersonView_PlayerSetModel")
hook.Remove("PlayerSpawn", "FirstPersonView_PlayerSpawn")
hook.Remove("PrePlayerDraw", "FirstPersonView_PrePlayerDraw")
hook.Remove("PostPlayerDraw", "FirstPersonView_PostPlayerDraw")
-- Удаление хуков на изменения ConVar
for identifier, _ in pairs(self.convarCallbacks) do
cvars.RemoveChangeCallback(identifier)
end
-- Восстановление костей головы, если они скрыты
self:RestoreHeadBones()
-- Очистка кешированных индексов костей
table.Empty(self.headBoneIndices)
self.initialized = false
Log("Все хуки удалены и кеш очищен.", false)
end
-- Функция инициализации модуля
function FirstPersonView:Initialize()
if self.initialized then
Log("Инициализация пропущена: уже инициализировано.", false)
return
end
self.initialized = true
InitializeConVars()
RefreshConfig()
SetupConVarChangeHooks()
-- Кеширование костей головы для локального игрока
self:CacheHeadBones()
-- Регистрация хуков
hook.Add("CalcView", "FirstPersonView_CalcView", function(ply, pos, angles, fov) return FirstPersonView:CalcView(ply, pos, angles, fov) end)
hook.Add("ShouldDrawLocalPlayer", "FirstPersonView_ShouldDrawLocalPlayer", function() return FirstPersonView:ShouldDrawLocalPlayer() end)
hook.Add("SetModel", "FirstPersonView_PlayerSetModel", function(ply) FirstPersonView:OnPlayerSetModel(ply) end)
hook.Add("PlayerSpawn", "FirstPersonView_PlayerSpawn", function(ply) FirstPersonView:OnPlayerSpawn(ply) end)
hook.Add("PrePlayerDraw", "FirstPersonView_PrePlayerDraw", function(ply) FirstPersonView:PrePlayerDraw(ply) end)
hook.Add("PostPlayerDraw", "FirstPersonView_PostPlayerDraw", function(ply) FirstPersonView:PostPlayerDraw(ply) end)
hook.Add("ShutDown", "FirstPersonView_ShutDown", function() FirstPersonView:RemoveHooks() end)
Log("FirstPersonView успешно инициализирован.", false)
end
---------------------------------------------------
-- Запуск инициализации, если модуль ещё не инициализирован
---------------------------------------------------
if not FirstPersonView.initialized then
-- Задержка инициализации до полной загрузки сущностей
hook.Add("InitPostEntity", "FirstPersonView_Initialize", function() FirstPersonView:Initialize() end)
end
-------------------------------------------------------------------
-- Конец модуля FirstPersonView
-------------------------------------------------------------------
end
-- Файл: lua/weapons/custom_weapon.lua
SWEP.PrintName = "Кастомное Оружие"
SWEP.Author = "ВашеИмя"
SWEP.Instructions = "Левый клик для стрельбы."
SWEP.Spawnable = true
SWEP.AdminOnly = false
-- Настройка боеприпасов
SWEP.Primary.ClipSize = 7 -- Размер магазина
SWEP.Primary.DefaultClip = 8 -- 7 в магазине + 1 в патронник
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "Pistol" -- Тип боеприпасов
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"
-- Отключаем стандартные руки и ViewModel
SWEP.UseHands = false
SWEP.ViewModel = "" -- Оставьте пустым для отключения
SWEP.ViewModelFOV = 54
-- Указываем модель для мира (что видят другие игроки)
SWEP.WorldModel = "models/deadfall/weapons/fb wz,35 vis.mdl" -- Укажите правильный путь к модели
-- Установка типа хватки
function SWEP:Initialize()
self:SetHoldType("pistol") -- Устанавливаем начальный тип хватки
if CLIENT then
self.ShowAmmoStartTime = 0 -- Время начала отображения текста
self.ShowAmmoDuration = 3 -- Время, в течение которого текст полностью видим (без учета фейдов)
self.FadeTime = 1 -- Время фейда (плавного появления и исчезновения)
self.CurrentHoldType = "pistol" -- Переменная для отслеживания текущего типа хватки
end
end
-- Создание клиентской модели оружия
if CLIENT then
function SWEP:CreateWeaponModel()
if IsValid(self.WeaponModel) then return end
self.WeaponModel = ClientsideModel(self.WorldModel)
if IsValid(self.WeaponModel) then
self.WeaponModel:SetNoDraw(true)
else
print("Ошибка: Не удалось создать клиентскую модель оружия по пути " .. self.WorldModel)
end
end
-- Обновление позиции и углов модели оружия
function SWEP:UpdateWeaponModel()
if not IsValid(self.WeaponModel) or not IsValid(self:GetOwner()) then return end
local owner = self:GetOwner()
local boneIndex = owner:LookupBone("ValveBiped.Bip01_R_Hand") -- Кость правой руки
if not boneIndex then return end
local boneMatrix = owner:GetBoneMatrix(boneIndex)
if not boneMatrix then return end
local pos, ang = boneMatrix:GetTranslation(), boneMatrix:GetAngles()
-- Настройка углов поворота для правильного отображения модели
ang:RotateAroundAxis(ang:Forward(), 10)
ang:RotateAroundAxis(ang:Right(), 180)
ang:RotateAroundAxis(ang:Up(), 180)
-- Настройка позиции относительно кости руки
pos = pos + ang:Forward() * -1.5 + ang:Right() * -2 + ang:Up() * 2.3
self.WeaponModel:SetRenderOrigin(pos)
self.WeaponModel:SetRenderAngles(ang)
end
-- Функция Think для отслеживания нажатия клавиши R (перезарядки) и изменения типа хватки
function SWEP:Think()
-- Существующая логика отображения патронов при перезарядке
if self.ShowAmmoStartTime == nil then self.ShowAmmoStartTime = 0 end
if self:GetOwner():KeyPressed(IN_RELOAD) then
self.ShowAmmoStartTime = CurTime() -- Начало отображения текста
end
-- Новая логика для изменения типа хватки при нажатии правой кнопки мыши
local currentOwner = self:GetOwner() -- Renamed from 'owner' to 'currentOwner'
if IsValid(currentOwner) then
if currentOwner:KeyDown(IN_ATTACK2) then -- Проверяем, удерживается ли правая кнопка мыши
if self.CurrentHoldType ~= "revolver" then
self:SetHoldType("revolver") -- Меняем тип хватки на "revolver"
self.CurrentHoldType = "revolver" -- Обновляем текущий тип хватки
end
else
if self.CurrentHoldType ~= "pistol" then
self:SetHoldType("pistol") -- Возвращаем тип хватки на "pistol"
self.CurrentHoldType = "pistol" -- Обновляем текущий тип хватки
end
end
end
-- Существующая логика функции Think
local currentTime = CurTime()
local elapsedTime = currentTime - self.ShowAmmoStartTime
local totalDisplayTime = self.ShowAmmoDuration + 2 * self.FadeTime -- Общее время отображения текста
if elapsedTime < 0 or elapsedTime > totalDisplayTime then
return -- Не отображаем текст
end
-- Вычисляем альфа-канал для плавного появления и исчезновения
local alpha = 255
if elapsedTime < self.FadeTime then
-- Фаза плавного появления
alpha = Lerp(elapsedTime / self.FadeTime, 0, 255)
elseif elapsedTime > (self.ShowAmmoDuration + self.FadeTime) then
-- Фаза плавного исчезновения
local fadeOutTime = elapsedTime - (self.ShowAmmoDuration + self.FadeTime)
alpha = Lerp(fadeOutTime / self.FadeTime, 255, 0)
else
alpha = 255 -- Полностью видимый текст
end
if not IsValid(self:GetOwner()) then return end
local owner = self:GetOwner()
local boneIndex = owner:LookupBone("ValveBiped.Bip01_R_Hand")
if not boneIndex then return end
local bonePos, boneAng = owner:GetBonePosition(boneIndex)
-- Настройка позиции текста относительно руки
local offset = Vector(0, 10, -5)
local textPos = bonePos + boneAng:Right() * offset.x + boneAng:Forward() * offset.y + boneAng:Up() * offset.z
-- Настройка угла поворота текста
local textAng = boneAng
textAng:RotateAroundAxis(boneAng:Right(), 90)
textAng:RotateAroundAxis(boneAng:Up(), 90)
textAng:RotateAroundAxis(boneAng:Forward(), 0)
cam.Start3D2D(textPos, textAng, 0.1)
draw.SimpleText("Патроны: " .. self:Clip1() .. "/" .. currentOwner:GetAmmoCount(self.Primary.Ammo), "DermaLarge", 0, 0, Color(255, 255, 255, alpha), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
cam.End3D2D()
end
-- Отрисовка модели оружия в мире и 3D текста с количеством патронов
function SWEP:DrawWorldModel()
-- Сначала отрисовываем модель оружия
if not IsValid(self.WeaponModel) then
self:CreateWeaponModel()
if not IsValid(self.WeaponModel) then return end
end
if IsValid(self:GetOwner()) then
self:UpdateWeaponModel()
self.WeaponModel:DrawModel()
else
self:DrawModel()
end
-- Затем отрисовываем 3D текст с количеством патронов, если время отображения не истекло
local currentTime = CurTime()
local elapsedTime = currentTime - self.ShowAmmoStartTime
local totalDisplayTime = self.ShowAmmoDuration + 2 * self.FadeTime -- Общее время отображения текста
if elapsedTime < 0 or elapsedTime > totalDisplayTime then
return -- Не отображаем текст
end
-- Вычисляем альфа-канал для плавного появления и исчезновения
local alpha = 255
if elapsedTime < self.FadeTime then
-- Фаза плавного появления
alpha = Lerp(elapsedTime / self.FadeTime, 0, 255)
elseif elapsedTime > (self.ShowAmmoDuration + self.FadeTime) then
-- Фаза плавного исчезновения
local fadeOutTime = elapsedTime - (self.ShowAmmoDuration + self.FadeTime)
alpha = Lerp(fadeOutTime / self.FadeTime, 255, 0)
else
alpha = 255 -- Полностью видимый текст
end
local currentOwner = self:GetOwner() -- Renamed from 'owner' to 'currentOwner'
if not IsValid(currentOwner) then return end
local boneIndex = currentOwner:LookupBone("ValveBiped.Bip01_R_Hand")
if not boneIndex then return end
local bonePos, boneAng = currentOwner:GetBonePosition(boneIndex)
-- Настройка позиции текста относительно руки
local offset = Vector(0, 10, -5)
local textPos = bonePos + boneAng:Right() * offset.x + boneAng:Forward() * offset.y + boneAng:Up() * offset.z
-- Настройка угла поворота текста
local textAng = boneAng
textAng:RotateAroundAxis(boneAng:Right(), 90)
textAng:RotateAroundAxis(boneAng:Up(), 90)
textAng:RotateAroundAxis(boneAng:Forward(), 0)
cam.Start3D2D(textPos, textAng, 0.1)
draw.SimpleText("Патроны: " .. self:Clip1() .. "/" .. currentOwner:GetAmmoCount(self.Primary.Ammo), "DermaLarge", 0, 0, Color(255, 255, 255, alpha), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
cam.End3D2D()
end
-- Удаление клиентской модели оружия
function SWEP:OnRemove()
if IsValid(self.WeaponModel) then
self.WeaponModel:Remove()
self.WeaponModel = nil
end
end
-- Очистка модели при холстере
function SWEP:Holster()
if IsValid(self.WeaponModel) then
self.WeaponModel:Remove()
self.WeaponModel = nil
end
return true
end
end
-- Деплой оружия
function SWEP:Deploy()
if CLIENT then self:CreateWeaponModel() end
self:SendWeaponAnim(ACT_VM_DRAW)
return true
end
-- Основная логика при первичной атаке (стрельба)
function SWEP:PrimaryAttack()
if not self:CanPrimaryAttack() then return end
self:SetNextPrimaryFire(CurTime() + self:GetPrimaryFireDelay())
local owner = self:GetOwner()
if not IsValid(owner) then return end
-- Воспроизвести звуковой эффект
self:EmitSound("Weapon_Pistol.Single")
-- Воспроизвести анимацию выстрела
self:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
owner:SetAnimation(PLAYER_ATTACK1) -- Анимация игрока при выстреле
-- Уменьшаем количество патронов в магазине
self:TakePrimaryAmmo(1)
-- Создать эффект выстрела (Muzzle Flash)
if CLIENT then
local vm = owner:GetViewModel()
if IsValid(vm) then
local attachmentID = vm:LookupAttachment("1") -- Обычно "1" это muzzle
local attachment = vm:GetAttachment(attachmentID)
if attachment then
local effect = EffectData()
effect:SetOrigin(attachment.Pos)
effect:SetAngles(attachment.Ang)
effect:SetScale(1)
util.Effect("MuzzleFlash", effect)
end
end
end
-- Логика выстрела пули со всеми эффектами
if SERVER then
local bullet = {}
bullet.Num = 1
bullet.Src = owner:GetShootPos() -- Позиция вылета пули
bullet.Dir = owner:GetAimVector() -- Направление вылета пули
bullet.Spread = Vector(0, 0, 0) -- Разброс (измените при необходимости)
bullet.Tracer = 1 -- Частота появления трассеров (1 = каждый выстрел)
bullet.TracerName = "Tracer" -- Эффект трассера ("Tracer" - стандартный)
bullet.Force = 5 -- Сила попадания
bullet.Damage = 10 -- Урон
bullet.AmmoType = self.Primary.Ammo
owner:FireBullets(bullet)
end
end
-- Задержка между выстрелами
function SWEP:GetPrimaryFireDelay()
return 0.5 -- Задержка между выстрелами в секундах
end
-- Проверка возможности атаки
function SWEP:CanPrimaryAttack()
if self:Clip1() <= 0 then
self:EmitSound("Weapon_Pistol.Empty") -- Звук пустого магазина
self:SetNextPrimaryFire(CurTime() + 0.2) -- Небольшая задержка перед следующим действием
return false
end
return true
end
-- Действие при вторичной атаке
function SWEP:SecondaryAttack()
-- Если вторичная атака не используется, оставьте пустым
-- Hold type изменяется динамически в функции Think()
end
-- Функция перезарядки оружия
function SWEP:Reload()
if self:GetOwner():KeyDown(IN_ATTACK) then -- Не перезаряжаться при атаке
return
end
if self:Clip1() >= self.Primary.ClipSize then -- Магазин полон
return
end
if self:GetOwner():GetAmmoCount(self.Primary.Ammo) <= 0 then -- Нет боеприпасов
return
end
self:DefaultReload(ACT_VM_RELOAD)
self:EmitSound("Weapon_Pistol.Reload") -- Звук перезарядки
self:SetNextPrimaryFire(CurTime() + 2) -- Устанавливаем задержку на время перезарядки
self:GetOwner():SetAnimation(PLAYER_RELOAD) -- Анимация перезарядки для игрока
end
Сделай framework, который совмещает эти файлы в одном аддоне и делает удобную модульность, ООП и SOLID.
To create a robust, modular, and maintainable framework for your Garry's Mod addon that combines the FirstPersonView module and the custom_weapon script, we'll structure the addon using Object-Oriented Programming (OOP) principles and the SOLID design principles. This approach enhances scalability, readability, and ease of maintenance.
Below is a comprehensive guide to building such a framework, including the directory structure, modularization, and example implementations.
Organizing your addon with a clear directory structure is crucial for maintainability and scalability. Here's a recommended structure:
garrysmod/
└── addons/
└── my_modular_addon/
├── lua/
│ ├── autorun/
│ │ └── my_modular_addon_init.lua
│ ├── framework/
│ │ ├── class.lua
│ │ └── module_loader.lua
│ ├── fpv/
│ │ ├── init.lua
│ │ └── cl_fpv.lua
│ └── weapons/
│ └── custom_weapon.lua
├── resource/
└── materials/
autorun/: Contains scripts that Garry's Mod automatically runs. my_modular_addon_init.lua initializes the framework and loads modules.
framework/: Houses the core framework files, including a simple class system and a module loader.
fpv/: Contains the FirstPersonView module scripts.
weapons/: Stores custom weapon scripts.
framework/class.lua)Implement a simple class system to facilitate OOP in Lua. This will allow you to define classes with inheritance, encapsulation, and other OOP features.
-- File: lua/framework/class.lua
local Class = {}
Class.__index = Class
-- Base Class
function Class:New(o)
o = o or {}
setmetatable(o, self)
self.__index = self
if self.Initialize then
self:Initialize(o)
end
return o
end
return Class
framework/module_loader.lua)Create a module loader to dynamically load and manage modules, ensuring adherence to the Open/Closed Principle.
-- File: lua/framework/module_loader.lua
local Class = include("class.lua")
local ModuleLoader = Class:New()
function ModuleLoader:Initialize()
self.modules = {}
end
function ModuleLoader:RegisterModule(name, module)
self.modules[name] = module
if module.Initialize then
module:Initialize()
end
end
function ModuleLoader:GetModule(name)
return self.modules[name]
end
function ModuleLoader:RemoveModule(name)
if self.modules[name] and self.modules[name].Remove then
self.modules[name]:Remove()
end
self.modules[name] = nil
end
return ModuleLoader
autorun/my_modular_addon_init.lua)This script initializes the framework and loads all modules.
-- File: lua/autorun/my_modular_addon_init.lua
if SERVER then
AddCSLuaFile("framework/class.lua")
AddCSLuaFile("framework/module_loader.lua")
AddCSLuaFile("fpv/cl_fpv.lua")
AddCSLuaFile("fpv/init.lua")
AddCSLuaFile("weapons/custom_weapon.lua")
end
-- Include the class system and module loader
local Class = include("framework/class.lua")
local ModuleLoader = include("framework/module_loader.lua")
-- Instantiate the module loader
MODULE_LOADER = ModuleLoader:New()
-- Function to load all modules
local function LoadModules()
-- Load FirstPersonView module
local fpv = include("fpv/init.lua")
if fpv then
MODULE_LOADER:RegisterModule("FirstPersonView", fpv)
end
-- Load custom weapons
local custom_weapon = include("weapons/custom_weapon.lua")
if custom_weapon then
MODULE_LOADER:RegisterModule("CustomWeapon", custom_weapon)
end
end
-- Hook into InitPostEntity to load modules after all entities are initialized
hook.Add("InitPostEntity", "MyModularAddon_LoadModules", LoadModules)
-- Ensure modules are loaded on client
if CLIENT then
timer.Simple(0, LoadModules)
end
fpv/init.lua and fpv/cl_fpv.lua)fpv/init.luaThis file defines the FirstPersonView module using the class system.
-- File: lua/fpv/init.lua
local Class = include("framework/class.lua")
local FirstPersonView = Class:New()
-- Initialization function
function FirstPersonView:Initialize()
if CLIENT then
include("cl_fpv.lua")
end
end
-- Cleanup function
function FirstPersonView:Remove()
if CLIENT then
-- Implement any necessary cleanup here
if IsValid(self.HPV_Frame) then
self.HPV_Frame:Remove()
end
end
end
return FirstPersonView
fpv/cl_fpv.luaThis file contains the client-side implementation of the FirstPersonView module.
weapons/custom_weapon.lua)Refactor the custom weapon script to follow OOP and SOLID principles. We'll define a base weapon class and extend it.
framework/base_weapon.lua (Optional for Reusability)First, create a base weapon class to encapsulate common weapon functionalities. This promotes reusability and adherence to the DRY (Don't Repeat Yourself) principle.
-- File: lua/framework/base_weapon.lua
local Class = include("class.lua")
local BaseWeapon = Class:New()
function BaseWeapon:Initialize()
self.Primary = self.Primary or {}
self.Secondary = self.Secondary or {}
-- Set default values if not provided
self.Primary.ClipSize = self.Primary.ClipSize or -1
self.Primary.DefaultClip = self.Primary.DefaultClip or -1
self.Primary.Automatic = self.Primary.Automatic or false
self.Primary.Ammo = self.Primary.Ammo or "none"
self.Secondary.ClipSize = self.Secondary.ClipSize or -1
self.Secondary.DefaultClip = self.Secondary.DefaultClip or -1
self.Secondary.Automatic = self.Secondary.Automatic or false
self.Secondary.Ammo = self.Secondary.Ammo or "none"
-- Weapon setup
self:SetHoldType(self.HoldType or "pistol")
end
-- Define other common weapon methods here
return BaseWeapon
weapons/custom_weapon.luaNow, implement the CustomWeapon by extending the BaseWeapon.
Inheritance and Encapsulation: CustomWeapon extends BaseWeapon, inheriting common functionalities. This promotes code reuse and adheres to the Single Responsibility Principle.
Modularity: The weapon's functionalities are organized into methods, making the code modular and easier to manage.
SOLID Principles:
CustomWeapon) can replace base classes (BaseWeapon) without altering the correctness.CustomWeapon) depend on abstractions (BaseWeapon).Ensure that the ConVars used by the FirstPersonView module are properly registered and documented. You can create a shared configuration file or handle them within the module itself, as shown in the FirstPersonView implementation.
Model Paths: Ensure that the model paths specified in WorldModel are correct and that the models are included in the addon.
Server-Side Resources: If your weapon uses custom models or sounds, make sure to include them and add them to the server resources using resource.AddFile.
Testing: Thoroughly test the addon in both single-player and multiplayer environments to ensure all modules interact seamlessly.
Documentation: Document your framework and modules to aid future development and maintenance.
By implementing a clear directory structure, utilizing a class-based system, and adhering to SOLID principles, you've created a modular and maintainable framework for your Garry's Mod addon. This setup not only organizes your existing FirstPersonView and custom_weapon scripts but also provides a scalable foundation for adding more features in the future.
Feel free to expand upon the base classes, add more modules, and enhance the framework as your addon grows!
Not yet included in a published release.
-- File: lua/fpv/cl_fpv.lua
if not CLIENT then return end
local MODULE = MODULE_LOADER:GetModule("FirstPersonView")
-------------------------------------------------------------------
-- FirstPersonView Module
-- Provides an accurate first-person camera positioned on the player's head.
-------------------------------------------------------------------
-- Configuration
MODULE.config = {
headBoneNames = {
"ValveBiped.Bip01_Head1", -- Priority list of head bone names
"ValveBiped.Bip01_Neck1",
"ValveBiped.Bip01_EyeLeft",
"ValveBiped.Bip01_EyeRight",
"ValveBiped.Bip01_Mouth"
},
cameraOffset = Vector(0, 0, 0), -- Default camera offset
drawLocalPlayer = true, -- Draw local player's model in first person
spectatorTeam = nil, -- Observer team ID (set during initialization)
notifyOnError = true, -- Notify player on errors
cameraCollision = true, -- Enable camera collision detection
maxCameraDistance = 60, -- Max camera distance to prevent clipping
hullSize = 2, -- Hull size for TraceHull
}
-- Cache for head bone indices
MODULE.headBoneIndices = {}
-- Initialization flag
MODULE.initialized = false
-- Error rate limiter
MODULE.errorRateLimiter = {
lastErrorTime = 0,
cooldown = 5, -- seconds
}
-- Original bone scales
MODULE.originalBoneScales = {}
-- ConVar change callbacks
MODULE.convarCallbacks = {}
-- Utility Functions
local function Log(message, isError)
if isError then
if MODULE.config.notifyOnError then chat.AddText(Color(255, 0, 0), "[FirstPersonView] " .. message) end
print("[FirstPersonView] " .. message)
else
print("[FirstPersonView] " .. message)
end
end
local function CanNotifyError()
local currentTime = CurTime()
if currentTime - MODULE.errorRateLimiter.lastErrorTime >= MODULE.errorRateLimiter.cooldown then
MODULE.errorRateLimiter.lastErrorTime = currentTime
return true
end
return false
end
-- Load Configuration from ConVars
local function LoadCameraOffset()
local offsetX = GetConVar("fvp_camera_offset_x"):GetFloat()
local offsetY = GetConVar("fvp_camera_offset_y"):GetFloat()
local offsetZ = GetConVar("fvp_camera_offset_z"):GetFloat()
MODULE.config.cameraOffset = Vector(offsetX, offsetY, offsetZ)
end
local function LoadHeadBoneNames()
local boneNamesString = GetConVar("fvp_head_bone_names"):GetString()
local boneNamesRaw = string.Split(boneNamesString, ",")
-- Trim spaces and ensure uniqueness
local uniqueBoneSet = {}
local uniqueBoneList = {}
for _, name in ipairs(boneNamesRaw) do
local trimmed = string.Trim(name)
if trimmed ~= "" and not uniqueBoneSet[trimmed] then
table.insert(uniqueBoneList, trimmed)
uniqueBoneSet[trimmed] = true
end
end
MODULE.config.headBoneNames = uniqueBoneList
end
local function LoadSpectatorTeam()
local spectatorTeam = GetConVar("fvp_spectator_team"):GetInt()
if spectatorTeam <= 0 then
spectatorTeam = TEAM_SPECTATOR or 8
end
MODULE.config.spectatorTeam = spectatorTeam
end
local function InitializeConVars()
MODULE.convarCallbacks.fvp_camera_offset_x = CreateClientConVar("fvp_camera_offset_x", "0", true, false, "FirstPersonView: Camera Offset X (Forward/Backward)")
MODULE.convarCallbacks.fvp_camera_offset_y = CreateClientConVar("fvp_camera_offset_y", "0", true, false, "FirstPersonView: Camera Offset Y (Left/Right)")
MODULE.convarCallbacks.fvp_camera_offset_z = CreateClientConVar("fvp_camera_offset_z", "0", true, false, "FirstPersonView: Camera Offset Z (Up/Down)")
MODULE.convarCallbacks.fvp_head_bone_names = CreateClientConVar("fvp_head_bone_names", "ValveBiped.Bip01_Head1,ValveBiped.Bip01_Neck1,ValveBiped.Bip01_EyeLeft,ValveBiped.Bip01_EyeRight,ValveBiped.Bip01_Mouth", true, false, "FirstPersonView: Comma-separated list of head bone names")
MODULE.convarCallbacks.fvp_spectator_team = CreateClientConVar("fvp_spectator_team", tostring(MODULE.config.spectatorTeam or (TEAM_SPECTATOR or 8)), true, false, "FirstPersonView: Observer Team ID")
end
local function RefreshConfig()
LoadCameraOffset()
LoadHeadBoneNames()
LoadSpectatorTeam()
end
local function AddConVarCallback(cvarName, callbackFunc, identifier)
local callback = function(convar, oldValue, newValue) callbackFunc() end
cvars.AddChangeCallback(cvarName, callback, identifier)
MODULE.convarCallbacks[identifier] = callback
end
local function SetupConVarChangeHooks()
AddConVarCallback("fvp_camera_offset_x", LoadCameraOffset, "FirstPersonView_CameraOffsetX")
AddConVarCallback("fvp_camera_offset_y", LoadCameraOffset, "FirstPersonView_CameraOffsetY")
AddConVarCallback("fvp_camera_offset_z", LoadCameraOffset, "FirstPersonView_CameraOffsetZ")
AddConVarCallback("fvp_head_bone_names", LoadHeadBoneNames, "FirstPersonView_HeadBoneNames")
AddConVarCallback("fvp_spectator_team", LoadSpectatorTeam, "FirstPersonView_SpectatorTeam")
end
-- Cache Head Bones
function MODULE:CacheHeadBones()
local ply = LocalPlayer()
if not IsValid(ply) then
if CanNotifyError() then Log("Local player is invalid. Cannot cache head bones.", true) end
return
end
local model = ply:GetModel()
if not model or model == "" then
if CanNotifyError() then Log("Local player's model is invalid. Cannot cache head bones.", true) end
return
end
-- Clear existing cached bones
table.Empty(self.headBoneIndices)
-- Iterate through bone names and cache indices
for _, boneName in ipairs(self.config.headBoneNames) do
local boneIndex = ply:LookupBone(boneName)
if boneIndex then
table.insert(self.headBoneIndices, boneIndex)
end
end
-- Warn if no head bones are found
if #self.headBoneIndices == 0 and CanNotifyError() then
Log("Warning: No valid head bones found for model '" .. model .. "'. Expected bones: " .. table.concat(self.config.headBoneNames, ", "), true)
end
end
-- Get Head Position
function MODULE:GetHeadPosition()
local ply = LocalPlayer()
if not IsValid(ply) then return nil end
if #self.headBoneIndices == 0 then
self:CacheHeadBones()
if #self.headBoneIndices == 0 then
Log("Using eye position as fallback for camera.", true)
return ply:EyePos()
end
end
local boneIndex = self.headBoneIndices[1]
local bonePos = ply:GetBonePosition(boneIndex)
if bonePos then
return bonePos
else
Log("Failed to get position for head bone index " .. tostring(boneIndex) .. ". Using eye position.", true)
return ply:EyePos()
end
end
-- Calculate View
function MODULE:CalcView(ply, pos, angles, fov)
if not IsValid(ply) then return end
if ply:Team() == self.config.spectatorTeam then return end
local headPos = self:GetHeadPosition()
if not headPos then return end
local playerAngles = ply:EyeAngles()
local desiredCameraPos = headPos + playerAngles:Forward() * self.config.cameraOffset.x + playerAngles:Right() * self.config.cameraOffset.y + playerAngles:Up() * self.config.cameraOffset.z
-- Collision Detection
if self.config.cameraCollision then
local trace = {}
trace.start = headPos
trace.endpos = desiredCameraPos
trace.filter = ply
trace.mask = MASK_SOLID
trace.min = Vector(-self.config.hullSize, -self.config.hullSize, -self.config.hullSize)
trace.max = Vector(self.config.hullSize, self.config.hullSize, self.config.hullSize)
local tr = util.TraceHull(trace)
if tr.Hit then
local distance = tr.HitPos:Distance(headPos)
if distance < self.config.maxCameraDistance then
desiredCameraPos = tr.HitPos
else
desiredCameraPos = headPos + playerAngles:Forward() * self.config.maxCameraDistance + playerAngles:Right() * self.config.cameraOffset.y + playerAngles:Up() * self.config.cameraOffset.z
end
end
end
-- Set the view
local view = {}
view.origin = desiredCameraPos
view.angles = playerAngles
view.fov = fov
view.drawviewer = self.config.drawLocalPlayer
return view
end
-- Should Draw Local Player
function MODULE:ShouldDrawLocalPlayer()
local ply = LocalPlayer()
if not IsValid(ply) then return true end
if ply:Team() == self.config.spectatorTeam then return false end
return self.config.drawLocalPlayer
end
-- Hide Head Bones
function MODULE:HideHeadBones()
local ply = LocalPlayer()
if not IsValid(ply) then return end
if #self.headBoneIndices == 0 then return end
for _, boneIndex in ipairs(self.headBoneIndices) do
if not self.originalBoneScales[boneIndex] then
local originalScale = ply:GetManipulateBoneScale(boneIndex) or Vector(1, 1, 1)
self.originalBoneScales[boneIndex] = originalScale
end
ply:ManipulateBoneScale(boneIndex, Vector(0, 0, 0))
end
end
-- Restore Head Bones
function MODULE:RestoreHeadBones()
local ply = LocalPlayer()
if not IsValid(ply) then return end
if #self.headBoneIndices == 0 then return end
for _, boneIndex in ipairs(self.headBoneIndices) do
if self.originalBoneScales[boneIndex] then
ply:ManipulateBoneScale(boneIndex, self.originalBoneScales[boneIndex])
self.originalBoneScales[boneIndex] = nil
else
ply:ManipulateBoneScale(boneIndex, Vector(1, 1, 1))
end
end
end
-- Pre Player Draw Hook
function MODULE:PrePlayerDraw(ply)
if ply ~= LocalPlayer() then return end
self:HideHeadBones()
end
-- Post Player Draw Hook
function MODULE:PostPlayerDraw(ply)
if ply ~= LocalPlayer() then return end
self:RestoreHeadBones()
end
-- On Player Set Model
function MODULE:OnPlayerSetModel(ply)
if ply ~= LocalPlayer() then return end
self:CacheHeadBones()
end
-- On Player Spawn
function MODULE:OnPlayerSpawn(ply)
if ply ~= LocalPlayer() then return end
timer.Simple(0.5, function()
if IsValid(ply) then
self:CacheHeadBones()
end
end)
end
-- Remove Hooks
function MODULE:Remove()
hook.Remove("CalcView", "FirstPersonView_CalcView")
hook.Remove("ShouldDrawLocalPlayer", "FirstPersonView_ShouldDrawLocalPlayer")
hook.Remove("SetModel", "FirstPersonView_PlayerSetModel")
hook.Remove("PlayerSpawn", "FirstPersonView_PlayerSpawn")
hook.Remove("PrePlayerDraw", "FirstPersonView_PrePlayerDraw")
hook.Remove("PostPlayerDraw", "FirstPersonView_PostPlayerDraw")
for identifier, _ in pairs(self.convarCallbacks) do
cvars.RemoveChangeCallback(identifier)
end
self:RestoreHeadBones()
table.Empty(self.headBoneIndices)
self.initialized = false
Log("All hooks removed and caches cleared.", false)
end
-- Calculate View Hook
function MODULE:AttachHooks()
hook.Add("CalcView", "FirstPersonView_CalcView", function(ply, pos, angles, fov)
return MODULE:CalcView(ply, pos, angles, fov)
end)
hook.Add("ShouldDrawLocalPlayer", "FirstPersonView_ShouldDrawLocalPlayer", function()
return MODULE:ShouldDrawLocalPlayer()
end)
hook.Add("SetModel", "FirstPersonView_PlayerSetModel", function(ply)
MODULE:OnPlayerSetModel(ply)
end)
hook.Add("PlayerSpawn", "FirstPersonView_PlayerSpawn", function(ply)
MODULE:OnPlayerSpawn(ply)
end)
hook.Add("PrePlayerDraw", "FirstPersonView_PrePlayerDraw", function(ply)
MODULE:PrePlayerDraw(ply)
end)
hook.Add("PostPlayerDraw", "FirstPersonView_PostPlayerDraw", function(ply)
MODULE:PostPlayerDraw(ply)
end)
hook.Add("ShutDown", "FirstPersonView_ShutDown", function()
MODULE:Remove()
end)
end
-- Initialization function called by ModuleLoader
function MODULE:Initialize()
if self.initialized then
Log("Initialization skipped: already initialized.", false)
return
end
self.initialized = true
InitializeConVars()
RefreshConfig()
SetupConVarChangeHooks()
self:CacheHeadBones()
self:AttachHooks()
Log("FirstPersonView successfully initialized.", false)
end
-- File: lua/weapons/custom_weapon.lua
DEFINE_BASECLASS("base_weapon.lua")
local Class = include("framework/class.lua")
local BaseWeapon = include("framework/base_weapon.lua")
local CustomWeapon = Class:New()
CustomWeapon.Base = "base_weapon.lua"
-- Weapon Information
CustomWeapon.PrintName = "Custom Weapon"
CustomWeapon.Author = "YourName"
CustomWeapon.Instructions = "Left click to shoot."
CustomWeapon.Spawnable = true
CustomWeapon.AdminOnly = false
-- Primary Fire
CustomWeapon.Primary = {
ClipSize = 7,
DefaultClip = 8,
Automatic = false,
Ammo = "Pistol"
}
-- Secondary Fire
CustomWeapon.Secondary = {
ClipSize = -1,
DefaultClip = -1,
Automatic = false,
Ammo = "none"
}
-- ViewModel and WorldModel
CustomWeapon.UseHands = false
CustomWeapon.ViewModel = "" -- Disable view model
CustomWeapon.ViewModelFOV = 54
CustomWeapon.WorldModel = "models/deadfall/weapons/fb wz,35 vis.mdl" -- Ensure correct model path
-- Initialize the weapon
function CustomWeapon:Initialize()
BaseClass.Initialize(self)
if CLIENT then
self.ShowAmmoStartTime = 0
self.ShowAmmoDuration = 3
self.FadeTime = 1
self.CurrentHoldType = "pistol"
self:CreateWeaponModel()
end
end
-- Create Client-Side Weapon Model
if CLIENT then
function CustomWeapon:CreateWeaponModel()
if IsValid(self.WeaponModel) then return end
self.WeaponModel = ClientsideModel(self.WorldModel)
if IsValid(self.WeaponModel) then
self.WeaponModel:SetNoDraw(true)
else
print("Error: Failed to create client-side weapon model at " .. self.WorldModel)
end
end
-- Update Weapon Model Position and Angles
function CustomWeapon:UpdateWeaponModel()
if not IsValid(self.WeaponModel) or not IsValid(self:GetOwner()) then return end
local owner = self:GetOwner()
local boneIndex = owner:LookupBone("ValveBiped.Bip01_R_Hand")
if not boneIndex then return end
local boneMatrix = owner:GetBoneMatrix(boneIndex)
if not boneMatrix then return end
local pos, ang = boneMatrix:GetTranslation(), boneMatrix:GetAngles()
ang:RotateAroundAxis(ang:Forward(), 10)
ang:RotateAroundAxis(ang:Right(), 180)
ang:RotateAroundAxis(ang:Up(), 180)
pos = pos + ang:Forward() * -1.5 + ang:Right() * -2 + ang:Up() * 2.3
self.WeaponModel:SetRenderOrigin(pos)
self.WeaponModel:SetRenderAngles(ang)
end
-- Think Function
function CustomWeapon:Think()
-- Handle ammo display on reload
if self.ShowAmmoStartTime == nil then self.ShowAmmoStartTime = 0 end
if self:GetOwner():KeyPressed(IN_RELOAD) then
self.ShowAmmoStartTime = CurTime()
end
-- Handle hold type change on right click
local currentOwner = self:GetOwner()
if IsValid(currentOwner) then
if currentOwner:KeyDown(IN_ATTACK2) then
if self.CurrentHoldType ~= "revolver" then
self:SetHoldType("revolver")
self.CurrentHoldType = "revolver"
end
else
if self.CurrentHoldType ~= "pistol" then
self:SetHoldType("pistol")
self.CurrentHoldType = "pistol"
end
end
end
-- Handle ammo display timing
local currentTime = CurTime()
local elapsedTime = currentTime - self.ShowAmmoStartTime
local totalDisplayTime = self.ShowAmmoDuration + 2 * self.FadeTime
if elapsedTime < 0 or elapsedTime > totalDisplayTime then
return
end
-- Calculate alpha for fade effect
local alpha = 255
if elapsedTime < self.FadeTime then
alpha = Lerp(elapsedTime / self.FadeTime, 0, 255)
elseif elapsedTime > (self.ShowAmmoDuration + self.FadeTime) then
local fadeOutTime = elapsedTime - (self.ShowAmmoDuration + self.FadeTime)
alpha = Lerp(fadeOutTime / self.FadeTime, 255, 0)
else
alpha = 255
end
if not IsValid(self:GetOwner()) then return end
local owner = self:GetOwner()
local boneIndex = owner:LookupBone("ValveBiped.Bip01_R_Hand")
if not boneIndex then return end
local bonePos, boneAng = owner:GetBonePosition(boneIndex)
local offset = Vector(0, 10, -5)
local textPos = bonePos + boneAng:Right() * offset.x + boneAng:Forward() * offset.y + boneAng:Up() * offset.z
local textAng = boneAng
textAng:RotateAroundAxis(boneAng:Right(), 90)
textAng:RotateAroundAxis(boneAng:Up(), 90)
textAng:RotateAroundAxis(boneAng:Forward(), 0)
cam.Start3D2D(textPos, textAng, 0.1)
draw.SimpleText("Ammo: " .. self:Clip1() .. "/" .. owner:GetAmmoCount(self.Primary.Ammo), "DermaLarge", 0, 0, Color(255, 255, 255, alpha), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
cam.End3D2D()
end
-- Draw World Model and Ammo Text
function CustomWeapon:DrawWorldModel()
if not IsValid(self.WeaponModel) then
self:CreateWeaponModel()
if not IsValid(self.WeaponModel) then return end
end
if IsValid(self:GetOwner()) then
self:UpdateWeaponModel()
self.WeaponModel:DrawModel()
else
self:DrawModel()
end
-- Draw Ammo Text
local currentTime = CurTime()
local elapsedTime = currentTime - self.ShowAmmoStartTime
local totalDisplayTime = self.ShowAmmoDuration + 2 * self.FadeTime
if elapsedTime < 0 or elapsedTime > totalDisplayTime then
return
end
local alpha = 255
if elapsedTime < self.FadeTime then
alpha = Lerp(elapsedTime / self.FadeTime, 0, 255)
elseif elapsedTime > (self.ShowAmmoDuration + self.FadeTime) then
local fadeOutTime = elapsedTime - (self.ShowAmmoDuration + self.FadeTime)
alpha = Lerp(fadeOutTime / self.FadeTime, 255, 0)
else
alpha = 255
end
local currentOwner = self:GetOwner()
if not IsValid(currentOwner) then return end
local boneIndex = currentOwner:LookupBone("ValveBiped.Bip01_R_Hand")
if not boneIndex then return end
local bonePos, boneAng = currentOwner:GetBonePosition(boneIndex)
local offset = Vector(0, 10, -5)
local textPos = bonePos + boneAng:Right() * offset.x + boneAng:Forward() * offset.y + boneAng:Up() * offset.z
local textAng = boneAng
textAng:RotateAroundAxis(boneAng:Right(), 90)
textAng:RotateAroundAxis(boneAng:Up(), 90)
textAng:RotateAroundAxis(boneAng:Forward(), 0)
cam.Start3D2D(textPos, textAng, 0.1)
draw.SimpleText("Ammo: " .. self:Clip1() .. "/" .. currentOwner:GetAmmoCount(self.Primary.Ammo), "DermaLarge", 0, 0, Color(255, 255, 255, alpha), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER)
cam.End3D2D()
end
-- Remove Weapon Model
function CustomWeapon:OnRemove()
if IsValid(self.WeaponModel) then
self.WeaponModel:Remove()
self.WeaponModel = nil
end
end
-- Holster Weapon
function CustomWeapon:Holster()
if IsValid(self.WeaponModel) then
self.WeaponModel:Remove()
self.WeaponModel = nil
end
return true
end
end
-- Deploy Weapon
function CustomWeapon:Deploy()
if CLIENT then self:CreateWeaponModel() end
self:SendWeaponAnim(ACT_VM_DRAW)
return true
end
-- Primary Attack (Shooting)
function CustomWeapon:PrimaryAttack()
if not self:CanPrimaryAttack() then return end
self:SetNextPrimaryFire(CurTime() + self:GetPrimaryFireDelay())
local owner = self:GetOwner()
if not IsValid(owner) then return end
-- Play sound and animation
self:EmitSound("Weapon_Pistol.Single")
self:SendWeaponAnim(ACT_VM_PRIMARYATTACK)
owner:SetAnimation(PLAYER_ATTACK1)
-- Reduce ammo
self:TakePrimaryAmmo(1)
-- Muzzle Flash Effect
if CLIENT then
local vm = owner:GetViewModel()
if IsValid(vm) then
local attachmentID = vm:LookupAttachment("1")
local attachment = vm:GetAttachment(attachmentID)
if attachment then
local effect = EffectData()
effect:SetOrigin(attachment.Pos)
effect:SetAngles(attachment.Ang)
effect:SetScale(1)
util.Effect("MuzzleFlash", effect)
end
end
end
-- Bullet Fire (Server-Side)
if SERVER then
local bullet = {}
bullet.Num = 1
bullet.Src = owner:GetShootPos()
bullet.Dir = owner:GetAimVector()
bullet.Spread = Vector(0, 0, 0)
bullet.Tracer = 1
bullet.TracerName = "Tracer"
bullet.Force = 5
bullet.Damage = 10
bullet.AmmoType = self.Primary.Ammo
owner:FireBullets(bullet)
end
end
-- Primary Fire Delay
function CustomWeapon:GetPrimaryFireDelay()
return 0.5 -- Seconds between shots
end
-- Can Primary Attack
function CustomWeapon:CanPrimaryAttack()
if self:Clip1() <= 0 then
self:EmitSound("Weapon_Pistol.Empty")
self:SetNextPrimaryFire(CurTime() + 0.2)
return false
end
return true
end
-- Secondary Attack (Unused)
function CustomWeapon:SecondaryAttack()
-- Secondary attack functionality (if any)
-- Hold type is changed dynamically in Think()
end
-- Reload Function
function CustomWeapon:Reload()
if self:GetOwner():KeyDown(IN_ATTACK) then return end
if self:Clip1() >= self.Primary.ClipSize then return end
if self:GetOwner():GetAmmoCount(self.Primary.Ammo) <= 0 then return end
self:DefaultReload(ACT_VM_RELOAD)
self:EmitSound("Weapon_Pistol.Reload")
self:SetNextPrimaryFire(CurTime() + 2)
self:GetOwner():SetAnimation(PLAYER_RELOAD)
end
-- Register the weapon with the game
if SERVER then
resource.AddFile("models/deadfall/weapons/fb wz,35 vis.mdl") -- Ensure the model is downloaded
end
-- Define SWEP base
SWEP.Base = "base_weapon.lua" -- Assuming you have a base_weapon.lua defined