Line data Source code
1 : package components
2 :
3 : import "time"
4 :
5 : // PlayTime はそのランの累積プレイ実時間を測るシングルトン。serde 非対象で、永続の実体はセーブ envelope。
6 : type PlayTime struct {
7 : total time.Duration
8 : lastTick time.Time
9 : }
10 :
11 : // Start は prior を累積の初期値にし、計測の起点を now に置く。新規は Start(0, now)、ロードは Start(保存値, now)。
12 : func (pt *PlayTime) Start(prior time.Duration, now time.Time) {
13 1 : pt.total = prior
14 1 : pt.lastTick = now
15 1 : }
16 :
17 : // Tick はラン進行中に毎フレーム呼び、前回からの実経過を累積へ足す。初回は起点だけ置く。
18 : func (pt *PlayTime) Tick(now time.Time) {
19 1 : if !pt.lastTick.IsZero() {
20 1 : pt.total += now.Sub(pt.lastTick)
21 1 : }
22 1 : pt.lastTick = now
23 : }
24 :
25 : // Elapsed は現在の累積プレイ実時間を返す。nil セーフ。
26 : func (pt *PlayTime) Elapsed() time.Duration {
27 1 : if pt == nil {
28 1 : return 0
29 1 : }
30 1 : return pt.total
31 : }
|