Implemented jumps

This commit is contained in:
Eduard Urbach 2024-01-26 22:45:26 +01:00
parent 48b8011199
commit f4e44c5882
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
9 changed files with 61 additions and 10 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

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
)