-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmesh.go
88 lines (74 loc) · 1.93 KB
/
mesh.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
package viewdrag
import "github.com/hajimehoshi/ebiten"
// Mesh represents set of triangles.
type Mesh struct {
// image basis for the mesh
image *ebiten.Image
// the mesh themselves
vertices []ebiten.Vertex
indices []uint16
// actually the bounds of the mesh
// base vector of the whole mesh
x, y int
// farthest vector in all of the meshes
width, height int
// dimensions of the viewport
scrWidth, scrHeight int
}
// NewMesh generates new mesh with farthest vector
func NewMesh(ebitenImage *ebiten.Image, vertices []ebiten.Vertex, indeces []uint16, x, y, screenWidth, screenHeight int) *Mesh {
w, h := Size(vertices)
return &Mesh{
image: ebitenImage,
vertices: vertices,
indices: indeces,
x: x,
y: y,
width: int(w),
height: int(h),
scrWidth: screenWidth,
scrHeight: screenHeight,
}
}
// Size returns the width and height of the mesh
func Size(vertices []ebiten.Vertex) (w, h float32) {
for _, v := range vertices {
if w < v.DstX {
w = v.DstX
}
if h < v.DstY {
h = v.DstY
}
}
return
}
// MoveBy asks for increments of the movements
func (m *Mesh) MoveBy(x, y int) {
m.x += x
m.y += y
m.x, m.y = keepSpriteInsideView(m.x, m.y, m.scrWidth, m.scrHeight, m.width, m.height)
}
// GetPosition gives the displacement position of the of the whole mesh
func (m *Mesh) GetPosition() (int, int) {
return m.x, m.y
}
// Draw draws the mesh.
func (m *Mesh) Draw(screen *ebiten.Image, dx, dy int, alpha float64) {
op := &ebiten.DrawTrianglesOptions{}
// op.GeoM.Translate(float64(m.x+dx), float64(m.y+dy))
vx := []ebiten.Vertex{}
for _, v := range m.vertices {
vx = append(vx, ebiten.Vertex{
DstX: v.DstX + float32(m.x+dx),
DstY: v.DstY + float32(m.y+dy),
ColorA: v.ColorA,
ColorB: v.ColorB,
ColorG: v.ColorG,
ColorR: v.ColorR,
SrcX: v.SrcX,
SrcY: v.SrcY,
})
}
op.ColorM.Scale(1, 1, 1, alpha)
screen.DrawTriangles(vx, m.indices, m.image, op)
}