aboutsummaryrefslogtreecommitdiff
path: root/go/game/range.go
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fischer@muhq.space>2023-01-12 20:58:14 +0100
committerFlorian Fischer <florian.fischer@muhq.space>2023-01-26 22:17:17 +0100
commit6acdf43bc056ed8c4c09508d700f4ff24934c920 (patch)
treecb5b8a185a1fe37038406fbe7577a359516b6818 /go/game/range.go
parent2f49c5e7030b63e8f4d2a8d0d4cb55691ef1fb3a (diff)
downloadmuhqs-game-6acdf43bc056ed8c4c09508d700f4ff24934c920.tar.gz
muhqs-game-6acdf43bc056ed8c4c09508d700f4ff24934c920.zip
implement unit movement
Diffstat (limited to 'go/game/range.go')
-rw-r--r--go/game/range.go39
1 files changed, 39 insertions, 0 deletions
diff --git a/go/game/range.go b/go/game/range.go
new file mode 100644
index 00000000..86ec2f50
--- /dev/null
+++ b/go/game/range.go
@@ -0,0 +1,39 @@
+package game
+
+import (
+ "math"
+)
+
+func IsPositionInRange(origin Position, pos Position, r int) bool {
+ x, y := float64(pos.X-origin.X), float64(pos.Y-origin.Y)
+ ax, ay := math.Abs(x), math.Abs(y)
+ var abs float64
+ if pos.X > pos.Y {
+ abs = ax + math.Floor(ay/2)
+ } else {
+ abs = ay + math.Floor(ax/2)
+ }
+
+ return abs <= float64(r)
+}
+
+func PositionsInRange(origin Position, r int, includeOrigin bool) []Position {
+ positions := []Position{}
+ for x := -r; x <= r; x++ {
+ for y := -r; y <= r; y++ {
+ pos := Position{origin.X + x, origin.Y + y}
+ if pos.X < 0 || pos.Y < 0 {
+ continue
+ }
+
+ if !includeOrigin && pos == origin {
+ continue
+ }
+
+ if IsPositionInRange(origin, pos, r) {
+ positions = append(positions, pos)
+ }
+ }
+ }
+ return positions
+}