-
Notifications
You must be signed in to change notification settings - Fork 0
/
objects.go
184 lines (156 loc) · 3.52 KB
/
objects.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"encoding/binary"
"math"
"golang.org/x/mobile/exp/f32"
"golang.org/x/mobile/exp/sprite"
"golang.org/x/mobile/exp/sprite/clock"
"golang.org/x/mobile/gl"
)
type Object struct {
// Position
X, Y float32
// Speed
Vx, Vy float32
// Rotation
Rx, Ry, Angle, AngleCenter float32
// Translation
Tx, Ty float32
// Scale
Sx, Sy, Scale float32
Width, Height float32
Sprite sprite.SubTex
Action Action
Dead bool
Time clock.Time
// Data contains any relevant information needed about the object
Data interface{}
// Object center
cx, cy float32
}
type Action interface {
Do(o *Object, t clock.Time)
}
func (o *Object) Reset() {
o.Tx, o.Ty = 0, 0
o.Sx, o.Sy, o.Scale = 0, 0, 0
o.Rx, o.Ry, o.Angle, o.AngleCenter = 0, 0, 0, 0
o.Time = 0
}
func (o *Object) Center() (float32, float32) {
if o.cx == 0 {
o.cx = o.X + o.Width/2
}
if o.cy == 0 {
o.cy = o.Y + o.Height/2
}
return o.cx, o.cy
}
func (o *Object) Arrange(e sprite.Engine, n *sprite.Node, t clock.Time) {
if o.Action != nil {
// Invoke the action
o.Action.Do(o, t)
}
// Set the texture
e.SetSubTex(n, o.Sprite)
if o.Dead {
// Do nothing if dead object
return
}
// Compute affine transformations
mv := &f32.Affine{}
mv.Identity()
// Apply translations
if o.Angle != 0 && o.Rx == o.Sx && o.Ry == o.Sy {
// Optim when angle and scale use the same transformation
mv.Translate(mv, o.Rx+o.Tx, o.Ry+o.Ty)
mv.Rotate(mv, -o.Angle)
mv.Scale(mv, o.Scale, o.Scale)
mv.Translate(mv, -o.Rx-o.Tx, -o.Ry-o.Ty)
} else {
if o.Angle != 0 {
mv.Translate(mv, o.Rx+o.Tx, o.Ry+o.Ty)
mv.Rotate(mv, -o.Angle)
mv.Translate(mv, -o.Rx-o.Tx, -o.Ry-o.Ty)
}
if o.Sx > 0 || o.Sy > 0 {
mv.Translate(mv, o.Sx+o.Tx, o.Sy+o.Ty)
mv.Scale(mv, o.Scale, o.Scale)
mv.Translate(mv, -o.Sx-o.Tx, -o.Sy-o.Ty)
}
}
if o.AngleCenter != 0 {
cx, cy := o.Center()
mv.Translate(mv, cx+o.Tx, cy+o.Ty)
mv.Rotate(mv, -o.AngleCenter)
mv.Translate(mv, -cx-o.Tx, -cy-o.Ty)
}
mv.Translate(mv, o.X+o.Tx, o.Y+o.Ty)
mv.Mul(mv, &f32.Affine{
{o.Width, 0, 0},
{0, o.Height, 0},
})
e.SetTransform(n, *mv)
}
const (
VShaderBasic = `#version 100
attribute vec4 position;
attribute vec4 color;
varying vec4 theColor;
uniform mat4 modelViewProjection;
void main() {
gl_Position = modelViewProjection * position;
theColor = color;
}`
FShaderBasic = `#version 100
precision mediump float;
varying vec4 theColor;
void main() {
gl_FragColor = theColor;
}`
)
type BlockModel struct {
ModelBase
block *Block
}
type Background struct {
ModelBase
angle float32
}
func NewBackground(glctx gl.Context) *Background {
b := &Background{}
vertices := make([]float32, 0)
for i := float64(0); i <= BgSegments; i++ {
if math.Mod(i, 2) == 0 {
// position
vertices = append(vertices, 0, 0, 0, 1)
// color
vertices = append(vertices, .11, .03, .81, 1)
b.vertexCount++
}
a := TwoPi * i / BgSegments
// position
vertices = append(vertices, float32(math.Sin(a)*windowRadius), float32(math.Cos(a)*windowRadius), 0, 1)
// color
vertices = append(vertices, .11, .03, .81, 1)
b.vertexCount++
}
data := f32.Bytes(binary.LittleEndian, vertices...)
b.Init(glctx, gl.TRIANGLES, data, VShaderBasic, FShaderBasic)
return b
}
func (t *Background) Draw() {
if t.angle > math.Pi {
t.angle = t.angle - math.Pi
} else {
if g.level.Win() {
t.angle += 0.03
} else {
t.angle += 0.01
}
}
modelViewBackup := *t.modelView
t.modelView.Mul(t.modelView, rotate(t.angle))
t.ModelBase.Draw()
t.modelView = &modelViewBackup
}