diff --git a/cmd/cli/guest/main.go b/cmd/cli/guest/main.go index 09712ba..c489c92 100644 --- a/cmd/cli/guest/main.go +++ b/cmd/cli/guest/main.go @@ -1,12 +1,14 @@ package main import ( + "context" "flag" "log/slog" "os" + "os/signal" + "syscall" - "github.com/EngoEngine/ecs" - + "code.ndumas.com/ndumas/muddy/core" "code.ndumas.com/ndumas/muddy/systems" ) @@ -19,35 +21,28 @@ func main() { flag.Parse() - 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{} + app := core.New(level) rs := &systems.RoomSystem{ 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)), ).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) + } diff --git a/components/components.go b/components/components.go index bd6838b..794c3dd 100644 --- a/components/components.go +++ b/components/components.go @@ -10,6 +10,10 @@ type Location struct { Exits []uint64 } +type LocationLink struct { + To, From uint64 +} + type Observable struct { Name string Description string diff --git a/core/core.go b/core/core.go new file mode 100644 index 0000000..f27528b --- /dev/null +++ b/core/core.go @@ -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") + } + } + +} diff --git a/systems/room.go b/systems/room.go index 0d9d6f3..dc135dc 100644 --- a/systems/room.go +++ b/systems/room.go @@ -3,12 +3,19 @@ package systems import ( "fmt" "log/slog" + "math/rand" "github.com/EngoEngine/ecs" "code.ndumas.com/ndumas/muddy/components" ) +type Exit struct { + *components.ID + *components.Observable + To, From uint64 +} + type Room struct { *components.ID *components.Location @@ -32,8 +39,9 @@ func (rs *RoomSystem) New(w *ecs.World) { ID: uint64(z), }, Location: &components.Location{ - X: int64(i), - Y: int64(j), + X: int64(i), + Y: int64(j), + Exits: make([]uint64, 0), }, Observable: &components.Observable{ 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) { + 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 }