-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert existing coroutines to Tasks
- Loading branch information
1 parent
77f9145
commit bd64492
Showing
6 changed files
with
57 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |