-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.go
54 lines (42 loc) · 1.47 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package stagehand
import (
"image"
"time"
ebiten "github.com/hajimehoshi/ebiten/v2"
)
// Clock helpers for mocking
type ClockInterface interface {
Since(time.Time) time.Duration
Now() time.Time
Until(time.Time) time.Duration
Sleep(time.Duration)
}
type RealClock struct{}
func (RealClock) Since(t time.Time) time.Duration { return time.Since(t) }
func (RealClock) Now() time.Time { return time.Now() }
func (RealClock) Until(t time.Time) time.Duration { return time.Until(t) }
func (RealClock) Sleep(d time.Duration) { time.Sleep(d) }
var Clock ClockInterface = RealClock{}
// MaxInt returns the maximum of two integers
func MaxInt(a, b int) int {
if a > b {
return a
}
return b
}
// Pre-draw returns the rendered frames for the given scenes.
func PreDraw[T any](bounds image.Rectangle, fromScene, toScene Scene[T]) (*ebiten.Image, *ebiten.Image) {
fromImg := ebiten.NewImage(bounds.Dx(), bounds.Dy())
fromScene.Draw(fromImg)
toImg := ebiten.NewImage(bounds.Dx(), bounds.Dy())
toScene.Draw(toImg)
return toImg, fromImg
}
// Converts a frequency(cycle/second) to a factor(change/cycle) for a given duration
func DurationToFactor(frequency float64, duration time.Duration) float64 {
return (1 / frequency) / duration.Seconds()
}
// Calculates the fraction of the duration that has passed since the initial time
func CalculateProgress(initialTime time.Time, duration time.Duration) float64 {
return float64(Clock.Since(initialTime)) / float64(duration)
}