-
Notifications
You must be signed in to change notification settings - Fork 26
/
entitymodels.go
450 lines (404 loc) · 12.4 KB
/
entitymodels.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// 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 (
"errors"
"math"
"github.com/go-gl/mathgl/mgl32"
"github.com/thinkofdeath/steven/entitysys"
"github.com/thinkofdeath/steven/render"
"github.com/thinkofdeath/steven/type/direction"
)
func init() {
addSystem(entitysys.Add, esPlayerModelAdd)
addSystem(entitysys.Tick, esPlayerModelTick)
addSystem(entitysys.Remove, esPlayerModelRemove)
// Generic removal
addSystem(entitysys.Remove, esModelRemove)
}
func appendBox(verts []*render.ModelVertex, x, y, z, w, h, d float32, textures [6]render.TextureInfo) []*render.ModelVertex {
return appendBoxExtra(verts, x, y, z, w, h, d, textures, [6][2]float64{
{1.0, 1.0},
{1.0, 1.0},
{1.0, 1.0},
{1.0, 1.0},
{1.0, 1.0},
{1.0, 1.0},
})
}
func appendBoxExtra(verts []*render.ModelVertex, x, y, z, w, h, d float32, textures [6]render.TextureInfo, extra [6][2]float64) []*render.ModelVertex {
for i, face := range faceVertices {
tex := textures[i]
if tex == nil {
continue
}
for _, v := range face.verts {
var rr, gg, bb byte = 255, 255, 255
if direction.Type(i) == direction.West || direction.Type(i) == direction.East {
rr = byte(255 * 0.8)
gg = byte(255 * 0.8)
bb = byte(255 * 0.8)
}
vert := &render.ModelVertex{
X: float32(v.X)*w + x,
Y: float32(v.Y)*h + y,
Z: float32(v.Z)*d + z,
Texture: tex,
TextureX: float64(v.TOffsetX) * extra[i][0],
TextureY: float64(v.TOffsetY) * extra[i][1],
R: rr,
G: gg,
B: bb,
A: 255,
}
verts = append(verts, vert)
}
}
return verts
}
// Player
type playerModelComponent struct {
model *render.Model
skin string
cape string
hasHead bool
hasNameTag bool
isFirstPerson bool
dir float64
time float64
idleTime float64
manualMove bool
walking bool
armTime float64
capeTime float64
heldModel *render.Model
heldMat mgl32.Mat4
}
func (p *playerModelComponent) Model() *render.Model { return p.model }
func (p *playerModelComponent) SwingArm() {
p.armTime = 15
}
func (p *playerModelComponent) SetCurrentItem(item *ItemStack) {
if p.heldModel != nil {
p.heldModel.Free()
}
if item == nil {
return
}
mdl := getModel(item.Type.Name())
if mdl == nil {
return
}
var blk Block
if bt, ok := item.Type.(*blockItem); ok {
blk = bt.block
}
mode := "thirdperson_righthand"
var out []*render.ModelVertex
if mdl.builtIn == builtInGenerated {
out, p.heldMat = genStaticModelFromItem(mdl, blk, mode)
} else if mdl.builtIn == builtInFalse {
out, p.heldMat = staticModelFromItem(mdl, blk, mode)
}
p.heldModel = render.NewModel([][]*render.ModelVertex{
out,
})
p.heldModel.Radius = 3
}
type PlayerModelComponent interface {
SwingArm()
SetCurrentItem(item *ItemStack)
}
const (
playerModelHead = iota
playerModelBody
playerModelLegLeft
playerModelLegRight
playerModelArmLeft
playerModelArmRight
playerModelCape
playerModelNameTag
)
func esPlayerModelAdd(p *playerModelComponent, pl PlayerComponent) {
uuid := pl.UUID()
info := Client.playerList.info[uuid]
if info == nil {
Client.network.SignalClose(errors.New("missing player info"))
return
}
skin := info.skin
p.skin = info.skinHash
cape := info.cape
p.cape = info.capeHash
if p.skin != "" {
render.RefSkin(p.skin)
}
if p.cape != "" {
render.RefSkin(p.cape)
}
var hverts []*render.ModelVertex
if p.hasHead {
hverts = appendBox(hverts, -4/16.0, 0, -4/16.0, 8/16.0, 8/16.0, 8/16.0, [6]render.TextureInfo{
direction.North: skin.Sub(8, 8, 8, 8),
direction.South: skin.Sub(24, 8, 8, 8),
direction.East: skin.Sub(0, 8, 8, 8),
direction.West: skin.Sub(16, 8, 8, 8),
direction.Up: skin.Sub(8, 0, 8, 8),
direction.Down: skin.Sub(16, 0, 8, 8),
})
hverts = appendBox(hverts, -4.2/16.0, -.2/16.0, -4.2/16.0, 8.4/16.0, 8.4/16.0, 8.4/16.0, [6]render.TextureInfo{
direction.North: skin.Sub(8+32, 8, 8, 8),
direction.South: skin.Sub(24+32, 8, 8, 8),
direction.East: skin.Sub(0+32, 8, 8, 8),
direction.West: skin.Sub(16+32, 8, 8, 8),
direction.Up: skin.Sub(8+32, 0, 8, 8),
direction.Down: skin.Sub(16+32, 0, 8, 8),
})
}
var cverts []*render.ModelVertex
if p.cape != "" {
cverts = appendBox(cverts, -5/16.0, -16/16.0, 0, 10/16.0, 16/16.0, 1/16.0, [6]render.TextureInfo{
direction.North: cape.Sub(11, 1, 10, 16),
direction.South: cape.Sub(1, 1, 10, 16),
direction.West: cape.Sub(0, 1, 1, 16),
direction.East: cape.Sub(21, 1, 1, 16),
direction.Up: cape.Sub(1, 0, 10, 1),
direction.Down: cape.Sub(11, 0, 10, 1),
})
}
bverts := appendBox(nil, -4/16.0, -6/16.0, -2/16.0, 8/16.0, 12/16.0, 4/16.0, [6]render.TextureInfo{
direction.North: skin.Sub(20, 20, 8, 12),
direction.South: skin.Sub(32, 20, 8, 12),
direction.West: skin.Sub(16, 20, 4, 12),
direction.East: skin.Sub(28, 20, 4, 12),
direction.Up: skin.Sub(20, 16, 8, 4),
direction.Down: skin.Sub(28, 16, 8, 4),
})
bverts = appendBox(bverts, -4.2/16.0, -6.2/16.0, -2.2/16.0, 8.4/16.0, 12.4/16.0, 4.4/16.0, [6]render.TextureInfo{
direction.North: skin.Sub(20, 20+16, 8, 12),
direction.South: skin.Sub(32, 20+16, 8, 12),
direction.West: skin.Sub(16, 20+16, 4, 12),
direction.East: skin.Sub(28, 20+16, 4, 12),
direction.Up: skin.Sub(20, 16+16, 8, 4),
direction.Down: skin.Sub(28, 16+16, 8, 4),
})
var lverts [4][]*render.ModelVertex
for i, off := range [][4]int{
{0, 16, 0, 32},
{16, 48, 0, 48},
{32, 48, 48, 48},
{40, 16, 40, 32},
} {
ox, oy := off[0], off[1]
lverts[i] = appendBox(nil, -2/16.0, -12/16.0, -2/16.0, 4/16.0, 12/16.0, 4/16.0, [6]render.TextureInfo{
direction.North: skin.Sub(ox+4, oy+4, 4, 12),
direction.South: skin.Sub(ox+12, oy+4, 4, 12),
direction.West: skin.Sub(ox+0, oy+4, 4, 12),
direction.East: skin.Sub(ox+8, oy+4, 4, 12),
direction.Up: skin.Sub(ox+4, oy, 4, 4),
direction.Down: skin.Sub(ox+8, oy, 4, 4),
})
ox, oy = off[2], off[3]
lverts[i] = appendBox(lverts[i], -2.2/16.0, -12.2/16.0, -2.2/16.0, 4.4/16.0, 12.4/16.0, 4.4/16.0, [6]render.TextureInfo{
direction.North: skin.Sub(ox+4, oy+4, 4, 12),
direction.South: skin.Sub(ox+12, oy+4, 4, 12),
direction.West: skin.Sub(ox+0, oy+4, 4, 12),
direction.East: skin.Sub(ox+8, oy+4, 4, 12),
direction.Up: skin.Sub(ox+4, oy, 4, 4),
direction.Down: skin.Sub(ox+8, oy, 4, 4),
})
}
var nverts []*render.ModelVertex
if p.hasNameTag {
nverts = createNameTag(info.name)
}
model := render.NewModel([][]*render.ModelVertex{
playerModelHead: hverts,
playerModelBody: bverts,
playerModelLegRight: lverts[0],
playerModelLegLeft: lverts[1],
playerModelArmRight: lverts[2],
playerModelArmLeft: lverts[3],
playerModelCape: cverts,
playerModelNameTag: nverts,
})
p.model = model
model.Radius = 3
}
func createNameTag(name string) (verts []*render.ModelVertex) {
width := render.SizeOfString(name) + 4
offset := -(width/2)*0.01 + (2 * 0.01)
for _, r := range name {
tex := render.CharacterTexture(r)
if tex == nil {
continue
}
s := render.SizeOfCharacter(r)
for _, v := range faceVertices[direction.North].verts {
vert := &render.ModelVertex{
X: float32(v.X)*float32(s*0.01) - float32(offset+s*0.01) - 0.01,
Y: float32(v.Y)*0.16 - 0.08 - 0.01,
Z: 0.05,
Texture: tex,
TextureX: float64(v.TOffsetX),
TextureY: float64(v.TOffsetY),
R: 64,
G: 64,
B: 64,
A: 255,
}
verts = append(verts, vert)
}
for _, v := range faceVertices[direction.North].verts {
vert := &render.ModelVertex{
X: float32(v.X)*float32(s*0.01) - float32(offset+s*0.01),
Y: float32(v.Y)*0.16 - 0.08,
Z: 0,
Texture: tex,
TextureX: float64(v.TOffsetX),
TextureY: float64(v.TOffsetY),
R: 255,
G: 255,
B: 255,
A: 255,
}
verts = append(verts, vert)
}
offset += (s + 2) * 0.01
}
return verts
}
func esPlayerModelRemove(p *playerModelComponent) {
if p.skin != "" {
render.FreeSkin(p.skin)
}
if p.cape != "" {
render.FreeSkin(p.cape)
}
if p.heldModel != nil {
p.heldModel.Free()
}
}
func esModelRemove(p interface {
Model() *render.Model
}) {
if p.Model() != nil {
p.Model().Free()
}
}
var moveLimit = 1e-5
func esPlayerModelTick(p *playerModelComponent,
pos PositionComponent, t *targetPositionComponent, r RotationComponent) {
x, y, z := pos.Position()
model := p.model
model.X, model.Y, model.Z = -float32(x), -float32(y), float32(z)
if p.heldModel != nil {
p.heldModel.X, p.heldModel.Y, p.heldModel.Z = -float32(x), -float32(y), float32(z)
p.heldModel.BlockLight, p.heldModel.SkyLight = p.model.BlockLight, p.model.SkyLight
}
offMat := mgl32.Translate3D(float32(x), -float32(y), float32(z)).
Mul4(mgl32.Rotate3DY(math.Pi - float32(r.Yaw())).Mat4())
if p.cape != "" {
if t.stillTime < 5.0 {
if p.capeTime < 30 {
p.capeTime += Client.delta
} else {
p.capeTime = 30
}
} else {
if p.capeTime > 0 {
p.capeTime -= Client.delta
} else {
p.capeTime = 0
}
}
p.model.Matrix[playerModelCape] = offMat.Mul4(mgl32.Translate3D(0, -24/16.0, 2/16.0)).
Mul4(mgl32.Rotate3DX(float32(p.capeTime)/60.0 + 0.05).Mat4())
}
// TODO This isn't the most optimal way of doing this
if p.hasNameTag {
val := math.Atan2(x-render.Camera.X, z-render.Camera.Z)
model.Matrix[playerModelNameTag] = mgl32.Translate3D(float32(x), -float32(y), float32(z)).
Mul4(mgl32.Translate3D(0, -12/16.0-12/16.0-0.6, 0)).
Mul4(mgl32.Rotate3DY(float32(val)).Mat4())
}
model.Matrix[playerModelHead] = offMat.Mul4(mgl32.Translate3D(0, -12/16.0-12/16.0, 0)).
Mul4(mgl32.Rotate3DX(float32(r.Pitch())).Mat4())
model.Matrix[playerModelBody] = offMat.Mul4(mgl32.Translate3D(0, -12/16.0-6/16.0, 0))
time := p.time
dir := p.dir
if dir == 0 {
dir = 1
time = 15
}
ang := ((time / 15) - 1) * (math.Pi / 4)
model.Matrix[playerModelLegRight] = offMat.Mul4(mgl32.Translate3D(2/16.0, -12/16.0, 0)).
Mul4(mgl32.Rotate3DX(float32(ang)).Mat4())
model.Matrix[playerModelLegLeft] = offMat.Mul4(mgl32.Translate3D(-2/16.0, -12/16.0, 0)).
Mul4(mgl32.Rotate3DX(-float32(ang)).Mat4())
iTime := p.idleTime
iTime += Client.delta * 0.02
if iTime > math.Pi*2 {
iTime -= math.Pi * 2
}
p.idleTime = iTime
if p.armTime <= 0 {
p.armTime = 0
} else {
p.armTime -= Client.delta
}
model.Matrix[playerModelArmRight] = offMat.Mul4(mgl32.Translate3D(6/16.0, -12/16.0-12/16.0, 0))
model.Matrix[playerModelArmRight] = model.Matrix[playerModelArmRight].
Mul4(mgl32.Rotate3DX(-float32(ang * 0.75)).Mat4()).
Mul4(mgl32.Rotate3DZ(float32(math.Cos(iTime)*0.06) - 0.06).Mat4()).
Mul4(mgl32.Rotate3DX(float32(math.Sin(iTime)*0.06) - float32((7.5-math.Abs(p.armTime-7.5))/7.5)).Mat4())
if p.heldModel != nil {
p.heldModel.Matrix[0] = offMat.Mul4(mgl32.Translate3D(6/16.0, -12/16.0-12/16.0, 0.0)).
Mul4(mgl32.Rotate3DX(-float32(ang * 0.75)).Mat4()).
Mul4(mgl32.Rotate3DZ(float32(math.Cos(iTime)*0.06) - 0.06).Mat4()).
Mul4(mgl32.Rotate3DX(float32(math.Sin(iTime)*0.06) - float32((7.5-math.Abs(p.armTime-7.5))/7.5)).Mat4()).
Mul4(mgl32.Translate3D(0, 11/16.0, -4/16.0)).
Mul4(p.heldMat)
}
model.Matrix[playerModelArmLeft] = offMat.Mul4(mgl32.Translate3D(-6/16.0, -12/16.0-12/16.0, 0)).
Mul4(mgl32.Rotate3DX(float32(ang * 0.75)).Mat4()).
Mul4(mgl32.Rotate3DZ(-float32(math.Cos(iTime)*0.06) + 0.06).Mat4()).
Mul4(mgl32.Rotate3DX(-float32(math.Sin(iTime) * 0.06)).Mat4())
update := true
if (!p.manualMove && t.X == t.sX && t.Y == t.sY && t.Z == t.sZ) || (p.manualMove && !p.walking) {
if t.stillTime > 5.0 {
if math.Abs(time-15) <= 1.5*Client.delta {
time = 15
update = false
}
dir = math.Copysign(1, 15-time)
} else {
t.stillTime += Client.delta
}
} else {
t.stillTime = 0
}
if update {
time += Client.delta * 1.5 * dir
if time > 30 {
time = 30
dir = -1
} else if time < 0 {
time = 0
dir = 1
}
}
p.dir = dir
p.time = time
}