From 6acdf43bc056ed8c4c09508d700f4ff24934c920 Mon Sep 17 00:00:00 2001 From: Florian Fischer Date: Thu, 12 Jan 2023 20:58:14 +0100 Subject: implement unit movement --- go/game/range.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 go/game/range.go (limited to 'go/game/range.go') 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 +} -- cgit v1.2.3