Fix chat and other players not showing up

This commit is contained in:
2025-04-16 11:13:52 +02:00
parent 5bf962a18d
commit 555b8118f2
6 changed files with 80 additions and 33 deletions

View File

@ -2,6 +2,7 @@ package game
import (
"fmt"
"log"
"sync"
"time"
@ -54,6 +55,12 @@ func (c *Chat) HandleServerMessages(messages []*pb.ChatMessage) {
c.mutex.Lock()
defer c.mutex.Unlock()
if len(messages) == 0 {
return
}
log.Printf("Processing %d chat messages", len(messages))
// Convert protobuf messages to our local type
for _, msg := range messages {
localMsg := types.ChatMessage{
@ -69,6 +76,7 @@ func (c *Chat) HandleServerMessages(messages []*pb.ChatMessage) {
c.messages = c.messages[1:]
}
c.messages = append(c.messages, localMsg)
log.Printf("Added chat message from %s: %s", msg.Username, msg.Content)
// Scroll to latest message if it's not already visible
visibleMessages := int((chatHeight - inputHeight) / messageHeight)
@ -92,6 +100,9 @@ func (c *Chat) HandleServerMessages(messages []*pb.ChatMessage) {
ExpireTime: time.Now().Add(6 * time.Second),
}
otherPlayer.Unlock()
log.Printf("Added floating message to other player %d", msg.PlayerId)
} else {
log.Printf("Could not find other player %d to add floating message", msg.PlayerId)
}
}
}

View File

@ -24,6 +24,7 @@ type Game struct {
loginScreen *LoginScreen
isLoggedIn bool
cleanupOnce sync.Once
frameCounter int // For periodic logging
}
func New() *Game {
@ -129,7 +130,28 @@ func (g *Game) Update(deltaTime float32) {
g.Player.MoveTowards(g.Player.TargetPath[0], deltaTime, GetMapGrid())
}
// Periodically log information about other players
g.frameCounter++
if g.frameCounter%300 == 0 {
rl.TraceLog(rl.LogInfo, "There are %d other players", len(g.OtherPlayers))
for id, other := range g.OtherPlayers {
rl.TraceLog(rl.LogInfo, "Other player ID: %d, Position: (%f, %f, %f), Has model: %v",
id, other.PosActual.X, other.PosActual.Y, other.PosActual.Z, other.Model.Meshes != nil)
}
}
// Process other players
for _, other := range g.OtherPlayers {
if other == nil {
continue
}
// Make sure other players have models assigned
if other.Model.Meshes == nil {
g.AssignModelToPlayer(other)
}
// Update other player movement
if len(other.TargetPath) > 0 {
other.MoveTowards(other.TargetPath[0], deltaTime, GetMapGrid())
}
@ -167,8 +189,8 @@ func (g *Game) DrawMap() {
}
func (g *Game) DrawPlayer(player *types.Player, model rl.Model) {
player.Lock()
defer player.Unlock()
// No need for lock in rendering, we'll use a "take snapshot" approach
// This avoids potential deadlocks and makes the rendering more consistent
// Check for invalid model
if model.Meshes == nil || model.Meshes.VertexCount <= 0 {
@ -196,14 +218,14 @@ func (g *Game) DrawPlayer(player *types.Player, model rl.Model) {
if player.IsMoving && len(modelAsset.Animations.Walk) > 0 {
anim := modelAsset.Animations.Walk[0] // Use first walk animation
if anim.FrameCount > 0 {
player.AnimationFrame = player.AnimationFrame % anim.FrameCount
rl.UpdateModelAnimation(model, anim, player.AnimationFrame)
currentFrame := player.AnimationFrame % anim.FrameCount
rl.UpdateModelAnimation(model, anim, currentFrame)
}
} else if len(modelAsset.Animations.Idle) > 0 {
anim := modelAsset.Animations.Idle[0] // Use first idle animation
if anim.FrameCount > 0 {
player.AnimationFrame = player.AnimationFrame % anim.FrameCount
rl.UpdateModelAnimation(model, anim, player.AnimationFrame)
currentFrame := player.AnimationFrame % anim.FrameCount
rl.UpdateModelAnimation(model, anim, currentFrame)
}
}
}
@ -445,12 +467,15 @@ func (g *Game) AssignModelToPlayer(player *types.Player) {
return
}
modelIndex := int(player.ID) % len(g.Models)
// Make sure model index is positive for consistent player appearances
// Use abs value of ID to ensure consistent appearance for negative IDs
modelIndex := abs(int(player.ID)) % len(g.Models)
if modelIndex < 0 || modelIndex >= len(g.Models) {
// Prevent out of bounds access
modelIndex = 0
}
rl.TraceLog(rl.LogInfo, "Assigning model %d to player %d", modelIndex, player.ID)
modelAsset := g.Models[modelIndex]
// Validate model before assigning