WUT_Computer_Science/The project/Assets/Scripts/TickSystem/TickSystem.cs
Gabriel Ksawery Skowron-Rodriguez 98112df4a5 added TickSystem and an example tick receiver
+ basic TickSystem fully working:
++ add it to a scene for ticks to happen

+ ExampleTickReceiver reference monobehaviour
++ so everyone knows how to connect to the system
2022-05-16 13:34:11 +02:00

47 lines
1.2 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class TickSystem : MonoBehaviour
{
public class OnTickEventArgs : EventArgs
{
public int tickNumber;
}
public static Action<OnTickEventArgs> OnTick;
private const float TICK_PERIOD = 0.2f; // seconds between ticks
private int tick; // keep track of total ticks that have happened
public static TickSystem Instance;
private void Awake() // needs to be called by a scene initializer
{
if (Instance != null) // a TickSystem already exitst
{
Debug.LogError("A TickSystem already exists, FIX YOUR TIME SPAWNING");
Destroy(gameObject);
}//else this is the first TickSystem that has awoken in this scene
Instance = this;
tickTimer = 0f;
tick = 0;
}
private float tickTimer = 0f;
// Update is called once per frame
void Update()
{
tickTimer += Time.deltaTime;
if (tickTimer >= TICK_PERIOD)
{
tickTimer -= TICK_PERIOD;
tick++;
OnTick?.Invoke(new OnTickEventArgs { tickNumber = tick });
}
}
}