Skip to content

Commit

Permalink
refactor: convert existing coroutines to Tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
winstxnhdw committed Oct 14, 2024
1 parent 77f9145 commit bd64492
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 32 deletions.
14 changes: 5 additions & 9 deletions lc-hax/Scripts/Commands/Bomb/BombCommand.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
using System.Threading;
using System.Threading.Tasks;
using System;
using GameNetcodeStuff;

[Command("bomb")]
class BombCommand : ICommand, IJetpack {
Action BlowUpLocation(PlayerControllerB localPlayer, PlayerControllerB targetPlayer, JetpackItem jetpack) => () => {
localPlayer.DiscardHeldObject(placeObject: true, parentObjectTo: targetPlayer.NetworkObject);
Helper.ShortDelay(jetpack.ExplodeJetpackServerRpc);
};

public async Task Execute(string[] args, CancellationToken cancellationToken) {
if (Helper.LocalPlayer is not PlayerControllerB localPlayer) return;
if (args.Length is 0) {
Expand All @@ -32,8 +26,10 @@ public async Task Execute(string[] args, CancellationToken cancellationToken) {
return;
}

Helper.CreateComponent<WaitForBehaviour>("Throw Bomb")
.SetPredicate(() => localPlayer.ItemSlots[localPlayer.currentItemSlot] == jetpack)
.Init(this.BlowUpLocation(localPlayer, targetPlayer, jetpack));
GrabbableObject[] itemSlots = localPlayer.ItemSlots;
await Helper.WaitUntil(() => itemSlots[localPlayer.currentItemSlot] == jetpack, cancellationToken);
localPlayer.DiscardHeldObject(placeObject: true, parentObjectTo: targetPlayer.NetworkObject);
await Task.Delay(500, cancellationToken);
jetpack.ExplodeJetpackServerRpc();
}
}
5 changes: 2 additions & 3 deletions lc-hax/Scripts/Commands/FakeDeathCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ public async Task Execute(string[] args, CancellationToken cancellationToken) {
0
);

Helper.CreateComponent<WaitForBehaviour>("Respawn")
.SetPredicate(() => player.playersManager.shipIsLeaving)
.Init(player.KillPlayer);
await Helper.WaitUntil(() => player.playersManager.shipIsLeaving, cancellationToken);
player.KillPlayer();
}
}
15 changes: 7 additions & 8 deletions lc-hax/Scripts/Commands/MaskCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

[Command("mask")]
class MaskCommand : ICommand {
void SpawnMimicOnPlayer(PlayerControllerB player, HauntedMaskItem mask, ulong amount = 1) =>
Helper.CreateComponent<TransientBehaviour>("Mask").Init(_ => {
mask.CreateMimicServerRpc(player.isInsideFactory, player.transform.position);
}, amount * 0.1f, 0.1f);

public async Task Execute(string[] args, CancellationToken cancellationToken) {
if (Helper.LocalPlayer is not PlayerControllerB localPlayer) return;
if (Helper.Grabbables.First(grabbable => grabbable is HauntedMaskItem) is not HauntedMaskItem hauntedMaskItem) {
Expand All @@ -35,8 +30,12 @@ public async Task Execute(string[] args, CancellationToken cancellationToken) {
return;
}

Helper.CreateComponent<WaitForBehaviour>()
.SetPredicate(() => localPlayer.ItemSlots[localPlayer.currentItemSlot] is HauntedMaskItem)
.Init(() => this.SpawnMimicOnPlayer(targetPlayer, hauntedMaskItem, amount));
GrabbableObject[] itemSlots = localPlayer.ItemSlots;
await Helper.WaitUntil(() => localPlayer.ItemSlots[localPlayer.currentItemSlot] is HauntedMaskItem, cancellationToken);

for (ulong i = 0; i < amount; i++) {
hauntedMaskItem.CreateMimicServerRpc(targetPlayer.isInsideFactory, targetPlayer.transform.position);
await Task.Delay(100, cancellationToken);
}
}
}
31 changes: 19 additions & 12 deletions lc-hax/Scripts/Commands/Unlockable/HornCommand.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using System;

[Command("horn")]
class HornCommand : ICommand {
Action PullHornLater(int hornDuration) => () => {
ShipAlarmCord? shipAlarmCord = Helper.FindObject<ShipAlarmCord>();
shipAlarmCord?.PullCordServerRpc(-1);
Helper.CreateComponent<WaitForBehaviour>("Stop Pulling Horn")
.SetPredicate(time => time >= hornDuration)
.Init(() => shipAlarmCord?.StopPullingCordServerRpc(-1));
};

public async Task Execute(string[] args, CancellationToken cancellationToken) {
if (args.Length is 0) {
Chat.Print("Usage: horn <duration>");
return;
}

if (!int.TryParse(args[0], out int hornDuration)) {
if (!int.TryParse(args[0], out int hornDurationSeconds)) {
Chat.Print("Invalid duration!");
return;
}

Helper.BuyUnlockable(Unlockable.LOUD_HORN);
Helper.ReturnUnlockable(Unlockable.LOUD_HORN);
Helper.ShortDelay(this.PullHornLater(hornDuration));

if (await Helper.FindObject<ShipAlarmCord>(cancellationToken) is not ShipAlarmCord shipAlarmCord) {
return;
}

try {
shipAlarmCord.PullCordServerRpc(-1);
await Task.Delay(hornDurationSeconds * 1000, cancellationToken);
}

catch (Exception exception) {
throw exception;
}

finally {
shipAlarmCord.StopPullingCordServerRpc(-1);
}
}
}
13 changes: 13 additions & 0 deletions lc-hax/Scripts/Helpers/FindObjects.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;

static partial class Helper {
internal static T? FindObject<T>() where T : Component => Object.FindAnyObjectByType<T>();

internal static T[] FindObjects<T>(FindObjectsSortMode sortMode = FindObjectsSortMode.None) where T : Component =>
Object.FindObjectsByType<T>(sortMode);

internal static async Task<T?> FindObject<T>(CancellationToken cancellationToken) where T : Component {
T? obj = Helper.FindObject<T>();

while (obj is null && !cancellationToken.IsCancellationRequested) {
await Task.Yield();
obj = Helper.FindObject<T>();
}

return obj;
}
}
11 changes: 11 additions & 0 deletions lc-hax/Scripts/Helpers/WaitUntil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Threading;
using System.Threading.Tasks;

static partial class Helper {
internal static async Task WaitUntil(Func<bool> predicate, CancellationToken cancellationToken) {
while (!predicate() && !cancellationToken.IsCancellationRequested) {
await Task.Yield();
}
}
}

0 comments on commit bd64492

Please sign in to comment.