Added network skill usage

This commit is contained in:
Eduard Urbach 2024-02-16 15:51:23 +01:00
parent 89d730f0ae
commit c228d4e25a
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
12 changed files with 77 additions and 21 deletions

View File

@ -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://Main.gd" id="1_cw3ws"]
[ext_resource type="Script" path="res://network/Client.gd" id="2_8hxcx"] [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/PlayerJump.gd" id="9_o8sk8"]
[ext_resource type="Script" path="res://network/Chat.gd" id="10_y3len"] [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://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="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="Environment" uid="uid://dixa0yso2s1u3" path="res://world/Environment.tres" id="12_cscto"]
[ext_resource type="Script" path="res://world/Generate.gd" id="15_25nmg"] [ext_resource type="Script" path="res://world/Generate.gd" id="15_25nmg"]
@ -90,6 +91,10 @@ packet_type = 12
script = ExtResource("9_o8sk8") script = ExtResource("9_o8sk8")
packet_type = 13 packet_type = 13
[node name="PlayerUseSkill" type="Node" parent="Client"]
script = ExtResource("11_gyiep")
packet_type = 14
[node name="Chat" type="Node" parent="Client"] [node name="Chat" type="Node" parent="Client"]
unique_name_in_owner = true unique_name_in_owner = true
script = ExtResource("10_y3len") script = ExtResource("10_y3len")

View File

@ -4,7 +4,7 @@ signal message_received(message: String)
func send_message(message: String): func send_message(message: String):
var buffer := StreamPeerBuffer.new() var buffer := StreamPeerBuffer.new()
buffer.put_8(Packet.CHAT) buffer.put_u8(Packet.CHAT)
buffer.put_data(message.to_utf8_buffer()) buffer.put_data(message.to_utf8_buffer())
%Client.send(buffer.data_array) %Client.send(buffer.data_array)

View File

@ -24,7 +24,7 @@ func send_login():
var password := "password".sha256_text() var password := "password".sha256_text()
var buffer := StreamPeerBuffer.new() 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()) buffer.put_data(JSON.stringify([Global.username, password]).to_utf8_buffer())
%Client.send(buffer.data_array) %Client.send(buffer.data_array)
print("[%s] Connecting..." % Global.username) print("[%s] Connecting..." % Global.username)

View File

@ -17,8 +17,8 @@ func handle_packet(data: PackedByteArray):
func send_ping(): func send_ping():
var buffer := StreamPeerBuffer.new() var buffer := StreamPeerBuffer.new()
buffer.put_8(Packet.PING) buffer.put_u8(Packet.PING)
buffer.put_8(count) buffer.put_u8(count)
%Client.send(buffer.data_array) %Client.send(buffer.data_array)
history[count] = get_time() history[count] = get_time()
count = (count + 1) % HISTORY_SIZE count = (count + 1) % HISTORY_SIZE

View File

@ -8,7 +8,7 @@ func on_main_player_spawned(player: Player):
func on_jump(): func on_jump():
var buffer := StreamPeerBuffer.new() var buffer := StreamPeerBuffer.new()
buffer.put_8(PacketHandler.Packet.PLAYER_JUMP) buffer.put_u8(PacketHandler.Packet.PLAYER_JUMP)
%Client.send(buffer.data_array) %Client.send(buffer.data_array)
func handle_packet(data: PackedByteArray): func handle_packet(data: PackedByteArray):

View File

@ -46,7 +46,7 @@ func _physics_process(_delta):
func send_position(): func send_position():
var buffer := StreamPeerBuffer.new() 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.x)
buffer.put_float(Global.player.position.z) buffer.put_float(Global.player.position.z)
buffer.put_float(Global.player.movement.direction.x) buffer.put_float(Global.player.movement.direction.x)

View File

@ -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)

View File

@ -10,7 +10,7 @@ enum Packet {
PLAYER_REMOVE = 11, PLAYER_REMOVE = 11,
PLAYER_MOVE = 12, PLAYER_MOVE = 12,
PLAYER_JUMP = 13, PLAYER_JUMP = 13,
PLAYER_ATTACK = 14, PLAYER_USE_SKILL = 14,
CHAT = 20, CHAT = 20,
} }

View File

@ -37,10 +37,6 @@ window/stretch/mode="canvas_items"
window/stretch/aspect="expand" window/stretch/aspect="expand"
window/vsync/vsync_mode=0 window/vsync/vsync_mode=0
[editor]
run/main_run_args="--offline"
[gui] [gui]
theme/custom="res://ui/theme/theme.tres" theme/custom="res://ui/theme/theme.tres"

View File

@ -32,6 +32,7 @@ func NewRouter(game *Game) *Router {
router.Get(Login, game.Login) router.Get(Login, game.Login)
router.Get(PlayerMove, game.Move) router.Get(PlayerMove, game.Move)
router.Get(PlayerJump, game.Jump) router.Get(PlayerJump, game.Jump)
router.Get(PlayerUseSkill, game.UseSkill)
router.Get(Chat, game.Chat) router.Get(Chat, game.Chat)
return router return router
} }

View File

@ -6,15 +6,15 @@ import (
// Byte prefixes to indicate the packet type. // Byte prefixes to indicate the packet type.
const ( const (
Ping = 1 Ping = 1
Login = 2 Login = 2
Logout = 3 Logout = 3
PlayerAdd = 10 PlayerAdd = 10
PlayerRemove = 11 PlayerRemove = 11
PlayerMove = 12 PlayerMove = 12
PlayerJump = 13 PlayerJump = 13
PlayerAttack = 14 PlayerUseSkill = 14
Chat = 20 Chat = 20
) )
// Packet represents a single UDP datagram. // Packet represents a single UDP datagram.

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

@ -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
}