Files
galaxy-game/pkg/generator/plotter.go
T
2025-09-11 23:26:32 +03:00

61 lines
1.6 KiB
Go

package generator
import (
"errors"
"fmt"
"math/rand"
"github.com/iliadenisov/galaxy/pkg/bitmap"
)
type Plotter struct {
factor float32
clearFn func()
circleFn func(x, y int, r float32)
freeCountFn func() int
freeNumberToCoordFn func(int) (int, int)
}
func NewPlotter(width, height uint, factor float32) (Plotter, error) {
if factor > 1 || factor <= 0 {
return Plotter{}, fmt.Errorf("factor should be: 0 > F <= 1")
}
sectorsX := uint32(float32(width) / factor)
sectorsY := uint32(float32(height) / factor)
bm := bitmap.NewBitmap(sectorsX, sectorsY)
return Plotter{
factor: factor,
clearFn: bm.Clear,
circleFn: func(x, y int, r float32) { bm.Circle(x, y, r, true) },
freeCountFn: bm.FreeCount,
freeNumberToCoordFn: func(n int) (x int, y int) {
x, y, err := bm.GetFreeN(n)
if err != nil {
panic(err)
}
return
},
}, nil
}
func (p Plotter) RandomFreePoint(deadZoneRaduis float32) (float32, float32, error) {
fsCount := p.freeCountFn()
if fsCount == 0 {
return 0, 0, errors.New("no more space for planets")
}
next := rand.Intn(fsCount)
x, y := p.freeNumberToCoordFn(next)
p.PlotDeadZone(x, y, deadZoneRaduis)
planetX := float32(x)*p.factor + rand.Float32()*p.factor // TODO: correct shift?
planetY := float32(y)*p.factor + rand.Float32()*p.factor
return planetX, planetY, nil
}
func (p Plotter) MarkDeadZone(x, y float32, radius float32) {
p.PlotDeadZone(int(x/p.factor), int(y/p.factor), radius)
}
func (p Plotter) PlotDeadZone(x, y int, radius float32) {
p.circleFn(x, y, radius/p.factor)
}