forked from elliotmr/tmx
/
camera.go
61 lines (52 loc) · 1.09 KB
/
camera.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
55
56
57
58
59
60
61
package ebitentmx
import (
"math"
"time"
"github.com/hajimehoshi/ebiten"
"fmt"
)
func NewCamera() *Camera {
return &Camera{
updated: time.Now(),
opts: &ebiten.DrawImageOptions{
GeoM: ebiten.GeoM{},
ColorM: ebiten.ColorM{},
Filter: ebiten.FilterNearest,
},
}
}
type Camera struct {
updated time.Time
DT time.Duration
opts *ebiten.DrawImageOptions
}
func (c *Camera) StartUpdate(now time.Time) {
c.DT = now.Sub(c.updated)
}
func (c *Camera) Draw(src, dest *ebiten.Image) error {
c.updated = c.updated.Add(c.DT)
return dest.DrawImage(src, c.opts)
}
func (c *Camera) Pan(dir float64, rate float64) {
var sin, cos float64
switch dir {
case 0:
sin, cos = 0.0, 1.0
case math.Pi / 2:
sin, cos = 1.0, 0.0
case math.Pi:
sin, cos = 0.0, -1.0
case 3 * math.Pi / 2:
sin, cos = -1.0, 0.0
default:
sin, cos = math.Sincos(dir)
}
c.opts.GeoM.Translate(sin * rate * 0.001 / c.DT.Seconds(), cos * rate * 0.001 / c.DT.Seconds())
}
func (c *Camera) String() string {
return fmt.Sprintf(
"[x: %0.2f, y: %0.2f]",
c.opts.GeoM.Element(0, 2),
c.opts.GeoM.Element(1, 2),
)
}