All checks were successful
Create and publish a Docker image 🚀 / build-and-push-image (push) Successful in 1m19s
48 lines
786 B
Go
48 lines
786 B
Go
package controllers
|
|
|
|
import (
|
|
"server/models"
|
|
"time"
|
|
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
func (h *Hub) broadcastSnapshot() {
|
|
h.Mu.RLock()
|
|
defer h.Mu.RUnlock()
|
|
|
|
w := models.Writer{}
|
|
w.WriteU16(uint16(len(h.Entities)))
|
|
|
|
for _, e := range h.Entities {
|
|
w.WriteU32(e.GetID())
|
|
w.WriteU8(uint8(e.GetType()))
|
|
x, y, z := e.GetPosition()
|
|
w.WriteF32(x)
|
|
w.WriteF32(y)
|
|
w.WriteF32(z)
|
|
w.WriteF32(e.GetYaw())
|
|
}
|
|
|
|
msg := models.Message{
|
|
Type: MSG_SNAPSHOT,
|
|
Version: 1,
|
|
Payload: w.Bytes(),
|
|
}
|
|
|
|
for _, conn := range h.Clients {
|
|
if conn != nil {
|
|
_ = conn.WriteMessage(websocket.BinaryMessage, msg.Encode())
|
|
}
|
|
}
|
|
}
|
|
|
|
func (h *Hub) updateWorld() {
|
|
ticker := time.NewTicker(50 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
|
|
for range ticker.C {
|
|
h.broadcastSnapshot()
|
|
}
|
|
}
|