Added send function for players

This commit is contained in:
Eduard Urbach 2024-02-15 21:01:42 +01:00
parent 2e7b385b46
commit dc0845cc71
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
4 changed files with 13 additions and 7 deletions

View File

@ -34,7 +34,7 @@ func (game *Game) ChatCommand(player *Player, message string) bool {
switch message { switch message {
case "/logout": case "/logout":
game.server.Send(Logout, nil, player.address) player.Send(Logout, nil)
game.players.Remove(player) game.players.Remove(player)
} }

View File

@ -72,7 +72,7 @@ func (game *Game) Run() {
// Broadcast sends the packet to all players. // Broadcast sends the packet to all players.
func (game *Game) Broadcast(code byte, data []byte) { func (game *Game) Broadcast(code byte, data []byte) {
game.players.Each(func(player *Player) { game.players.Each(func(player *Player) {
game.server.Send(code, data, player.address) player.Send(code, data)
}) })
} }
@ -83,6 +83,6 @@ func (game *Game) BroadcastOthers(code byte, data []byte, exclude *Player) {
return return
} }
game.server.Send(code, data, player.address) player.Send(code, data)
}) })
} }

View File

@ -73,7 +73,7 @@ func (game *Game) sendLoginSuccess(player *Player) {
response := []byte{Success} response := []byte{Success}
response = AppendString(response, player.ID) response = AppendString(response, player.ID)
response = AppendString(response, player.authToken) response = AppendString(response, player.authToken)
game.server.Send(Login, response, player.address) player.Send(Login, response)
} }
func getLoginData(data []byte) (string, string, error) { func getLoginData(data []byte) (string, string, error) {

View File

@ -10,6 +10,7 @@ import (
type Player struct { type Player struct {
*Account *Account
game *Game game *Game
server *Server
authToken string authToken string
address *net.UDPAddr address *net.UDPAddr
direction Vector3 direction Vector3
@ -22,6 +23,7 @@ func NewPlayer(address *net.UDPAddr, account *Account, game *Game) *Player {
Account: account, Account: account,
address: address, address: address,
game: game, game: game,
server: game.server,
} }
} }
@ -49,17 +51,21 @@ func (player *Player) State() []byte {
return state return state
} }
// Send sends a message to the player.
func (player *Player) Send(code byte, data []byte) {
player.server.Send(code, data, player.address)
}
// OnConnect is executed when a connection has been established. // OnConnect is executed when a connection has been established.
func (player *Player) OnConnect() { func (player *Player) OnConnect() {
fmt.Printf("%s connected.\n", player.Name) fmt.Printf("%s connected.\n", player.Name)
players := player.game.players players := player.game.players
server := player.game.server
players.Each(func(other *Player) { players.Each(func(other *Player) {
server.Send(PlayerAdd, other.State(), player.address) player.Send(PlayerAdd, other.State())
if other != player { if other != player {
server.Send(PlayerAdd, player.State(), other.address) other.Send(PlayerAdd, player.State())
} }
}) })
} }