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

Improvement Sandbox ENT`s #2109

Open
wants to merge 7 commits 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
78 changes: 48 additions & 30 deletions garrysmod/gamemodes/sandbox/entities/entities/base_gmodentity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,67 +7,85 @@ ENT.Spawnable = false
if ( CLIENT ) then
ENT.MaxWorldTipDistance = 256

local FrameLast = FrameNumber()
local TraceEndpos = Vector()
local TraceConfig = { endpos = TraceEndpos, output = {} }

local CurrentLookedAt
function ENT:BeingLookedAtByLocalPlayer()
local ply = LocalPlayer()
if ( !IsValid( ply ) ) then return false end

local view = ply:GetViewEntity()
local dist = self.MaxWorldTipDistance
dist = dist * dist
local Frame = FrameNumber()
if ( Frame != FrameLast ) then
FrameLast = Frame

-- If we're spectating a player, perform an eye trace
if ( view:IsPlayer() ) then
return view:EyePos():DistToSqr( self:GetPos() ) <= dist && view:GetEyeTrace().Entity == self
end
local viewer, TraceResult = GetViewEntity()
if ( viewer:IsPlayer() ) then
TraceResult = viewer:GetEyeTrace()
else
local startpos = viewer:GetPos()
TraceConfig.start = startpos
TraceConfig.filter = viewer

TraceEndpos:Set( viewer:GetForward() )
TraceEndpos:Mul( 32768 )
TraceEndpos:Add( startpos )

-- If we're not spectating a player, perform a manual trace from the entity's position
local pos = view:GetPos()
TraceResult = util.TraceLine( TraceConfig )
end

if ( pos:DistToSqr( self:GetPos() ) <= dist ) then
return util.TraceLine( {
start = pos,
endpos = pos + ( view:GetAngles():Forward() * dist ),
filter = view
} ).Entity == self
CurrentLookedAt = TraceResult.Entity

local MaxWorldTipDistance = CurrentLookedAt.MaxWorldTipDistance
if ( not MaxWorldTipDistance or TraceResult.Fraction * 32768 > MaxWorldTipDistance ) then
CurrentLookedAt = nil
end
end

return false
return self == CurrentLookedAt

end

function ENT:Think()
local text = self:GetOverlayText()

if ( text != "" && self:BeingLookedAtByLocalPlayer() && !self:GetNoDraw() ) then
AddWorldTip( self:EntIndex(), text, 0.5, self:GetPos(), self )
if ( !self:BeingLookedAtByLocalPlayer() or self:GetNoDraw() ) then
return
end

halo.Add( { self }, color_white, 1, 1, 1, true, true )
local text = self:GetOverlayText()
if ( !text or text == "" ) then
return
end

AddWorldTip( self:EntIndex(), text, 0.5, self:GetPos(), self )

halo.Add( { self }, color_white, 1, 1, 1, true, true )

end
end

function ENT:SetOverlayText( text )

self:SetNWString( "GModOverlayText", text )

end

function ENT:GetOverlayText()

local txt = self:GetNWString( "GModOverlayText" )

if ( txt == "" ) then
return ""
end

if ( game.SinglePlayer() ) then
return txt
end
return txt
end

local PlayerName = self:GetPlayerName()

if ( !PlayerName or PlayerName == "" ) then
return txt
end
return txt
end

return txt .. "\n(" .. PlayerName .. ")"
return string.format( "%s\n(%s)", txt, PlayerName )

end

Expand Down Expand Up @@ -141,4 +159,4 @@ function ENT:GetPlayerName()

return self:GetNWString( "FounderName", "" )

end
end
46 changes: 34 additions & 12 deletions garrysmod/gamemodes/sandbox/entities/entities/gmod_emitter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,23 @@ function ENT:Think()
if ( !self:GetOn() ) then return end

self.Delay = self.Delay or 0
local curtime = CurTime()

if ( self.Delay > CurTime() ) then return end
self.Delay = CurTime() + self:GetDelay()
if ( self.Delay > curtime ) then return end
self.Delay = curtime + self:GetDelay()

--
-- Find our effect table
--
local Effect = self:GetEffect()

local EffectTable = list.Get( "EffectType" )[ Effect ]
local EffectTable = list.GetForEdit( "EffectType" )[ self:GetEffect() ]
if ( !EffectTable ) then return end

local Angle = self:GetAngles()
EffectTable.func( self, self:GetPos() + Angle:Forward() * 12, Angle, self:GetScale() )
local Pos = Angle:Forward()
Pos:Mul( 12 )
Pos:Add( self:GetPos() )

EffectTable.func( self, Pos, Angle, self:GetScale() )

end

Expand Down Expand Up @@ -127,9 +130,12 @@ list.Set( "EffectType", "sparks", {
material = "gui/effects/sparks.png",
func = function( ent, pos, angle, scale )

local normal = angle:Forward()
normal:Mul( scale )

local effectdata = EffectData()
effectdata:SetOrigin( pos )
effectdata:SetNormal( angle:Forward() * scale )
effectdata:SetNormal( normal )
effectdata:SetMagnitude( scale / 2 )
effectdata:SetRadius( 8 - scale )
util.Effect( "Sparks", effectdata, true, true )
Expand All @@ -155,9 +161,12 @@ list.Set( "EffectType", "manhacksparks", {
material = "gui/effects/manhacksparks.png",
func = function( ent, pos, angle, scale )

local normal = angle:Forward()
normal:Mul( scale )

local effectdata = EffectData()
effectdata:SetOrigin( pos )
effectdata:SetNormal( angle:Forward() * scale )
effectdata:SetNormal( normal )
util.Effect( "manhacksparks", effectdata, true, true )

end
Expand All @@ -168,9 +177,12 @@ list.Set( "EffectType", "bloodspray", {
material = "gui/effects/bloodspray.png",
func = function( ent, pos, angle, scale )

local normal = angle:Forward()
normal:Mul( math.max( 3, scale ) / 3 )

local effectdata = EffectData()
effectdata:SetOrigin( pos )
effectdata:SetNormal( angle:Forward() * math.max( 3, scale ) / 3 )
effectdata:SetNormal( normal )
effectdata:SetMagnitude( 1 )
effectdata:SetScale( scale + 9 )
effectdata:SetColor( 0 )
Expand All @@ -185,9 +197,12 @@ list.Set( "EffectType", "striderblood", {
material = "gui/effects/striderblood.png",
func = function( ent, pos, angle, scale )

local normal = angle:Forward()
normal:Mul( ( 6 - scale ) / 2 )

local effectdata = EffectData()
effectdata:SetOrigin( pos )
effectdata:SetNormal( angle:Forward() * ( 6 - scale ) / 2 )
effectdata:SetNormal( normal )
effectdata:SetScale( scale / 2 )
util.Effect( "StriderBlood", effectdata, true, true )

Expand Down Expand Up @@ -250,9 +265,12 @@ list.Set( "EffectType", "cball_bounce", {
material = "gui/effects/cball_bounce.png",
func = function( ent, pos, angle, scale )

local normal = angle:Forward()
normal:Mul( scale / 4 )

local effectdata = EffectData()
effectdata:SetOrigin( pos )
effectdata:SetNormal( angle:Forward() * scale / 4 )
effectdata:SetNormal( normal )
effectdata:SetRadius( scale * 2 )
util.Effect( "cball_bounce", effectdata, true, true )

Expand Down Expand Up @@ -393,8 +411,12 @@ list.Set( "EffectType", "muzzleeffect", {
material = "gui/effects/muzzleeffect.png",
func = function( ent, pos, angle, scale )

local origin = angle:Forward()
origin:Mul( 4 )
origin:Add( pos )

local effectdata = EffectData()
effectdata:SetOrigin( pos + angle:Forward() * 4 )
effectdata:SetOrigin( origin )
effectdata:SetAngles( angle )
effectdata:SetScale( scale )
util.Effect( "MuzzleEffect", effectdata, true, true )
Expand Down
32 changes: 22 additions & 10 deletions garrysmod/gamemodes/sandbox/entities/entities/gmod_hoverball.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,36 @@ function ENT:UpdateLabel()

end

local colGlow, vecGlow = Color( 255, 255, 255, 255 ), Vector( 0, 0, 0 )
function ENT:DrawEffects()

if ( !self:GetEnabled() ) then return end

local vOffset = self:GetPos()
local vPlayerEyes = LocalPlayer():EyePos()
local vDiff = ( vOffset - vPlayerEyes ):GetNormalized()
local vNormal = EyePos()
vNormal:Negate()
vNormal:Add( vOffset )
vNormal:Normalize()

render.SetMaterial( self.Glow )
local color = Color( 70, 180, 255, 255 )
render.DrawSprite( vOffset - vDiff * 2, 22, 22, color )

local Distance = math.abs( ( self:GetTargetZ() - self:GetPos().z ) * math.sin( CurTime() * 20 ) ) * 0.05
color.r = color.r * math.Clamp( Distance, 0, 1 )
color.b = color.b * math.Clamp( Distance, 0, 1 )
color.g = color.g * math.Clamp( Distance, 0, 1 )
vecGlow:Set( vNormal )
vecGlow:Mul( -2 )
vecGlow:Add( vOffset )

colGlow:SetUnpacked( 70, 180, 255, 255 )
render.DrawSprite( vecGlow, 22, 22, colGlow )

vecGlow:Set( vNormal )
vecGlow:Mul( 4 )
vecGlow:Add( vOffset )

local Distance = math.Clamp( math.abs( ( self:GetTargetZ() - vOffset.z ) * math.sin( CurTime() * 20 ) ) * 0.05, 0, 1 )
colGlow:SetUnpacked( 70 * Distance, 180 * Distance, 255 * Distance, 255 )

render.DrawSprite( vecGlow, 48, 48, colGlow )
render.DrawSprite( vecGlow, 52, 52, colGlow )

render.DrawSprite( vOffset + vDiff * 4, 48, 48, color )
render.DrawSprite( vOffset + vDiff * 4, 52, 52, color )
end

ENT.WantsTranslucency = true -- If model is opaque, still call DrawTranslucent
Expand Down
54 changes: 27 additions & 27 deletions garrysmod/gamemodes/sandbox/entities/entities/gmod_lamp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ local defaultOffset = Vector( 5, 0, 0 )
local defaultAngle = Angle( 0, 0, 0 )
function ENT:GetLightInfo()

local lightInfo = {}
if ( list.Get( "LampModels" )[ self:GetModel() ] ) then
lightInfo = list.Get( "LampModels" )[ self:GetModel() ]
end
local lightInfo = list.GetForEdit( "LampModels" )[ self:GetModel() ] or {}

lightInfo.Offset = lightInfo.Offset or defaultOffset
lightInfo.Angle = lightInfo.Angle or defaultAngle
Expand All @@ -80,12 +77,12 @@ if ( SERVER ) then

self.BaseClass.Think( self )

if ( !IsValid( self.flashlight ) ) then return end
local flashlight = self.flashlight
if ( !IsValid( flashlight ) ) then return end
if ( flashlight:GetColor() == self:GetColor() ) then return end

if ( string.FromColor( self.flashlight:GetColor() ) != string.FromColor( self:GetColor() ) ) then
self.flashlight:SetColor( self:GetColor() )
self:UpdateLight()
end
flashlight:SetColor( self:GetColor() )
self:UpdateLight()

end

Expand Down Expand Up @@ -200,16 +197,16 @@ function ENT:GetOverlayText()
end

local matLight = Material( "sprites/light_ignorez" )
local colLight = Color( 255, 255, 255, 255 )
--local matBeam = Material( "effects/lamp_beam" )
function ENT:DrawEffects()

-- No glow if we're not switched on!
if ( !self:GetOn() ) then return end

local lightInfo = self:GetLightInfo()

local LightPos = self:LocalToWorld( lightInfo.Offset )
local LightNrm = self:LocalToWorldAngles( lightInfo.Angle ):Forward()

-- glow sprite
--[[
Expand All @@ -224,29 +221,32 @@ function ENT:DrawEffects()
render.EndBeam()
--]]

local ViewNormal = self:GetPos() - EyePos()
local Distance = ViewNormal:Length()
ViewNormal:Normalize()
local ViewDot = ViewNormal:Dot( LightNrm * -1 )
local Visibile = util.PixelVisible( LightPos, 16, self.PixVis )
if ( !Visibile ) then return end

if ( ViewDot >= 0 ) then
local ViewNormal = EyePos()
ViewNormal:Negate()
ViewNormal:Add( LightPos )

local Distance = ViewNormal:Length()

render.SetMaterial( matLight )
local Visibile = util.PixelVisible( LightPos, 16, self.PixVis )
local LightNrm = self:LocalToWorldAngles( lightInfo.Angle ):Forward()
local ViewDot = -ViewNormal:Dot( LightNrm ) / Distance
if ( ViewDot < 0 ) then return end

if ( !Visibile ) then return end
local Size = math.Clamp( Distance * Visibile * ViewDot * lightInfo.Scale, 64, 512 )
Distance = math.Clamp( Distance, 32, 800 )
local Alpha = math.Clamp( ( 1000 - Distance ) * Visibile * ViewDot, 0, 100 )

local Size = math.Clamp( Distance * Visibile * ViewDot * lightInfo.Scale, 64, 512 )
render.SetMaterial( matLight )

Distance = math.Clamp( Distance, 32, 800 )
local Alpha = math.Clamp( ( 1000 - Distance ) * Visibile * ViewDot, 0, 100 )
local Col = self:GetColor()
Col.a = Alpha
local r, g, b = self:GetColor4Part()
colLight:SetUnpacked( r, g, b, Alpha )

render.DrawSprite( LightPos, Size, Size, Col )
render.DrawSprite( LightPos, Size * 0.4, Size * 0.4, Color( 255, 255, 255, Alpha ) )
render.DrawSprite( LightPos, Size, Size, colLight )

end
colLight:SetUnpacked( 255, 255, 255, Alpha )
render.DrawSprite( LightPos, Size * 0.4, Size * 0.4, colLight )

end

Expand Down
Loading