Skip to content

Commit

Permalink
Diagonals and floats are annoying
Browse files Browse the repository at this point in the history
  • Loading branch information
bobtfish committed Jan 10, 2021
1 parent 71faf5a commit 087c981
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ Bug / missing feature list:

Bugs:
- Players can attack walls?
- Line of sight calculation seems a bit harsh currently
7 changes: 6 additions & 1 deletion logical/vec.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ func (v Vec) Path() []Vec {
Xcurrent, Ycurrent = Xstep, Ystep
path := make([]Vec, 0)
for Xcurrent < float64(w.X) || Ycurrent < float64(w.Y) {
path = append(path, V(int(Xcurrent)*Xsign, int(Ycurrent)*Ysign))
nextV := V(int(Xcurrent)*Xsign, int(Ycurrent)*Ysign)
path = append(path, nextV)
// Deal with cases where we are 1 step away in integer terms, but not there yet in floats
if (int(Xcurrent) == w.X || int(Xcurrent)+1 == w.X) && (int(Ycurrent) == w.Y || int(Ycurrent)+1 == w.Y) {
break
}
Xcurrent += Xstep
Ycurrent += Ystep
}
Expand Down
13 changes: 13 additions & 0 deletions logical/vec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,16 @@ func TestPathHalfDiagonalReverse(t *testing.T) {
t.Errorf("Path[2] not v(-3, -1) is v(%d, %d)", path[2].X, path[2].Y)
}
}

func TestPathFloatsAreAnnoying(t *testing.T) {
path := V(7, 3).Path()
if len(path) != 6 {
t.Errorf("Path len != 6 is %d: %v", len(path), path)
}
if path[5].Y != 2 {
t.Errorf("Path[5] Y not 2 is %d", path[5].Y)
}
if path[5].X != 6 {
t.Errorf("Path[5] X not 6 is %d", path[5].X)
}
}
2 changes: 1 addition & 1 deletion screen/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func init() {
}

func intToChar(i int) string {
return string('A' + i)
return string('A' + rune(i))
}

func drawMainBorder(win pixel.Target, sd render.SpriteDrawer) {
Expand Down

0 comments on commit 087c981

Please sign in to comment.