-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathentitysystems.go
165 lines (143 loc) · 4.31 KB
/
entitysystems.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
// 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 (
"math"
"github.com/go-gl/mathgl/mgl32"
"github.com/thinkofdeath/steven/entitysys"
"github.com/thinkofdeath/steven/render"
)
func init() {
addSystem(entitysys.Tick, esMoveToTarget)
addSystem(entitysys.Tick, esRotateToTarget)
addSystem(entitysys.Tick, esDrawOutline)
addSystem(entitysys.Tick, esLightModel)
addSystem(entitysys.Tick, esMoveChunk)
}
func esDrawOutline(p PositionComponent, s SizeComponent, d DebugComponent) {
x, y, z := p.Position()
bounds := s.Bounds().Shift(float32(x), float32(y), float32(z))
r, g, b := d.DebugColor()
render.DrawBox(
float64(bounds.Min.X()),
float64(bounds.Min.Y()),
float64(bounds.Min.Z()),
float64(bounds.Max.X()),
float64(bounds.Max.Y()),
float64(bounds.Max.Z()),
r, g, b, 255,
)
}
// updates the Colors of the model to fake lighting
func esLightModel(p PositionComponent, s SizeComponent, m interface {
Model() *render.Model
}) {
if m.Model() == nil {
return
}
xx, yy, zz := p.Position()
bounds := s.Bounds()
bounds = bounds.Shift(float32(xx), float32(yy), float32(zz))
c := mgl32.Vec3{float32(xx), float32(yy), float32(zz)}
c[1] += bounds.Max.Sub(bounds.Min).Mul(0.5).Y()
var light, skyLight float32
var count float32
for y := bounds.Min.Y(); y <= bounds.Max.Y(); y++ {
for z := bounds.Min.Z() - 1; z <= bounds.Max.Z()+1; z++ {
for x := bounds.Min.X() - 1; x <= bounds.Max.X()+1; x++ {
bx, by, bz := int(math.Floor(float64(x))), int(math.Floor(float64(y))), int(math.Floor(float64(z)))
bl := float32(chunkMap.BlockLight(bx, by, bz))
sl := float32(chunkMap.SkyLight(bx, by, bz))
dist := 1.0 - c.Sub(mgl32.Vec3{float32(bx) + 0.5, float32(by) + 0.5, float32(bz) + 0.5}).Len()
if dist < 0 {
continue
}
light += bl * dist
skyLight += sl * dist
count += dist
}
}
}
light /= count
skyLight /= count
model := m.Model()
model.BlockLight, model.SkyLight = light, skyLight
}
// Moves the entity from the previous chunk to its
// new chunk. Allows for optimized lookups
func esMoveChunk(e Entity, p *positionComponent) {
cx, cz := int(p.X)>>4, int(p.Z)>>4
if cx != p.CX || cz != p.CZ {
oc := chunkMap[chunkPosition{p.CX, p.CZ}]
if oc != nil {
oc.removeEntity(e)
}
c := chunkMap[chunkPosition{cx, cz}]
if c != nil {
c.addEntity(e)
p.CX, p.CZ = cx, cz
}
}
}
// Smoothly moves the entity from its current position to the target
// location
func esMoveToTarget(p PositionComponent, t *targetPositionComponent) {
px, py, pz := p.Position()
tx, ty, tz := t.TargetPosition()
if t.pX != tx || t.pY != ty || t.pZ != tz || t.time >= 4 {
t.sX, t.sY, t.sZ = px, py, pz
t.time = 0
t.pX = tx
t.pY = ty
t.pZ = tz
}
sx, sy, sz := t.sX, t.sY, t.sZ
dx, dy, dz := tx-sx, ty-sy, tz-sz
t.time = math.Min(4.0, t.time+Client.delta)
px = sx + dx*(1/4.0)*t.time
py = sy + dy*(1/4.0)*t.time
pz = sz + dz*(1/4.0)*t.time
p.SetPosition(px, py, pz)
}
// Smoothly rotates the entity from its current rotation to the target
// rotation
func esRotateToTarget(r RotationComponent, t *targetRotationComponent) {
py, pp := r.Yaw(), r.Pitch()
ty, tp := t.TargetYaw(), t.TargetPitch()
if t.pPitch != tp || t.pYaw != ty || t.time >= 4 {
t.sYaw, t.sPitch = py, pp
t.time = 0
t.pPitch = tp
t.pYaw = ty
}
sy, sp := t.sYaw, t.sPitch
dy, dp := ty-sy, tp-sp
// Make sure we go for the shortest route.
// e.g. (in degrees) 1 to 359 is quicker
// to decrease to wrap around than it is
// to increase all the way around
if dy > math.Pi || dy < -math.Pi {
sy += math.Copysign(math.Pi*2, dy)
dy = ty - sy
}
if dp > math.Pi || dp < -math.Pi {
sp += math.Copysign(math.Pi*2, dp)
dp = tp - sp
}
t.time = math.Min(4.0, t.time+Client.delta)
py = sy + dy*(1/4.0)*t.time
pp = sp + dp*(1/4.0)*t.time
r.SetPitch(pp)
r.SetYaw(py)
}