Repository navigation
Expand file tree
/
Copy pathmap.go
More file actions
93 lines (79 loc) · 2.8 KB
/
Copy pathmap.go
File metadata and controls
93 lines (79 loc) · 2.8 KB
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
package tinyrogue
import "strconv"
// GameMap holds all the level and aggregate information for the entire world.
type GameMap struct {
Name string
Dungeons []Dungeon
CurrentDungeon string
CurrentLevel string
}
// NewGameMap creates a new set of maps for the entire game
// Using the predefined levels and dungeons.
func NewGameMap(name string, dungeons []Dungeon, startDungeon string, startLevel string) *GameMap {
return &GameMap{
Name: name,
Dungeons: dungeons,
CurrentDungeon: startDungeon,
CurrentLevel: startLevel}
}
// NewGeneratedGameMap generated a new set of dungeons and levels for the entire game.
func NewGeneratedGameMap(name string, dungeonCount int, levelCount int, floors, walls string) *GameMap {
dungeons := make([]Dungeon, 0)
for i := 0; i < dungeonCount; i++ {
d := NewDungeon(name+"-"+strconv.Itoa(i), floors, walls)
for j := 0; j < levelCount; j++ {
nextLevel := NewLevel(d.Name+"-"+strconv.Itoa(j), d.FloorTypes, d.WallTypes)
d.Levels = append(d.Levels, nextLevel)
}
dungeons = append(dungeons, d)
}
logDebug("Total dungeons created: " + strconv.Itoa(len(dungeons)))
// generate the first level of the first dungeon
firstDungeon := &dungeons[0]
firstLevel := firstDungeon.Levels[0]
firstLevel.Generate()
// put exit on first level to second level
if levelCount > 1 {
portalImg := CurrentGame().Images["portal"]
p := NewPortal("portal", &portalImg, firstDungeon, firstDungeon.NextLevel(firstLevel))
// Get a starting position first (where player would spawn)
startPos := firstLevel.OpenLocation()
// Place exit at a location reachable from start
exitPos := firstLevel.OpenLocationReachableFrom(startPos)
firstLevel.SetExit(p, exitPos)
}
return &GameMap{Name: name, Dungeons: dungeons, CurrentDungeon: firstDungeon.Name, CurrentLevel: firstLevel.Name}
}
// NewSingleGameMap creates a single level generated game map.
func NewSingleLevelGameMap() *GameMap {
return NewGeneratedGameMap("Dungeon", 1, 1, "floor", "wall")
}
// NewSingleGameMapWithTerrain creates a single level generated game map.
func NewSingleGameMapWithTerrain(floors, walls string) *GameMap {
return NewGeneratedGameMap("Dungeon", 1, 1, floors, walls)
}
// Dungeon returns a Dungeon by name.
func (gm *GameMap) Dungeon(name string) *Dungeon {
for _, d := range gm.Dungeons {
if d.Name == name {
return &d
}
}
return nil
}
// NextDungeon returns the next Dungeon in the list.
func (gm *GameMap) NextDungeon() *Dungeon {
for i, d := range gm.Dungeons {
if d.Name == gm.CurrentDungeon {
if i+1 < len(gm.Dungeons) {
return &gm.Dungeons[i+1]
}
}
}
return nil
}
// SetCurrentLevel sets the current Dungeon and Level in the game map.
func (gm *GameMap) SetCurrentLevel(d *Dungeon, l *Level) {
gm.CurrentDungeon = d.Name
gm.CurrentLevel = l.Name
}