Reworking player_info and syncing the player list.

This commit is contained in:
unfa 2021-08-21 15:03:51 +02:00
parent 59e947dddb
commit 53e7f60e1a
3 changed files with 105 additions and 70 deletions

View file

@ -2,7 +2,8 @@ extends CharacterBody3D
@export var mouse_sensitivity := 0.35
@onready var hud = get_tree().root.get_node("Main").get_node("HUD")
@onready var main = get_tree().root.get_node("Main")
@onready var hud = main.get_node("HUD")
@onready var crosshair = hud.get_node("Crosshair")
@onready var vignette = hud.get_node("Vignette")
@ -25,29 +26,8 @@ extends CharacterBody3D
@onready var climb_check_y = climb_check.position.y
@onready var ground_check_y = ground_check.position.y
class PlayerInfo:
var name: String
var team: int
var color: Color
var focus: int #"res://Main.gd".GameFocus.GAME
func _init(name: String, team: int, color: Color):
self.name = name
self.team = team
self.color = color
self.focus = 0 #false
func serialize():
return {
'name': self.name,
'team': str(self.team),
'color': self.color.to_html(),
'focus': self.focus,
}
var input_active = false
var player_info: PlayerInfo
var base_fov = 90
var view_zoom_target := 1.0
@ -94,21 +74,6 @@ var jump := 14
var velocity := Vector3.ZERO
var gravity_vec := Vector3.ZERO
@puppetsync func set_info(info) -> void:
player_info = PlayerInfo.new(info['name'], info['team'].to_int(), Color(info['color']))
print("set_info - rpc called from ", get_tree().get_rpc_sender_id())
@master func update_info() -> void:
rpc(&'set_info', player_info.serialize())
@master func generate_info() -> void:
var player_name = ""
for i in range(0, 4):
player_name += ['a','b','c', 'd', 'e', 'f'][randi() % 5]
var color = Color(randf(),randf(),randf())
rpc(&'set_info', PlayerInfo.new(player_name, 0, color).serialize() )
@puppet func update_movement(player_transform, head_rotation):
global_transform = player_transform
head.set_rotation(head_rotation)
@ -117,7 +82,7 @@ func _ready() -> void:
#Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
view_zoom_target = 1.0
generate_info()
# generate_info()
rpc_config(&'move_and_slide', MultiplayerAPI.RPC_MODE_REMOTE)
rpc_config(&"aim", MultiplayerAPI.RPC_MODE_REMOTE)

View file

@ -53,9 +53,7 @@ func _unhandled_input(_event) -> void:
# doesn't work over network due to missing RPC implementation in Godot 4
@remotesync func chat_message(sender_id: int, recipient_team, message: String) -> void:
var sender_info = main.get_node("Players").get_node(str(sender_id)).player_info
var sender_info = main.player_list[sender_id]
chat_history.append_bbcode('\n' + '[b][color=' + sender_info.color.to_html() +']' + str(sender_info.name) + '[/color][/b] : [i]' + message + '[/i]')
func _on_Editor_text_submitted(new_text):

View file

@ -20,24 +20,73 @@ var player_scene = preload("res://Assets/Characters/Player.tscn")
@onready var chat = hud.get_node("Chat")
var local_player: Node = null
class PlayerInfo:
var name: String
var team: int
var color: Color
var focus: GameFocus
class PlayerList:
var items = {}
func _init():#name: String, team: int, color: Color):
# self.name = name
# self.team = team
# self.color = color
# self.focus = 0
var player_name = ""
for i in range(0, 4):
player_name += ['a','b','c', 'd', 'e', 'f'][randi() % 5]
self.name = player_name
self.color = Color(randf(),randf(),randf())
self.team = 0
func serialize():
return {
'name': self.name,
'team': str(self.team),
'color': self.color.to_html(),
'focus': self.focus,
}
func set(name: String, team: int, color: Color, focus: int):
self.name = name
self.team = team
self.color = color
self.focus = focus
func store(pid, item):
items[pid] = item
func deserialize(info):
#var info_parsed = info.parse_json(info)
self.name = info['name']
self.team = info['team'].to_int()
self.color = Color.html(info['color'])
self.focus = info['focus']
func erase(pid):
items.erase(pid)
#func generate():
func update(pid, item):
if items[pid]:
items[pid] = item
var local_player_info: PlayerInfo
#class PlayerList:
# var items = {}
#
# func store(pid, item):
# items[pid] = item
#
# func erase(pid):
# items.erase(pid)
#
# func update(pid, item):
# if items[pid]:
# items[pid] = item
#
# func get():
# return items
func get():
return items
var player_list = []
var player_list = {}:
set(player_list):
if role in [NetworkRole.SERVER, NetworkRole.DEDICATED_SERVER]:
for i in player_list:
rpc(&'player_list_update', i.serialize())
else:
rpc_id(1, &'player_list_update', player_list.serialize())
var focus = GameFocus.MENU :
set(new_focus):
@ -61,7 +110,15 @@ var focus = GameFocus.MENU :
focus = new_focus
#@remote set_info(info) -> void:
# player_info = PlayerInfo.new(info['name'], info['team'].to_int(), Color(info['color']))
# print("set_info - rpc called from ", get_tree().get_rpc_sender_id())
#@master func update_info() -> void:
# rpc(&'set_info', player_info.serialize())
#@master func generate_info() -> void:
#rpc(&'set_info', PlayerInfo.new(player_name, 0, color).serialize() )
func _input(_event) -> void:
if Input.is_action_just_pressed("ui_cancel"):
@ -70,7 +127,18 @@ func _input(_event) -> void:
elif focus == GameFocus.MENU:
focus = GameFocus.GAME
#@remote func update_player_list(player_list):
# self.player_list = player_list
@remote func player_list_update(info):
var new_info = PlayerInfo.deserialize(info)
var id = get_tree().get_rpc_sender_id()
print("Updating player_list wit hnew info: ", new_info)
player_list[id] = new_info
print("Player list: ", player_list)
print("Player list item for id ", id, ": ", player_list[id])
func create_player(id: int, is_local: bool) -> void:
var new_player
@ -78,7 +146,6 @@ func create_player(id: int, is_local: bool) -> void:
new_player = player_scene.instance()
else:
new_player = player_scene.instantiate()
var spawnpoint = $Map/SpawnPoints.get_children()[randi() % len($Map/SpawnPoints.get_children())]
new_player.name = str(id)
new_player.global_transform = spawnpoint.global_transform
@ -86,17 +153,21 @@ func create_player(id: int, is_local: bool) -> void:
$Players.add_child(new_player)
if is_local:
local_player = $Players.get_node(str(id))
local_player = new_player #$Players.get_node(str(id))
local_player.get_node("Head/Camera").current = true
else:
$Players.get_node(str(id) + "/Head/Camera").current = false
var new_info = PlayerInfo.new()
#new_info.generate()
local_player_info = new_info
rpc_id(1,&'player_list_update', local_player_info.serialize())
$NetworkTesting/TextEdit.text = local_player_info.name
$NetworkTesting/ColorPickerButton.color = local_player_info.color
else: # make sure we use the right camera
new_player.get_node("Head/Camera").current = false
if local_player:
local_player.get_node("Head/Camera").current = true
$NetworkTesting/TextEdit.text = local_player.player_info.name
$NetworkTesting/ColorPickerButton.color = local_player.player_info.color
func start_dedicated_server():
func start_dedicated_server():
role = NetworkRole.DEDICATED_SERVER
peer.create_server(NET_PORT, 16)
get_tree().network_peer = peer
@ -122,8 +193,11 @@ func _on_Connect_pressed():
func _player_connected(id) -> void:
print("player connected, id: ", id)
create_player(id, false)
if local_player:
local_player.rpc(&"update_info")
# if local_player:
# local_player.rpc(&"update_info")
#
# if role in [NetworkRole.SERVER, NetworkRole.DEDICATED_SERVER]:
# rpc_id(id, "player_list_update", player_list)
func _player_disconnected(id) -> void:
print("player disconnected, id: ", id)
@ -134,6 +208,7 @@ func _connected_ok() -> void:
create_player(id, true)
focus = GameFocus.GAME
role = NetworkRole.CLIENT
# todo pull player list from server
func _connected_fail() -> void:
print("connection to server failed")
@ -155,10 +230,7 @@ func _ready() -> void:
func _on_TextEdit_text_submitted(new_text):
local_player.player_info.name = new_text
local_player.update_info()
local_player_info.name = new_text
func _on_ColorPickerButton_color_changed(color):
local_player.player_info.color = color
local_player.update_info()
local_player_info.color = color