-
Notifications
You must be signed in to change notification settings - Fork 26
/
blockentity.go
128 lines (111 loc) · 3.32 KB
/
blockentity.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
// 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 (
"fmt"
"github.com/go-gl/mathgl/mgl32"
"github.com/thinkofdeath/steven/encoding/nbt"
"github.com/thinkofdeath/steven/entitysys"
"github.com/thinkofdeath/steven/render"
)
func init() {
addSystem(entitysys.Add, esSkullAdd)
addSystem(entitysys.Remove, esSkullRemove)
addSystem(entitysys.Add, esSignAdd)
addSystem(entitysys.Tick, lightBlockModel)
}
// updates the Colors of the model to fake lighting
func lightBlockModel(m interface {
Model() *render.Model
}, be BlockEntity) {
model := m.Model()
bp := be.Position()
bx, by, bz := bp.X, bp.Y, bp.Z
bl := float32(chunkMap.BlockLight(bx, by, bz))
sl := float32(chunkMap.SkyLight(bx, by, bz))
model.BlockLight, model.SkyLight = bl, sl
}
// BlockEntity is the interface for which all block entities
// must implement
type BlockEntity interface {
BlockComponent
}
type blockComponent struct {
Location Position
}
func (bc *blockComponent) Position() Position {
return bc.Location
}
func (bc *blockComponent) SetPosition(p Position) {
bc.Location = p
}
// BlockComponent is a component that defines the location
// of an entity when attached to a block.
type BlockComponent interface {
Position() Position
SetPosition(p Position)
}
// BlockNBTComponent is implemented by block entities that
// load information from nbt.
type BlockNBTComponent interface {
Deserilize(tag *nbt.Compound)
CanHandleAction(action int) bool
}
type blockBreakComponent struct {
blockComponent
stage int
model *render.Model
}
func (b *blockBreakComponent) SetStage(stage int) { b.stage = stage }
func (b *blockBreakComponent) Stage() int { return b.stage }
func (b *blockBreakComponent) Model() *render.Model { return b.model }
func (b *blockBreakComponent) Update() {
if b.model != nil {
b.model.Free()
}
bounds := chunkMap.Block(b.Location.X, b.Location.Y, b.Location.Z).CollisionBounds()
tex := render.GetTexture(fmt.Sprintf("blocks/destroy_stage_%d", b.stage))
var verts []*render.ModelVertex
for _, bo := range bounds {
// Slightly bigger than the block to prevent clipping
bo = bo.Grow(0.01, 0.01, 0.01)
verts = appendBox(verts,
bo.Min.X(), bo.Min.Y(), bo.Min.Z(),
bo.Max.X()-bo.Min.X(), bo.Max.Y()-bo.Min.Y(), bo.Max.Z()-bo.Min.Z(),
[6]render.TextureInfo{
tex, tex, tex, tex, tex, tex,
})
}
b.model = render.NewModel([][]*render.ModelVertex{verts})
b.model.Matrix[0] = mgl32.Translate3D(
float32(b.Location.X),
-float32(b.Location.Y),
float32(b.Location.Z),
)
}
// BlockBreakComponent is implemented by the block break animation
// entity
type BlockBreakComponent interface {
SetStage(stage int)
Stage() int
Update()
}
func newBlockBreakEntity() BlockEntity {
type blockBreak struct {
networkComponent
blockBreakComponent
}
b := &blockBreak{}
return b
}