-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble_jump.nut
365 lines (304 loc) · 11.8 KB
/
double_jump.nut
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
355
356
357
358
359
360
361
362
363
364
365
if (!Entities.FindByName(null, "doublejump_timer")) {
SpawnEntityFromTable("logic_timer", {
targetname = "doublejump_timer",
RefireTime = 0.05,
OnTimer = "!self,RunScriptCode,RespireDoubleJump.Think()"
});
}
if (!Entities.FindByName(null, "doublejump_cleanup_timer")) {
SpawnEntityFromTable("logic_timer", {
targetname = "doublejump_cleanup_timer",
RefireTime = 30.0,
OnTimer = "!self,RunScriptCode,RespireDoubleJump.PeriodicCleanup()"
});
}
::RespireDoubleJump <- {
Config = {
SOUND = {
JUMP = "player/jumplanding_zombie.wav"
},
JUMP_SETTINGS = {
BASE_FORCE = 270,
MIN_FORCE = 200,
MAX_FORCE = 350,
HEALTH_SCALING = false,
MAX_JUMPS = 1
},
TIMING = {
THINK_INTERVAL = 0.05,
CLEANUP_INTERVAL = 30.0,
JUMP_DELAY = 0.05
},
REQUIREMENTS = {
MIN_HEALTH = 15,
ALLOW_WHILE_CROUCHED = false
},
DEBUG = {
ENABLED = false
},
CONSTANTS = {
IN_JUMP = 2,
INVALID_GROUND_ENTITY = -1
}
},
PLAYER_STATE = {},
AIRBORNE_PLAYERS = {},
function Log(message, level = "DEBUG") {
if (this.Config.DEBUG.ENABLED || level != "DEBUG") {
printl("[Double Jump " + level + "] " + message);
}
},
function OnGameEvent_player_jump(params) {
this.HandlePlayerJump(params.userid);
},
function OnGameEvent_player_falldamage(params) {
this.HandlePlayerLand(params.userid);
},
function OnGameEvent_player_spawn(params) {
local player = GetPlayerFromUserID(params.userid);
if (player && player.IsSurvivor()) {
this.InitPlayerState(params.userid);
}
},
function OnGameEvent_player_death(params) {
if ("userid" in params) {
local userID = params.userid;
local player = GetPlayerFromUserID(userID);
if (player && player.IsSurvivor()) {
this.CleanupPlayerState(userID);
this.Log("Cleaned up state for dead survivor: " + userID);
}
}
},
function InitPlayerState(userID) {
this.PLAYER_STATE[userID] <- {
canDoubleJump = true,
hasDoubleJumped = false,
initialJumpTime = Time(),
lastJumpButtonState = false,
currentJumpButtonState = false,
loggedGroundStatus = false,
lastActivityTime = Time(),
jumpCount = 0
};
this.Log("UserID's State Initialized: " + userID);
}
function CleanupPlayerState(userID) {
if (userID in this.PLAYER_STATE) {
delete this.PLAYER_STATE[userID];
}
if (userID in this.AIRBORNE_PLAYERS) {
delete this.AIRBORNE_PLAYERS[userID];
}
this.Log("Player state cleaned up: " + userID);
},
function ResetPlayerState(userID) {
local player = GetPlayerFromUserID(userID);
if (this.IsValidPlayer(player)) {
this.PLAYER_STATE[userID] = {
canDoubleJump = true,
hasDoubleJumped = false,
initialJumpTime = 0,
lastJumpButtonState = false,
currentJumpButtonState = false,
loggedGroundStatus = true,
lastActivityTime = Time(),
jumpCount = 0
};
this.Log("Reset state for inactive player: " + userID);
} else {
this.CleanupPlayerState(userID);
}
},
function HandlePlayerJump(userID) {
local player = GetPlayerFromUserID(userID);
if (!player || !player.IsSurvivor()) {
this.Log("Invalid player or not a survivor in [HandlePlayerJump] for userID: " + userID, "WARNING");
return;
}
if (!(userID in this.PLAYER_STATE)) {
this.InitPlayerState(userID);
}
local state = this.PLAYER_STATE[userID];
state.initialJumpTime = Time();
state.canDoubleJump = player.GetHealth() > this.Config.REQUIREMENTS.MIN_HEALTH;
state.hasDoubleJumped = false;
state.loggedGroundStatus = false;
state.lastActivityTime = Time();
this.AIRBORNE_PLAYERS[userID] <- true;
this.Log("Player jumped, set state to airborne: " + userID);
},
function HandlePlayerLand(userID) {
local player = GetPlayerFromUserID(userID);
if (!this.IsValidPlayer(player)) {
this.Log("Invalid player in [HandlePlayerLand] for userID: " + userID, "WARNING");
this.CleanupPlayerState(userID);
return;
}
if (userID in this.AIRBORNE_PLAYERS) {
delete this.AIRBORNE_PLAYERS[userID];
if (userID in this.PLAYER_STATE) {
this.PLAYER_STATE[userID].lastActivityTime = Time();
this.PLAYER_STATE[userID].canDoubleJump = true;
this.PLAYER_STATE[userID].hasDoubleJumped = false;
}
this.Log("Player landed, removed state for airborne: " + userID);
}
},
function HandleDoubleJump(player) {
try {
if (!this.IsValidPlayer(player)) return;
local velocity = player.GetVelocity();
local currentHealth = player.GetHealth();
local adjustedForce = this.Config.JUMP_SETTINGS.BASE_FORCE;
if (this.Config.JUMP_SETTINGS.HEALTH_SCALING) {
local healthPercentage = currentHealth / player.GetMaxHealth();
local forceRange = this.Config.JUMP_SETTINGS.MAX_FORCE - this.Config.JUMP_SETTINGS.MIN_FORCE;
adjustedForce = this.Config.JUMP_SETTINGS.MIN_FORCE + (forceRange * healthPercentage);
adjustedForce = max(this.Config.JUMP_SETTINGS.MIN_FORCE,
min(this.Config.JUMP_SETTINGS.MAX_FORCE, adjustedForce));
this.Log("Health-scaled jump force: " + adjustedForce + " (Health: " + currentHealth + ")", "DEBUG");
}
velocity.z = adjustedForce;
player.SetVelocity(velocity);
NetProps.SetPropFloat(player, "localdata.m_Local.m_flFallVelocity", 0.0);
if (!EmitSoundOn(this.Config.SOUND.JUMP, player)) {
this.Log("Failed to play double jump sound", "WARNING");
}
this.Log("Double Jump executed by player: " + player.GetPlayerName() + " with force: " + adjustedForce);
} catch (error) {
this.Log("Error in HandleDoubleJump: " + error, "ERROR");
}
}
function IsPlayerMidair(player) {
return NetProps.GetPropInt(player, "m_hGroundEntity") == this.Config.CONSTANTS.INVALID_GROUND_ENTITY;
},
function Think() {
local currentTime = Time();
local playersToRemove = [];
local playersToUpdate = {};
foreach (userID, _ in this.AIRBORNE_PLAYERS) {
if (!(userID in this.PLAYER_STATE)) {
this.InitPlayerState(userID);
continue;
}
local player = GetPlayerFromUserID(userID);
if (!this.IsValidPlayer(player)) {
playersToRemove.append(userID);
continue;
}
playersToUpdate[userID] <- {
player = player,
state = this.PLAYER_STATE[userID],
buttons = player.GetButtonMask(),
onGround = !this.IsPlayerMidair(player)
};
}
foreach (userID, data in playersToUpdate) {
local state = data.state;
if (!("jumpCount" in state)) {
state.jumpCount <- 0;
}
state.currentJumpButtonState = (data.buttons & this.Config.CONSTANTS.IN_JUMP) != 0;
if (data.onGround) {
if (!state.loggedGroundStatus) {
state.canDoubleJump = data.player.GetHealth() > this.Config.REQUIREMENTS.MIN_HEALTH;
state.hasDoubleJumped = false;
state.jumpCount = 0;
this.Log("Player is on ground, double jump resets: " + userID);
state.loggedGroundStatus = true;
}
playersToRemove.append(userID);
} else {
state.loggedGroundStatus = false;
if (this.CheckDoubleJumpConditions(state, currentTime)) {
this.HandleDoubleJump(data.player);
state.hasDoubleJumped = true;
state.jumpCount++;
this.Log("Player has double jumped: " + userID + " (Jump count: " + state.jumpCount + ")");
}
}
state.lastJumpButtonState = state.currentJumpButtonState;
state.lastActivityTime = currentTime;
}
foreach (userID in playersToRemove) {
delete this.AIRBORNE_PLAYERS[userID];
}
},
function CheckDoubleJumpConditions(state, currentTime) {
if (currentTime - state.initialJumpTime <= this.Config.TIMING.JUMP_DELAY) return false;
return state.canDoubleJump &&
!state.hasDoubleJumped &&
state.currentJumpButtonState &&
!state.lastJumpButtonState;
},
function IsValidPlayer(player) {
return player &&
player.IsValid() &&
player.IsSurvivor() &&
player.GetHealth() > 0 &&
!player.IsDead() &&
!player.IsDying() &&
!player.IsIncapacitated();
},
function PeriodicCleanup() {
local currentTime = Time();
local playersToReset = [];
foreach (userID, state in this.PLAYER_STATE) {
local player = GetPlayerFromUserID(userID);
if (!this.IsValidPlayer(player)) {
this.CleanupPlayerState(userID);
this.Log("Removed state for invalid player: " + userID);
} else if (currentTime - state.lastActivityTime > this.Config.TIMING.CLEANUP_INTERVAL) {
if (!(userID in this.AIRBORNE_PLAYERS)) {
playersToReset.append(userID);
}
}
}
foreach (userID in playersToReset) {
this.ResetPlayerState(userID);
}
this.Log("Periodic cleanup completed. Reset " + playersToReset.len() + " inactive players.");
},
// Compatibility for my Dashing script. If you see this, this is yet to release.
function DashingConnection(userID) {
if (!(userID in this.PLAYER_STATE)) {
this.InitPlayerState(userID);
}
local state = this.PLAYER_STATE[userID];
state.initialJumpTime = Time();
state.lastActivityTime = Time();
this.AIRBORNE_PLAYERS[userID] <- true;
this.Log("Player dashed, updated activity time: " + userID);
},
}
function UpdateConfig(newSettings) {
foreach (category, values in newSettings) {
if (category in RespireDoubleJump.Config) {
foreach (key, value in values) {
if (key in RespireDoubleJump.Config[category]) {
RespireDoubleJump.Config[category][key] = value;
}
}
}
}
}
::RespireDoubleJump.Performance <- {
thinkTime = 0,
lastThinkDuration = 0,
totalThinkCalls = 0,
function TrackThinkPerformance() {
local startTime = Time();
if (this.thinkTime > 0) {
this.lastThinkDuration = startTime - this.thinkTime;
this.totalThinkCalls++;
}
this.thinkTime = startTime;
}
}
RespireDoubleJump.Init <- function () {
PrecacheSound(this.Config.SOUND.JUMP);
__CollectEventCallbacks(this, "OnGameEvent_", "GameEventCallbacks", RegisterScriptGameEventListener);
this.Log("Respire's Double Jump v2 Loaded", "INFO");
}
RespireDoubleJump.Init();