new server
This commit is contained in:
43
controllers/players.go
Normal file
43
controllers/players.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"server/models"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
func (h *Hub) handShake(msg []byte, conn *websocket.Conn) *models.Player {
|
||||
reader := models.NewReader(msg)
|
||||
newID := reader.ReadU32()
|
||||
name := reader.ReadString()
|
||||
|
||||
player := &models.Player{
|
||||
Entity: models.Entity{
|
||||
Type: models.EntityPlayer,
|
||||
ID: newID,
|
||||
},
|
||||
Name: name,
|
||||
Conn: conn,
|
||||
}
|
||||
|
||||
h.Mu.Lock()
|
||||
h.Players[player.ID] = player
|
||||
h.Mu.Unlock()
|
||||
|
||||
return player
|
||||
}
|
||||
|
||||
func (h *Hub) updatePosition(x, y float32, player *models.Player) {
|
||||
h.Mu.Lock()
|
||||
defer h.Mu.Unlock()
|
||||
|
||||
player.X = x
|
||||
player.Y = y
|
||||
}
|
||||
|
||||
func (h *Hub) removePlayer(id uint32) {
|
||||
h.Mu.Lock()
|
||||
defer h.Mu.Unlock()
|
||||
|
||||
delete(h.Players, id)
|
||||
}
|
||||
Reference in New Issue
Block a user