Protobuf changes

This commit is contained in:
2025-01-13 00:31:15 +01:00
parent 4f36c2ee1f
commit 4012a2ed92
9 changed files with 150 additions and 59 deletions
+43 -34
View File
@@ -6,7 +6,6 @@ import (
"time"
pb "gitea.boner.be/bdnugget/goonserver/actions"
rl "github.com/gen2brain/raylib-go/raylib"
"google.golang.org/protobuf/proto"
)
@@ -42,39 +41,51 @@ func ConnectToServer() (net.Conn, int32, error) {
}
func HandleServerCommunication(conn net.Conn, playerID int32, player *Player, otherPlayers map[int32]*Player) {
// Ticker for sending actions aligned with server ticks
ticker := time.NewTicker(ServerTickRate)
defer ticker.Stop()
// Goroutine to handle sending player's actions to the server
go func() {
for {
for range ticker.C {
if len(player.ActionQueue) > 0 {
// Process the first action in the queue
actionData := player.ActionQueue[0]
action := &pb.Action{
PlayerId: playerID,
Type: pb.Action_MOVE,
X: int32(actionData.X),
Y: int32(actionData.Y),
// Bundle all actions that occurred during this tick
actions := make([]*pb.Action, 0, len(player.ActionQueue))
for _, actionData := range player.ActionQueue {
action := &pb.Action{
PlayerId: playerID,
Type: pb.Action_MOVE,
X: int32(actionData.X),
Y: int32(actionData.Y),
}
actions = append(actions, action)
}
// Serialize the action
data, err := proto.Marshal(action)
// Create a batch message
batch := &pb.ActionBatch{
PlayerId: playerID,
Actions: actions,
Tick: player.CurrentTick,
}
// Serialize the batch
data, err := proto.Marshal(batch)
if err != nil {
log.Printf("Failed to marshal action: %v", err)
log.Printf("Failed to marshal action batch: %v", err)
continue
}
// Send action to server
// Send batch to server
_, err = conn.Write(data)
if err != nil {
log.Printf("Failed to send action to server: %v", err)
log.Printf("Failed to send actions to server: %v", err)
return
}
// Remove the action from the queue once it's sent
player.ActionQueue = player.ActionQueue[1:]
// Clear the action queue after sending
player.ActionQueue = player.ActionQueue[:0]
}
// Add a delay to match the server's tick rate
time.Sleep(TickRate)
}
}()
@@ -93,27 +104,25 @@ func HandleServerCommunication(conn net.Conn, playerID int32, player *Player, ot
continue
}
// Check for tick synchronization
tickDiff := serverMessage.CurrentTick - player.CurrentTick
if tickDiff > MaxTickDesync {
log.Printf("Client too far behind (tick diff: %d), forcing resync", tickDiff)
player.ForceResync(serverMessage.Players[playerID])
}
// Update player's current tick
player.CurrentTick = serverMessage.CurrentTick
// Update other players' states
for _, state := range serverMessage.Players {
if state.PlayerId != playerID {
if otherPlayer, exists := otherPlayers[state.PlayerId]; exists {
// Instead of directly setting position, set up path for smooth movement
targetTile := Tile{X: int(state.X), Y: int(state.Y)}
if otherPlayer.PosTile != targetTile {
otherPlayer.TargetPath = []Tile{targetTile}
}
// Calculate interpolation based on server tick
otherPlayer.UpdatePosition(state, ServerTickRate)
} else {
// Initialize new player
otherPlayers[state.PlayerId] = &Player{
PosTile: Tile{X: int(state.X), Y: int(state.Y)},
PosActual: rl.Vector3{
X: float32(state.X * TileSize),
Y: float32(state.Y * TileHeight),
Z: float32(state.Y * TileSize),
},
ID: state.PlayerId,
Speed: 50.0, // Make sure to set the speed for smooth movement
}
otherPlayers[state.PlayerId] = NewPlayer(state)
}
}
}