new server
This commit is contained in:
46
controllers/world.go
Normal file
46
controllers/world.go
Normal 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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user