89 lines
1.8 KiB
Go
89 lines
1.8 KiB
Go
package generator
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
|
|
"github.com/iliadenisov/galaxy/pkg/generator/plotter"
|
|
)
|
|
|
|
type Map struct {
|
|
Width uint32
|
|
Height uint32
|
|
HomePlanets []PlanetarySystem
|
|
FreePlanets []Planet
|
|
plotter plotter.Plotter
|
|
}
|
|
|
|
type Coordinate struct {
|
|
X, Y float32
|
|
}
|
|
|
|
type Planet struct {
|
|
Position Coordinate
|
|
Size float32
|
|
Resources float32 // Сырьё
|
|
}
|
|
|
|
type PlanetarySystem struct {
|
|
HW Planet
|
|
DW []Planet
|
|
}
|
|
|
|
func NewMap(width, height, players uint32) (*Map, error) {
|
|
p, err := plotter.NewPlotter(width, height, defaultFactor)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("NewPlotter: %s", err)
|
|
}
|
|
return &Map{
|
|
Width: width,
|
|
Height: height,
|
|
HomePlanets: make([]PlanetarySystem, players),
|
|
plotter: p,
|
|
}, nil
|
|
}
|
|
|
|
func (m *Map) CreatePlanets(num int, deadZoneRadius float32, size, resources func() float32) error {
|
|
for range num {
|
|
coord, err := m.NewCoordinate(deadZoneRadius)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
planet := NewPlanet(coord, size(), resources())
|
|
m.AddPlanet(planet)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *Map) AddPlanet(planet Planet) {
|
|
m.FreePlanets = append(m.FreePlanets, planet)
|
|
}
|
|
|
|
func (m Map) NewCoordinate(deadZoneRaduis float32) (Coordinate, error) {
|
|
if x, y, err := m.plotter.RandomFreePoint(deadZoneRaduis); err != nil {
|
|
return Coordinate{}, fmt.Errorf("NewCoordinate: RandomFreePoint: %s", err)
|
|
} else {
|
|
return Coordinate{X: x, Y: y}, nil
|
|
}
|
|
}
|
|
|
|
func NewPlanet(c Coordinate, size, resources float32) Planet {
|
|
return Planet{
|
|
Position: c,
|
|
Size: size,
|
|
Resources: resources,
|
|
}
|
|
}
|
|
|
|
// RandI returns a random float32 value between min and max
|
|
func RandI(min, max float32) float32 {
|
|
return min + rand.Float32()*(max-min)
|
|
}
|
|
|
|
// RandIFn is a wrapper for the [RandI] func
|
|
func RandIFn(min, max float32) func() float32 {
|
|
return func() float32 {
|
|
return RandI(min, max)
|
|
}
|
|
}
|