Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Door System #107

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add Door System
  • Loading branch information
Snappy committed Apr 30, 2022
commit 3032d7602c59339a68e1a71f4391a123178c608d
42 changes: 42 additions & 0 deletions doorsystem/cl_hooks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
local PLUGIN = PLUGIN

local classes = {
["func_door"] = true,
["prop_door_rotating"] = true
}

local function DrawDoorAccess(ply)

for k, v in pairs(ents.FindInSphere(ply:GetPos(), 150)) do
for a, b in pairs(PLUGIN.doors) do
if ( game.GetMap() == a ) then
for stuff, stuff2 in pairs(b) do
if ( classes[v:GetClass()] ) and ( tostring(stuff2.id) == v:MapCreationID() ) then
local pos = v.LocalToWorld(v, v:OBBCenter()):ToScreen()

draw.DrawText(stuff2.name, "BudgetLabel", pos.x, pos.y, ColorAlpha(color_white, 255), TEXT_ALIGN_CENTER)

if ( stuff2.access ) then
for c, d in pairs(PLUGIN.access) do
if ( stuff2.access == c ) then
draw.DrawText(d.name, "BudgetLabel", pos.x, pos.y - 30, ColorAlpha(d["color"], 255), TEXT_ALIGN_CENTER)
end
end
end
end
end
end
end
end
end

function PLUGIN:HUDPaint()
local ply = LocalPlayer()
local char = ply:GetCharacter()

if not ( ply or char ) then return end

if not ( ply:IsCombine() ) then return end

DrawDoorAccess(ply)
end
253 changes: 253 additions & 0 deletions doorsystem/entities/weapons/ix_keys.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,253 @@
local PLUGIN = PLUGIN
AddCSLuaFile()

if (CLIENT) then
SWEP.PrintName = "Keys"
SWEP.Slot = 0
SWEP.SlotPos = 2
SWEP.DrawAmmo = false
SWEP.DrawCrosshair = false
end

SWEP.Author = "Chessnut"
SWEP.Instructions = "Primary Fire: Lock\nSecondary Fire: Unlock"
SWEP.Purpose = "Hitting things and knocking on doors."
SWEP.Drop = false

SWEP.ViewModelFOV = 45
SWEP.ViewModelFlip = false
SWEP.AnimPrefix = "rpg"

SWEP.ViewTranslation = 4

SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = ""
SWEP.Primary.Damage = 5
SWEP.Primary.Delay = 0.75

SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = 0
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = ""

SWEP.ViewModel = Model("models/weapons/c_arms_animations.mdl")
SWEP.WorldModel = ""

SWEP.UseHands = false
SWEP.LowerAngles = Angle(0, 5, -14)
SWEP.LowerAngles2 = Angle(0, 5, -22)

SWEP.IsAlwaysLowered = true
SWEP.FireWhenLowered = true
SWEP.HoldType = "passive"

-- luacheck: globals ACT_VM_FISTS_DRAW ACT_VM_FISTS_HOLSTER
ACT_VM_FISTS_DRAW = 2
ACT_VM_FISTS_HOLSTER = 1

function SWEP:Holster()
if (!IsValid(self.Owner)) then
return
end

local viewModel = self.Owner:GetViewModel()

if (IsValid(viewModel)) then
viewModel:SetPlaybackRate(1)
viewModel:ResetSequence(ACT_VM_FISTS_HOLSTER)
end

return true
end

function SWEP:Precache()
end

function SWEP:Initialize()
self:SetHoldType(self.HoldType)
end

function SWEP:PrimaryAttack()
local time = ix.config.Get("doorLockTime", 1)
local time2 = math.max(time, 1)

self:SetNextPrimaryFire(CurTime() + time2)
self:SetNextSecondaryFire(CurTime() + time2)

if (!IsFirstTimePredicted()) then
return
end

if (CLIENT) then
return
end

local data = {}
data.start = self.Owner:GetShootPos()
data.endpos = data.start + self.Owner:GetAimVector()*96
data.filter = self.Owner
local entity = util.TraceLine(data).Entity

--[[
Locks the entity if the contiditon fits:
1. The entity is door and client has access to the door.
2. The entity is vehicle and the "owner" variable is same as client's character ID.
--]]

if ( IsValid( entity ) and entity:IsDoor() ) then
for k, v in pairs(PLUGIN.doors) do
if ( game.GetMap() == k ) then
for stuff, stuff2 in pairs(v) do
if ( entity:MapCreationID() == stuff2.id ) then
for a, b in pairs(PLUGIN.access) do
if ( a == stuff2.access ) then
if ( b.checkAccess(self.Owner) ) then
if ( entity:GetClass() == "prop_door_rotating" ) then
self.Owner:SetAction("@locking", time, function()
self:ToggleLock(entity, true)
end)
end
else
self.Owner:ChatNotify("You do not have access to this door.")
return
end
end
end
end
end
end
end
end

if (IsValid(entity) and
(
(entity:IsDoor() and entity:CheckDoorAccess(self.Owner)) or
(entity:IsVehicle() and entity.CPPIGetOwner and entity:CPPIGetOwner() == self.Owner)
)
) then
self.Owner:SetAction("@locking", time, function()
self:ToggleLock(entity, true)
end)

return
end
end

function SWEP:ToggleLock(door, state)
if (IsValid(self.Owner) and self.Owner:GetPos():Distance(door:GetPos()) > 96) then
return
end

if (door:IsDoor()) then
local partner = door:GetDoorPartner()

if (state) then
if (IsValid(partner)) then
partner:Fire("lock")
end

door:Fire("lock")
self.Owner:EmitSound("doors/door_latch3.wav")

hook.Run("PlayerLockedDoor", self.Owner, door, partner)
else
if (IsValid(partner)) then
partner:Fire("unlock")
end

door:Fire("unlock")
self.Owner:EmitSound("doors/door_latch1.wav")

hook.Run("PlayerUnlockedDoor", self.Owner, door, partner)
end
elseif (door:IsVehicle()) then
if (state) then
door:Fire("lock")

if (door.IsSimfphyscar) then
door.IsLocked = true
end

self.Owner:EmitSound("doors/door_latch3.wav")
hook.Run("PlayerLockedVehicle", self.Owner, door)
else
door:Fire("unlock")

if (door.IsSimfphyscar) then
door.IsLocked = nil
end

self.Owner:EmitSound("doors/door_latch1.wav")
hook.Run("PlayerUnlockedVehicle", self.Owner, door)
end
end
end

function SWEP:SecondaryAttack()
local time = ix.config.Get("doorLockTime", 1)
local time2 = math.max(time, 1)

self:SetNextPrimaryFire(CurTime() + time2)
self:SetNextSecondaryFire(CurTime() + time2)

if (!IsFirstTimePredicted()) then
return
end

if (CLIENT) then
return
end

local data = {}
data.start = self.Owner:GetShootPos()
data.endpos = data.start + self.Owner:GetAimVector()*96
data.filter = self.Owner
local entity = util.TraceLine(data).Entity


--[[
Unlocks the entity if the contiditon fits:
1. The entity is door and client has access to the door.
2. The entity is vehicle and the "owner" variable is same as client's character ID.
]]--

if ( IsValid( entity ) and entity:IsDoor() ) then
for k, v in pairs(PLUGIN.doors) do
if ( game.GetMap() == k ) then
for stuff, stuff2 in pairs(v) do
if ( entity:MapCreationID() == stuff2.id ) then
for a, b in pairs(PLUGIN.access) do
if ( a == stuff2.access ) then
if ( b.checkAccess(self.Owner) ) then
if ( entity:GetClass() == "prop_door_rotating" ) then
self.Owner:SetAction("@unlocking", time, function()
self:ToggleLock(entity, false)
end)
end
else
self.Owner:ChatNotify("You do not have access to this door.")
return
end
end
end
end
end
end
end
end

if (IsValid(entity) and
(
(entity:IsDoor() and entity:CheckDoorAccess(self.Owner)) or
(entity:IsVehicle() and entity.CPPIGetOwner and entity:CPPIGetOwner() == self.Owner)
)
) then
self.Owner:SetAction("@unlocking", time, function()
self:ToggleLock(entity, false)
end)

return
end
end
74 changes: 74 additions & 0 deletions doorsystem/sh_entity.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
local ENTITY = FindMetaTable("Entity")

function ENTITY:NearEntity(entity, radius)
for k, v in ipairs(ents.FindInSphere(self:GetPos(), radius or 96)) do
if ( v:GetClass() == entity:GetClass() ) then
return true
end
end
return false
end

if ( CLIENT ) then
net.Receive("ixEntityNameRequest", function(len)
local name = net.ReadString()
local ent = net.ReadEntity()
if not( IsValid(ent) ) then return end

ent._name = name
end)

net.Receive("ixEntityIDRequest", function(len)
local id = net.ReadString()
local ent = net.ReadEntity()
if not( IsValid(ent) ) then return end

ent._id = id
end)

function ENTITY:GetName()
if not ( self._name ) then
net.Start("ixEntityNameRequest")
net.WriteEntity(self)
net.SendToServer()
return ""
end

return self._name
end

function ENTITY:MapCreationID()
if not ( self._id ) then
net.Start("ixEntityIDRequest")
net.WriteEntity(self)
net.SendToServer()
return ""
end

return self._id
end
else
util.AddNetworkString("ixEntityNameRequest")
net.Receive("ixEntityNameRequest", function(len, ply)
local ent = net.ReadEntity()
if not ( IsValid(ent) ) then return end
local name = ent:GetName()

net.Start("ixEntityNameRequest")
net.WriteString(name)
net.WriteEntity(ent)
net.Send(ply)
end)

util.AddNetworkString("ixEntityIDRequest")
net.Receive("ixEntityIDRequest", function(len, ply)
local ent = net.ReadEntity()
if not ( IsValid(ent) ) then return end
local name = tostring(ent:MapCreationID())

net.Start("ixEntityIDRequest")
net.WriteString(name)
net.WriteEntity(ent)
net.Send(ply)
end)
end
Loading