2023-02-17 07:04:29 +03:00
|
|
|
package gx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2023-02-17 16:40:46 +03:00
|
|
|
"github.com/surdeus/godat/src/sparsex"
|
|
|
|
"fmt"
|
2023-02-17 07:04:29 +03:00
|
|
|
)
|
|
|
|
|
2023-02-17 16:40:46 +03:00
|
|
|
// The type represents order of drawing.
|
|
|
|
type Layer int
|
|
|
|
|
2023-02-17 07:04:29 +03:00
|
|
|
type WindowConfig struct {
|
|
|
|
Title string
|
|
|
|
Width, Height int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Engine struct {
|
|
|
|
wcfg *WindowConfig
|
2023-02-17 16:40:46 +03:00
|
|
|
layers *sparsex.Sparse[Layer, *[]Behaver]
|
|
|
|
behavers []Behaver
|
2023-02-17 07:04:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type engine Engine
|
|
|
|
|
|
|
|
func New(
|
|
|
|
cfg *WindowConfig,
|
|
|
|
) *Engine {
|
|
|
|
return &Engine{
|
|
|
|
wcfg: cfg,
|
2023-02-17 16:40:46 +03:00
|
|
|
layers: sparsex.New[
|
|
|
|
Layer,
|
|
|
|
*[]Behaver,
|
|
|
|
](true),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Engine) Add(l Layer, b Behaver) {
|
|
|
|
g, ok := e.layers.Get(l)
|
|
|
|
if !ok {
|
|
|
|
e.layers.Set(
|
|
|
|
l,
|
|
|
|
&[]Behaver{b},
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
set := append(*g, b)
|
|
|
|
*g = set
|
2023-02-17 07:04:29 +03:00
|
|
|
}
|
2023-02-17 16:40:46 +03:00
|
|
|
|
|
|
|
e.behavers = append(e.behavers, b)
|
2023-02-17 07:04:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) Update() error {
|
2023-02-17 16:40:46 +03:00
|
|
|
eng := (*Engine)(e)
|
|
|
|
for _, v := range eng.behavers {
|
|
|
|
v.Update(eng)
|
|
|
|
fmt.Println(v)
|
|
|
|
}
|
|
|
|
|
2023-02-17 07:04:29 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-17 16:40:46 +03:00
|
|
|
|
2023-02-17 07:04:29 +03:00
|
|
|
func (e *engine) Draw(s *ebiten.Image) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *engine) Layout(ow, oh int) (int, int) {
|
|
|
|
return e.wcfg.Width, e.wcfg.Height
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *Engine) Run() error {
|
|
|
|
ebiten.SetWindowTitle(e.wcfg.Title)
|
|
|
|
ebiten.SetWindowSize(e.wcfg.Width, e.wcfg.Height)
|
|
|
|
|
|
|
|
return ebiten.RunGame((*engine)(e))
|
|
|
|
}
|
|
|
|
|