improve update network

This commit is contained in:
AleaJactaEst 2023-11-26 13:06:37 +01:00
parent ad622360f5
commit 3564ff8d58
4 changed files with 76 additions and 45 deletions

View file

@ -12,6 +12,7 @@ var camrot_v:float = 0.0
@export var v_sensitivity:float = 0.5
var save_rot:Vector3
var tick:int = 0
# Get the gravity from the project settings to be synced with RigidBody nodes.
@ -61,6 +62,14 @@ func get_my_rotation() -> Vector3:
return $Armature.get_global_rotation()
func update_player(ctick:int, pos:Vector3, rot:Vector3):
#print(tick, " - ", ctick)
if tick != ctick:
set_my_position(pos)
set_my_rotation(rot)
tick = ctick
func set_my_position(pos:Vector3):
self.set_global_position(pos)

View file

@ -83,9 +83,10 @@ func _on_connexion_updated(new_state:Multi.Connexion):
$Panel.show()
func _on_update_me(pos:Vector3, rot:Vector3):
self.set_player_position(pos)
self.set_player_rotation(rot)
func _on_update_me(tick:int, pos:Vector3, rot:Vector3):
$PlayerSpawnLocation.get_child(0).update_player(tick, pos, rot)
#self.set_player_position(pos)
#self.set_player_rotation(rot)
#self.get_node("CharacterBody3D").set_enable_event(true)
#for idx in $PlayerSpawnLocation.get_child_count():
# $PlayerSpawnLocation.queue_free()
@ -95,7 +96,7 @@ func _on_update_me(pos:Vector3, rot:Vector3):
#self.set_player_position(pos)
func _on_update_player(id:int, pos:Vector3, rot:Vector3):
func _on_update_player(id:int, tick:int, pos:Vector3, rot:Vector3):
var child = $Players.find_child(str(id), false, false)
if child == null:
print("Add player : ", id)
@ -106,15 +107,17 @@ func _on_update_player(id:int, pos:Vector3, rot:Vector3):
instance.set_visible(true)
instance.set_id(id)
var child2 = $Players.find_child(str(id), false, false)
child2.set_my_position(pos)
child2.set_my_rotation(rot)
child2.update_player(tick, pos, rot)
#child2.set_my_position(pos)
#child2.set_my_rotation(rot)
else:
print("Update player : ", id, " ", pos, " ", rot)
#print(pos)
#pos.y += 10.0
child.set_my_position(pos)
child.set_my_rotation(rot)
#child.set_visible(true)
child.update_player(tick, pos, rot)
#child.set_my_position(pos)
#child.set_my_rotation(rot)
##child.set_visible(true)
func _on_remove_player(id:int):

View file

@ -1,6 +1,12 @@
extends Node
signal connexion_updated(new_state)
signal update_my_position(tick, pos, rot)
signal update_player_position(id, tick, pos, rot)
signal remove_player(id)
@export var listen_ip:String = "127.0.0.1":
set = set_ip
@ -53,12 +59,6 @@ enum Connexion{
id = value
signal connexion_updated(new_state)
signal update_my_position(pos, rot)
signal update_player_position(id, pos, rot)
signal remove_player(id)
var enet:ENetConnection
var dataEnet:ENetPacketPeer
var errorEnet:Error
@ -123,49 +123,59 @@ func bad_account():
func get_event_received():
var data = dataEnet.get_packet()
var data:PackedFlow = PackedFlow.new(dataEnet)
var command = data.get_u8()
#var data = dataEnet.get_packet()
#print("get: ", data)
if data[0] == 1: # Return connexion
if data[1] == 0: # OK
id = data.decode_u64(2)
var x:float = data.decode_double(2+8)
var y:float = data.decode_double(2+16)
var z:float = data.decode_double(2+24)
var rx:float = data.decode_double(2+32)
var ry:float = data.decode_double(2+40)
var rz:float = data.decode_double(2+48)
var _tick:int = data.decode_double(2+1)
if command == 1: # Return connexion
var state = data.get_u8()
if state == 0: # OK
id = data.get_u64()
var x:float = data.get_f64()
var y:float = data.get_f64()
var z:float = data.get_f64()
var rx:float = data.get_f64()
var ry:float = data.get_f64()
var rz:float = data.get_f64()
var tick:int = data.get_u8()
#for i in range(56,74):
# var tick1:int = data.decode_u8(2+i)
# print("tick ", i, " :", tick1)
print("MyID:", id)
#print("id:", id, " x:", x, " y:", y, " z:", z)
update_my_position.emit(Vector3(x, y, z), Vector3(rx, ry, rz))
print("id:", id, " tick:", tick, " x:", x, " y:", y, " z:", z)
update_my_position.emit(tick, Vector3(x, y, z), Vector3(rx, ry, rz))
#self.set_player_position(Vector3(x, y, z))
update_state(Connexion.CONNECTED)
else: # KO
bad_account()
elif data[0] == 3: # Get position
elif command == 3: # Get position
if id == 0:
return
var nb:int = data[1]
var nb:int = data.get_u8()
var pos:int = 2
for i in nb:
var mid:int = data.decode_u64(pos)
var mx:float = data.decode_double(pos+8)
var my:float = data.decode_double(pos+16)
var mz:float = data.decode_double(pos+24)
var rx:float = data.decode_double(pos+32)
var ry:float = data.decode_double(pos+40)
var rz:float = data.decode_double(pos+48)
var tick:int = data.decode_u8(pos+56)
var mid:int = data.get_u64()
var mx:float = data.get_f64()
var my:float = data.get_f64()
var mz:float = data.get_f64()
var rx:float = data.get_f64()
var ry:float = data.get_f64()
var rz:float = data.get_f64()
var tick:int = data.get_u8()
#for ii in range(56,74):
# var tick1:int = data.decode_u8(pos+ii)
# print("tick ", ii, " :", tick1)
pos += 57
if mid == id:
#print("Me id:", mid, " x:", mx, " y:", my, " z:", mz)
continue
print("pos:", pos, " id:", mid, " tick:", tick, " x:", mx, " y:", my, " z:", mz, " rx:", rx, " ry:", ry, " rz:", rz)
update_player_position.emit(mid, Vector3(mx, my, mz), Vector3(rx, ry, rz))
var nbuserremove:int = data[pos]
update_player_position.emit(mid, tick, Vector3(mx, my, mz), Vector3(rx, ry, rz))
var nbuserremove:int = data.get_u8()
pos += 1
for i in nbuserremove:
var mid = data.decode_u64(pos)
var mid = data.get_u64()
pos += 8
if mid == id:
print("Try to remove me :", mid)

View file

@ -159,7 +159,7 @@ impl User {
self.position_updated = false;
self.lasttime = Instant::now();
self.sendfull = 0;
self.lastupdate = 0;
self.lastupdate = 1;
}
pub fn set_inactive(&mut self) {
self.active = false;
@ -361,7 +361,16 @@ impl Users {
pub fn add(&mut self, username:String, address: Address) -> u64 {
let id = self.get_new_id();
self.player_active += 1;
self.users.push( User { active: true, username:username, address: address, id: id, x: 0.0, y: 10.0, z:0.0, rx: 0.0, ry: 0.0, rz:0.0, position_updated:true, lasttime: Instant::now(), sendfull:10, lastupdate:0} );
self.users.push( User { active: true,
username:username,
address: address,
id: id,
x: 0.0, y: 10.0, z:0.0,
rx: 0.0, ry: 0.0, rz:0.0,
position_updated:true,
lasttime: Instant::now(),
sendfull:10,
lastupdate:1} );
id
}
pub fn update_pos_rot(&mut self, address: Address, x:f64, y:f64, z:f64, rx:f64, ry:f64, rz:f64, tick:u8) {
@ -743,7 +752,7 @@ fn main() -> anyhow::Result<()> {
let mut launch_cleanup : u8 = 60;
let mut last_update:Instant = Instant::now();
let mut stat:Statistics = Statistics::new();
let mut tick : u8 = 0;
let mut tick : u8 = 1;
if cli.trace {
loginit(LevelFilter::Trace).unwrap();
@ -775,7 +784,7 @@ fn main() -> anyhow::Result<()> {
info!("Started");
loop {
trace!("users: {}", users);
if tick == 255 {
if tick < 255 {
tick += 1;
} else {
tick = 1;