Implemented jumps

This commit is contained in:
2024-01-26 22:45:26 +01:00
parent 48b8011199
commit f4e44c5882
9 changed files with 61 additions and 10 deletions

View File

@ -30,6 +30,18 @@ func (game *Game) Run() {
<-close
}
// BroadcastOthers sends the packet to all other players.
func (game *Game) BroadcastOthers(code byte, data []byte, exclude *Player) {
game.players.Each(func(other *Player) bool {
if other == exclude {
return true
}
game.server.Send(code, data, other.address)
return true
})
}
// start starts all game systems.
func (game *Game) start() {
go game.network()
@ -42,6 +54,7 @@ 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.SetHandler(packet.PlayerJump, game.Jump)
game.server.Run(4242)
}

13
server/game/Jump.go Normal file
View File

@ -0,0 +1,13 @@
package game
import (
"net"
"server/game/packet"
)
// Jump broadcasts the jump action.
func (game *Game) Jump(data []byte, address *net.UDPAddr, server *Server) error {
player := game.players.Get(address)
game.BroadcastOthers(packet.PlayerJump, []byte(player.ID), player)
return nil
}

View File

@ -17,14 +17,6 @@ func (game *Game) Move(data []byte, address *net.UDPAddr, server *Server) error
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
})
game.BroadcastOthers(packet.PlayerMove, update, player)
return nil
}

View File

@ -7,4 +7,6 @@ const (
PlayerAdd = 10
PlayerRemove = 11
PlayerMove = 12
PlayerJump = 13
PlayerAttack = 14
)