32 lines
723 B
Go

package game
import (
"encoding/binary"
"errors"
"math"
"net"
)
var (
ErrUnknownAddress = errors.New("unknown address")
)
// Move updates the location and direction the client is currently moving towards.
func (game *Game) Move(data []byte, address *net.UDPAddr) error {
player := game.players.ByAddress(address)
if player == nil {
return ErrUnknownAddress
}
player.Position.X = math.Float32frombits(binary.LittleEndian.Uint32(data))
player.Position.Z = math.Float32frombits(binary.LittleEndian.Uint32(data[4:]))
update := []byte(player.ID)
update = AppendFloat(update, player.Position.X)
update = AppendFloat(update, player.Position.Z)
game.BroadcastOthers(PlayerMove, update, player)
return nil
}