You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.0 KiB
Go

package core
import (
"context"
"io"
"log/slog"
"time"
"github.com/EngoEngine/ecs"
)
type Printer struct {
}
type AppConfig struct {
LogDest io.Writer
LogLevel string
Port string
TickInterval time.Duration
}
type App struct {
Config AppConfig
W *ecs.World
L *slog.Logger
}
func New(ac AppConfig) *App {
var logLevel slog.Level
switch ac.LogLevel {
case "INFO", "info":
logLevel = slog.LevelInfo
case "DEBUG", "debug":
logLevel = slog.LevelDebug
case "ERROR", "error":
logLevel = slog.LevelError
}
l := slog.New(slog.NewTextHandler(
ac.LogDest, &slog.HandlerOptions{
Level: logLevel,
},
),
)
world := &ecs.World{}
return &App{
L: l,
W: world,
Config: ac,
}
}
func (a *App) Run(ctx context.Context) {
go func() {
t := time.NewTicker(a.Config.TickInterval)
for range t.C {
a.W.Update(1)
}
}()
s := NewServer(ctx, a.L, a.Config.Port)
go s.Start()
for {
select {
case <-ctx.Done():
return
case session := <-s.Sessions:
go session.Start()
}
}
}