From f4e44c588243f2259ecce84c79734bcbc5c58d58 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 26 Jan 2024 22:45:26 +0100 Subject: [PATCH] Implemented jumps --- client/character/Character.gd | 3 +++ client/network/PlayerAdd.gd | 3 +++ client/network/PlayerJump.gd | 17 +++++++++++++++++ client/network/shared/PacketHandler.gd | 2 ++ client/world/Main.tscn | 8 +++++++- server/game/Game.go | 13 +++++++++++++ server/game/Jump.go | 13 +++++++++++++ server/game/Move.go | 10 +--------- server/game/packet/packet.go | 2 ++ 9 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 client/network/PlayerJump.gd create mode 100644 server/game/Jump.go diff --git a/client/character/Character.gd b/client/character/Character.gd index 7f23e9b..57122a2 100644 --- a/client/character/Character.gd +++ b/client/character/Character.gd @@ -1,6 +1,8 @@ class_name Character extends CharacterBody3D +signal jumped + @export var model: Node3D @export var move_speed: float = 4.5 @export var rotation_speed: float = 20.0 @@ -37,6 +39,7 @@ func jump(): return velocity.y = JUMP_VELOCITY + jumped.emit() func dash(): print("dash") diff --git a/client/network/PlayerAdd.gd b/client/network/PlayerAdd.gd index 0146188..02c5655 100644 --- a/client/network/PlayerAdd.gd +++ b/client/network/PlayerAdd.gd @@ -1,5 +1,7 @@ extends PacketHandler +signal main_player_spawned(player: Player) + @export var player_scene: PackedScene func _ready(): @@ -26,6 +28,7 @@ func handle_packet(data: PackedByteArray, _peer: PacketPeer): if player_id == Global.account_id: Global.player = player player.controller = PlayerController.new() + main_player_spawned.emit(player) else: player.controller = ProxyController.new() diff --git a/client/network/PlayerJump.gd b/client/network/PlayerJump.gd new file mode 100644 index 0000000..b5bb247 --- /dev/null +++ b/client/network/PlayerJump.gd @@ -0,0 +1,17 @@ +extends PacketHandler + +func _ready(): + %PlayerAdd.main_player_spawned.connect(on_main_player_spawned) + +func on_main_player_spawned(player: Player): + player.jumped.connect(on_jump) + +func on_jump(): + var buffer := StreamPeerBuffer.new() + buffer.put_8(PacketHandler.Packet.PLAYER_JUMP) + %Client.send(buffer.data_array) + +func handle_packet(data: PackedByteArray, _peer: PacketPeer): + var player_id := data.get_string_from_ascii() + var player := %Players.get_node(player_id) + player.jump() diff --git a/client/network/shared/PacketHandler.gd b/client/network/shared/PacketHandler.gd index 7b3f2e8..7f23e55 100644 --- a/client/network/shared/PacketHandler.gd +++ b/client/network/shared/PacketHandler.gd @@ -9,6 +9,8 @@ enum Packet { PLAYER_ADD = 10, PLAYER_REMOVE = 11, PLAYER_MOVE = 12, + PLAYER_JUMP = 13, + PLAYER_ATTACK = 14, } @export var packet_type: Packet diff --git a/client/world/Main.tscn b/client/world/Main.tscn index 099769f..7ba6e89 100644 --- a/client/world/Main.tscn +++ b/client/world/Main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=18 format=3 uid="uid://b40y7iuskv1ar"] +[gd_scene load_steps=19 format=3 uid="uid://b40y7iuskv1ar"] [ext_resource type="Script" path="res://Game.gd" id="1_uxop2"] [ext_resource type="Script" path="res://network/Client.gd" id="2_00qe8"] @@ -8,6 +8,7 @@ [ext_resource type="PackedScene" uid="uid://2lcnu3dy54lx" path="res://player/Player.tscn" id="6_jxdko"] [ext_resource type="Script" path="res://network/PlayerMove.gd" id="7_6xfox"] [ext_resource type="Script" path="res://world/Sun.gd" id="8_leyo3"] +[ext_resource type="Script" path="res://network/PlayerJump.gd" id="8_p6kmr"] [ext_resource type="Environment" uid="uid://dixa0yso2s1u3" path="res://world/Environment.tres" id="9_4u3r5"] [ext_resource type="CameraAttributesPractical" uid="uid://b835orxyqq6w5" path="res://camera/CameraAttributes.tres" id="10_4uslp"] [ext_resource type="PackedScene" uid="uid://tgmbtt7u172g" path="res://world/Arena.blend" id="11_ffpbh"] @@ -47,6 +48,7 @@ wait_time = 5.0 autostart = true [node name="PlayerAdd" type="Node" parent="Client"] +unique_name_in_owner = true script = ExtResource("5_sd8xh") player_scene = ExtResource("6_jxdko") packet_type = 10 @@ -55,6 +57,10 @@ packet_type = 10 script = ExtResource("7_6xfox") packet_type = 12 +[node name="PlayerJump" type="Node" parent="Client"] +script = ExtResource("8_p6kmr") +packet_type = 13 + [node name="Statistics" type="Timer" parent="Client"] autostart = true diff --git a/server/game/Game.go b/server/game/Game.go index 5fea9a0..dce248a 100644 --- a/server/game/Game.go +++ b/server/game/Game.go @@ -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) } diff --git a/server/game/Jump.go b/server/game/Jump.go new file mode 100644 index 0000000..3211700 --- /dev/null +++ b/server/game/Jump.go @@ -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 +} diff --git a/server/game/Move.go b/server/game/Move.go index b77e4bf..9581ee5 100644 --- a/server/game/Move.go +++ b/server/game/Move.go @@ -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 } diff --git a/server/game/packet/packet.go b/server/game/packet/packet.go index 3eec437..815c8bc 100644 --- a/server/game/packet/packet.go +++ b/server/game/packet/packet.go @@ -7,4 +7,6 @@ const ( PlayerAdd = 10 PlayerRemove = 11 PlayerMove = 12 + PlayerJump = 13 + PlayerAttack = 14 )