generator is generating now continent numbers and all is configurable

This commit is contained in:
Dawid Wysokiński 2020-06-22 15:42:19 +02:00
parent 791e7fb506
commit 0543938c2c
4 changed files with 72 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package generator
import (
"fmt"
"image"
"image/color"
"image/png"
@ -10,25 +11,44 @@ import (
"github.com/tribalwarshelp/shared/models"
)
const (
defaultBackgroundColor = "#69380e"
defaultMapSize = 1000
)
type Marker struct {
Villages []*models.Village
Color string
Name string
}
type Config struct {
Markers []*Marker
Destination io.Writer
MapSize int
Markers []*Marker
Destination io.Writer
MapSize int
ContinentGrid bool
ContinentNumbers bool
BackgroundColor string
}
func (cfg *Config) init() {
if cfg.BackgroundColor == "" {
cfg.BackgroundColor = defaultBackgroundColor
}
if cfg.MapSize <= 0 {
cfg.MapSize = defaultMapSize
}
}
func Generate(cfg Config) error {
cfg.init()
upLeft := image.Point{0, 0}
lowRight := image.Point{cfg.MapSize, cfg.MapSize}
img := image.NewRGBA(image.Rectangle{upLeft, lowRight})
backgroundColor, err := parseHexColorFast("#69380e")
backgroundColor, err := parseHexColorFast(cfg.BackgroundColor)
if err != nil {
return err
return errors.Wrap(err, "map-generator")
}
// Background.
@ -50,14 +70,30 @@ func Generate(cfg Config) error {
}
//Continents
for y := cfg.MapSize / 10; y < cfg.MapSize; y += cfg.MapSize / 10 {
for x := 0; x < cfg.MapSize; x++ {
img.Set(x, y, color.Black)
if cfg.ContinentGrid {
for y := cfg.MapSize / 10; y < cfg.MapSize; y += cfg.MapSize / 10 {
for x := 0; x < cfg.MapSize; x++ {
img.Set(x, y, color.Black)
}
}
for x := cfg.MapSize / 10; x < cfg.MapSize; x += cfg.MapSize / 10 {
for y := 0; y < cfg.MapSize; y++ {
img.Set(x, y, color.Black)
}
}
}
for x := cfg.MapSize / 10; x < cfg.MapSize; x += cfg.MapSize / 10 {
for y := 0; y < cfg.MapSize; y++ {
img.Set(x, y, color.Black)
if cfg.ContinentNumbers {
continent := 0
for y := cfg.MapSize / 10; y <= cfg.MapSize; y += cfg.MapSize / 10 {
for x := cfg.MapSize / 10; x <= cfg.MapSize; x += cfg.MapSize / 10 {
continentStr := fmt.Sprintf("%d", continent)
if continent < 10 {
continentStr = fmt.Sprintf("0%d", continent)
}
drawText(img, x-16, y-3, continentStr)
continent++
}
}
}

View File

@ -2,7 +2,12 @@ package generator
import (
"fmt"
"image"
"image/color"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
)
var (
@ -45,3 +50,16 @@ func parseHexColorFast(s string) (color.RGBA, error) {
}
return c, err
}
func drawText(img *image.RGBA, x, y int, text string) {
col := color.RGBA{200, 100, 0, 255}
point := fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)}
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(col),
Face: basicfont.Face7x13,
Dot: point,
}
d.DrawString(text)
}

5
go.mod
View File

@ -3,6 +3,7 @@ module github.com/tribalwarshelp/map-generator
go 1.14
require (
github.com/pkg/errors v0.9.1 // indirect
github.com/tribalwarshelp/shared v0.0.0-20200622084436-3a768c8bf574 // indirect
github.com/pkg/errors v0.9.1
github.com/tribalwarshelp/shared v0.0.0-20200622084436-3a768c8bf574
golang.org/x/image v0.0.0-20200618115811-c13761719519
)

3
go.sum
View File

@ -2,3 +2,6 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/tribalwarshelp/shared v0.0.0-20200622084436-3a768c8bf574 h1:y2EoH6zRK9Uc0AeswnJRYUUIQYcSLZB5VDFuxPCKxNM=
github.com/tribalwarshelp/shared v0.0.0-20200622084436-3a768c8bf574/go.mod h1:tf+2yTHasV6jAF3V2deZ9slNoCyBzC0fMdTjI7clf6Y=
golang.org/x/image v0.0.0-20200618115811-c13761719519 h1:1e2ufUJNM3lCHEY5jIgac/7UTjd6cgJNdatjPdFWf34=
golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=