NebulaModTeam / nebula

A multiplayer mod for the game Dyson Sphere Program
GNU General Public License v3.0
824 stars 125 forks source link

Add tick based tasks to nebula #625

Closed Reousa closed 5 months ago

Reousa commented 7 months ago

Nebula Tasks

Creating a new task

One time task that runs on the next frame:

new NebulaTask(ISimulationTicker);

Task that repeats 5 times, once every 2 seconds:

new NebulaTask(ISimulationTicker, 5);

Task that repeats indefinitely until cancelled:

new NebulaTask(ISimulationTicker, true);

All constructors have an optional final argument for how many seconds to wait between executions.

Setting a condition

A task can be provided a condition predicate to limit it's execution. If provided, a task will only execute and count an execution when both the time condition is met, and the custom condition is met.

task.SetCondition(() => { do things; return true or false; });

Example

string foo = "Hi.";

var task = new NebulaTask(SimulationTicker, timeInterval)
            .SetCondition(() => !foo.IsEmpty())
            .OnExecuted(() => Log("Task executed"))
            .OnCompleted(() => Log("Task completed."))
            .Start();