32 lines
723 B
Go
Raw Normal View History

2024-01-26 16:14:20 +00:00
package game
import (
"encoding/binary"
2024-01-29 16:51:31 +00:00
"errors"
2024-01-26 16:14:20 +00:00
"math"
"net"
2024-01-29 16:51:31 +00:00
)
var (
ErrUnknownAddress = errors.New("unknown address")
2024-01-26 16:14:20 +00:00
)
// Move updates the location and direction the client is currently moving towards.
2024-01-29 16:51:31 +00:00
func (game *Game) Move(data []byte, address *net.UDPAddr) error {
player := game.players.ByAddress(address)
if player == nil {
return ErrUnknownAddress
}
2024-01-26 16:14:20 +00:00
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)
2024-01-30 10:33:32 +00:00
game.BroadcastOthers(PlayerMove, update, player)
2024-01-26 16:14:20 +00:00
return nil
}