-
Notifications
You must be signed in to change notification settings - Fork 78
/
weapon_ttt_binoculars.lua
354 lines (279 loc) · 7.65 KB
/
weapon_ttt_binoculars.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
---
-- @class SWEP
-- @section weapon_ttt_binoculars
if SERVER then
AddCSLuaFile()
end
DEFINE_BASECLASS("weapon_tttbase")
SWEP.HoldType = "camera"
if CLIENT then
SWEP.PrintName = "binoc_name"
SWEP.Slot = 7
SWEP.ShowDefaultViewModel = false
SWEP.ShowDefaultWorldModel = false
SWEP.EquipMenuData = {
type = "item_weapon",
desc = "binoc_desc",
}
SWEP.Icon = "vgui/ttt/icon_binoc"
end
SWEP.Base = "weapon_tttbase"
SWEP.ViewModel = "models/weapons/v_crowbar.mdl"
SWEP.WorldModel = "models/Items/combine_rifle_cartridge01.mdl"
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"
SWEP.Primary.Delay = 1.0
SWEP.Primary.Cone = 0.075
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = true
SWEP.Secondary.Ammo = "none"
SWEP.Secondary.Delay = 0.2
SWEP.Kind = WEAPON_EQUIP2
SWEP.CanBuy = { ROLE_DETECTIVE } -- only detectives can buy
SWEP.WeaponID = AMMO_BINOCULARS
SWEP.builtin = true
SWEP.AllowDrop = true
SWEP.ZoomLevels = {
0,
30,
20,
10,
}
SWEP.ProcessingDelay = 5
SWEP.decayRate = 0.5
---
-- @ignore
function SWEP:SetupDataTables()
self:NetworkVar("Entity", 0, "ProcessTarget")
self:NetworkVar("Float", 0, "Progress")
self:NetworkVar("Float", 1, "AwayTime")
self:NetworkVar("Int", 0, "ZoomAmount")
return BaseClass.SetupDataTables(self)
end
---
-- @param Entity target The new processing target.
-- @realm shared
function SWEP:SetNewTarget(target)
self:SetProcessTarget(target)
self:SetProgress(0)
self:SetAwayTime(0)
end
---
-- @ignore
function SWEP:PrimaryAttack()
self:SetNextPrimaryFire(CurTime() + 0.1)
local corpse = self:GetTargetingCorpse()
if corpse == nil or self:GetProcessTarget() ~= NULL and self:GetProcessTarget() == corpse then
return
end
self:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
if SERVER then
self:SetNewTarget(corpse)
end
end
local click = Sound("weapons/sniper/sniper_zoomin.wav")
---
-- @ignore
function SWEP:SecondaryAttack()
self:SetNextSecondaryFire(CurTime() + self.Secondary.Delay)
if CLIENT and IsFirstTimePredicted() then
LocalPlayer():EmitSound(click)
end
self:CycleZoom()
end
---
-- @ignore
function SWEP:SetZoomLevel(level)
self:SetZoomAmount(level)
local owner = self:GetOwner()
if IsValid(owner) then
owner:SetFOV(self.ZoomLevels[level], 0.3)
end
end
---
-- @ignore
function SWEP:CycleZoom()
self:SetZoomAmount(self:GetZoomAmount() + 1)
if not self.ZoomLevels[self:GetZoomAmount()] then
self:SetZoomAmount(1)
end
self:SetZoomLevel(self:GetZoomAmount())
end
---
-- @ignore
function SWEP:PreDrop()
self:SetZoomLevel(1)
self:SetNewTarget(NULL)
return BaseClass.PreDrop(self)
end
---
-- @ignore
function SWEP:Holster()
self:SetZoomLevel(1)
self:SetNewTarget(NULL)
return true
end
---
-- @ignore
function SWEP:Deploy()
self:SetZoomLevel(1)
return BaseClass.Deploy(self)
end
---
-- @ignore
function SWEP:OnRemove()
BaseClass.OnRemove(self)
self:SetZoomLevel(1)
end
---
-- @ignore
function SWEP:Reload()
local playSound = false
if self:GetZoomAmount() > 1 or self:GetProgress() > 0 then
self:SetZoomLevel(1)
playSound = true
end
if CLIENT and IsFirstTimePredicted() and playSound then
LocalPlayer():EmitSound(click)
end
self:SetNewTarget(NULL)
return false
end
---
-- @return boolean
-- @realm shared
function SWEP:IsTargetingCorpse()
local ent = self:GetOwner():GetEyeTrace(MASK_SHOT).Entity
return IsValid(ent) and ent:IsPlayerRagdoll()
end
---
-- @return Entity
-- @realm shared
function SWEP:GetTargetingCorpse()
local ent = self:GetOwner():GetEyeTrace(MASK_SHOT).Entity
if IsValid(ent) and ent:IsPlayerRagdoll() then
return ent
end
end
local confirm = Sound("npc/turret_floor/click1.wav")
---
-- @realm shared
function SWEP:IdentifyCorpse()
local owner = self:GetOwner()
if SERVER then
local tr = owner:GetEyeTrace(MASK_SHOT)
CORPSE.ShowSearch(owner, tr.Entity, false, true)
elseif IsFirstTimePredicted() then
LocalPlayer():EmitSound(confirm)
end
end
---
-- @ignore
function SWEP:Think()
BaseClass.Think(self)
if self:GetProcessTarget() == NULL then
return
end
local tickDirection = engine.TickInterval()
local awayTime = self:GetAwayTime()
if self:GetTargetingCorpse() ~= self:GetProcessTarget() then
awayTime = awayTime + tickDirection
self:SetAwayTime(awayTime)
tickDirection = tickDirection * (-self.decayRate * awayTime)
else
self:SetAwayTime(0)
end
local newProgress = self:GetProgress() + tickDirection
self:SetProgress(newProgress)
if newProgress < 0 then
self:SetNewTarget(NULL)
elseif newProgress > self.ProcessingDelay then
self:IdentifyCorpse()
self:SetNewTarget(NULL)
end
end
if CLIENT then
local TryT = LANG.TryTranslation
local GetPT = LANG.GetParamTranslation
local mathRound = math.Round
local mathClamp = math.Clamp
local mathCeil = math.ceil
---
-- @ignore
function SWEP:Initialize()
self:AddTTT2HUDHelp("binoc_help_pri", "binoc_help_sec")
self:AddHUDHelpLine("binoc_help_reload", Key("+reload", "undefined_key"))
BaseClass.Initialize(self)
end
---
-- @realm client
function SWEP:InitializeCustomModels()
self:AddCustomWorldModel("wmodel", {
type = "Model",
model = "models/Items/combine_rifle_cartridge01.mdl",
bone = "ValveBiped.Bip01_R_Hand",
rel = "",
pos = Vector(4, 5, 0),
angle = Angle(0, 80, -20),
size = Vector(0.7, 0.7, 0.7),
color = Color(255, 255, 255, 255),
surpresslightning = false,
material = "",
skin = 0,
bodygroup = {},
})
end
---
-- @ignore
function SWEP:AdjustMouseSensitivity()
if self:GetZoomAmount() > 0 then
return 1 / self:GetZoomAmount()
end
return -1
end
hook.Add("TTTRenderEntityInfo", "HUDDrawTargetIDBinocular", function(tData)
local client = LocalPlayer()
local ent = tData:GetEntity()
if not IsValid(client) or not client:IsTerror() or not client:Alive() then
return
end
local c_wep = client:GetActiveWeapon()
if
not IsValid(ent)
or not IsValid(c_wep)
or not ent:IsPlayerRagdoll()
or c_wep:GetClass() ~= "weapon_ttt_binoculars"
or c_wep:GetProcessTarget() ~= ent
then
return
end
local progress =
mathRound(mathClamp((c_wep:GetProgress() / c_wep.ProcessingDelay) * 100, 0, 100))
tData:AddDescriptionLine(
GetPT("binoc_progress", { progress = progress }),
client.GetRoleColor and client:GetRoleColor() or roles.INNOCENT.color
)
end)
---
-- @ignore
function SWEP:DrawHUD()
BaseClass.DrawHUD(self)
local client = LocalPlayer()
local scale = appearance.GetGlobalScale()
local color = appearance.SelectFocusColor(
client.GetRoleColor and client:GetRoleColor() or roles.INNOCENT.color
)
draw.ShadowedText(
TryT("binoc_zoom_level") .. ": " .. self:GetZoomAmount(),
"TargetID_Description",
mathCeil(ScrW() * 0.5) + 45 * scale,
mathCeil(ScrH() * 0.5) - 35 * scale,
color,
TEXT_ALIGN_LEFT,
TEXT_ALIGN_CENTER
)
end
end