Skip to content

Commit

Permalink
Add local velocity persistence to holograms (#1940)
Browse files Browse the repository at this point in the history
  • Loading branch information
thegrb93 authored Dec 25, 2024
1 parent 2ba4a6c commit b2187df
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 46 deletions.
37 changes: 18 additions & 19 deletions lua/starfall/libs_sh/hologram.lua
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,13 @@ if SERVER then
-- @param Vector vel New local velocity
function hologram_methods:setLocalVelocity(vel)
local holo = getholo(self)
local ent_tbl = Ent_GetTable(holo)
vel = vunwrap1(vel)
checkpermission(instance, holo, "hologram.setRenderProperty")

Ent_SetLocalVelocity(holo, vunwrap1(vel))
Ent_SetLocalVelocity(holo, vel)

local sfParent = SF.Parent.Get(holo)
if sfParent then sfParent.localVel:Set(vel) end
end
hologram_methods.setVel = hologram_methods.setLocalVelocity

Expand All @@ -177,9 +180,13 @@ if SERVER then
-- @param Angle angvel *Vector* local angular velocity.
function hologram_methods:setLocalAngularVelocity(angvel)
local holo = getholo(self)
angvel = aunwrap1(angvel)
checkpermission(instance, holo, "hologram.setRenderProperty")

Ent_SetLocalAngularVelocity(holo, aunwrap1(angvel))
Ent_SetLocalAngularVelocity(holo, angvel)

local sfParent = SF.Parent.Get(holo)
if sfParent then sfParent.localAngVel:Set(angvel) end
end
hologram_methods.setAngVel = hologram_methods.setLocalAngularVelocity

Expand All @@ -205,10 +212,8 @@ else

Ent_SetPos(holo, SF.clampPos(vunwrap1(vec)))

local sfParent = Ent_GetTable(holo).sfParent
if sfParent and Ent_IsValid(sfParent.parent) then
sfParent:updateTransform()
end
local sfParent = SF.Parent.Get(holo)
if sfParent and Ent_IsValid(sfParent.parent) then sfParent:updateTransform() end
end

--- Sets the hologram's angles.
Expand All @@ -220,10 +225,8 @@ else

Ent_SetAngles(holo, aunwrap1(ang))

local sfParent = Ent_GetTable(holo).sfParent
if sfParent and Ent_IsValid(sfParent.parent) then
sfParent:updateTransform()
end
local sfParent = SF.Parent.Get(holo)
if sfParent and Ent_IsValid(sfParent.parent) then sfParent:updateTransform() end
end

--- Sets the hologram's position local to its parent.
Expand All @@ -235,10 +238,8 @@ else

Ent_SetLocalPos(holo, SF.clampPos(vunwrap1(vec)))

local sfParent = Ent_GetTable(holo).sfParent
if sfParent and Ent_IsValid(sfParent.parent) then
sfParent:updateTransform()
end
local sfParent = SF.Parent.Get(holo)
if sfParent and Ent_IsValid(sfParent.parent) then sfParent:updateTransform() end
end

--- Sets the hologram's angles local to its parent.
Expand All @@ -250,10 +251,8 @@ else

Ent_SetLocalAngles(holo, aunwrap1(ang))

local sfParent = Ent_GetTable(holo).sfParent
if sfParent and Ent_IsValid(sfParent.parent) then
sfParent:updateTransform()
end
local sfParent = SF.Parent.Get(holo)
if sfParent and Ent_IsValid(sfParent.parent) then sfParent:updateTransform() end
end

--- Sets the texture filtering function when viewing a close texture
Expand Down
47 changes: 20 additions & 27 deletions lua/starfall/sflib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local dgetmeta = debug.getmetatable
local TypeID = TypeID
local math_Clamp = math.Clamp
local ENT_META,NPC_META,PHYS_META,PLY_META,VEH_META,WEP_META = FindMetaTable("Entity"),FindMetaTable("NPC"),FindMetaTable("PhysObj"),FindMetaTable("Player"),FindMetaTable("Vehicle"),FindMetaTable("Weapon")
local Ent_EntIndex,Ent_Fire,Ent_FollowBone,Ent_GetAngles,Ent_GetChildren,Ent_GetClass,Ent_GetCreationID,Ent_GetParent,Ent_GetPos,Ent_GetTable,Ent_IsScripted,Ent_IsValid,Ent_IsWorld,Ent_SetAngles,Ent_SetParent,Ent_SetPos = ENT_META.EntIndex,ENT_META.Fire,ENT_META.FollowBone,ENT_META.GetAngles,ENT_META.GetChildren,ENT_META.GetClass,ENT_META.GetCreationID,ENT_META.GetParent,ENT_META.GetPos,ENT_META.GetTable,ENT_META.IsScripted,ENT_META.IsValid,ENT_META.IsWorld,ENT_META.SetAngles,ENT_META.SetParent,ENT_META.SetPos
local Ent_EntIndex,Ent_Fire,Ent_FollowBone,Ent_GetAngles,Ent_GetChildren,Ent_GetClass,Ent_GetCreationID,Ent_GetParent,Ent_GetPos,Ent_GetTable,Ent_IsScripted,Ent_IsValid,Ent_IsWorld,Ent_SetAngles,Ent_SetLocalAngularVelocity,Ent_SetLocalVelocity,Ent_SetParent,Ent_SetPos = ENT_META.EntIndex,ENT_META.Fire,ENT_META.FollowBone,ENT_META.GetAngles,ENT_META.GetChildren,ENT_META.GetClass,ENT_META.GetCreationID,ENT_META.GetParent,ENT_META.GetPos,ENT_META.GetTable,ENT_META.IsScripted,ENT_META.IsValid,ENT_META.IsWorld,ENT_META.SetAngles,ENT_META.SetLocalAngularVelocity,ENT_META.SetLocalVelocity,ENT_META.SetParent,ENT_META.SetPos
local function Ent_IsNPC(ent) return dgetmeta(ent)==NPC_META end
local function Ent_IsPlayer(ent) return dgetmeta(ent)==PLY_META end
local function Ent_IsVehicle(ent) return dgetmeta(ent)==VEH_META end
Expand Down Expand Up @@ -658,6 +658,8 @@ SF.Parent = {
self.applyParent = nil
self.removeParent = nil
end
Ent_SetLocalVelocity(self.ent, self.localVel)
Ent_SetLocalAngularVelocity(self.ent, self.localAngVel)
end,

fix = function(self)
Expand Down Expand Up @@ -685,37 +687,28 @@ SF.Parent = {
end,
},

Get = function(ent, create)
local sfParent = Ent_GetTable(ent).sfParent
if not sfParent and create then
sfParent = setmetatable({
ent = ent,
children = {},
localVel = Vector(),
localAngVel = Angle()
}, SF.Parent)
Ent_GetTable(ent).sfParent = sfParent
end
return sfParent
end,

__call = function(meta, child, parent, type, param)
if parent then
if SF.ParentChainTooLong(parent, child) then SF.Throw("Parenting chain cannot exceed 16 or crash may occur", 3) end

local sfParent
sfParent = Ent_GetTable(parent).sfParent
if not sfParent then
sfParent = setmetatable({
ent = parent,
children = {}
}, meta)
Ent_GetTable(parent).sfParent = sfParent
end

sfParent = Ent_GetTable(child).sfParent
if not sfParent then
sfParent = setmetatable({
ent = child,
children = {}
}, meta)
Ent_GetTable(child).sfParent = sfParent
end

sfParent:setParent(parent, type, param)
SF.Parent.Get(parent, true)
SF.Parent.Get(child, true):setParent(parent, type, param)
else
local sfParent = Ent_GetTable(child).sfParent
if sfParent then
sfParent:setParent()
else
Ent_SetParent(child)
end
if sfParent then sfParent:setParent() else Ent_SetParent(child) end
end
end
}
Expand Down

0 comments on commit b2187df

Please sign in to comment.