Skip to content

Instantly share code, notes, and snippets.

@RiRi-380
Created August 17, 2024 10:49
Show Gist options
  • Save RiRi-380/e9e97efd0d0c1081d5aee03971cb31b4 to your computer and use it in GitHub Desktop.
Save RiRi-380/e9e97efd0d0c1081d5aee03971cb31b4 to your computer and use it in GitHub Desktop.
TOOL.Category = "Construction"
TOOL.Name = "Smart Door Control Switch"
TOOL.ClientConVar = {
["size"] = "10",
["visible"] = "0", -- Default to visible (checkbox off)
}
if CLIENT then
language.Add("tool.smart_door_control_switch.name", "Smart Door Control Switch")
language.Add("tool.smart_door_control_switch.desc", "Create switches to control doors")
language.Add("tool.smart_door_control_switch.0", "Left click to create a switch, Right click to remove")
CreateClientConVar("smart_door_control_switch_size", "10", true, false)
CreateClientConVar("smart_door_control_switch_visible", "0", true, false) -- Default to visible (checkbox off)
end
if SERVER then
util.AddNetworkString("SmartDoorControlSwitch_Notify")
util.AddNetworkString("SmartDoorControlSwitch_Toggle")
end
-- Custom switch entity
local ENT = {}
ENT.Type = "anim"
ENT.Base = "base_anim"
ENT.PrintName = "Smart Door Control Switch"
ENT.Author = "Your Name"
ENT.Spawnable = false
ENT.AdminSpawnable = false
function ENT:Initialize()
self:SetModel("models/hunter/blocks/cube025x025x025.mdl") -- Simple cube model
self:SetMoveType(MOVETYPE_VPHYSICS)
self:SetSolid(SOLID_VPHYSICS)
self:SetUseType(SIMPLE_USE)
self:SetColor(Color(255, 255, 255)) -- White color
if SERVER then
self:PhysicsInit(SOLID_VPHYSICS)
local phys = self:GetPhysicsObject()
if IsValid(phys) then
phys:EnableMotion(false)
end
end
self:SetNWBool("Invisible", false) -- Default to visible
end
function ENT:Use(activator, caller)
if SERVER and IsValid(activator) and activator:IsPlayer() then
if SmartDoorMaster and SmartDoorMaster.Doors then
local useTimer = GetConVar("smart_door_master_use_timer"):GetBool()
local timerDelay = useTimer and GetConVar("smart_door_master_timer"):GetFloat() or 0
local openDirection = GetConVar("smart_door_master_open_direction"):GetInt()
for _, door in pairs(SmartDoorMaster.Doors) do
if IsValid(door) then
timer.Simple(timerDelay, function()
if IsValid(door) and SmartDoorMaster.ToggleDoor then
SmartDoorMaster.ToggleDoor(door, activator)
else
print("Door became invalid during delay or ToggleDoor function not found")
end
end)
end
end
end
end
end
function ENT:Draw()
if not self:GetNWBool("Invisible", false) then
self:DrawModel()
end
end
scripted_ents.Register(ENT, "smart_door_control_switch")
function TOOL:LeftClick(trace)
if CLIENT then return true end
local ply = self:GetOwner()
if not IsValid(ply) then return false end
local pos = trace.HitPos
local ang = trace.HitNormal:Angle()
ang:RotateAroundAxis(ang:Up(), 90)
local switch = ents.Create("smart_door_control_switch")
switch:SetPos(pos)
switch:SetAngles(ang)
switch:Spawn()
local size = self:GetClientNumber("size", 10)
switch:SetModelScale(size / 10)
undo.Create("Smart Door Control Switch")
undo.AddEntity(switch)
undo.SetPlayer(ply)
undo.Finish()
return true
end
function TOOL:RightClick(trace)
if CLIENT then return true end
local ent = trace.Entity
if IsValid(ent) and ent:GetClass() == "smart_door_control_switch" then
ent:Remove()
return true
end
return false
end
function TOOL.BuildCPanel(panel)
panel:AddControl("Header", {Description = "Create switches to control doors"})
panel:AddControl("Slider", {
Label = "Switch Size",
Command = "smart_door_control_switch_size",
Type = "Float",
Min = 1,
Max = 50
})
panel:AddControl("CheckBox", {
Label = "Invisible Switch",
Command = "smart_door_control_switch_visible"
})
end
if SERVER then
hook.Add("Think", "SmartDoorControlSwitch_UpdateVisibility", function()
local invisible = GetConVar("smart_door_control_switch_visible"):GetBool()
for _, ent in pairs(ents.FindByClass("smart_door_control_switch")) do
ent:SetNWBool("Invisible", invisible)
end
end)
end
if CLIENT then
net.Receive("SmartDoorControlSwitch_Notify", function()
local message = net.ReadString()
notification.AddLegacy(message, NOTIFY_GENERIC, 3)
end)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment