LastMUD/internal/game/game.go

156 lines
3 KiB
Go
Raw Normal View History

2025-06-20 16:26:39 +03:00
package game
import (
"code.haedhutner.dev/mvv/LastMUD/internal/game/logic/world"
2025-06-20 16:26:39 +03:00
"context"
"sync"
"time"
"code.haedhutner.dev/mvv/LastMUD/internal/ecs"
"code.haedhutner.dev/mvv/LastMUD/internal/game/data"
"code.haedhutner.dev/mvv/LastMUD/internal/game/logic"
2025-06-20 16:26:39 +03:00
"code.haedhutner.dev/mvv/LastMUD/internal/logging"
2025-06-22 17:54:07 +03:00
"github.com/google/uuid"
2025-06-20 16:26:39 +03:00
)
const TickRate = 50 * time.Millisecond
2025-06-20 16:26:39 +03:00
type Output struct {
connId uuid.UUID
contents []byte
closeConnection bool
2025-06-22 17:54:07 +03:00
}
func (g Output) Id() uuid.UUID {
2025-06-22 17:54:07 +03:00
return g.connId
}
func (g Output) Contents() []byte {
2025-06-22 17:54:07 +03:00
return g.contents
2025-06-20 16:26:39 +03:00
}
func (g Output) ShouldCloseConnection() bool {
return g.closeConnection
}
type Game struct {
2025-06-20 16:26:39 +03:00
ctx context.Context
wg *sync.WaitGroup
2025-06-22 17:54:07 +03:00
world *World
2025-06-22 17:54:07 +03:00
output chan Output
2025-06-20 16:26:39 +03:00
}
func CreateGame(ctx context.Context, wg *sync.WaitGroup) (game *Game) {
game = &Game{
wg: wg,
ctx: ctx,
output: make(chan Output),
world: CreateGameWorld(),
2025-06-20 16:26:39 +03:00
}
ecs.RegisterSystems(game.world.World, logic.CreateSystems()...)
2025-06-22 22:27:56 +03:00
2025-06-20 16:26:39 +03:00
wg.Add(1)
go game.start()
return
}
// ConsumeNextOutput will block if no output present
func (game *Game) ConsumeNextOutput() *Output {
2025-06-24 16:37:26 +03:00
select {
case output := <-game.output:
return &output
default:
return nil
}
}
func (game *Game) ConnectPlayer(connectionId uuid.UUID) {
world.CreatePlayerConnectEvent(game.world.World, connectionId)
}
func (game *Game) DisconnectPlayer(connectionId uuid.UUID) {
world.CreatePlayerDisconnectEvent(game.world.World, connectionId)
}
func (game *Game) SendPlayerCommand(connectionId uuid.UUID, command string) {
world.CreatePlayerCommandEvent(game.world.World, connectionId, command)
}
func (game *Game) start() {
2025-06-20 16:26:39 +03:00
defer game.wg.Done()
defer game.shutdown()
logging.Info("Starting LastMUD...")
lastTick := time.Now()
for {
now := time.Now()
if game.shouldStop() {
break
}
game.tick(now.Sub(lastTick))
// Tick at regular intervals
if time.Since(lastTick) < TickRate {
time.Sleep(TickRate - time.Since(lastTick))
}
lastTick = now
}
}
func (game *Game) consumeOutputs() {
entities := ecs.FindEntitiesWithComponents(game.world.World, data.TypeConnectionId, data.TypeContents)
for _, entity := range entities {
output := Output{}
connId, _ := ecs.GetComponent[data.ConnectionIdComponent](game.world.World, entity)
output.connId = connId.ConnectionId
contents, hasContents := ecs.GetComponent[data.ContentsComponent](game.world.World, entity)
if hasContents {
output.contents = contents.Contents
}
_, shouldClose := ecs.GetComponent[data.CloseConnectionComponent](game.world.World, entity)
output.closeConnection = shouldClose
game.enqeueOutput(output)
}
2025-06-27 09:45:55 +03:00
ecs.DeleteEntities(game.world.World, entities...)
}
func (game *Game) shutdown() {
2025-06-20 16:26:39 +03:00
logging.Info("Stopping LastMUD...")
2025-06-22 17:54:07 +03:00
close(game.output)
2025-06-20 16:26:39 +03:00
}
func (game *Game) shouldStop() bool {
2025-06-20 16:26:39 +03:00
select {
case <-game.ctx.Done():
return true
default:
return false
}
}
func (game *Game) enqeueOutput(output Output) {
2025-06-22 17:54:07 +03:00
game.output <- output
}
func (game *Game) tick(delta time.Duration) {
game.world.Tick(delta)
game.consumeOutputs()
2025-06-20 16:26:39 +03:00
}