Skip to content

Commit

Permalink
Add player color functions (thegrb93#1604)
Browse files Browse the repository at this point in the history
* Add Player:getPlayerColor()

* Add player color set/get to holograms

* Condense

* Use DTVar instead of net stream

* Style
  • Loading branch information
legokidlogan authored Jan 28, 2024
1 parent 55a93eb commit da8617c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 17 deletions.
20 changes: 20 additions & 0 deletions lua/entities/starfall_hologram/cl_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@ ENT.IsHologram = true
ENT.DefaultMaterial = Material( "hunter/myplastic" )
ENT.Material = ENT.DefaultMaterial

local VECTOR_PLAYER_COLOR_DISABLED = Vector(-1, -1, -1)

function ENT:Initialize()
self.clips = {}
self.sf_userrenderbounds = false
self:SetupBones()
self:OnScaleChanged(nil, nil, self:GetScale())

if self:EntIndex() == -1 then
self:SetPlayerColorInternal(VECTOR_PLAYER_COLOR_DISABLED)
else
self:OnPlayerColorChanged(nil, nil, self:GetPlayerColorInternal())
end

-- Fixes future SetParent calls not keeping offset from the parent
self:SetParent(Entity(0))
self:SetParent()
Expand Down Expand Up @@ -42,6 +50,18 @@ function ENT:OnScaleChanged(name, old, scale)
end
end

function ENT:OnPlayerColorChanged(name, old, color)
if color == VECTOR_PLAYER_COLOR_DISABLED then
self.GetPlayerColor = nil -- The material proxy will break if this is not removed when disabling player color.
else
-- Having this function is what causes player color to actually be applied.
-- https://github.com/garrynewman/garrysmod/blob/master/garrysmod/lua/matproxy/player_color.lua
function self:GetPlayerColor()
return color
end
end
end

function ENT:Draw(flags)
local selfTbl = self:GetTable()
if self:GetColor().a ~= 255 then
Expand Down
6 changes: 5 additions & 1 deletion lua/entities/starfall_hologram/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")

local VECTOR_PLAYER_COLOR_DISABLED = Vector(-1, -1, -1)

function ENT:Initialize()
self.BaseClass.Initialize()
self:SetSolid(SOLID_NONE)
Expand All @@ -14,6 +16,7 @@ function ENT:Initialize()
self.clipdata = ""

self:SetScale(Vector(1,1,1))
self:SetPlayerColorInternal(VECTOR_PLAYER_COLOR_DISABLED)
self:SetSuppressEngineLighting(false)

self.updateClip = false
Expand Down Expand Up @@ -42,9 +45,10 @@ function ENT:Think()
clipdata:writeInt16(v.entity and v.entity:EntIndex() or 0)
end
self.clipdata = clipdata:getString()

self:TransmitClips()
end

if self.AutomaticFrameAdvance then
self:NextThink(CurTime())
return true
Expand Down
6 changes: 3 additions & 3 deletions lua/entities/starfall_hologram/shared.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ ENT.AdminSpawnable = false
ENT.IsSFHologram = true

function ENT:SetupDataTables()

self:NetworkVar( "Vector", 0, "Scale" )
self:NetworkVar( "Vector", 1, "PlayerColorInternal" )
self:NetworkVar( "Bool", 0, "SuppressEngineLighting" )

if ( CLIENT ) then
if CLIENT then
self:NetworkVarNotify( "Scale", self.OnScaleChanged )
self:NetworkVarNotify( "PlayerColorInternal", self.OnPlayerColorChanged )
end

end
31 changes: 31 additions & 0 deletions lua/starfall/libs_sh/hologram.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ local ang_meta, awrap, aunwrap = instance.Types.Angle, instance.Types.Angle.Wrap
local vec_meta, vwrap, vunwrap = instance.Types.Vector, instance.Types.Vector.Wrap, instance.Types.Vector.Unwrap
local mtx_meta, mwrap, munwrap = instance.Types.VMatrix, instance.Types.VMatrix.Wrap, instance.Types.VMatrix.Unwrap

local VECTOR_PLAYER_COLOR_DISABLED = Vector(-1, -1, -1)

local getent
instance:AddHook("initialize", function()
getent = instance.Types.Entity.GetEntity
Expand Down Expand Up @@ -304,6 +306,35 @@ else
end
end

--- Sets the player color of a hologram
-- The part of the model that is colored is determined by the model itself, and is different for each model
-- The format is Vector(r,g,b), and each color should be between 0 and 1
-- @shared
-- @param Vector? color The player color to use, or nil to disable
function hologram_methods:setPlayerColor(color)
local holo = getholo(self)

checkpermission(instance, holo, "hologram.setRenderProperty")

color = color ~= nil and vunwrap(color) or VECTOR_PLAYER_COLOR_DISABLED

holo:SetPlayerColorInternal(color)
end

--- Gets the player color of a hologram
-- The part of the model that is colored is determined by the model itself, and is different for each model
-- The format is Vector(r,g,b), and each color should be between 0 and 1
-- @shared
-- @return Vector? color The player color to use, or nil if disabled
function hologram_methods:getPlayerColor()
local holo = getholo(self)
local color = holo:GetPlayerColorInternal()

if color == VECTOR_PLAYER_COLOR_DISABLED then return nil end

return vwrap(color)
end

--- Updates a clip plane
-- @shared
-- @param number index Whatever number you want the clip to be
Expand Down
35 changes: 22 additions & 13 deletions lua/starfall/libs_sh/players.lua
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,15 @@ function player_methods:getWeaponColor()
return vwrap(getply(self):GetWeaponColor())
end

--- Returns a player's color
-- The part of the model that is colored is determined by the model itself, and is different for each model
-- The format is Vector(r,g,b), and each color should be between 0 and 1
-- @shared
-- @return Vector The color
function player_methods:getPlayerColor()
return vwrap(getply(self):GetPlayerColor())
end

--- Returns the entity that the player is standing on
-- @shared
-- @return Entity Ground entity
Expand Down Expand Up @@ -657,7 +666,7 @@ if SERVER then
ply.sf_say_cd = CurTime() + 0.5
ply:Say(text, teamOnly)
end

--- Sets the armor of the player.
-- @server
-- @param number newarmor New armor value.
Expand All @@ -667,7 +676,7 @@ if SERVER then
checkvalidnumber(val)
ent:SetArmor(val)
end

--- Sets the maximum armor for player. You can still set a player's armor above this amount with Player:setArmor.
-- @server
-- @param number newmaxarmor New max armor value.
Expand All @@ -677,7 +686,7 @@ if SERVER then
checkvalidnumber(val)
ent:SetMaxArmor(val)
end

--- Sets Crouched Walk Speed
-- @server
-- @param number newcwalkspeed New Crouch Walk speed, This is a multiplier from 0 to 1.
Expand All @@ -687,7 +696,7 @@ if SERVER then
checkvalidnumber(val)
ent:SetCrouchedWalkSpeed(math.Clamp(val,0,1))
end

--- Sets Duck Speed
-- @server
-- @param number newduckspeed New Duck speed, This is a multiplier from 0 to 1.
Expand All @@ -697,7 +706,7 @@ if SERVER then
checkvalidnumber(val)
ent:SetDuckSpeed(math.Clamp(val,0.005,0.995))
end

--- Sets UnDuck Speed
-- @server
-- @param number newunduckspeed New UnDuck speed, This is a multiplier from 0 to 1.
Expand All @@ -707,7 +716,7 @@ if SERVER then
checkvalidnumber(val)
ent:SetUnDuckSpeed(math.Clamp(val,0.005,0.995))
end

--- Sets Ladder Climb Speed, probably unstable
-- @server
-- @param number newladderclimbspeed New Ladder Climb speed.
Expand All @@ -717,7 +726,7 @@ if SERVER then
checkvalidnumber(val)
ent:SetLadderClimbSpeed(math.max(val,0))
end

--- Sets Max Speed
-- @server
-- @param number newmaxspeed New Max speed.
Expand All @@ -727,7 +736,7 @@ if SERVER then
checkvalidnumber(val)
ent:SetMaxSpeed(math.max(val,0))
end

--- Sets Run Speed ( +speed )
-- @server
-- @param number newrunspeed New Run speed.
Expand All @@ -737,7 +746,7 @@ if SERVER then
checkvalidnumber(val)
ent:SetRunSpeed(math.max(val,0))
end

--- Sets Slow Walk Speed ( +walk )
-- @server
-- @param number newslowwalkspeed New Slow Walk speed.
Expand All @@ -747,7 +756,7 @@ if SERVER then
checkvalidnumber(val)
ent:SetSlowWalkSpeed(math.max(val,0))
end

--- Sets Walk Speed
-- @server
-- @param number newwalkspeed New Walk speed.
Expand All @@ -757,7 +766,7 @@ if SERVER then
checkvalidnumber(val)
ent:SetWalkSpeed(math.max(val,0))
end

--- Sets Jump Power
-- @server
-- @param number newjumppower New Jump Power.
Expand All @@ -767,7 +776,7 @@ if SERVER then
checkvalidnumber(val)
ent:SetJumpPower(math.max(val,0))
end

--- Sets Step Size
-- @server
-- @param number newstepsize New Step Size.
Expand All @@ -777,7 +786,7 @@ if SERVER then
checkvalidnumber(val)
ent:SetStepSize(math.max(val,0))
end

--- Sets Friction
-- @server
-- @param number newfriction New Friction.
Expand Down

0 comments on commit da8617c

Please sign in to comment.