new server

This commit is contained in:
Smile Rex
2026-01-14 23:10:23 +03:00
parent 83f356cea9
commit c1df49fde1
12 changed files with 342 additions and 69 deletions

46
controllers/world.go Normal file
View File

@@ -0,0 +1,46 @@
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.Players)))
for _, p := range h.Players {
w.WriteU32(p.ID)
w.WriteU8(uint8(models.EntityPlayer))
w.WriteF32(p.X)
w.WriteF32(p.Y)
w.WriteF32(p.Z)
w.WriteF32(p.Yaw)
}
msg := models.Message{
Type: MSG_SNAPSHOT,
Version: 1,
Payload: w.Bytes(),
}
for _, p := range h.Players {
if p.Conn != nil {
_ = p.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()
}
}