2025-06-22 17:54:07 +03:00
|
|
|
package game
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2025-06-22 22:27:56 +03:00
|
|
|
"code.haedhutner.dev/mvv/LastMUD/internal/game/command"
|
2025-06-25 20:14:07 +03:00
|
|
|
"code.haedhutner.dev/mvv/LastMUD/internal/game/components"
|
|
|
|
"code.haedhutner.dev/mvv/LastMUD/internal/game/ecs"
|
2025-06-22 22:27:56 +03:00
|
|
|
"code.haedhutner.dev/mvv/LastMUD/internal/logging"
|
2025-06-22 17:54:07 +03:00
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PlayerJoinEvent struct {
|
|
|
|
connectionId uuid.UUID
|
|
|
|
}
|
|
|
|
|
2025-06-22 22:27:56 +03:00
|
|
|
func (game *LastMUDGame) CreatePlayerJoinEvent(connId uuid.UUID) *PlayerJoinEvent {
|
2025-06-22 17:54:07 +03:00
|
|
|
return &PlayerJoinEvent{
|
|
|
|
connectionId: connId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pje *PlayerJoinEvent) Type() EventType {
|
|
|
|
return PlayerJoin
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pje *PlayerJoinEvent) Handle(game *LastMUDGame, delta time.Duration) {
|
2025-06-25 20:14:07 +03:00
|
|
|
p, err := CreatePlayer(game.world.World, pje.connectionId, components.PlayerStateJoining)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logging.Error("Unabled to create player: ", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
game.enqeueOutput(game.CreateOutput(p.AsUUID(), []byte("Welcome to LastMUD!")))
|
|
|
|
game.enqeueOutput(game.CreateOutput(p.AsUUID(), []byte("Please enter your name:")))
|
2025-06-22 17:54:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type PlayerLeaveEvent struct {
|
|
|
|
connectionId uuid.UUID
|
|
|
|
}
|
|
|
|
|
2025-06-22 22:27:56 +03:00
|
|
|
func (game *LastMUDGame) CreatePlayerLeaveEvent(connId uuid.UUID) *PlayerLeaveEvent {
|
2025-06-22 17:54:07 +03:00
|
|
|
return &PlayerLeaveEvent{
|
|
|
|
connectionId: connId,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ple *PlayerLeaveEvent) Type() EventType {
|
2025-06-22 22:27:56 +03:00
|
|
|
return PlayerLeave
|
2025-06-22 17:54:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ple *PlayerLeaveEvent) Handle(game *LastMUDGame, delta time.Duration) {
|
2025-06-25 20:14:07 +03:00
|
|
|
ecs.DeleteEntity(game.world.World, ecs.CreateEntity(ple.connectionId))
|
2025-06-22 22:27:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type PlayerCommandEvent struct {
|
|
|
|
connectionId uuid.UUID
|
|
|
|
command *command.CommandContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *LastMUDGame) CreatePlayerCommandEvent(connId uuid.UUID, cmdString string) (event *PlayerCommandEvent, err error) {
|
2025-06-24 16:37:26 +03:00
|
|
|
cmdCtx, err := command.CreateCommandContext(game.commandRegistry(), cmdString)
|
2025-06-22 22:27:56 +03:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
event = &PlayerCommandEvent{
|
|
|
|
connectionId: connId,
|
|
|
|
command: cmdCtx,
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pce *PlayerCommandEvent) Type() EventType {
|
|
|
|
return PlayerCommand
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pce *PlayerCommandEvent) Handle(game *LastMUDGame, delta time.Duration) {
|
|
|
|
if player == nil {
|
|
|
|
logging.Error("Unable to handle player command from player with id", pce.connectionId, ": Player does not exist")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-06-24 16:37:26 +03:00
|
|
|
event := pce.parseCommandIntoEvent(game, player)
|
|
|
|
}
|
|
|
|
|
2025-06-25 20:14:07 +03:00
|
|
|
func (pce *PlayerCommandEvent) parseCommandIntoEvent(game *LastMUDGame, player ecs.Entity) GameEvent {
|
2025-06-22 22:27:56 +03:00
|
|
|
switch pce.command.Command().Definition().Name() {
|
|
|
|
case SayCommand:
|
|
|
|
speech, err := pce.command.Command().Parameters()[0].AsString()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logging.Error("Unable to handle player speech from player with id", pce.connectionId, ": Speech could not be parsed: ", err.Error())
|
2025-06-24 16:37:26 +03:00
|
|
|
return nil
|
2025-06-22 22:27:56 +03:00
|
|
|
}
|
|
|
|
|
2025-06-24 16:37:26 +03:00
|
|
|
return game.CreatePlayerSayEvent(player, speech)
|
2025-06-22 22:27:56 +03:00
|
|
|
}
|
2025-06-24 16:37:26 +03:00
|
|
|
|
|
|
|
return nil
|
2025-06-22 22:27:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type PlayerSayEvent struct {
|
|
|
|
player *Player
|
|
|
|
speech string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *LastMUDGame) CreatePlayerSayEvent(player *Player, speech string) *PlayerSayEvent {
|
|
|
|
return &PlayerSayEvent{
|
|
|
|
player: player,
|
|
|
|
speech: speech,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pse *PlayerSayEvent) Type() EventType {
|
|
|
|
return PlayerSpeak
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pse *PlayerSayEvent) Handle(game *LastMUDGame, delta time.Duration) {
|
|
|
|
for _, p := range pse.player.CurrentRoom().Players() {
|
|
|
|
game.enqeueOutput(game.CreateOutput(p.Identity(), []byte(pse.player.id.String()+" in "+pse.player.CurrentRoom().Name+": "+pse.speech)))
|
|
|
|
}
|
2025-06-22 17:54:07 +03:00
|
|
|
}
|