-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.go
134 lines (117 loc) · 2.44 KB
/
world.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
package main
import (
"github.com/hajimehoshi/ebiten/v2"
)
type World struct {
SizeX, SizeY int
ambientLight float64
player *Player
entities []*Entity
chars []*Character
}
func NewWorld(sizex, sizey int) World {
w := World{
SizeX: sizex,
SizeY: sizey,
ambientLight: 0.1,
}
var entities []*Entity
for x := 0; x < sizex; x++ {
for y := 0; y < sizey; y++ {
entities = append(entities, &Entity{
X: x,
Y: y,
Image: Images["ground"],
Description: "ground",
Walkable: true,
Visibility: w.ambientLight,
})
}
}
w.entities = entities
player := NewPlayer()
player.X = 25
player.Y = 20
w.player = player
w.chars = []*Character{
&Character{
Actor: Actor{
Entity: Entity{
X: 30,
Y: 20,
Image: Images["character"],
Visibility: 1.0,
ObstructsView: true,
Description: "character",
},
},
Speed: 1.0,
},
&Character{
Actor: Actor{
Entity: Entity{
X: 25,
Y: 25,
Image: Images["character"],
Visibility: 1.0,
ObstructsView: true,
Description: "character",
},
},
Speed: 1.0,
},
}
return w
}
func (w *World) CalculateVisibility() {
x, y := w.player.X, w.player.Y
visrange := w.player.VisibilityRange
// Reset visibility to default value
for _, e := range w.entities {
e.Visibility = w.ambientLight
}
for _, c := range w.chars {
c.Visibility = w.ambientLight
}
circle := CircleThickPoints(x, y, visrange)
for _, cp := range circle {
line := LinePoints(x, y, cp.X, cp.Y)
var (
brightness = 1.0
falloff float64 = (brightness - w.ambientLight) / float64(len(line))
)
for _, lp := range line {
if lp.X < 0 || lp.Y < 0 {
continue
}
ent := w.At(lp.X, lp.Y)
ent.Visibility = brightness
brightness -= falloff
// We draw the thing that is obstructing the view, and next iteration stop drawing
if ent.ObstructsView {
break
}
}
}
}
func (w *World) At(x, y int) *Entity {
for _, c := range w.chars {
if c.X == x && c.Y == y {
return &c.Entity
}
}
for _, ent := range w.entities {
if ent.X == x && ent.Y == y {
return ent
}
}
panic("entity not found at position")
}
func (w *World) Draw(screen *ebiten.Image) {
for _, ent := range w.entities {
ent.Draw(screen)
}
}
func (w *World) Tiles() *builder {
return newBuilder(w)
}