-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
lights.go
198 lines (173 loc) · 4.81 KB
/
lights.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
package main
import (
"math"
_ "time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
)
// Light structure for a light source
type light struct {
bounds *Bounds
color pixel.RGBA
angleSpread float64
angle float64
radius float32
redrawDt float64
imd *imdraw.IMDraw
canvas *pixelgl.Canvas
uPosX float32
uPosY float32
life float64
dynamic bool
objectBounds []*Bounds
updateObjectBoundsDt float64
objectCD bool
unlimitedLife bool
ownerBounds *Bounds
blinkFrequency float64
blinkDt float64
}
// Create a light
// Life == -1, infinite life
func (l *light) create(x, y, angle, spread, radius float64, color pixel.RGBA, dynamic bool, life float64) {
l.canvas = pixelgl.NewCanvas(pixel.R(0, 0, radius*2, radius*2))
l.canvas.SetComposeMethod(pixel.ComposeOver)
l.imd = imdraw.New(l.canvas)
// Default hit objects. Must explicitly be set otherwise.
l.objectCD = true
l.radius = float32(radius)
l.angle = angle
l.angleSpread = spread
l.color = color
l.life = life
l.dynamic = dynamic
l.bounds = &Bounds{
X: x,
Y: y,
Width: float64(radius * 2),
Height: float64(radius * 2),
entity: entity(l),
}
l.uPosX = float32(l.bounds.Width / 2)
l.uPosY = float32(l.bounds.Height / 2)
l.canvas.SetUniform("uPosX", &l.uPosY)
l.canvas.SetUniform("uPosY", &l.uPosY)
l.canvas.SetUniform("uRadius", &l.radius)
l.canvas.SetFragmentShader(fragmentShaderLight)
global.gWorld.AddObject(l.bounds)
}
// Hit a light soruce
func (l *light) hit(x, y, vx, vy float64, power int) {
}
// Destroy light
func (l *light) destroy() {
global.gWorld.qt.Remove(l.bounds)
}
// Get position
func (l *light) getPosition() pixel.Vec {
return pixel.Vec{X: l.bounds.X, Y: l.bounds.Y}
}
// Draw
func (l *light) draw(dt, elapsed float64) {
l.redrawDt += dt
if !l.dynamic {
if global.gRand.randFloat() < 0.01 {
return
}
// If "position" is destroyed, remove light.
if !global.gWorld.IsRegular(l.bounds.X, l.bounds.Y+1) {
global.gWorld.qt.Remove(l.bounds)
return
}
}
if l.objectCD {
l.updateObjectBoundsDt += dt
if l.updateObjectBoundsDt > 1/2 {
l.updateObjectBounds()
l.updateObjectBoundsDt = 0
}
}
if l.redrawDt > 1/20 {
l.redrawDt = 0
if l.dynamic {
l.life -= dt
if l.life <= 0 && !l.unlimitedLife {
global.gWorld.qt.Remove(l.bounds)
return
}
}
l.canvas.Clear(pixel.RGBA{R: 0, G: 0, B: 0, A: 0})
l.shine()
}
if l.blinkFrequency > 0 {
l.blinkDt += dt
if l.blinkDt <= l.blinkFrequency {
return
} else if l.blinkDt >= l.blinkFrequency {
l.blinkDt = 0
}
}
l.canvas.Draw(global.gWin, pixel.IM.Moved(pixel.V(l.bounds.X, l.bounds.Y)))
}
// Fetch object bounds around light
func (l *light) updateObjectBounds() {
// TBD: this might cause a lot of GC?
l.objectBounds = make([]*Bounds, 0)
for _, b := range global.gWorld.qt.RetrieveIntersections(&Bounds{X: l.bounds.X - float64(l.radius), Y: l.bounds.Y - float64(l.radius), Width: float64(l.radius * 2), Height: float64(l.radius * 2)}) {
switch b.entity.(type) {
case *chunk:
continue
case *light:
continue
case *mob:
continue
}
if b != l.ownerBounds {
l.objectBounds = append(l.objectBounds, b)
}
}
}
// Shine!
func (l *light) shine() {
addTo := float64(1 / l.radius)
l.imd.Clear()
l.imd.Push(pixel.Vec{X: l.bounds.Width / 2, Y: l.bounds.Height / 2})
l.imd.Color = l.color
last := pixel.Vec{X: -1, Y: -1}
// Raytrace around position (Using a bit of non-granular approach to speed up things)
for curAngle := l.angle - (l.angleSpread / 2); curAngle < l.angle+(l.angleSpread/2); curAngle += addTo * (180 / math.Pi) * 7 {
rads := curAngle * (math.Pi / 180)
end := pixel.Vec{X: l.bounds.X, Y: l.bounds.Y}
// Find next foreground.
for !global.gWorld.IsRegular(end.X, end.Y) && math.Abs((end.X-l.bounds.X)) < float64(l.radius) && math.Abs(end.Y-l.bounds.Y) < float64(l.radius) {
// Check if object, only if light should hit objects.
if l.objectCD {
next := false
for _, b := range l.objectBounds {
if end.X >= b.X && end.X < b.X+b.Width {
if end.Y >= b.Y && end.Y < b.Y+b.Height {
next = true
break
}
}
}
if next {
break
}
}
end.X += math.Cos(rads)
end.Y += math.Sin(rads)
}
if last.X == -1 {
last = end
}
l.imd.Push(pixel.Vec{X: end.X - l.bounds.X + l.bounds.Width/2, Y: end.Y - l.bounds.Y + l.bounds.Height/2})
}
// Add the first position again so we close the polygon if its 360 degrees
if l.angleSpread == 360 {
l.imd.Push(pixel.Vec{X: last.X - l.bounds.X + l.bounds.Width/2, Y: last.Y - l.bounds.Y + l.bounds.Height/2})
}
l.imd.Polygon(0)
l.imd.Draw(l.canvas)
}