From 087c981ed3e73af7b2a0bd3a5c3aa80a3cee0bdb Mon Sep 17 00:00:00 2001 From: Tomas Doran Date: Sun, 10 Jan 2021 21:06:04 +0000 Subject: [PATCH] Diagonals and floats are annoying --- TODO | 1 - logical/vec.go | 7 ++++++- logical/vec_test.go | 13 +++++++++++++ screen/utils.go | 2 +- 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/TODO b/TODO index 1ae74da..8f70d63 100644 --- a/TODO +++ b/TODO @@ -18,4 +18,3 @@ Bug / missing feature list: Bugs: - Players can attack walls? - - Line of sight calculation seems a bit harsh currently diff --git a/logical/vec.go b/logical/vec.go index ac321f6..8d08ba7 100644 --- a/logical/vec.go +++ b/logical/vec.go @@ -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 } diff --git a/logical/vec_test.go b/logical/vec_test.go index 3274df9..65b848d 100644 --- a/logical/vec_test.go +++ b/logical/vec_test.go @@ -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) + } +} diff --git a/screen/utils.go b/screen/utils.go index d3bcae5..8eebb9b 100644 --- a/screen/utils.go +++ b/screen/utils.go @@ -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) {