From 2ea838b7bd816cbc397fd88c52abfd35fb5b0635 Mon Sep 17 00:00:00 2001 From: Bob Blackmon Date: Sun, 2 Apr 2017 23:14:21 -0400 Subject: Updated docs Also moved example. --- zone_example.lua | 68 ++++++++++++++++++++++++++++++++++++++ zones/lua/autorun/zone_example.lua | 68 -------------------------------------- zones/lua/zones.lua | 54 ++++++++++++++++++++++++++---- 3 files changed, 116 insertions(+), 74 deletions(-) create mode 100644 zone_example.lua delete mode 100644 zones/lua/autorun/zone_example.lua diff --git a/zone_example.lua b/zone_example.lua new file mode 100644 index 0000000..509eb88 --- /dev/null +++ b/zone_example.lua @@ -0,0 +1,68 @@ +--This is an example usage of the zones system. +AddCSLuaFile("zones.lua") +include("zones.lua") + +zones.RegisterClass("Arena Zone",Color(255,0,0)) + +--Use this to set default properties. Only called on server. +hook.Add("OnZoneCreated","Arena Zone",function(zone,class,zoneID) + if class == "Arena Zone" then + + zone.DmgMul = 1 + + end +end) + +-- Use this hook to let a player change a zone after making it or with the edit tool. +-- class is zone.class, zone is the zone's full table, DPanel is a panel to parent your things to, zoneID is the zone's ID, DFrame is the whole frame. +-- Return your preferred width and height for the panel and the frame will size to it. +hook.Add("ShowZoneOptions","Arena Zone",function(zone,class,DPanel,zoneID,DFrame) + if class == "Arena Zone" then + local w,h = 500, 400 + + local mulbl = Label("Damage Multiplier:") + mulbl:SetParent(DPanel) + mulbl:SetPos(5,5) + mulbl:SetTextColor(color_black) + mulbl:SizeToContents() + + local mul = vgui.Create("DNumberWang",DPanel) --parent to the panel. + mul:SetPos(5,mulbl:GetTall()+10) + mul:SetValue(zone.DmgMul) + mul:SetDecimals(1) + mul:SetMinMax(0,10) + function mul:OnValueChanged(new) + net.Start("arena_zone") + net.WriteFloat(zoneID) + net.WriteFloat(new) + net.SendToServer() + end + + + + return w, h -- Specify the width and height for the DPanel container. The frame will resize accordingly. + + end +end) + +if SERVER then + util.AddNetworkString("arena_zone") + net.Receive("arena_zone",function(len,ply) + local id, new = net.ReadFloat(), net.ReadFloat() + if not ply:IsAdmin() then return end + + zones.List[id].DmgMul = new + zones.Sync() + + end) +end + +hook.Add("ScalePlayerDamage","Arena Zone",function(ply, hitgroup, dmginfo) + local zone = ply:GetCurrentZone() + if zone then + if zone.class == "Arena Zone" then + dmginfo:ScaleDamage(zone.DmgMul) + + end + end +end) diff --git a/zones/lua/autorun/zone_example.lua b/zones/lua/autorun/zone_example.lua deleted file mode 100644 index 509eb88..0000000 --- a/zones/lua/autorun/zone_example.lua +++ /dev/null @@ -1,68 +0,0 @@ ---This is an example usage of the zones system. -AddCSLuaFile("zones.lua") -include("zones.lua") - -zones.RegisterClass("Arena Zone",Color(255,0,0)) - ---Use this to set default properties. Only called on server. -hook.Add("OnZoneCreated","Arena Zone",function(zone,class,zoneID) - if class == "Arena Zone" then - - zone.DmgMul = 1 - - end -end) - --- Use this hook to let a player change a zone after making it or with the edit tool. --- class is zone.class, zone is the zone's full table, DPanel is a panel to parent your things to, zoneID is the zone's ID, DFrame is the whole frame. --- Return your preferred width and height for the panel and the frame will size to it. -hook.Add("ShowZoneOptions","Arena Zone",function(zone,class,DPanel,zoneID,DFrame) - if class == "Arena Zone" then - local w,h = 500, 400 - - local mulbl = Label("Damage Multiplier:") - mulbl:SetParent(DPanel) - mulbl:SetPos(5,5) - mulbl:SetTextColor(color_black) - mulbl:SizeToContents() - - local mul = vgui.Create("DNumberWang",DPanel) --parent to the panel. - mul:SetPos(5,mulbl:GetTall()+10) - mul:SetValue(zone.DmgMul) - mul:SetDecimals(1) - mul:SetMinMax(0,10) - function mul:OnValueChanged(new) - net.Start("arena_zone") - net.WriteFloat(zoneID) - net.WriteFloat(new) - net.SendToServer() - end - - - - return w, h -- Specify the width and height for the DPanel container. The frame will resize accordingly. - - end -end) - -if SERVER then - util.AddNetworkString("arena_zone") - net.Receive("arena_zone",function(len,ply) - local id, new = net.ReadFloat(), net.ReadFloat() - if not ply:IsAdmin() then return end - - zones.List[id].DmgMul = new - zones.Sync() - - end) -end - -hook.Add("ScalePlayerDamage","Arena Zone",function(ply, hitgroup, dmginfo) - local zone = ply:GetCurrentZone() - if zone then - if zone.class == "Arena Zone" then - dmginfo:ScaleDamage(zone.DmgMul) - - end - end -end) diff --git a/zones/lua/zones.lua b/zones/lua/zones.lua index c0a252f..c881e09 100644 --- a/zones/lua/zones.lua +++ b/zones/lua/zones.lua @@ -16,7 +16,8 @@ local version = 1.1 -- Older versions will not run if a newer version is used in Since multiple scripts might use the zones system, don't assume that every zone is related to your script. To register a zone class, use zones.RegisterClass(class, color); use a unique string like "Scriptname Room". When a zone class is registered, admins can use the tool to create new ones. - When a new zone is created, the "ShowZoneOptions" hook is called clientside. See the hook below for documentation. + When a new zone is created, the "OnZoneCreated" hook is called serverside. See the hook below for documentation. + When a player edits a zone's properties, the "ShowZoneOptions" hook is called clientside. See the hook below. Use zones.FindByClass() to find all zones which are of a given class. Use ply:GetCurrentZone() to find the zone that a player is standing in. @@ -26,7 +27,7 @@ local version = 1.1 -- Older versions will not run if a newer version is used in You should not put this file directly in lua/autorun. License: - YOU MAY use/edit this however you want. + YOU MAY use/edit this however you want, as long as you give proper attribution. YOU MAY distribute this with other scripts whether they are paid or free. YOU MAY NOT distribute this on its own. It must accompany another script. @@ -49,8 +50,10 @@ end zones = zones or {} zones.version = version +zones.Classes = zones.Classes or {} zones.List = zones.List or {} ---[[ --Example of a zone. +--[[ + --Example structure of a zone. zones.List[1] = { -- 1 is the zone ID. Automatically assigned. -- points, height, bounds, and class are reserved. @@ -82,11 +85,49 @@ zones.List = zones.List or {} -- Zones can have any other values saved to them. If you save a player, make sure to save it as a steamid. } + + -- Example of the ShowZoneOptions hook. + -- This hook lets you build your custom VGUI for your zone class which will pop up when players make a new zone or edit an existing one. + -- Arguments are: + -- zone - The full zone table of the zone we are editing. + -- class - The class of the zone. + -- DPanel - The DPanel which your VGUI elements should be parented to. + -- zoneID - The ID of the zone. + -- DFrame - The DFrame which holds it all. Not likely you will need this but it's here anyway. + -- You must return: + -- width, height; How large you want the DPanel to be. It will automatically resize. + hook.Add("ShowZoneOptions","hookname_unique",function(zone,class,DPanel,zoneID,DFrame) + if class == "Baby Got Back" then --always check class. + local w,h = 80, 100 + + local mul = vgui.Create("DNumberWang",DPanel) --parent to the panel. + mul:SetPos(5,10) + mul:SetValue(zone.onlyIfShes) --The default value should be set in the OnZoneCreated hook. + mul:SetDecimals(1) + mul:SetMinMax(0,10) + function mul:OnValueChanged(new) + -- Do your stuff here. + end + + return w, h + end + end) + + -- Example of the OnZoneCreated hook. + -- This hook lets you build your custom VGUI for your zone class which will pop up when players make a new zone or edit an existing one. + -- Arguments are: + -- zone - The full zone table of the zone we are editing. + -- class - The class of the zone. + -- zoneID - The ID of the zone. + hook.Add("OnZoneCreated","hookname_unique",function(zone,class,zoneID) + if class == "Baby Got Back" then --always check class. + zone.waistSize = Vector(36,24,36) + zone.onlyIfShes = "5'3\"" + end + end) ]] -zones.Classes = zones.Classes or {} - -//Common interface functions +//Common interface functions: -- Registers a zone class which can then be created using weapon_zone_designator function zones.RegisterClass(class,color) @@ -157,6 +198,7 @@ function zones.GetID(zone) end +//Getting into the meat of the API: if SERVER then util.AddNetworkString("zones_sync") util.AddNetworkString("zones_class") -- cgit v1.2.3-70-g09d2