/
gmod_cameraprop.lua
207 lines (127 loc) · 3.82 KB
/
gmod_cameraprop.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
AddCSLuaFile()
if ( CLIENT ) then
CreateConVar( "cl_drawcameras", "1", 0, "Should the cameras be visible?" )
end
ENT.Type = "anim"
ENT.PrintName = "Camera"
ENT.RenderGroup = RENDERGROUP_BOTH
local CAMERA_MODEL = Model( "models/dav0r/camera.mdl" )
function ENT:SetupDataTables()
self:NetworkVar( "Int", 0, "Key" )
self:NetworkVar( "Bool", 0, "On" )
self:NetworkVar( "Vector", 0, "vecTrack" )
self:NetworkVar( "Entity", 0, "entTrack" )
self:NetworkVar( "Entity", 1, "Player" )
end
function ENT:Initialize()
if ( SERVER ) then
self:SetModel( CAMERA_MODEL )
self:PhysicsInit( SOLID_VPHYSICS )
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
self:DrawShadow( false )
-- Don't collide with the player
self:SetCollisionGroup( COLLISION_GROUP_WEAPON )
local phys = self:GetPhysicsObject()
if ( IsValid( phys ) ) then
phys:Sleep()
end
end
end
function ENT:SetTracking( Ent, LPos )
if ( IsValid( Ent ) ) then
self:SetMoveType( MOVETYPE_NONE )
self:SetSolid( SOLID_BBOX )
else
self:SetMoveType( MOVETYPE_VPHYSICS )
self:SetSolid( SOLID_VPHYSICS )
end
self:NextThink( CurTime() )
self:SetvecTrack( LPos )
self:SetentTrack( Ent )
end
function ENT:SetLocked( locked )
if ( locked == 1 ) then
self.PhysgunDisabled = true
self:SetMoveType( MOVETYPE_NONE )
self:SetSolid( SOLID_BBOX )
self:SetCollisionGroup( COLLISION_GROUP_WORLD )
else
self.PhysgunDisabled = false
end
self.locked = locked
end
function ENT:OnTakeDamage( dmginfo )
if ( self.locked ) then return end
self:TakePhysicsDamage( dmginfo )
end
function ENT:OnRemove()
if ( IsValid( self.UsingPlayer ) ) then
self.UsingPlayer:SetViewEntity( self.UsingPlayer )
end
end
if ( SERVER ) then
numpad.Register( "Camera_On", function ( pl, ent )
if ( !IsValid( ent ) ) then return false end
pl:SetViewEntity( ent )
pl.UsingCamera = ent
ent.UsingPlayer = pl
end )
numpad.Register( "Camera_Toggle", function ( pl, ent, idx, buttoned )
-- The camera was deleted or something - return false to remove this entry
if ( !IsValid( ent ) ) then return false end
if ( !IsValid( pl ) ) then return false end
-- Something else changed players view entity
if ( pl.UsingCamera && pl.UsingCamera == ent && pl:GetViewEntity() != ent ) then
pl.UsingCamera = nil
ent.UsingPlayer = nil
end
if ( pl.UsingCamera && pl.UsingCamera == ent ) then
pl:SetViewEntity( pl )
pl.UsingCamera = nil
ent.UsingPlayer = nil
else
pl:SetViewEntity( ent )
pl.UsingCamera = ent
ent.UsingPlayer = pl
end
end )
numpad.Register( "Camera_Off", function( pl, ent )
if ( !IsValid( ent ) ) then return false end
if ( pl.UsingCamera && pl.UsingCamera == ent ) then
pl:SetViewEntity( pl )
pl.UsingCamera = nil
ent.UsingPlayer = nil
end
end )
end
function ENT:Think()
if ( CLIENT ) then
self:TrackEntity( self:GetentTrack(), self:GetvecTrack() )
end
end
function ENT:TrackEntity( ent, lpos )
if ( !IsValid( ent ) ) then return end
local WPos = ent:LocalToWorld( lpos )
if ( ent:IsPlayer() ) then
WPos = WPos + ent:GetViewOffset() * 0.85
end
local CamPos = self:GetPos()
local Ang = WPos - CamPos
Ang = Ang:Angle()
self:SetAngles( Ang )
end
function ENT:CanTool( ply, trace, mode )
if ( self:GetMoveType() == MOVETYPE_NONE ) then return false end
return true
end
function ENT:Draw()
if ( GetConVarNumber( "cl_drawcameras" ) == 0 ) then return end
-- Don't draw the camera if we're taking pics
local ply = LocalPlayer()
local wep = ply:GetActiveWeapon()
if ( IsValid( wep ) ) then
if ( wep:GetClass() == "gmod_camera" ) then return end
end
self:DrawModel()
end