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
-
ObjectComponentBehaviourMonoBehaviourSimulationRunner
- Inherited Members
-
MonoBehaviour.IsInvoking()MonoBehaviour.CancelInvoke()MonoBehaviour.StopCoroutine(Coroutine)MonoBehaviour.StopAllCoroutines()MonoBehaviour.destroyCancellationTokenMonoBehaviour.useGUILayoutMonoBehaviour.didStartMonoBehaviour.didAwakeMonoBehaviour.runInEditModeBehaviour.enabledBehaviour.isActiveAndEnabledComponent.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.transformComponent.transformHandleComponent.gameObjectComponent.tagObject.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.nameObject.hideFlags
Constructors
SimulationRunner()
public SimulationRunner()
Properties
Baked
The baked level backing the current simulation (templates include decorator changes).
public BakedLevel Baked { get; }
Property Value
CommandCodec
Optional codec for custom commands, used when saving and loading command logs. Set before start.
public ICommandCodec CommandCodec { get; set; }
Property Value
CommandLog
Command log of the current run when recording or replaying, otherwise null.
public CommandLog CommandLog { get; }
Property Value
DefaultRangeYScale
The runner's own range Y scale setting (used when the level does not override it).
public float DefaultRangeYScale { get; }
Property Value
ForcedUpgradeRank
Rank forced on every track (negative: the purchased ranks). Applies on the next start.
public int ForcedUpgradeRank { get; set; }
Property Value
InterpolationAlpha
Fraction of a tick elapsed since the last step, for render interpolation.
public float InterpolationAlpha { get; }
Property Value
IsPaused
Whether the game clock is stopped (time scale zero).
public bool IsPaused { get; }
Property Value
IsReplaying
Whether the current run replays a log (live commands are ignored).
public bool IsReplaying { get; }
Property Value
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
ProfilerMarkers
Whether stages are wrapped in Unity Profiler markers (applies on the next start).
public bool ProfilerMarkers { get; set; }
Property Value
RangeYScale
Effective vertical range multiplier of the running simulation.
public float RangeYScale { get; }
Property Value
RecordCommands
Records commands from the next start on.
public bool RecordCommands { get; set; }
Property Value
RecordingFile
Recording file (see the Inspector tooltip); empty disables saving.
public string RecordingFile { get; set; }
Property Value
RemainsCapacity
Bodies kept for raising abilities (see SetRemains(int, float)).
public int RemainsCapacity { get; }
Property Value
RemainsLifetime
Game seconds a body stays available (see SetRemains(int, float)).
public float RemainsLifetime { get; }
Property Value
ReplayFile
Replay file (see the Inspector tooltip).
public string ReplayFile { get; set; }
Property Value
ReplayOnStart
Replays ReplayFile from the next start on.
public bool ReplayOnStart { get; set; }
Property Value
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
ResumeScale
The speed restored when unpausing.
public float ResumeScale { get; }
Property Value
Simulation
The running simulation, or null before start or after a bake failure.
public Simulation Simulation { get; }
Property Value
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
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
TimeScale
Game speed multiplier; zero pauses.
public float TimeScale { get; set; }
Property Value
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
UpgradeRecord
Shop, purchased ranks and forced rank of the current run (what a replay needs), or null.
public UpgradeRunRecord UpgradeRecord { get; }
Property Value
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
Upgrades
The permanent-upgrade snapshot of the current run, or null when the run applies none.
public UpgradeSnapshot Upgrades { get; }
Property Value
UseCampaignUpgrades
Whether the open campaign slot (CampaignSession) supplies the shop and the purchased ranks.
public bool UseCampaignUpgrades { get; set; }
Property Value
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
decoratorTemplateDecorator
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
realDeltaTimefloat
Configure(LevelAuthoring, LevelAsset)
Assigns the level sources; call before StartSimulation().
public void Configure(LevelAuthoring sceneSource, LevelAsset assetSource)
Parameters
sceneSourceLevelAuthoringassetSourceLevelAsset
CreateConfig()
Builds the simulation settings from the Inspector values.
public SimConfig CreateConfig()
Returns
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
commandICommand
RemoveTemplateDecorator(TemplateDecorator)
Removes a decorator added with AddTemplateDecorator(TemplateDecorator).
public bool RemoveTemplateDecorator(TemplateDecorator decorator)
Parameters
decoratorTemplateDecorator
Returns
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
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
SetPaused(bool)
Stops or resumes the game clock, remembering the last non-zero speed.
public void SetPaused(bool paused)
Parameters
pausedbool
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
SetSpeed(float)
Selects a non-zero game speed (and resumes).
public void SetSpeed(float scale)
Parameters
scalefloat
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
simulationSimulationbakedBakedLevel
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
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