-
Notifications
You must be signed in to change notification settings - Fork 26
/
blocksign.go
159 lines (143 loc) · 4.27 KB
/
blocksign.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
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package steven
import (
"github.com/go-gl/mathgl/mgl32"
"github.com/thinkofdeath/steven/format"
"github.com/thinkofdeath/steven/render"
"github.com/thinkofdeath/steven/type/direction"
"github.com/thinkofdeath/steven/ui"
)
type signComponent struct {
model *render.Model
lines [4]format.AnyComponent
position Position
ox, oy, oz float64
rotation float64
hasStand bool
}
type SignComponent interface {
Update(lines [4]format.AnyComponent)
}
func (s *signComponent) Model() *render.Model {
return s.model
}
func (s *signComponent) Update(lines [4]format.AnyComponent) {
s.free()
s.lines = lines
s.create()
}
func (s *signComponent) free() {
if s.model != nil {
s.model.Free()
}
}
func (s *signComponent) create() {
const yS = (6.0 / 16.0) / 4.0
const xS = yS / 16.0
var verts []*render.ModelVertex
for i, line := range s.lines {
if line.Value == nil {
continue
}
format.ConvertLegacy(line)
// Hijack ui.Formatted's component parsing to split
// up components into ui.Text elements.
// TODO(Think) Move this into some common place for
// easier reuse in other places?
wrap := &format.TextComponent{}
wrap.Color = format.Black
wrap.Extra = []format.AnyComponent{line}
f := ui.NewFormatted(format.Wrap(wrap), 0, 0)
offset := 0.0
for _, txt := range f.Text {
str := txt.Value()
for _, r := range str {
tex := render.CharacterTexture(r)
if tex == nil {
continue
}
s := render.SizeOfCharacter(r)
if r == ' ' {
offset += (s + 2) * xS
continue
}
for _, v := range faceVertices[direction.North].verts {
vert := &render.ModelVertex{
X: float32(v.X)*float32(s*xS) - float32(offset+s*xS) + float32(f.Width*xS*0.5),
Y: float32(v.Y)*yS - yS*float32(i-1),
Z: -.6 / 16.0,
Texture: tex,
TextureX: float64(v.TOffsetX),
TextureY: float64(v.TOffsetY),
R: byte(txt.R()),
G: byte(txt.G()),
B: byte(txt.B()),
A: 255,
}
verts = append(verts, vert)
}
offset += (s + 2) * xS
}
}
}
wood := render.GetTexture("blocks/planks_oak")
// The backboard
verts = appendBoxExtra(verts, -0.5, -4/16.0, -0.5/16.0, 1.0, 8/16.0, 1/16.0, [6]render.TextureInfo{
direction.Up: wood.Sub(0, 0, 16, 2),
direction.Down: wood.Sub(0, 0, 16, 2),
direction.East: wood.Sub(0, 0, 2, 12),
direction.West: wood.Sub(0, 0, 2, 12),
direction.North: wood.Sub(0, 4, 16, 12),
direction.South: wood.Sub(0, 4, 16, 12),
}, [6][2]float64{
direction.Up: {1.5, 1.0},
direction.Down: {1.5, 1.0},
direction.East: {1.0, 1.0},
direction.West: {1.0, 1.0},
direction.North: {1.5, 1.0},
direction.South: {1.5, 1.0},
})
if s.hasStand {
// Stand
log := render.GetTexture("blocks/log_oak")
verts = appendBox(verts, -0.5/16.0, -0.25-9/16.0, -0.5/16.0, 1/16.0, 9/16.0, 1/16.0, [6]render.TextureInfo{
direction.Up: log.Sub(0, 0, 2, 2),
direction.Down: log.Sub(0, 0, 2, 2),
direction.East: log.Sub(0, 0, 2, 12),
direction.West: log.Sub(0, 0, 2, 12),
direction.North: log.Sub(0, 0, 2, 12),
direction.South: log.Sub(0, 0, 2, 12),
})
}
s.model = render.NewModel([][]*render.ModelVertex{
verts,
})
s.model.Radius = 2
x, y, z := s.position.X, s.position.Y, s.position.Z
s.model.X, s.model.Y, s.model.Z = -float32(x)-0.5, -float32(y)-0.5, float32(z)+0.5
s.model.Matrix[0] = mgl32.Translate3D(
float32(x)+0.5,
-float32(y)-0.5,
float32(z)+0.5,
).Mul4(mgl32.Rotate3DY(float32(s.rotation)).Mat4()).
Mul4(mgl32.Translate3D(float32(s.ox), float32(-s.oy), float32(s.oz)))
}
func esSignAdd(s *signComponent, b BlockComponent) {
s.position = b.Position()
s.create()
}
func esSignRemove(s *signComponent) {
s.free()
}