-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.go
68 lines (58 loc) · 1.24 KB
/
world.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package main
import (
"fmt"
"math"
"math/rand"
"strings"
"github.com/google/uuid"
)
func NewWorld() {
n, err := NewNomad()
if err != nil {
fmt.Println(err)
}
r := NewRedis()
for i := 0; i < WorldStartingFlowers; i++ {
x := rand.Intn(WorldX)
y := rand.Intn(WorldY)
f := NewFlower(Location{X: x, Y: y})
r.SaveFlower(*f, true)
fmt.Printf("Created flower at %d %d\n", x, y)
}
for j := 0; j < WorldStartingHives; j++ {
x := rand.Intn(WorldX)
y := rand.Intn(WorldY)
h := NewHive(Location{X: x, Y: y})
h.SpawnBees(n, r, HiveStartingBees)
r.SaveHive(*h, true)
fmt.Printf("Created hive at %d %d\n", x, y)
}
}
type Location struct {
X int
Y int
}
func (a Location) distance(b Location) int {
dX := float64(b.X - a.X)
dY := float64(b.Y - a.Y)
return int(math.Sqrt(dX*dX + dY*dY))
}
func (a Location) bearing(b Location) int {
dX := float64(b.X - a.X)
dY := float64(b.Y - a.Y)
return int(math.Atan2(dY, dX))
}
func (a Location) moveTo(b Location) Location {
dX := float64(b.X - a.X)
dY := float64(b.Y - a.Y)
if math.Abs(dX) > math.Abs(dY) {
a.X += int(dX / math.Abs(dX))
} else {
a.Y += int(dY / math.Abs(dY))
}
return a
}
func NewId() string {
id := uuid.NewString()
return strings.Replace(id, "-", "", -1)
}