Added player movement

This commit is contained in:
2024-01-26 17:14:20 +01:00
parent 2cd93f2a39
commit 8370afd9a1
12 changed files with 148 additions and 23 deletions

View File

@ -41,6 +41,7 @@ func (game *Game) start() {
func (game *Game) network() {
game.server.SetHandler(packet.Ping, game.Ping)
game.server.SetHandler(packet.Login, game.Login)
game.server.SetHandler(packet.PlayerMove, game.Move)
game.server.Run(4242)
}

View File

@ -3,7 +3,6 @@ package game
import (
"crypto/rand"
"encoding/base64"
"encoding/binary"
"encoding/json"
"errors"
"net"
@ -46,20 +45,14 @@ func (game *Game) Login(data []byte, address *net.UDPAddr, server *Server) error
player.KeepAlive()
response := []byte{Success}
response = appendString(response, account.ID)
response = appendString(response, player.authToken)
response = AppendString(response, account.ID)
response = AppendString(response, player.authToken)
server.Send(packet.Login, response, address)
game.onLogin(player)
return nil
}
func appendString(data []byte, str string) []byte {
data = binary.LittleEndian.AppendUint32(data, uint32(len(str)))
data = append(data, []byte(str)...)
return data
}
// Inform the newly logged in player about existing players.
// Also inform existing players about the newly logged in player.
func (game *Game) onLogin(player *Player) {

30
server/game/Move.go Normal file
View File

@ -0,0 +1,30 @@
package game
import (
"encoding/binary"
"math"
"net"
"server/game/packet"
)
// Move updates the location and direction the client is currently moving towards.
func (game *Game) Move(data []byte, address *net.UDPAddr, server *Server) error {
player := game.players.Get(address)
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.players.Each(func(other *Player) bool {
if other == player {
return true
}
game.server.Send(packet.PlayerMove, update, other.address)
return true
})
return nil
}

View File

@ -40,9 +40,9 @@ func (c *Player) Tick() {
// State returns the player state (ID, name and position).
func (player *Player) State() []byte {
state := []byte{}
state = appendString(state, player.ID)
state = appendString(state, player.Name)
state = appendVector3(state, player.Position)
state = AppendString(state, player.ID)
state = AppendString(state, player.Name)
state = AppendVector3(state, player.Position)
return state
}

10
server/game/String.go Normal file
View File

@ -0,0 +1,10 @@
package game
import "encoding/binary"
// AppendString appends the length of the string followed by its contents.
func AppendString(data []byte, str string) []byte {
data = binary.LittleEndian.AppendUint32(data, uint32(len(str)))
data = append(data, []byte(str)...)
return data
}

View File

@ -12,8 +12,8 @@ type Vector3 struct {
Z float32 `json:"z"`
}
// appendVector3 adds the raw bits of the vector to the given byte slice in XYZ order.
func appendVector3(data []byte, vector Vector3) []byte {
// AppendVector3 appends the raw bits of the vector to the given byte slice in XYZ order.
func AppendVector3(data []byte, vector Vector3) []byte {
bits := math.Float32bits(vector.X)
data = binary.LittleEndian.AppendUint32(data, bits)
@ -23,3 +23,17 @@ func appendVector3(data []byte, vector Vector3) []byte {
bits = math.Float32bits(vector.Z)
return binary.LittleEndian.AppendUint32(data, bits)
}
// AppendFloat appends the raw bits of the float to the given byte slice in XYZ order.
func AppendFloat(data []byte, value float32) []byte {
bits := math.Float32bits(value)
return binary.LittleEndian.AppendUint32(data, bits)
}
func Vector3FromBytes(data []byte) Vector3 {
return Vector3{
X: math.Float32frombits(binary.LittleEndian.Uint32(data)),
Y: math.Float32frombits(binary.LittleEndian.Uint32(data[4:])),
Z: math.Float32frombits(binary.LittleEndian.Uint32(data[8:])),
}
}