-
Notifications
You must be signed in to change notification settings - Fork 5
/
layer.go
96 lines (83 loc) · 2.36 KB
/
layer.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package tuile
import (
"errors"
"image"
"io"
"github.com/bquenin/tmxmap"
)
// Layer structure
type Layer struct {
origin image.Point
width, height int
pixelWidth, pixelHeight int
tileWidth, tileHeight int
tiles []*tmxmap.TileInfo
tileSet *tmxmap.TileSet
Image *image.Paletted
repeat bool
disabled bool
transformed bool
angle float64 //radians
translation Vector
scale Vector
}
func NewLayerWithReader(tileMap *tmxmap.Map, reader io.Reader) (*Layer, error) {
var err error
tileMap.TileSets[0].Image.Image, _, err = image.Decode(reader)
if err != nil {
return nil, err
}
return NewLayer(tileMap)
}
// NewLayer instantiates a new layer
func NewLayer(tileMap *tmxmap.Map) (*Layer, error) {
image, ok := tileMap.TileSets[0].Image.Image.(*image.Paletted)
if !ok {
return nil, errors.New("tileset Image is not paletted")
}
return &Layer{
tiles: tileMap.Layers[0].Tiles,
tileSet: &tileMap.TileSets[0],
Image: image,
width: tileMap.Layers[0].Width,
height: tileMap.Layers[0].Height,
pixelWidth: tileMap.Layers[0].Width * tileMap.TileSets[0].TileWidth,
pixelHeight: tileMap.Layers[0].Height * tileMap.TileSets[0].TileHeight,
tileWidth: tileMap.TileSets[0].TileWidth,
tileHeight: tileMap.TileSets[0].TileHeight,
repeat: false,
scale: NewVector(1, 1),
}, nil
}
func (l *Layer) SetOrigin(x int, y int) {
l.origin = image.Pt(x, y)
}
func (l *Layer) SetRepeat(repeat bool) {
l.repeat = repeat
}
func (l *Layer) Disable() {
l.disabled = true
}
func (l *Layer) Enable() {
l.disabled = false
}
func (l *Layer) SetRotation(angle float64) {
l.transformed = true
l.angle = angle
}
func (l *Layer) SetScale(sx, sy float64) {
l.transformed = true
l.scale = NewVector(sx, sy)
}
func (l *Layer) SetTranslation(dx, dy float64) {
l.transformed = true
l.translation = NewVector(dx, dy)
}
func (l *Layer) transform(left, right Vector) (Vector, Vector) {
dx, dy := float64(l.origin.X)+l.translation.X, float64(l.origin.Y)+l.translation.Y
translate := IM.Translate(NewVector(-dx, -dy))
rotation := translate.Rotate(l.angle)
scale := rotation.Scale(l.scale)
result := scale.Translate(NewVector(dx, dy))
return left.Mul(result), right.Mul(result)
}