-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtile.go
More file actions
135 lines (112 loc) · 2.47 KB
/
tile.go
File metadata and controls
135 lines (112 loc) · 2.47 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
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
package main
import (
"fmt"
"github.com/Pyroarsonist/map-gonerator/helpers"
"math/rand"
)
type Tile struct {
passability int
threatLevel int
isHeroStartLocation bool
isDestination bool
isHeroTrace bool
isHeroDeath bool
}
type Tiles []Tile
type TileMap []Tiles
type TileCoordinate struct {
width int
height int
}
type MapRender struct {
topology string
threat string
}
func createEmptyTileMap(size int) TileMap {
tMap := make(TileMap, size)
for i := 0; i < size; i++ {
tMap[i] = make(Tiles, size)
}
return tMap
}
func CreateRandomTileMap(config Config) TileMap {
fmt.Println("Generating random tiles")
r := helpers.GetRandomGenerator()
tMap := createEmptyTileMap(config.size)
for i := 0; i < config.size; i++ {
for j := 0; j < config.size; j++ {
var neighbours []int
/**
. . .
n x
*/
if i > 0 {
neighbours = append(neighbours, tMap[i-1][j].passability)
}
/**
. n .
. x
*/
if j > 0 {
neighbours = append(neighbours, tMap[i][j-1].passability)
}
/**
n . .
. x
*/
if i > 0 && j > 0 {
neighbours = append(neighbours, tMap[i-1][j-1].passability)
}
/**
. . n
. x
*/
if i < config.size-1 && j > 0 {
neighbours = append(neighbours, tMap[i+1][j-1].passability)
}
var passabilityArr []int
for n := range neighbours {
for w := n - config.maxHeightDiff; w <= n+config.maxHeightDiff; w++ {
if w > 0 {
passabilityArr = append(passabilityArr, w)
}
}
}
if len(passabilityArr) == 0 {
//todo: add water tiles (negative values)
passabilityArr = append(passabilityArr, r.Intn(config.maxTilePassability))
}
helpers.MakeUnique(&passabilityArr)
passability := helpers.GetRandomItem(passabilityArr)
tMap[i][j] = Tile{
passability: passability,
threatLevel: rand.Intn(config.maxThreatLevel),
}
}
}
return tMap
}
func (tileMap TileMap) GetRandomCoordinate() TileCoordinate {
r := helpers.GetRandomGenerator()
//todo: maybe refactor tilemap to data and size
size := len(tileMap[0])
return TileCoordinate{
width: r.Intn(size),
height: r.Intn(size),
}
}
func (tile *Tile) setHeroStartLocation() {
tile.isHeroStartLocation = true
tile.threatLevel = 0
}
func (tile *Tile) setDestinationPresence() {
tile.isDestination = true
tile.threatLevel = 0
}
func (tile *Tile) setHeroTrace() {
tile.isHeroTrace = true
tile.threatLevel = 0
}
func (tile *Tile) setHeroDeath() {
tile.isHeroDeath = true
}