summaryrefslogtreecommitdiff
path: root/gamemode/player_class/player_base.lua
blob: 02733a87dc80dad0d8d23a10cf6e3f6710c0b4d5 (plain)
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
AddCSLuaFile()

DEFINE_BASECLASS( "player_default" )

local PLAYER = {}

function PLAYER:GetHandsModel()

	return { model = "models/weapons/c_arms_cstrike.mdl", skin = 1, body = "0100000" }

end

function PLAYER:HandlePlayerJumping( velocity )

	if ( self.Player:GetMoveType() == MOVETYPE_NOCLIP ) then
		self.Player.m_bJumping = false;
		return
	end

	-- airwalk more like hl2mp, we airwalk until we have 0 velocity, then it's the jump animation
	-- underwater we're alright we airwalking
	if ( !self.Player.m_bJumping && !self.Player:OnGround() && self.Player:WaterLevel() <= 0 ) then

		if ( !self.Player.m_fGroundTime ) then

			self.Player.m_fGroundTime = CurTime()

		elseif (CurTime() - self.Player.m_fGroundTime) > 0 && velocity:Length2D() < 0.5 then

			self.Player.m_bJumping = true
			self.Player.m_bFirstJumpFrame = false
			self.Player.m_flJumpStartTime = 0

		end
	end

	if self.Player.m_bJumping then

		if self.Player.m_bFirstJumpFrame then

			self.Player.m_bFirstJumpFrame = false
			self.Player:AnimRestartMainSequence()

		end

		if ( self.Player:WaterLevel() >= 2 ) ||	( (CurTime() - self.Player.m_flJumpStartTime) > 0.2 && self.Player:OnGround() ) then

			self.Player.m_bJumping = false
			self.Player.m_fGroundTime = nil
			self.Player:AnimRestartMainSequence()

		end

		if self.Player.m_bJumping then
			self.Player.CalcIdeal = ACT_MP_JUMP
			return true
		end
	end

	return false
end

function PLAYER:HandlePlayerDucking( velocity )

	if ( !self.Player:Crouching() ) then return false end

	if ( velocity:Length2D() > 0.5 ) then
		self.Player.CalcIdeal = ACT_MP_CROUCHWALK
	else
		self.Player.CalcIdeal = ACT_MP_CROUCH_IDLE
	end

	return true

end

function PLAYER:HandlePlayerNoClipping( velocity )

	if ( self.Player:InVehicle() ) then return end

	if ( self.Player:GetMoveType() != MOVETYPE_NOCLIP ) then

		if ( self.Player.m_bWasNoclipping ) then

			self.Player.m_bWasNoclipping = nil
			self.Player:AnimResetGestureSlot( GESTURE_SLOT_CUSTOM )
			if ( CLIENT ) then self.Player:SetIK( true ); end

		end

		return

	end

	if ( !self.Player.m_bWasNoclipping ) then

		self.Player:AnimRestartGesture( GESTURE_SLOT_CUSTOM, ACT_GMOD_NOCLIP_LAYER, false )
		if ( CLIENT ) then self.Player:SetIK( false ); end

	end


	return true

end

function PLAYER:HandlePlayerVaulting( velocity )

	if ( velocity:Length() < 1000 ) then return end
	if ( self.Player:IsOnGround() ) then return end

	self.Player.CalcIdeal = ACT_MP_SWIM
	return true

end

function PLAYER:HandlePlayerSwimming( velocity )

	if ( self.Player:WaterLevel() < 2 ) then
		self.Player.m_bInSwim = false
		return false
	end

	if ( velocity:Length2D() > 10 ) then
		self.Player.CalcIdeal = ACT_MP_SWIM
	else
		self.Player.CalcIdeal = ACT_MP_SWIM_IDLE
	end

	self.Player.m_bInSwim = true

	return true

end

function PLAYER:HandlePlayerLanding( velocity, WasOnGround )

	if ( self.Player:GetMoveType() == MOVETYPE_NOCLIP ) then return end

	if ( self.Player:IsOnGround() && !WasOnGround ) then
		self.Player:AnimRestartGesture( GESTURE_SLOT_JUMP, ACT_LAND, true );
	end

end

function PLAYER:HandlePlayerDriving()

	return false

end

function PLAYER:UpdateAnimation( velocity, maxseqgroundspeed )

	local len = velocity:Length()
	local movement = 1.0

	if ( len > 0.2 ) then
			movement =  ( len / maxseqgroundspeed )
	end

	rate = math.min( movement, 2 )

	-- if we're under water we want to constantly be swimming..
	if ( self.Player:WaterLevel() >= 2 ) then
		rate = math.max( rate, 0.5 )
	elseif ( !self.Player:IsOnGround() && len >= 1000 ) then
		rate = 0.1;
	end

	self.Player:SetPlaybackRate( rate )

	if ( CLIENT ) then
		self:GrabEarAnimation()
		self:MouthMoveAnimation()
	end

end

function PLAYER:GrabEarAnimation()

	self.Player.ChatGestureWeight = self.Player.ChatGestureWeight or 0

	-- Don't show this when we're playing a taunt!
	if ( self.Player:IsPlayingTaunt() ) then return end

	if ( self.Player:IsTyping() ) then
		self.Player.ChatGestureWeight = math.Approach( self.Player.ChatGestureWeight, 1, FrameTime() * 5.0 );
	else
		self.Player.ChatGestureWeight = math.Approach( self.Player.ChatGestureWeight, 0, FrameTime()  * 5.0 );
	end

	if ( self.Player.ChatGestureWeight > 0 ) then

		self.Player:AnimRestartGesture( GESTURE_SLOT_VCD, ACT_GMOD_IN_CHAT, true )
		self.Player:AnimSetGestureWeight( GESTURE_SLOT_VCD, self.Player.ChatGestureWeight )

	end

end

function PLAYER:MouthMoveAnimation()

	local FlexNum = self.Player:GetFlexNum() - 1
	if ( FlexNum <= 0 ) then return end

	for i=0, FlexNum-1 do

		local Name = self.Player:GetFlexName( i )

		if ( Name == "jaw_drop" || Name == "right_part" || Name == "left_part" || Name == "right_mouth_drop" || Name == "left_mouth_drop"  ) then

			if ( self.Player:IsSpeaking() ) then
				self.Player:SetFlexWeight( i, math.Clamp( self.Player:VoiceVolume() * 2, 0, 2 ) )
			else
				self.Player:SetFlexWeight( i, 0 )
			end
		end

	end

end

function PLAYER:CalcMainActivity( velocity )

	self.Player.CalcIdeal = ACT_MP_STAND_IDLE
	self.Player.CalcSeqOverride = -1

	self:HandlePlayerLanding( velocity, self.Player.m_bWasOnGround );

	if ( self:HandlePlayerNoClipping( velocity ) ||
		self:HandlePlayerDriving() ||
		self:HandlePlayerVaulting( velocity ) ||
		self:HandlePlayerJumping( velocity ) ||
		self:HandlePlayerDucking( velocity ) ||
		self:HandlePlayerSwimming( velocity ) ) then

	else

		local len2d = velocity:Length2D()
		if ( len2d > 150 ) then self.Player.CalcIdeal = ACT_MP_RUN elseif ( len2d > 0.5 ) then self.Player.CalcIdeal = ACT_MP_WALK end

	end

	-- a bit of a hack because we're missing ACTs for a couple holdtypes
	local weapon = self.Player:GetActiveWeapon()
	local ht = "pistol"

	if ( IsValid( weapon ) ) then ht = weapon:GetHoldType() end

	if ( self.Player.CalcIdeal == ACT_MP_CROUCH_IDLE &&	( ht == "knife" || ht == "melee2" ) ) then
		self.Player.CalcSeqOverride = self.Player:LookupSequence("cidle_" .. ht)
	end

	self.Player.m_bWasOnGround = self.Player:IsOnGround()
	self.Player.m_bWasNoclipping = (self.Player:GetMoveType() == MOVETYPE_NOCLIP)

	return self.Player.CalcIdeal, self.Player.CalcSeqOverride

end

local IdleActivity = ACT_HL2MP_IDLE
local IdleActivityTranslate = {}
	IdleActivityTranslate [ ACT_MP_STAND_IDLE ] 				= IdleActivity
	IdleActivityTranslate [ ACT_MP_WALK ] 						= IdleActivity+1
	IdleActivityTranslate [ ACT_MP_RUN ] 						= IdleActivity+2
	IdleActivityTranslate [ ACT_MP_CROUCH_IDLE ] 				= IdleActivity+3
	IdleActivityTranslate [ ACT_MP_CROUCHWALK ] 				= IdleActivity+4
	IdleActivityTranslate [ ACT_MP_ATTACK_STAND_PRIMARYFIRE ] 	= IdleActivity+5
	IdleActivityTranslate [ ACT_MP_ATTACK_CROUCH_PRIMARYFIRE ]	= IdleActivity+5
	IdleActivityTranslate [ ACT_MP_RELOAD_STAND ]		 		= IdleActivity+6
	IdleActivityTranslate [ ACT_MP_RELOAD_CROUCH ]		 		= IdleActivity+6
	IdleActivityTranslate [ ACT_MP_JUMP ] 						= ACT_HL2MP_JUMP_SLAM
	IdleActivityTranslate [ ACT_MP_SWIM_IDLE ] 					= ACT_MP_SWIM_IDLE
	IdleActivityTranslate [ ACT_MP_SWIM ] 						= ACT_MP_SWIM
	IdleActivityTranslate [ ACT_LAND ] 							= ACT_LAND

function PLAYER:TranslateActivity( act )

	local act = act
	local newact = self.Player:TranslateWeaponActivity( act )

	-- select idle anims if the weapon didn't decide
	if ( act == newact ) then
		return IdleActivityTranslate[ act ]
	end

	return newact

end

function PLAYER:DoAnimationEvent( event, data )
	if event == PLAYERANIMEVENT_ATTACK_PRIMARY then

		if self.Player:Crouching() then
			self.Player:AnimRestartGesture( GESTURE_SLOT_ATTACK_AND_RELOAD, ACT_MP_ATTACK_CROUCH_PRIMARYFIRE, true )
		else
			self.Player:AnimRestartGesture( GESTURE_SLOT_ATTACK_AND_RELOAD, ACT_MP_ATTACK_STAND_PRIMARYFIRE, true )
		end

		return ACT_VM_PRIMARYATTACK

	elseif event == PLAYERANIMEVENT_ATTACK_SECONDARY then

		-- there is no gesture, so just fire off the VM event
		return ACT_VM_SECONDARYATTACK

	elseif event == PLAYERANIMEVENT_RELOAD then

		if self.Player:Crouching() then
			self.Player:AnimRestartGesture( GESTURE_SLOT_ATTACK_AND_RELOAD, ACT_MP_RELOAD_CROUCH, true )
		else
			self.Player:AnimRestartGesture( GESTURE_SLOT_ATTACK_AND_RELOAD, ACT_MP_RELOAD_STAND, true )
		end

		return ACT_INVALID

	elseif event == PLAYERANIMEVENT_JUMP then

		self.Player.m_bJumping = true
		self.Player.m_bFirstJumpFrame = true
		self.Player.m_flJumpStartTime = CurTime()

		self.Player:AnimRestartMainSequence()

		return ACT_INVALID

	elseif event == PLAYERANIMEVENT_CANCEL_RELOAD then

		self.Player:AnimResetGestureSlot( GESTURE_SLOT_ATTACK_AND_RELOAD )

		return ACT_INVALID
	end

	return nil
end

player_manager.RegisterClass( "player_base", PLAYER, "player_default" )