diff options
Diffstat (limited to 'internal/model/terrain.go')
| -rw-r--r-- | internal/model/terrain.go | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/internal/model/terrain.go b/internal/model/terrain.go new file mode 100644 index 0000000..7772620 --- /dev/null +++ b/internal/model/terrain.go @@ -0,0 +1,43 @@ +package model + +import "math/rand" + +type TerrainColor struct { + Color string `yaml:"color"` + Weight int `yaml:"weight"` +} + +type Terrain struct { + Name string `yaml:"name"` + Symbol string `yaml:"symbol"` + ASCII string `yaml:"ascii"` + Colors []TerrainColor `yaml:"colors"` +} + +func (t Terrain) GetSymbol(unicode bool) string { + if unicode { + return t.Symbol + } + return t.ASCII +} + +func (t Terrain) PickColor() string { + if len(t.Colors) == 0 { + return "0" + } + if len(t.Colors) == 1 { + return t.Colors[0].Color + } + total := 0 + for _, c := range t.Colors { + total += c.Weight + } + r := rand.Intn(total) + for _, c := range t.Colors { + r -= c.Weight + if r < 0 { + return c.Color + } + } + return t.Colors[0].Color +} |
