Skip to content

Instantly share code, notes, and snippets.

Created November 17, 2017 13:14
Show Gist options
  • Save anonymous/bbd53f89cbc64f2dde5b7668c762be95 to your computer and use it in GitHub Desktop.
Save anonymous/bbd53f89cbc64f2dde5b7668c762be95 to your computer and use it in GitHub Desktop.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
[System.Serializable]
public class TimedAction {
public DateTime time_started { get; private set; }
public float duration { get; private set; }
public double action_ratio { get; private set; } // 0 to 1.
private double time_progress;
private double delta;
private DateTime _last_date = DateTime.Now;
public TimedAction(float duration, DateTime date)
{
this.time_started = date;
this.duration = duration;
}
public void Update(DateTime new_date)
{
delta = MsElapsed (new_date, _last_date);
this.time_progress += delta/1000f;
action_ratio = Mathf.Min ((float)time_progress / duration, 1f);
_last_date = new_date;
}
private double MsElapsed(DateTime a, DateTime b)
{
return a.Subtract(b).TotalMilliseconds;
}
public string SerializeToJSON()
{
TimedActionStruct s;
s.time_started = this.time_started.ToFileTimeUtc();
s.duration = this.duration;
s._last_date = this._last_date.ToFileTimeUtc();
string json = JsonUtility.ToJson(s, true);
return json; // TODO: Display the content of the string.
}
}
[Serializable]
public struct TimedActionStruct {
public long time_started;
public float duration;
// public double action_ratio; Calculable avec: time_progress / duration
// public double time_progress; Calculable avec: delta/1000 * msElapsed(new_date, _last_date)
// public double delta; Calculable avec le _last_date;
public long _last_date;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment