- commit
- 3b8bf1780f96211df1bff3c27159046a647ede2b
- parent
- 61c7c761cf74e646e94f5ff1e50c3644fd46a454
- Author
- Tobias Bengfort <tobias.bengfort@posteo.de>
- Date
- 2024-09-25 05:52
make monster move towards closest player
Diffstat
| M | game.go | 16 | +++++++++++++++- |
| M | geo.go | 28 | ++++++++++++++++++++++++++++ |
2 files changed, 43 insertions, 1 deletions
diff --git a/game.go b/game.go
@@ -98,11 +98,25 @@ func (monster *Monster) run() {
98 98 case <-monster.quit:
99 99 return
100 100 case <-ticker.C:
-1 101 bestDist := 100000
-1 102 dir := "left"
-1 103 for player := range monster.Game.Players {
-1 104 dist := monster.Pos.Dist(player.Pos)
-1 105 if dist < bestDist {
-1 106 bestDist = dist
-1 107 dir = monster.Pos.Dir(player.Pos)
-1 108 }
-1 109 }
-1 110
-1 111 if bestDist > 10 || !monster.Game.IsFree(monster.Pos.Move(dir)) {
-1 112 dir = RandomDir()
-1 113 }
-1 114
101 115 monster.Game.MMsg <- MonsterMessage{
102 116 monster,
103 117 Message{
104 118 "action": "move",
105 -1 "dir": RandomDir(),
-1 119 "dir": dir,
106 120 },
107 121 }
108 122 }
diff --git a/geo.go b/geo.go
@@ -16,6 +16,34 @@ type Rect struct {
16 16
17 17 var dirs = []string{"up", "right", "down", "left"}
18 18
-1 19 func dist(a int, b int) int {
-1 20 if a > b {
-1 21 return a - b
-1 22 } else {
-1 23 return b - a
-1 24 }
-1 25 }
-1 26
-1 27 func (point *Point) Dist(other Point) int {
-1 28 return dist(point.X, other.X) + dist(point.Y, other.Y)
-1 29 }
-1 30
-1 31 func (point *Point) Dir(other Point) string {
-1 32 if dist(point.X, other.X) > dist(point.Y, other.Y) {
-1 33 if point.X > other.X {
-1 34 return "left"
-1 35 } else {
-1 36 return "right"
-1 37 }
-1 38 } else {
-1 39 if point.Y > other.Y {
-1 40 return "up"
-1 41 } else {
-1 42 return "down"
-1 43 }
-1 44 }
-1 45 }
-1 46
19 47 func (point *Point) Move(dir string) Point {
20 48 if dir == "up" {
21 49 return Point{point.X, point.Y - 1}