Fixed player data being messed up with respawns.

This commit is contained in:
unfa 2021-08-29 22:47:40 +02:00
parent 9664960cd8
commit 1a2bf9f11a
3 changed files with 26 additions and 15 deletions

View file

@ -149,17 +149,22 @@ func damage(hp: int):
#if target.health <= 0: # int(name) is the player instance node name signifying owner's PID
# die()
func die():
func die(killer_pid: int):
# if get_tree().get_rpc_sender_id() != get_network_master():
# print ("Death requested by a non-master. Ignoring")
# return
main.rpc(&'destroy_player', self.get_network_master())
main.chat.rpc(&'chat_notification', "Player " + main.player_list.players[self.get_network_master()].name + " is dead")
main.chat.rpc(&'chat_notification', "Player [/i][b][color=" + main.player_list.players[self.get_network_master()].color.to_html() + "]" + main.player_list.players[self.get_network_master()].name + "[/color][/b][i] was killed by " + main.player_list.players[killer_pid].name )
#queue_free()
func update_color() -> void: #change player's wolrdmodel color
func update_color(pid) -> void: #change player's wolrdmodel color
## @onready var player_material =
## var material = player_material
if pid != get_network_master():
print("Requesting color change for a different PID")
return
var player_material = mesh.mesh.surface_get_material(0).duplicate()
player_material.albedo_color = main.player_list.get(self.get_network_master()).color
print("Updating color of player pid ", self.get_network_master(), " to ", player_material.albedo_color )

View file

@ -35,9 +35,13 @@ var impact_player = preload("res://Assets/Effects/ImpactPlayer.tscn")
#print("SHOT HIT ", ray['collider'])
ray['collider'].damage(50) # apply damage
if main.player_list.get(ray['collider'].get_network_master()).health <= 0: # if he ded
ray['collider'].die()
# boardcast the new health value to all peers TODO: fix this
ray['collider'].die(self.get_network_master())
main.player_list.players[player.get_network_master()].score += 1 # give the killer a point
main.rpc(&'player_list_update', main.player_list.get(player.get_network_master()).serialize(), player.get_network_master())
# boardcast the new health value to all peers
main.rpc(&'player_list_update', main.player_list.get(ray['collider'].get_network_master()).serialize(), ray['collider'].get_network_master())
impact_vfx = impact_player.instantiate()
else:

View file

@ -153,6 +153,7 @@ func _input(_event) -> void:
@remote func player_list_update(info, pid = get_tree().get_rpc_sender_id()):
var new_info = PlayerInfo.new()
new_info.deserialize(info)
print("Recieved player info: ", info)
if player_list.players.has(pid):
var old_name = player_list.get(pid).name
@ -161,8 +162,8 @@ func _input(_event) -> void:
var old_color = player_list.get(pid).color
if old_color != new_info.color:
chat.chat_notification("Player [b]" + new_info.name + "[/b] changed color to [b][color=" + new_info.color.to_html() + "]" + new_info.name + "[/color][/b]")
$Players.get_node(str(pid)).update_color()
#chat.chat_notification("Player [b]" + new_info.name + "[/b] changed color to [b][color=" + new_info.color.to_html() + "]" + new_info.name + "[/color][/b]")
$Players.get_node(str(pid)).update_color(pid)
elif get_tree().network_peer.get_unique_id() == 1:
for i in player_list.players.keys(): #notify existing players the new one has joined
@ -182,7 +183,7 @@ func push_local_player_info(): #
#print("PLayer info vsr2str: ", var2str(player_list.get(id)) )
if id != 1:
rpc_id(1, &'player_list_update', player_list.get(id).serialize())
rpc_id(1, &'player_list_update', player_list.get(id).serialize(), get_tree().get_network_unique_id())
else:
rpc(&'player_list_update', player_list.get(id).serialize(), 1)
@ -193,7 +194,6 @@ func push_local_player_info(): #
player_node.name = str(player_node.name) + "_dead" # avoids name collision when instancing another player scene
player_node.queue_free()
#player.queue_free()
var is_local = true if pid == local_player.get_network_master() else false
@ -213,16 +213,17 @@ func create_player(pid: int, is_local:= false, respawn:= false) -> void:
if not respawn: # first spawn
new_info = PlayerInfo.new() # generate name, color etc
elif is_local: # respawn, only works for local players
else: # respawn
new_info = player_list.players[pid] # reuse previous name, color etc
new_info.health = 100 # give the respawned player full health
player_list.set(pid, new_info)
if get_tree().network_peer.get_unique_id() != 1: # if we're not the server - update the server
rpc_id(1, &'player_list_update', new_info.serialize()) # send local player info to the server
else: # if we're the server, update the clients
rpc(&'player_list_update', new_info.serialize(), pid)
# superfluous and harmful code that messes up player data:
# if get_tree().network_peer.get_unique_id() != 1: # if we're not the server - update the server
# rpc_id(1, &'player_list_update', new_info.serialize(), pid) # send local player info to the server
# else: # if we're the server, update the clients
# rpc(&'player_list_update', new_info.serialize(), pid)
if is_local:
local_player = new_player #$Players.get_node(str(id))
@ -265,6 +266,7 @@ func _player_connected(pid) -> void:
if get_tree().network_peer.get_unique_id() == 1: # if we're the server
for i in player_list.players.keys(): # send the player_list to the new client
#pass
rpc_id(pid, &'player_list_update', player_list.get(i).serialize(), i) # send local player info to the server
# if local_player:
@ -318,4 +320,4 @@ func _on_TextEdit_text_submitted(new_text):
func _on_ColorPickerButton_color_changed(color):
player_list.players[get_tree().network_peer.get_unique_id()].color = color
push_local_player_info()
local_player.rpc(&'set_color', color)
#local_player.rpc(&'set_color', color)