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
|
local Camdist = 60 --Dont change this
local Camcdist = 60 --Dont change this
local CamMin = 20
local CamMax = 200
local Campos = Vector( 0 , 0 , 0 )
local Aimpos = Vector( 0 , 0 , 0 )
local AimVect = Vector( 0 , 0 , 0 )
local RPos = Campos
local CamEnt = NULL
local DefDCam = nil
local EnabledFT = true
local CamZNear = 1
local CamZFar = nil
local CamKey = KEY_C
local CamHeight = 0
local NewFunc = nil
--I was wondering if I should use the vgui functions or the bindpress. The advantage is that the vgui one works even when your mouse is out.
--However, it might get annoying when you are trying to scroll a scrollbar. ;P
vgui.GetWorldPanel():SetWorldClicker(false)
hook.Add("PlayerBindPress","CameraScroll",function(ply,bind,pressed)
self = GM or GAMEMODE
if (EnabledFT and (!CamKey or input.IsKeyDown(CamKey))) then
if (bind == "invnext") then
self:AddCameraDistance(10)
return true
end
if (bind == "invprev") then
self:AddCameraDistance(-10)
return true
end
end
end)
function GM:SetCameraKey(KEY)
CamKey = KEY
end
function GM:SetCameraDistance(dist)
if (dist > CamMax) then dist = CamMax end
if (dist < CamMin) then dist = CamMin end
if (Camdist != dist and LocalPlayer():Alive()) then
if (dist == CamMin) then OnCameraModeSwitch(true)
elseif (Camdist == CamMin) then OnCameraModeSwitch(false) end
end
Camdist = dist
end
function GM:AddCameraDistance(add)
local dist = Camdist + add
if (dist > CamMax) then dist = CamMax end
if (dist < CamMin) then dist = CamMin end
if (Camdist != dist and LocalPlayer():Alive()) then
if (dist == CamMin) then self:OnCameraModeSwitch(true)
elseif (Camdist == CamMin) then self:OnCameraModeSwitch(false) end
end
Camdist = dist
end
function GM:SetEnableThirdPerson(bool)
EnabledFT = bool
end
function IsFirstPerson()
return (Camdist <= CamMin or !EnabledFT)
end
function GM:OnCameraModeSwitch(bFirstPerson)
end
function GetCameraPos()
return RPos
end
function SetCameraObject(Ent)
CamEnt = Ent
end
function SetDefaultDeathPos(Pos)
DefDCam = Pos
end
function SetCameraMinDistance(Dist)
CamMin = Dist
end
function SetCameraMaxDistance(Dist)
CamMax = Dist
end
function SetCameraZNear(Near)
CamZNear = Near
end
function SetCameraZFar(Far)
CamZFar = Far
end
function SetCameraOffsetHeight(H)
CamHeight=H
end
function OverrideDefaultGFCamera(Fun)
NewFunc = Fun
end
hook.Add("CalcView","View",function(ply, origin, angles, fov)
if (NewFunc) then
local Cal = NewFunc(ply, origin, angles, fov)
if (Cal) then return Cal end
end
local view = {}
AimPos = ply:GetAimVector()
AimVect = AimVect + (AimPos-AimVect)/2
view.znear = CamZNear
view.zfar = CamZFar
view.origin = origin
if (!ply:Alive()) then
if (DefDCam) then origin = DefDCam end
view.origin = origin + AimPos * -80
RPos = view.origin
return view
end
if (!EnabledFT) then return view end
if (Camdist > CamMin or IsValid(CamEnt)) then
Camcdist = Camcdist + (Camdist-Camcdist)/16
local Distance = Camcdist
Campos = ply:GetShootPos()
Campos.z = Campos.z
if (IsValid(CamEnt)) then Campos = CamEnt:GetPos() end
local Aimer = AimVect
local data = {}
data.start = Campos
data.endpos = Campos - Aimer * Distance
data.filter = ply
data.mask = MASK_SOLID_BRUSHONLY
data.mins = ply:OBBMaxs()/3*-1
data.maxs = ply:OBBMaxs()/3
local trace = util.TraceHull( data )
view.origin = trace.HitPos
view.angles = Aimer:Angle()
end
view.origin.z = view.origin.z + CamHeight
RPos = view.origin or origin
return view
end)
|