fs storage
This commit is contained in:
@@ -0,0 +1,75 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
|
||||
"galaxy/util"
|
||||
|
||||
"galaxy/game/internal/generator/plotter"
|
||||
)
|
||||
|
||||
type Map struct {
|
||||
Width uint32
|
||||
Height uint32
|
||||
HomePlanets []PlanetarySystem
|
||||
FreePlanets []Planet
|
||||
plotter plotter.Plotter
|
||||
}
|
||||
|
||||
type Coordinate struct {
|
||||
X, Y float64
|
||||
}
|
||||
|
||||
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(pc PlanetClass, num int, deadZoneRadius float64, size, resources func() float64) error {
|
||||
for range num {
|
||||
coord, err := m.NewCoordinate(deadZoneRadius)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
planet := NewPlanet(pc, 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 float64) (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 (m Map) ShortDistance(from, to Coordinate) float64 {
|
||||
return util.ShortDistance(m.Width, m.Height, from.X, from.Y, to.X, to.Y)
|
||||
}
|
||||
|
||||
// RandI returns a random float64 value between min and max
|
||||
func RandI(min, max float64) float64 {
|
||||
return min + rand.Float64()*(max-min)
|
||||
}
|
||||
|
||||
// RandIFn is a wrapper for the [RandI] func
|
||||
func RandIFn(min, max float64) func() float64 {
|
||||
return func() float64 {
|
||||
return RandI(min, max)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user