refactoring the core app logic a bit

main
Nick Dumas 2 weeks ago
parent 55ddb818d9
commit 2819cfffc8

@ -1,12 +1,14 @@
package main package main
import ( import (
"context"
"flag" "flag"
"log/slog" "log/slog"
"os" "os"
"os/signal"
"syscall"
"github.com/EngoEngine/ecs" "code.ndumas.com/ndumas/muddy/core"
"code.ndumas.com/ndumas/muddy/systems" "code.ndumas.com/ndumas/muddy/systems"
) )
@ -19,35 +21,28 @@ func main() {
flag.Parse() flag.Parse()
var logLevel slog.Level app := core.New(level)
switch level {
case "INFO", "info":
logLevel = slog.LevelInfo
case "DEBUG", "debug":
logLevel = slog.LevelDebug
case "ERROR", "error":
logLevel = slog.LevelError
}
l := slog.New(slog.NewTextHandler(
os.Stderr, &slog.HandlerOptions{
Level: logLevel,
},
),
)
world := ecs.World{}
rs := &systems.RoomSystem{ rs := &systems.RoomSystem{
Rooms: make(map[uint64]*systems.Room, 0), Rooms: make(map[uint64]*systems.Room, 0),
L: l.With(slog.String("system", "room")), L: app.L.With(slog.String("system", "room")),
} }
world.AddSystem(rs) app.W.AddSystem(rs)
l.With( app.L.With(
slog.Int("room count", len(rs.Rooms)), slog.Int("room count", len(rs.Rooms)),
).Info("setup complete") ).Info("setup complete")
world.Update(1) ctx, cancel := context.WithCancel(context.Background())
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
cancel()
}()
app.Run(ctx)
} }

@ -10,6 +10,10 @@ type Location struct {
Exits []uint64 Exits []uint64
} }
type LocationLink struct {
To, From uint64
}
type Observable struct { type Observable struct {
Name string Name string
Description string Description string

@ -0,0 +1,69 @@
package core
import (
"bufio"
"context"
"log/slog"
"os"
"time"
"github.com/EngoEngine/ecs"
)
type App struct {
W *ecs.World
L *slog.Logger
}
func New(level string) *App {
var logLevel slog.Level
switch level {
case "INFO", "info":
logLevel = slog.LevelInfo
case "DEBUG", "debug":
logLevel = slog.LevelDebug
case "ERROR", "error":
logLevel = slog.LevelError
}
l := slog.New(slog.NewTextHandler(
os.Stderr, &slog.HandlerOptions{
Level: logLevel,
},
),
)
world := &ecs.World{}
return &App{
L: l,
W: world,
}
}
func (a *App) Run(ctx context.Context) {
go func() {
t := time.NewTicker(time.Millisecond * 30)
for range t.C {
a.W.Update(1)
}
}()
scanner := bufio.NewScanner(os.Stdin)
var line string
for {
select {
case <-ctx.Done():
a.L.Error("cancelled")
return
default:
if scanner.Scan() {
line = scanner.Text()
}
a.L.Info("scanning input")
a.L.With(slog.String("input", line)).Info("received command")
}
}
}

@ -3,12 +3,19 @@ package systems
import ( import (
"fmt" "fmt"
"log/slog" "log/slog"
"math/rand"
"github.com/EngoEngine/ecs" "github.com/EngoEngine/ecs"
"code.ndumas.com/ndumas/muddy/components" "code.ndumas.com/ndumas/muddy/components"
) )
type Exit struct {
*components.ID
*components.Observable
To, From uint64
}
type Room struct { type Room struct {
*components.ID *components.ID
*components.Location *components.Location
@ -32,8 +39,9 @@ func (rs *RoomSystem) New(w *ecs.World) {
ID: uint64(z), ID: uint64(z),
}, },
Location: &components.Location{ Location: &components.Location{
X: int64(i), X: int64(i),
Y: int64(j), Y: int64(j),
Exits: make([]uint64, 0),
}, },
Observable: &components.Observable{ Observable: &components.Observable{
Name: fmt.Sprintf("Room (%d,%d)", i, j), Name: fmt.Sprintf("Room (%d,%d)", i, j),
@ -53,6 +61,16 @@ func (rs *RoomSystem) New(w *ecs.World) {
} }
func (rs *RoomSystem) Update(dt float32) { func (rs *RoomSystem) Update(dt float32) {
rs.L.Debug("tick")
weather := []string{
"The sky is clear and birds sing.",
"It's dark and rainy.",
"It's raining men, hallelujah!",
}
if rand.Intn(2000) > 1990 {
fmt.Println(weather[rand.Intn(len(weather))])
}
// flush changes to persistent storage // flush changes to persistent storage
} }

Loading…
Cancel
Save