-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdungeon.go
147 lines (130 loc) · 2.87 KB
/
dungeon.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package dungeongenerator
import (
"image"
"math/rand"
"sync"
"time"
)
type Dungeon struct {
*Room
roomCounter int
lock sync.Mutex
Rooms []*Room
}
type Room struct {
roomNumber int
size image.Rectangle
doorNorth *Room
doorSouth *Room
doorEast *Room
doorWest *Room
//monster *Monster
}
func NewDungeon() *Dungeon {
return &Dungeon{}
}
func (r *Room) GetRoom() int {
return r.roomNumber
}
func (r *Room) SetNorthDoor(end *Room) {
r.doorNorth = end
}
func (r *Room) SetSouthDoor(end *Room) {
r.doorSouth = end
}
func (r *Room) SetEastDoor(end *Room) {
r.doorEast = end
}
func (r *Room) SetWestDoor(end *Room) {
r.doorWest = end
}
func (r *Room) GetNorthDoor() *Room {
if r.doorNorth != nil {
return r.doorNorth
}
return nil
}
func (r *Room) GetSouthDoor() *Room {
if r.doorSouth != nil {
return r.doorSouth
}
return nil
}
func (r *Room) GetEastDoor() *Room {
if r.doorEast != nil {
return r.doorEast
}
return nil
}
func (r *Room) GetWestDoor() *Room {
if r.doorWest != nil {
return r.doorWest
}
return nil
}
func (d *Dungeon) CreateDungeon(rootRoom *Room, depth int) {
r := rootRoom
for i := 0; i < depth; i++ {
r = d.createRoom(r)
}
}
func (d *Dungeon) createRoom(room *Room) *Room {
tempCreated := []*Room{}
doors := []int{0, 1, 2, 3}
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(doors), func(i, j int) { doors[i], doors[j] = doors[j], doors[i] })
amountOfDoors := rand.Intn(4)
if amountOfDoors == 0 {
amountOfDoors = 2
}
for i := 0; i <= amountOfDoors; i++ {
switch doors[i] {
case 0:
if room.doorNorth == nil {
d.roomCounter++
room.SetNorthDoor(&Room{roomNumber: d.roomCounter, doorSouth: room, size: image.Rect(room.size.Min.X+2, room.size.Min.Y+2, room.size.Max.X+2, room.size.Max.Y+2)})
d.lock.Lock()
tempCreated = append(tempCreated, room.doorNorth)
d.Rooms = append(d.Rooms, room.doorNorth)
d.lock.Unlock()
} else {
break
}
case 1:
if room.doorEast == nil {
d.roomCounter++
room.SetEastDoor(&Room{roomNumber: d.roomCounter, doorWest: room})
d.lock.Lock()
tempCreated = append(tempCreated, room.doorEast)
d.Rooms = append(d.Rooms, room.doorEast)
d.lock.Unlock()
} else {
break
}
case 2:
if room.doorSouth == nil {
d.roomCounter++
room.SetSouthDoor(&Room{roomNumber: d.roomCounter, doorNorth: room})
d.lock.Lock()
tempCreated = append(tempCreated, room.doorSouth)
d.Rooms = append(d.Rooms, room.doorSouth)
d.lock.Unlock()
} else {
break
}
case 3:
if room.doorWest == nil {
d.roomCounter++
room.SetWestDoor(&Room{roomNumber: d.roomCounter, doorEast: room})
d.lock.Lock()
tempCreated = append(tempCreated, room.doorWest)
d.Rooms = append(d.Rooms, room.doorWest)
d.lock.Unlock()
} else {
break
}
}
}
randRoom := rand.Intn(len(tempCreated))
return tempCreated[randRoom]
}