@@ -6,7 +6,6 @@ expected of any grid-based implementation
6
6
package fov
7
7
8
8
import (
9
- "fmt"
10
9
"math"
11
10
)
12
11
@@ -17,9 +16,14 @@ type GridMap interface {
17
16
IsOpaque (x , y int ) bool
18
17
}
19
18
19
+ //point to hold a x, y position
20
+ type point struct {
21
+ x , y int
22
+ }
23
+
20
24
// gridSet is an efficient and idiomatic way to implement sets in go, as an empty struct takes up no space
21
25
// and nothing more than a set of keys is needed to store the range of visible cells
22
- type gridSet map [string ]struct {}
26
+ type gridSet map [point ]struct {}
23
27
24
28
// View is the item which stores the visible set of cells any time it is called. This should be called any time
25
29
// a player's position is updated
@@ -35,8 +39,8 @@ func New() *View {
35
39
// Compute takes a GridMap implementation along with the x and y coordinates representing a player's current
36
40
// position and will internally update the visibile set of tiles within the provided radius `r`
37
41
func (v * View ) Compute (grid GridMap , px , py , radius int ) {
38
- v .Visible = make (map [string ]struct {})
39
- v .Visible [fmt . Sprintf ( "%d,%d" , px , py ) ] = struct {}{}
42
+ v .Visible = make (map [point ]struct {})
43
+ v .Visible [point { px , py } ] = struct {}{}
40
44
for i := 1 ; i <= 8 ; i ++ {
41
45
v .fov (grid , px , py , 1 , 0 , 1 , i , radius )
42
46
}
@@ -65,7 +69,7 @@ func (v *View) fov(grid GridMap, px, py, dist int, lowSlope, highSlope float64,
65
69
if grid .InBounds (mapx , mapy ) && distTo (px , py , mapx , mapy ) < rad {
66
70
// As long as a tile is within the bounds of the map, if we visit it at all, it is considered visible
67
71
// That's the efficiency of shadowcasting, you just dont visit tiles that aren't visible
68
- v .Visible [fmt . Sprintf ( "%d,%d" , mapx , mapy ) ] = struct {}{}
72
+ v .Visible [point { mapx , mapy } ] = struct {}{}
69
73
}
70
74
71
75
if grid .InBounds (mapx , mapy ) && grid .IsOpaque (mapx , mapy ) {
@@ -90,7 +94,7 @@ func (v *View) fov(grid GridMap, px, py, dist int, lowSlope, highSlope float64,
90
94
// IsVisible takes in a set of x,y coordinates and will consult the visible set (as a gridSet) to determine
91
95
// whether that tile is visible.
92
96
func (v * View ) IsVisible (x , y int ) bool {
93
- if _ , ok := v .Visible [fmt . Sprintf ( "%d,%d" , x , y ) ]; ok {
97
+ if _ , ok := v .Visible [point { x , y } ]; ok {
94
98
return true
95
99
}
96
100
return false
@@ -117,4 +121,4 @@ func distTo(x1, y1, x2, y2 int) int {
117
121
vx := math .Pow (float64 (x1 - x2 ), 2 )
118
122
vy := math .Pow (float64 (y1 - y2 ), 2 )
119
123
return int (math .Sqrt (vx + vy ))
120
- }
124
+ }
0 commit comments