From e45066b2a886dacef8088c6bf1c1605a4c9ff029 Mon Sep 17 00:00:00 2001 From: bdnugget Date: Sun, 19 Jan 2025 21:52:56 +0100 Subject: [PATCH] Based db and accounts and log in where you logged out --- game/chat.go | 3 ++- game/game.go | 16 +++++++++++----- goonserver | 2 +- network/network.go | 23 ++++++++++++++++++++++- types/types.go | 1 + 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/game/chat.go b/game/chat.go index ea0dba1..df01004 100644 --- a/game/chat.go +++ b/game/chat.go @@ -53,6 +53,7 @@ func (c *Chat) HandleServerMessages(messages []*pb.ChatMessage) { for _, msg := range messages { localMsg := types.ChatMessage{ PlayerID: msg.PlayerId, + Username: msg.Username, Content: msg.Content, Time: time.Unix(0, msg.Timestamp), } @@ -117,7 +118,7 @@ func (c *Chat) Draw(screenWidth, screenHeight int32) { for i := startIdx; i < endIdx; i++ { msg := c.messages[i] - text := fmt.Sprintf("[%d]: %s", msg.PlayerID, msg.Content) + text := fmt.Sprintf("%s: %s", msg.Username, msg.Content) rl.DrawText(text, int32(chatX)+5, int32(messageY), 20, rl.White) messageY += messageHeight } diff --git a/game/game.go b/game/game.go index b432c46..5c4f001 100644 --- a/game/game.go +++ b/game/game.go @@ -68,16 +68,16 @@ func (g *Game) Update(deltaTime float32) { return } + // Assign model based on player ID + modelIndex := int(playerID) % len(g.Models) g.Player = &types.Player{ - PosActual: rl.NewVector3(5*types.TileSize, 0, 5*types.TileSize), - PosTile: GetTile(5, 5), Speed: 50.0, TargetPath: []types.Tile{}, UserData: g, QuitDone: make(chan struct{}), ID: playerID, - Model: g.Models[0].Model, - Texture: g.Models[0].Texture, + Model: g.Models[modelIndex].Model, + Texture: g.Models[modelIndex].Texture, } go network.HandleServerCommunication(conn, playerID, g.Player, g.OtherPlayers, g.QuitChan) @@ -208,7 +208,13 @@ func (g *Game) Render() { g.DrawMap() g.DrawPlayer(g.Player, g.Player.Model) for id, other := range g.OtherPlayers { - g.DrawPlayer(other, g.Models[int(id)%len(g.Models)].Model) + if other.Model.Meshes == nil { + // Assign model based on player ID for consistency + modelIndex := int(id) % len(g.Models) + other.Model = g.Models[modelIndex].Model + other.Texture = g.Models[modelIndex].Texture + } + g.DrawPlayer(other, other.Model) } rl.EndMode3D() diff --git a/goonserver b/goonserver index 52ab45f..3f7205d 160000 --- a/goonserver +++ b/goonserver @@ -1 +1 @@ -Subproject commit 52ab45fe53cba3ab2e09546c5ac0cd8bab3d73f4 +Subproject commit 3f7205d73e2c32fc8b28fba393c4bb8092e35d28 diff --git a/network/network.go b/network/network.go index e0bd2ea..c68953d 100644 --- a/network/network.go +++ b/network/network.go @@ -11,9 +11,12 @@ import ( "gitea.boner.be/bdnugget/goonscape/types" pb "gitea.boner.be/bdnugget/goonserver/actions" + rl "github.com/gen2brain/raylib-go/raylib" "google.golang.org/protobuf/proto" ) +const protoVersion = 1 + var serverAddr = "boner.be:6969" func SetServerAddr(addr string) { @@ -40,7 +43,8 @@ func ConnectToServer(username, password string, isRegistering bool) (net.Conn, i } authBatch := &pb.ActionBatch{ - Actions: []*pb.Action{authAction}, + Actions: []*pb.Action{authAction}, + ProtocolVersion: protoVersion, } if err := writeMessage(conn, authBatch); err != nil { @@ -69,6 +73,12 @@ func ConnectToServer(username, password string, isRegistering bool) (net.Conn, i return nil, 0, fmt.Errorf("failed to unmarshal auth response: %v", err) } + if response.ProtocolVersion > protoVersion { + conn.Close() + return nil, 0, fmt.Errorf("server requires newer protocol version (server: %d, client: %d)", + response.ProtocolVersion, protoVersion) + } + if !response.AuthSuccess { conn.Close() return nil, 0, fmt.Errorf(response.ErrorMessage) @@ -185,6 +195,17 @@ func HandleServerCommunication(conn net.Conn, playerID int32, player *types.Play for _, state := range serverMessage.Players { if state.PlayerId == playerID { + player.Lock() + // Update initial position if not set + if player.PosActual.X == 0 && player.PosActual.Z == 0 { + player.PosActual = rl.Vector3{ + X: float32(state.X * types.TileSize), + Y: 0, + Z: float32(state.Y * types.TileSize), + } + player.PosTile = types.Tile{X: int(state.X), Y: int(state.Y)} + } + player.Unlock() continue } diff --git a/types/types.go b/types/types.go index 094dcd5..3598281 100644 --- a/types/types.go +++ b/types/types.go @@ -39,6 +39,7 @@ type ModelAsset struct { type ChatMessage struct { PlayerID int32 + Username string Content string Time time.Time }