Skip to content
This repository has been archived by the owner on Mar 25, 2020. It is now read-only.
pointcache edited this page Jan 17, 2017 · 4 revisions

System is a simple MonoBehavior that most likely will use URSA's magic methods in conjunction with SystemsExecutor. To get nice looking label you can inherit from SystemBase.

This is an example of a system:

public class TimeSystem : SystemBase
{
    public double dayDelta, prevDayTotal;
    public double prevTotaltime;
    int prevhour = -1;
    int currentHour;

    void orderedFixedUpdate()
    {
        var time = Pool<TimeData>.First.data;

        time.TotalTimePassed += GetScaledDelta(time);
        time.DoubleTimeOfDay = time.TotalTimePassed % 1;
        time.DoubleDelta = time.TotalTimePassed - prevTotaltime;
        time.TimeOfDay.Value = (float) (time.TotalTimePassed % 1);

        if (currentHour != prevhour)
        {
            time.Hour.Value = currentHour;
            prevhour = time.Hour;
        }
        currentHour = (int)time.TimeOfDay.Remap(0f, 1f, 0f, 24f);
        time.Delta = (float)time.DoubleDelta;

        if (time.TotalTimePassed - prevDayTotal > 1.0)
        {
            time.DoubleTimeOfDay = 0.0 + time.DoubleDelta;
            time.TimeOfDay.Value = (float)time.DoubleTimeOfDay;
            prevDayTotal = time.TotalTimePassed;
        }
        prevTotaltime = time.TotalTimePassed;
    }

    int GetCurrentHour(double tod)
    {
        return (int)(tod * 24.0);
    }

    double GetDaylenght(TimeData.Data time)
    {
        return 1440.0 / time.DayLength;
    }

    double GetScaledDelta(TimeData.Data time)
    {
        double scaled = Time.deltaTime * GetDaylenght(time);
        double scaleBacktoHour = scaled.Remap(0, 1440, 0, 1);
        double getMinute = scaleBacktoHour / 60.0;
        double applyMult = getMinute * time.SpeedMultiplier;
        return applyMult;
    }
}

Despite most other systems being dependent on the work of this one, since all it does is modify the Time component, it is completely decoupled from the rest of the game. You can destroy it and no error will be thrown.

Clone this wiki locally