Skip to content

Commit c5c9163

Browse files
committed
feat: add player name tags above head - close #1
adding check_fall_and_respawn & player name tags above head.
1 parent efdeee8 commit c5c9163

File tree

4 files changed

+87
-40
lines changed

4 files changed

+87
-40
lines changed

level/scenes/player.tscn

Lines changed: 38 additions & 27 deletions
Large diffs are not rendered by default.

level/scripts/level.gd

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
extends Node3D
22

3+
@onready var nick_input: LineEdit = $Menu/MainContainer/MainMenu/Option1/NickInput
34
@onready var address_input = $Menu/MainContainer/MainMenu/Option3/AddressInput
45
@onready var players_container = $PlayersContainer
56
@onready var menu = $Menu
@@ -18,7 +19,7 @@ func _on_host_pressed():
1819

1920
func _on_client_pressed():
2021
menu.hide()
21-
Network.join_game(address_input.text)
22+
Network.join_game(address_input.text, nick_input.text)
2223

2324
func _add_player(id: int):
2425
if not multiplayer.is_server() or id == 1:
@@ -27,6 +28,11 @@ func _add_player(id: int):
2728
if players_container.has_node(str(id)):
2829
return
2930

31+
print("-------------- ADDING ------------------")
32+
print("Player id: ", id)
33+
print(Network.players)
34+
print("---------------------------------------")
35+
3036
var player = player_scene.instantiate()
3137
player.name = str(id)
3238
player.position = get_spawn_point()

level/scripts/network.gd

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ const SERVER_PORT: int = 5466
55
const MAX_PLAYERS : int = 10
66

77
var players = {}
8-
var player_info = {"nick": "host"}
8+
var player_info = {
9+
"nick" : "host"
10+
}
911

1012
signal player_connected(peer_id, player_info)
1113
signal server_disconnected
@@ -24,14 +26,18 @@ func start_host():
2426
return error
2527
multiplayer.multiplayer_peer = peer
2628

27-
func join_game(address):
29+
players[1] = player_info # server
30+
player_connected.emit(1, player_info)
31+
32+
func join_game(address, nickname):
2833
if !address:
2934
address = SERVER_ADDRESS
3035
var peer = ENetMultiplayerPeer.new()
3136
var error = peer.create_client(address, SERVER_PORT)
3237
if error:
3338
return error
3439
multiplayer.multiplayer_peer = peer
40+
player_info["nick"] = nickname
3541

3642
func _on_connected_ok():
3743
var peer_id = multiplayer.get_unique_id()

level/scripts/player.gd

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
extends CharacterBody3D
22
class_name Character
33

4-
const NORMAL_SPEED = 5.0
5-
const SPRINT_SPEED = 9.0
4+
const NORMAL_SPEED = 6.0
5+
const SPRINT_SPEED = 10.0
66
const JUMP_VELOCITY = 10
77

8-
var current_speed: float
8+
@onready var nickname = $PlayerNick/Nickname as Label3D
99

1010
@export_category("Objects")
1111
@export var _body: Node3D = null
1212
@export var _spring_arm_offset: Node3D = null
1313

14+
var _current_speed: float
15+
var _respawn_point = Vector3(0, 5, 0)
1416
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
1517

1618
func _enter_tree():
1719
set_multiplayer_authority(str(name).to_int())
1820
$SpringArmOffset/SpringArm3D/Camera3D.current = is_multiplayer_authority()
19-
21+
22+
func _ready():
23+
if multiplayer.is_server():
24+
$SpringArmOffset/SpringArm3D/Camera3D.current = false
25+
26+
change_nick(Network.players[multiplayer.get_unique_id()]["nick"])
27+
2028
func _physics_process(delta):
2129
if not is_multiplayer_authority():
2230
return
@@ -34,6 +42,7 @@ func _physics_process(delta):
3442
_move()
3543
move_and_slide()
3644
_body.animate(velocity)
45+
_check_fall_and_respawn()
3746

3847
func _move() -> void:
3948
var _input_direction: Vector2 = Vector2.ZERO
@@ -53,18 +62,33 @@ func _move() -> void:
5362
_direction = _direction.rotated(Vector3.UP, _spring_arm_offset.rotation.y)
5463

5564
if _direction:
56-
velocity.x = _direction.x * current_speed
57-
velocity.z = _direction.z * current_speed
65+
velocity.x = _direction.x * _current_speed
66+
velocity.z = _direction.z * _current_speed
5867
_body.apply_rotation(velocity)
5968
return
6069

61-
velocity.x = move_toward(velocity.x, 0, current_speed)
62-
velocity.z = move_toward(velocity.z, 0, current_speed)
70+
velocity.x = move_toward(velocity.x, 0, _current_speed)
71+
velocity.z = move_toward(velocity.z, 0, _current_speed)
6372

6473
func is_running() -> bool:
6574
if Input.is_action_pressed("shift"):
66-
current_speed = SPRINT_SPEED
75+
_current_speed = SPRINT_SPEED
6776
return true
6877
else:
69-
current_speed = NORMAL_SPEED
78+
_current_speed = NORMAL_SPEED
7079
return false
80+
81+
func _check_fall_and_respawn():
82+
if global_transform.origin.y < -15.0:
83+
_respawn()
84+
85+
func _respawn():
86+
global_transform.origin = _respawn_point
87+
velocity = Vector3.ZERO
88+
89+
@rpc("any_peer", "reliable")
90+
func change_nick(new_nick: String):
91+
if nickname:
92+
nickname.text = new_nick
93+
else:
94+
print("error: nickname is null")

0 commit comments

Comments
 (0)