Class SimulationRunner

Namespace
TDMaker.Unity.View
Assembly
TDMaker.Unity.dll

Owns a Simulation and advances it with the core SimulationClock from Update (catch-up limit = catch-up ticks × speed per frame). Events produced during a frame are dispatched in emission order through EventRaised and then cleared, so the event buffer (Event Capacity) must hold every tick a frame catches up (at most catch-up ticks × speed). Commands queued after the game ended are rejected by the core with GameEnded on the next frame (the clock no longer steps, so the runner flushes them).

[DefaultExecutionOrder(-100)]
[AddComponentMenu("2D Tower Defense Maker/Simulation Runner")]
public sealed class SimulationRunner : MonoBehaviour
Inheritance
Object
Component
Behaviour
MonoBehaviour
SimulationRunner
Inherited Members
MonoBehaviour.IsInvoking()
MonoBehaviour.CancelInvoke()
MonoBehaviour.StopCoroutine(Coroutine)
MonoBehaviour.StopAllCoroutines()
MonoBehaviour.destroyCancellationToken
MonoBehaviour.useGUILayout
MonoBehaviour.didStart
MonoBehaviour.didAwake
MonoBehaviour.runInEditMode
Behaviour.enabled
Behaviour.isActiveAndEnabled
Component.GetComponent<T>()
Component.TryGetComponent<T>(out T)
Component.GetComponentInChildren<T>()
Component.GetComponentsInChildren<T>()
Component.GetComponentInParent<T>()
Component.GetComponentsInParent<T>()
Component.GetComponents<T>()
Component.GetComponentIndex()
Component.CompareTag(TagHandle)
Component.transform
Component.transformHandle
Component.gameObject
Component.tag
Object.GetEntityId()
Object.GetInstanceID()
Object.GetHashCode()
Object.InstantiateAsync<T>(T)
Object.InstantiateAsync<T>(T, Transform)
Object.InstantiateAsync<T>(T, Vector3, Quaternion)
Object.InstantiateAsync<T>(T, Transform, Vector3, Quaternion)
Object.Instantiate(Object, Vector3, Quaternion)
Object.Instantiate(Object, Vector3, Quaternion, Transform)
Object.Instantiate(Object)
Object.Instantiate(Object, Scene)
Object.Instantiate<T>(T, InstantiateParameters)
Object.Instantiate<T>(T, Vector3, Quaternion, InstantiateParameters)
Object.Instantiate(Object, Transform)
Object.Instantiate<T>(T)
Object.Instantiate<T>(T, Vector3, Quaternion)
Object.Instantiate<T>(T, Vector3, Quaternion, Transform)
Object.Instantiate<T>(T, Transform)
Object.Destroy(Object)
Object.DestroyImmediate(Object)
Object.DontDestroyOnLoad(Object)
Object.DestroyObject(Object)
Object.FindObjectsOfType<T>()
Object.FindObjectsByType<T>(FindObjectsSortMode)
Object.FindObjectsByType<T>(FindObjectsInactive, FindObjectsSortMode)
Object.FindObjectOfType<T>()
Object.FindFirstObjectByType<T>()
Object.FindAnyObjectByType<T>()
Object.FindFirstObjectByType<T>(FindObjectsInactive)
Object.FindAnyObjectByType<T>(FindObjectsInactive)
Object.FindObjectsByType<T>()
Object.FindObjectsByType<T>(FindObjectsInactive)
Object.ToString()
Object.name
Object.hideFlags

Constructors

SimulationRunner()

public SimulationRunner()

Properties

Baked

The baked level backing the current simulation (templates include decorator changes).

public BakedLevel Baked { get; }

Property Value

BakedLevel

CommandCodec

Optional codec for custom commands, used when saving and loading command logs. Set before start.

public ICommandCodec CommandCodec { get; set; }

Property Value

ICommandCodec

CommandLog

Command log of the current run when recording or replaying, otherwise null.

public CommandLog CommandLog { get; }

Property Value

CommandLog

DefaultRangeYScale

The runner's own range Y scale setting (used when the level does not override it).

public float DefaultRangeYScale { get; }

Property Value

float

ForcedUpgradeRank

Rank forced on every track (negative: the purchased ranks). Applies on the next start.

public int ForcedUpgradeRank { get; set; }

Property Value

int

InterpolationAlpha

Fraction of a tick elapsed since the last step, for render interpolation.

public float InterpolationAlpha { get; }

Property Value

float

IsPaused

Whether the game clock is stopped (time scale zero).

public bool IsPaused { get; }

Property Value

bool

IsReplaying

Whether the current run replays a log (live commands are ignored).

public bool IsReplaying { get; }

Property Value

bool

Profiler

Optional core profiling hook (per-tick and per-stage callbacks, see ISimProfiler) installed on every new simulation. Chained behind the Unity Profiler markers when those are active; may be changed while running.

public ISimProfiler Profiler { get; set; }

Property Value

ISimProfiler

ProfilerMarkers

Whether stages are wrapped in Unity Profiler markers (applies on the next start).

public bool ProfilerMarkers { get; set; }

Property Value

bool

RangeYScale

Effective vertical range multiplier of the running simulation.

public float RangeYScale { get; }

Property Value

float

RecordCommands

Records commands from the next start on.

public bool RecordCommands { get; set; }

Property Value

bool

RecordingFile

Recording file (see the Inspector tooltip); empty disables saving.

public string RecordingFile { get; set; }

Property Value

string

RemainsCapacity

Bodies kept for raising abilities (see SetRemains(int, float)).

public int RemainsCapacity { get; }

Property Value

int

RemainsLifetime

Game seconds a body stays available (see SetRemains(int, float)).

public float RemainsLifetime { get; }

Property Value

float

ReplayFile

Replay file (see the Inspector tooltip).

public string ReplayFile { get; set; }

Property Value

string

ReplayOnStart

Replays ReplayFile from the next start on.

public bool ReplayOnStart { get; set; }

Property Value

bool

ReplayUpgrades

Exact ranks for the next replay (overrides every other source). When a replay file is loaded and this is null, the ranks recorded next to it (<file>.upgrades.json) are used.

public UpgradeRunRecord ReplayUpgrades { get; set; }

Property Value

UpgradeRunRecord

ResumeScale

The speed restored when unpausing.

public float ResumeScale { get; }

Property Value

float

Simulation

The running simulation, or null before start or after a bake failure.

public Simulation Simulation { get; }

Property Value

Simulation

StartOnPlay

Whether Start creates the simulation automatically. Turn it off when a menu or flow script picks the level and calls StartSimulation().

public bool StartOnPlay { get; set; }

Property Value

bool

StartingGoldOverride

Debug: starting gold used instead of the level's when zero or more (applies on the next start).

public int StartingGoldOverride { get; set; }

Property Value

int

TimeScale

Game speed multiplier; zero pauses.

public float TimeScale { get; set; }

Property Value

float

UpgradeRanks

Purchased rank per track key for the next start, overriding the campaign slot (for example your own save system). Null uses the open campaign slot when UseCampaignUpgrades is on.

public Func<string, int> UpgradeRanks { get; set; }

Property Value

Func<string, int>

UpgradeRecord

Shop, purchased ranks and forced rank of the current run (what a replay needs), or null.

public UpgradeRunRecord UpgradeRecord { get; }

Property Value

UpgradeRunRecord

UpgradeShop

Star-upgrade shop applied on the next start (null: the open campaign slot's shop, see UseCampaignUpgrades).

public UpgradeShopAsset UpgradeShop { get; set; }

Property Value

UpgradeShopAsset

Upgrades

The permanent-upgrade snapshot of the current run, or null when the run applies none.

public UpgradeSnapshot Upgrades { get; }

Property Value

UpgradeSnapshot

UseCampaignUpgrades

Whether the open campaign slot (CampaignSession) supplies the shop and the purchased ranks.

public bool UseCampaignUpgrades { get; set; }

Property Value

bool

Methods

AddTemplateDecorator(TemplateDecorator)

Adds a template decorator applied to every baked template on each (re)start, in registration order. Register it before the runner starts (for example in Awake or OnEnable).

public void AddTemplateDecorator(TemplateDecorator decorator)

Parameters

decorator TemplateDecorator

Advance(float)

Advances the clock by realDeltaTime × TimeScale through SimulationClock (at most catch-up ticks × speed per call), then dispatches the frame's events. Called from Update; tools (replays, screenshot capture in edit mode) may call it directly.

public void Advance(float realDeltaTime)

Parameters

realDeltaTime float

Configure(LevelAuthoring, LevelAsset)

Assigns the level sources; call before StartSimulation().

public void Configure(LevelAuthoring sceneSource, LevelAsset assetSource)

Parameters

sceneSource LevelAuthoring
assetSource LevelAsset

CreateConfig()

Builds the simulation settings from the Inspector values.

public SimConfig CreateConfig()

Returns

SimConfig

Enqueue(ICommand)

Queues a player command for the next tick. Ignored while replaying. After victory or defeat the command is still queued and the core rejects it with GameEnded (a CommandRejected event) on the next frame.

public void Enqueue(ICommand command)

Parameters

command ICommand

RemoveTemplateDecorator(TemplateDecorator)

Removes a decorator added with AddTemplateDecorator(TemplateDecorator).

public bool RemoveTemplateDecorator(TemplateDecorator decorator)

Parameters

decorator TemplateDecorator

Returns

bool

Restart()

Discards the current world and starts again.

public void Restart()

SaveRecording()

Seals the current recording and writes it to RecordingFile (once per run). Called automatically when the game ends, on restart and when the runner is disabled. Returns the written path, or null.

public string SaveRecording()

Returns

string

SetCapacities(int, int, int, int, int, int, int)

Sets the capacities used by the next start (see the Inspector tooltips). Values below the Inspector minimums are raised to them.

public void SetCapacities(int entities, int events, int effects, int areas, int schedule, int commands = 128, int chains = 64)

Parameters

entities int
events int
effects int
areas int
schedule int
commands int
chains int

SetPaused(bool)

Stops or resumes the game clock, remembering the last non-zero speed.

public void SetPaused(bool paused)

Parameters

paused bool

SetRemains(int, float)

Sets the body storage used by the next start: capacity (0 keeps none) and game seconds a body stays available.

public void SetRemains(int capacity, float lifetime)

Parameters

capacity int
lifetime float

SetSpeed(float)

Selects a non-zero game speed (and resumes).

public void SetSpeed(float scale)

Parameters

scale float

SpawnHeroes(Simulation, BakedLevel)

Queues every hero of a baked level at its spawn point (they appear after the first tick).

public static void SpawnHeroes(Simulation simulation, BakedLevel baked)

Parameters

simulation Simulation
baked BakedLevel

StartSimulation()

Bakes the level and creates a fresh simulation.

public void StartSimulation()

Events

EventRaised

Raised for every simulation event in emission order.

public event SimEventHandler EventRaised

Event Type

SimEventHandler

SimulationStarted

Raised after a new simulation is created (initial start or restart) and before its first step. Register extensions (systems, components, hooks, command handlers) here.

public event Action<SimulationRunner> SimulationStarted

Event Type

Action<SimulationRunner>