Skip to content

Commit

Permalink
fix: increase speed when player got hit until round end
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalle Minkner committed Dec 26, 2024
1 parent b7401e7 commit 6464b62
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/RollTheDice+DiceIncreaseSpeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ namespace RollTheDice
public partial class RollTheDice : BasePlugin
{
private List<CCSPlayerController> _playersWithIncreasedSpeed = new();
private Dictionary<CCSPlayerController, float> _playersWithIncreasedSpeedValue = new();

private Dictionary<string, string> DiceIncreaseSpeed(CCSPlayerController player, CCSPlayerPawn playerPawn)
{
_playersWithIncreasedSpeed.Add(player);
var speedIncrease = _random.NextDouble() * (2.0 - 1.5) + 1.5;
playerPawn.VelocityModifier *= (float)speedIncrease;
_playersWithIncreasedSpeedValue[player] = (float)playerPawn.VelocityModifier;
Utilities.SetStateChanged(playerPawn, "CCSPlayerPawn", "m_flVelocityModifier");
var percentageIncrease = (speedIncrease - 1.0) * 100;
return new Dictionary<string, string>
Expand All @@ -37,6 +39,33 @@ private void ResetDiceIncreaseSpeed()
Utilities.SetStateChanged(playerPawn, "CCSPlayerPawn", "m_flVelocityModifier");
}
_playersWithIncreasedSpeed.Clear();
_playersWithIncreasedSpeedValue.Clear();
}

private void CreateDiceIncreaseSpeedEventHandler()
{
RegisterEventHandler<EventPlayerHurt>(EventDiceIncreaseSpeedOnPlayerHurt);
}

private void RemoveDDiceIncreaseSpeedEventHandler()
{
DeregisterEventHandler<EventPlayerHurt>(EventDiceIncreaseSpeedOnPlayerHurt);
}

private HookResult EventDiceIncreaseSpeedOnPlayerHurt(EventPlayerHurt @event, GameEventInfo info)
{
var victim = @event.Userid;
// ignore if player is not in the list
if (!_playersWithIncreasedSpeed.Contains(victim)) return HookResult.Continue;
// check if player is valid
if (victim == null || victim.PlayerPawn == null || !victim.PlayerPawn.IsValid || victim.PlayerPawn.Value == null || victim.LifeState != (byte)LifeState_t.LIFE_ALIVE) return HookResult.Continue;
// get player pawn
var playerPawn = victim.PlayerPawn.Value!;
// set player speed
playerPawn.VelocityModifier = _playersWithIncreasedSpeedValue[victim];
// set state changed
Utilities.SetStateChanged(playerPawn, "CCSPlayerPawn", "m_flVelocityModifier");
return HookResult.Continue;
}
}
}
2 changes: 2 additions & 0 deletions src/RollTheDice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public override void Load(bool hotReload)
CreateDicePlayerDisguiseAsPlantEventHandler();
CreateDicePlayerRespawnEventHandler();
CreateDiceBigTaserBatteryEventHandler();
CreateDiceIncreaseSpeedEventHandler();
// print message if hot reload
if (hotReload)
{
Expand Down Expand Up @@ -67,6 +68,7 @@ public override void Unload(bool hotReload)
RemoveDiceBigTaserBatteryListeners();
RemoveDicePlayerCloakListeners();
RemoveDiceNoExplosivesEventHandler();
RemoveDDiceIncreaseSpeedEventHandler();
Console.WriteLine(Localizer["core.unload"]);
}

Expand Down

0 comments on commit 6464b62

Please sign in to comment.