From c228d4e25a8c5a69ec00a95be841c2c0f4f44323 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 16 Feb 2024 15:51:23 +0100 Subject: [PATCH] Added network skill usage --- client/Main.tscn | 7 +++++- client/network/Chat.gd | 2 +- client/network/Login.gd | 2 +- client/network/Ping.gd | 4 ++-- client/network/PlayerJump.gd | 2 +- client/network/PlayerMove.gd | 2 +- client/network/PlayerUseSkill.gd | 24 +++++++++++++++++++++ client/network/shared/PacketHandler.gd | 2 +- client/project.godot | 4 ---- server/game/Game.go | 1 + server/game/Packet.go | 18 ++++++++-------- server/game/UseSkill.go | 30 ++++++++++++++++++++++++++ 12 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 client/network/PlayerUseSkill.gd create mode 100644 server/game/UseSkill.go diff --git a/client/Main.tscn b/client/Main.tscn index a3dc047..4927e70 100644 --- a/client/Main.tscn +++ b/client/Main.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=28 format=3 uid="uid://b40y7iuskv1ar"] +[gd_scene load_steps=29 format=3 uid="uid://b40y7iuskv1ar"] [ext_resource type="Script" path="res://Main.gd" id="1_cw3ws"] [ext_resource type="Script" path="res://network/Client.gd" id="2_8hxcx"] @@ -12,6 +12,7 @@ [ext_resource type="Script" path="res://network/PlayerJump.gd" id="9_o8sk8"] [ext_resource type="Script" path="res://network/Chat.gd" id="10_y3len"] [ext_resource type="Script" path="res://world/Sun.gd" id="11_4jb08"] +[ext_resource type="Script" path="res://network/PlayerUseSkill.gd" id="11_gyiep"] [ext_resource type="PackedScene" uid="uid://cpdoq0oh84mfw" path="res://camera/Camera.tscn" id="12_aljdh"] [ext_resource type="Environment" uid="uid://dixa0yso2s1u3" path="res://world/Environment.tres" id="12_cscto"] [ext_resource type="Script" path="res://world/Generate.gd" id="15_25nmg"] @@ -90,6 +91,10 @@ packet_type = 12 script = ExtResource("9_o8sk8") packet_type = 13 +[node name="PlayerUseSkill" type="Node" parent="Client"] +script = ExtResource("11_gyiep") +packet_type = 14 + [node name="Chat" type="Node" parent="Client"] unique_name_in_owner = true script = ExtResource("10_y3len") diff --git a/client/network/Chat.gd b/client/network/Chat.gd index 3c23c58..0315f21 100644 --- a/client/network/Chat.gd +++ b/client/network/Chat.gd @@ -4,7 +4,7 @@ signal message_received(message: String) func send_message(message: String): var buffer := StreamPeerBuffer.new() - buffer.put_8(Packet.CHAT) + buffer.put_u8(Packet.CHAT) buffer.put_data(message.to_utf8_buffer()) %Client.send(buffer.data_array) diff --git a/client/network/Login.gd b/client/network/Login.gd index 21483bf..d626b16 100644 --- a/client/network/Login.gd +++ b/client/network/Login.gd @@ -24,7 +24,7 @@ func send_login(): var password := "password".sha256_text() var buffer := StreamPeerBuffer.new() - buffer.put_8(Packet.LOGIN) + buffer.put_u8(Packet.LOGIN) buffer.put_data(JSON.stringify([Global.username, password]).to_utf8_buffer()) %Client.send(buffer.data_array) print("[%s] Connecting..." % Global.username) diff --git a/client/network/Ping.gd b/client/network/Ping.gd index 64b33a8..ce5ba15 100644 --- a/client/network/Ping.gd +++ b/client/network/Ping.gd @@ -17,8 +17,8 @@ func handle_packet(data: PackedByteArray): func send_ping(): var buffer := StreamPeerBuffer.new() - buffer.put_8(Packet.PING) - buffer.put_8(count) + buffer.put_u8(Packet.PING) + buffer.put_u8(count) %Client.send(buffer.data_array) history[count] = get_time() count = (count + 1) % HISTORY_SIZE diff --git a/client/network/PlayerJump.gd b/client/network/PlayerJump.gd index da6af83..5d2aba2 100644 --- a/client/network/PlayerJump.gd +++ b/client/network/PlayerJump.gd @@ -8,7 +8,7 @@ func on_main_player_spawned(player: Player): func on_jump(): var buffer := StreamPeerBuffer.new() - buffer.put_8(PacketHandler.Packet.PLAYER_JUMP) + buffer.put_u8(PacketHandler.Packet.PLAYER_JUMP) %Client.send(buffer.data_array) func handle_packet(data: PackedByteArray): diff --git a/client/network/PlayerMove.gd b/client/network/PlayerMove.gd index b4d8d8c..d6f2c64 100644 --- a/client/network/PlayerMove.gd +++ b/client/network/PlayerMove.gd @@ -46,7 +46,7 @@ func _physics_process(_delta): func send_position(): var buffer := StreamPeerBuffer.new() - buffer.put_8(PacketHandler.Packet.PLAYER_MOVE) + buffer.put_u8(PacketHandler.Packet.PLAYER_MOVE) buffer.put_float(Global.player.position.x) buffer.put_float(Global.player.position.z) buffer.put_float(Global.player.movement.direction.x) diff --git a/client/network/PlayerUseSkill.gd b/client/network/PlayerUseSkill.gd new file mode 100644 index 0000000..8b4b579 --- /dev/null +++ b/client/network/PlayerUseSkill.gd @@ -0,0 +1,24 @@ +extends PacketHandler + +func _ready(): + %PlayerAdd.main_player_spawned.connect(on_main_player_spawned) + +func on_main_player_spawned(player: Player): + player.controller.used_skill.connect(on_skill_used) + +func on_skill_used(slot: int): + var buffer := StreamPeerBuffer.new() + buffer.put_u8(PacketHandler.Packet.PLAYER_USE_SKILL) + buffer.put_u8(slot) + %Client.send(buffer.data_array) + +func handle_packet(data: PackedByteArray): + var buffer := StreamPeerBuffer.new() + buffer.data_array = data + + var player_id_length := buffer.get_size() - 1 + var player_id := buffer.get_string(player_id_length) + var slot := buffer.get_u8() + + var player := Global.players.get_player(player_id) + player.controller.used_skill.emit(slot) diff --git a/client/network/shared/PacketHandler.gd b/client/network/shared/PacketHandler.gd index f26a75c..5cdcb83 100644 --- a/client/network/shared/PacketHandler.gd +++ b/client/network/shared/PacketHandler.gd @@ -10,7 +10,7 @@ enum Packet { PLAYER_REMOVE = 11, PLAYER_MOVE = 12, PLAYER_JUMP = 13, - PLAYER_ATTACK = 14, + PLAYER_USE_SKILL = 14, CHAT = 20, } diff --git a/client/project.godot b/client/project.godot index ef56d2e..194cc60 100644 --- a/client/project.godot +++ b/client/project.godot @@ -37,10 +37,6 @@ window/stretch/mode="canvas_items" window/stretch/aspect="expand" window/vsync/vsync_mode=0 -[editor] - -run/main_run_args="--offline" - [gui] theme/custom="res://ui/theme/theme.tres" diff --git a/server/game/Game.go b/server/game/Game.go index 4e036ee..555b681 100644 --- a/server/game/Game.go +++ b/server/game/Game.go @@ -32,6 +32,7 @@ func NewRouter(game *Game) *Router { router.Get(Login, game.Login) router.Get(PlayerMove, game.Move) router.Get(PlayerJump, game.Jump) + router.Get(PlayerUseSkill, game.UseSkill) router.Get(Chat, game.Chat) return router } diff --git a/server/game/Packet.go b/server/game/Packet.go index d87e523..c816989 100644 --- a/server/game/Packet.go +++ b/server/game/Packet.go @@ -6,15 +6,15 @@ import ( // Byte prefixes to indicate the packet type. const ( - Ping = 1 - Login = 2 - Logout = 3 - PlayerAdd = 10 - PlayerRemove = 11 - PlayerMove = 12 - PlayerJump = 13 - PlayerAttack = 14 - Chat = 20 + Ping = 1 + Login = 2 + Logout = 3 + PlayerAdd = 10 + PlayerRemove = 11 + PlayerMove = 12 + PlayerJump = 13 + PlayerUseSkill = 14 + Chat = 20 ) // Packet represents a single UDP datagram. diff --git a/server/game/UseSkill.go b/server/game/UseSkill.go new file mode 100644 index 0000000..5864c44 --- /dev/null +++ b/server/game/UseSkill.go @@ -0,0 +1,30 @@ +package game + +import ( + "errors" + "net" +) + +var ( + ErrInvalidPacketLength = errors.New("invalid packet length") +) + +// UseSkill tries to use a player skill. +func (game *Game) UseSkill(data []byte, address *net.UDPAddr) error { + player := game.players.ByAddress(address) + + if player == nil { + return ErrUnknownAddress + } + + if len(data) != 1 { + return ErrInvalidPacketLength + } + + slot := data[0] + broadcast := []byte(player.ID) + broadcast = append(broadcast, slot) + + game.BroadcastOthers(PlayerUseSkill, broadcast, player) + return nil +}