Skip to content

Commit c718cd9

Browse files
RedArcaneArchernorendren
authored andcommitted
point type to hold x, y coord
1 parent 507aaa0 commit c718cd9

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

fov/fov.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ expected of any grid-based implementation
66
package fov
77

88
import (
9-
"fmt"
109
"math"
1110
)
1211

@@ -17,9 +16,14 @@ type GridMap interface {
1716
IsOpaque(x, y int) bool
1817
}
1918

19+
//point to hold a x, y position
20+
type point struct {
21+
x, y int
22+
}
23+
2024
// gridSet is an efficient and idiomatic way to implement sets in go, as an empty struct takes up no space
2125
// 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{}
2327

2428
// View is the item which stores the visible set of cells any time it is called. This should be called any time
2529
// a player's position is updated
@@ -35,8 +39,8 @@ func New() *View {
3539
// Compute takes a GridMap implementation along with the x and y coordinates representing a player's current
3640
// position and will internally update the visibile set of tiles within the provided radius `r`
3741
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{}{}
4044
for i := 1; i <= 8; i++ {
4145
v.fov(grid, px, py, 1, 0, 1, i, radius)
4246
}
@@ -65,7 +69,7 @@ func (v *View) fov(grid GridMap, px, py, dist int, lowSlope, highSlope float64,
6569
if grid.InBounds(mapx, mapy) && distTo(px, py, mapx, mapy) < rad {
6670
// As long as a tile is within the bounds of the map, if we visit it at all, it is considered visible
6771
// 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{}{}
6973
}
7074

7175
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,
9094
// IsVisible takes in a set of x,y coordinates and will consult the visible set (as a gridSet) to determine
9195
// whether that tile is visible.
9296
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 {
9498
return true
9599
}
96100
return false
@@ -117,4 +121,4 @@ func distTo(x1, y1, x2, y2 int) int {
117121
vx := math.Pow(float64(x1-x2), 2)
118122
vy := math.Pow(float64(y1-y2), 2)
119123
return int(math.Sqrt(vx + vy))
120-
}
124+
}

0 commit comments

Comments
 (0)