Skip to content

Instantly share code, notes, and snippets.

@Leopotam
Created October 1, 2020 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leopotam/f6399d3076a1fcf2bfd2ded5f19042e2 to your computer and use it in GitHub Desktop.
Save Leopotam/f6399d3076a1fcf2bfd2ded5f19042e2 to your computer and use it in GitHub Desktop.
// startup.
sealed class BattleStartup : MonoBehaviour {
EcsWorld _world;
EcsSystems _systems;
void Start () {
_world = new EcsWorld ();
_systems = new EcsSystems (_world);
_systems
.Add (new TimeSystem ())
.Add (new TimeTestSystem ())
.Inject (new TimeService ())
.Init ();
}
void Update () {
_systems?.Run ();
}
void OnDestroy () {
if (_systems != null) {
_systems.Destroy ();
_systems = null;
_world.Destroy ();
_world = null;
}
}
}
// injected shared data.
sealed class TimeService {
public float Time;
public float DeltaTime;
public float SmoothDeltaTime;
}
// time service to save engine time into shared state.
sealed class TimeSystem : IEcsRunSystem {
// auto-injected fields.
readonly TimeService _time = default;
public void Run () {
_time.Time = Time.time;
_time.DeltaTime = Time.deltaTime;
_time.SmoothDeltaTime = Time.smoothDeltaTime;
}
}
// time-consuming system.
sealed class TimeTestSystem : IEcsRunSystem {
// auto-injected fields.
readonly TimeService _time = default;
public void Run () {
Debug.Log (_time.DeltaTime);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment