-
Notifications
You must be signed in to change notification settings - Fork 0
/
blind_spot.go
290 lines (253 loc) · 7.86 KB
/
blind_spot.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
package systems
import (
"image/color"
"math"
"sort"
"github.com/hajimehoshi/ebiten/v2"
gc "github.com/kijimaD/ruins/lib/components"
ec "github.com/kijimaD/ruins/lib/engine/components"
w "github.com/kijimaD/ruins/lib/engine/world"
"github.com/kijimaD/ruins/lib/utils/camera"
ecs "github.com/x-hgg-x/goecs/v2"
)
var (
// 影生成時の、マスクのベースとして使う黒画像。
// TODO: Resourceに配置すべき
shadowImage = ebiten.NewImage(1000, 1000)
)
// 遮蔽物を隠す
func BlindSpotSystem(world w.World, screen *ebiten.Image) {
gameComponents := world.Components.Game.(*gc.Components)
var pos *gc.Position
world.Manager.Join(
gameComponents.Position,
gameComponents.Player,
gameComponents.SpriteRender,
).Visit(ecs.Visit(func(entity ecs.Entity) {
pos = gameComponents.Position.Get(entity).(*gc.Position)
}))
shadowImage.Fill(color.Black)
rays := rayCasting(float64(pos.X), float64(pos.Y), world)
// 全面が黒の画像から、三角形の部分をブレンドで引いて、影になっている部分だけ黒で残す
{
opt := &ebiten.DrawTrianglesOptions{}
opt.Address = ebiten.AddressRepeat
opt.Blend = ebiten.BlendSourceOut
for i, line := range rays {
nextLine := rays[(i+1)%len(rays)]
// Draw triangle of area between rays
// vertices: 頂点
v := rayVertices(float64(pos.X), float64(pos.Y), nextLine.X2, nextLine.Y2, line.X2, line.Y2)
shadowImage.DrawTriangles(v, []uint16{0, 1, 2}, blackImage, opt)
}
}
// Draw rays
// for _, r := range rays {
// vector.StrokeLine(screen, float32(r.X1), float32(r.Y1), float32(r.X2), float32(r.Y2), 1, color.RGBA{255, 255, 0, 150}, true)
// }
{
op := &ebiten.DrawImageOptions{}
op.ColorScale.ScaleAlpha(0.9)
camera.SetTranslate(world, op)
screen.DrawImage(shadowImage, op)
}
}
func rayVertices(x1, y1, x2, y2, x3, y3 float64) []ebiten.Vertex {
return []ebiten.Vertex{
{DstX: float32(x1), DstY: float32(y1), ColorR: 0, ColorG: 0, ColorB: 0, ColorA: 0},
{DstX: float32(x2), DstY: float32(y2), ColorR: 1, ColorG: 1, ColorB: 1, ColorA: 1},
{DstX: float32(x3), DstY: float32(y3), ColorR: 1, ColorG: 1, ColorB: 1, ColorA: 1},
}
}
func rayCasting(cx, cy float64, world w.World) []line {
const rayLength = 10000 // something large enough to reach all objects
objects := []Object{}
gameComponents := world.Components.Game.(*gc.Components)
world.Manager.Join(
gameComponents.SpriteRender,
gameComponents.BlockView,
gameComponents.Player.Not(),
).Visit(ecs.Visit(func(entity ecs.Entity) {
switch {
case entity.HasComponent(gameComponents.Position):
pos := gameComponents.Position.Get(entity).(*gc.Position)
spriteRender := gameComponents.SpriteRender.Get(entity).(*ec.SpriteRender)
sprite := spriteRender.SpriteSheet.Sprites[spriteRender.SpriteNumber]
x := float64(pos.X - sprite.Width/2)
y := float64(pos.Y - sprite.Height/2)
w := float64(sprite.Width)
h := float64(sprite.Height)
objects = append(objects, Object{walls: rect(x, y, w, h)})
case entity.HasComponent(gameComponents.GridElement):
grid := gameComponents.GridElement.Get(entity).(*gc.GridElement)
spriteRender := gameComponents.SpriteRender.Get(entity).(*ec.SpriteRender)
sprite := spriteRender.SpriteSheet.Sprites[spriteRender.SpriteNumber]
x := float64(int(grid.Row) * sprite.Width)
y := float64(int(grid.Col) * sprite.Height)
w := float64(sprite.Width)
h := float64(sprite.Height)
objects = append(objects, Object{walls: rect(x, y, w, h)})
}
}))
// 外周の壁。rayが必ずどこかに当たるようにしないといけない
{
var pos *gc.Position
world.Manager.Join(
gameComponents.Position,
gameComponents.Player,
gameComponents.SpriteRender,
).Visit(ecs.Visit(func(entity ecs.Entity) {
pos = gameComponents.Position.Get(entity).(*gc.Position)
}))
screenWidth := float64(world.Resources.ScreenDimensions.Width)
screenHeight := float64(world.Resources.ScreenDimensions.Height)
objects = append(objects, Object{rect(float64(pos.X)-screenWidth, float64(pos.Y)-screenHeight, float64(screenWidth*2), float64(screenHeight*2))})
}
var rays []line
for _, obj := range objects {
// Cast two rays per point
for _, p := range obj.points() {
l := line{cx, cy, p[0], p[1]}
angle := l.angle()
// 微妙に角度をつけて影を自然に見せる(直線にならないようにする)
for _, offset := range []float64{-0.005, 0.005} {
points := [][2]float64{}
ray := newRay(cx, cy, rayLength, angle+offset)
// Unpack all objects
for _, o := range objects {
// 矩形の1辺ごとに交差を検証する
for _, wall := range o.walls {
if px, py, ok := intersection(ray, wall); ok {
points = append(points, [2]float64{px, py})
}
}
}
// 視点から最も近い交点までの線分を rays スライスに追加する
// 最も近いものだけなので、オブジェクトで遮られるとその先に線分は出ない
min := math.Inf(1)
minIdx := -1
for i, p := range points {
d2 := (cx-p[0])*(cx-p[0]) + (cy-p[1])*(cy-p[1])
if d2 < min {
min = d2
minIdx = i
}
}
rays = append(rays, line{cx, cy, points[minIdx][0], points[minIdx][1]})
}
}
}
// Sort rays based on angle, otherwise light triangles will not come out right
sort.Slice(rays, func(i int, j int) bool {
return rays[i].angle() < rays[j].angle()
})
return rays
}
// 始点と終点
type line struct {
X1, Y1, X2, Y2 float64
}
func (l *line) angle() float64 {
return math.Atan2(l.Y2-l.Y1, l.X2-l.X1)
}
type Object struct {
walls []line
}
// 基本的に4点
func (o Object) points() [][2]float64 {
// すべてのセグメントの終点を取得
var points [][2]float64
for _, wall := range o.walls {
points = append(points, [2]float64{wall.X2, wall.Y2})
}
// パスが閉じてない場合、最初のポイントを足すことでパスを閉じる
// p := [2]float64{o.walls[0].X1, o.walls[0].Y1}
// if p[0] != points[len(points)-1][0] && p[1] != points[len(points)-1][1] {
// points = append(points, [2]float64{o.walls[0].X1, o.walls[0].Y1})
// }
return points
}
func rect(x, y, w, h float64) []line {
return []line{
{x, y, x, y + h},
{x, y + h, x + w, y + h},
{x + w, y + h, x + w, y},
{x + w, y, x, y},
}
}
// 角度を変えて新しい線を生成する
func newRay(x, y, length, angle float64) line {
return line{
X1: x,
Y1: y,
X2: x + length*math.Cos(angle),
Y2: y + length*math.Sin(angle),
}
}
// intersection calculates the intersection of given two lines.
func intersection(l1, l2 line) (float64, float64, bool) {
// https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line
denom := (l1.X1-l1.X2)*(l2.Y1-l2.Y2) - (l1.Y1-l1.Y2)*(l2.X1-l2.X2)
tNum := (l1.X1-l2.X1)*(l2.Y1-l2.Y2) - (l1.Y1-l2.Y1)*(l2.X1-l2.X2)
uNum := -((l1.X1-l1.X2)*(l1.Y1-l2.Y1) - (l1.Y1-l1.Y2)*(l1.X1-l2.X1))
if denom == 0 {
return 0, 0, false
}
t := tNum / denom
if t > 1 || t < 0 {
return 0, 0, false
}
u := uNum / denom
if u > 1 || u < 0 {
return 0, 0, false
}
x := l1.X1 + t*(l1.X2-l1.X1)
y := l1.Y1 + t*(l1.Y2-l1.Y1)
return x, y, true
}
func drawRect(screen *ebiten.Image, img *ebiten.Image, x, y, width, height float32, opt *ebiten.DrawTrianglesOptions) {
sx, sy := -width/2, -height/2
vs := []ebiten.Vertex{
{
DstX: x,
DstY: y,
SrcX: sx,
SrcY: sy,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: x + width,
DstY: y,
SrcX: sx + width,
SrcY: sy,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: x,
DstY: y + height,
SrcX: sx,
SrcY: sy + height,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
{
DstX: x + width,
DstY: y + height,
SrcX: sx + width,
SrcY: sy + height,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
},
}
screen.DrawTriangles(vs, []uint16{0, 1, 2, 1, 2, 3}, img, opt)
}