69 lines
1.3 KiB
Go
69 lines
1.3 KiB
Go
|
package generator
|
||
|
|
||
|
import (
|
||
|
"image"
|
||
|
"image/color"
|
||
|
"image/png"
|
||
|
"io"
|
||
|
|
||
|
"github.com/pkg/errors"
|
||
|
"github.com/tribalwarshelp/shared/models"
|
||
|
)
|
||
|
|
||
|
type Marker struct {
|
||
|
Villages []*models.Village
|
||
|
Color string
|
||
|
}
|
||
|
|
||
|
type Config struct {
|
||
|
Markers []*Marker
|
||
|
Destination io.Writer
|
||
|
MapSize int
|
||
|
}
|
||
|
|
||
|
func Generate(cfg Config) error {
|
||
|
upLeft := image.Point{0, 0}
|
||
|
lowRight := image.Point{cfg.MapSize, cfg.MapSize}
|
||
|
|
||
|
img := image.NewRGBA(image.Rectangle{upLeft, lowRight})
|
||
|
backgroundColor, err := parseHexColorFast("#69380e")
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
// Background.
|
||
|
for y := 0; y < cfg.MapSize; y++ {
|
||
|
for x := 0; x < cfg.MapSize; x++ {
|
||
|
img.Set(x, y, backgroundColor)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Markers
|
||
|
for _, marker := range cfg.Markers {
|
||
|
parsedColor, err := parseHexColorFast(marker.Color)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
for _, village := range marker.Villages {
|
||
|
img.Set(village.X, village.Y, parsedColor)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//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)
|
||
|
}
|
||
|
}
|
||
|
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 err := png.Encode(cfg.Destination, img); err != nil {
|
||
|
return errors.Wrap(err, "map-generator")
|
||
|
}
|
||
|
return nil
|
||
|
}
|