Implement RPC Node
This commit is contained in:
parent
f7ee533d5a
commit
605e43273e
13 changed files with 524 additions and 206 deletions
|
|
@ -10,11 +10,11 @@ public partial class Player : CharacterBody3D
|
|||
[Export] public double NetworkPhysicsSyncInterval = 0.05;
|
||||
|
||||
public Control GameMenu { get; set; }
|
||||
|
||||
public Character PlayerData { get; set; }
|
||||
|
||||
private Vector3 gravityVelocity = Vector3.Zero;
|
||||
private Vector3 movementVelocity = Vector3.Zero;
|
||||
public Vector3 GravityVelocity { get; set; } = Vector3.Zero;
|
||||
public Vector3 MovementVelocity { get; set; } = Vector3.Zero;
|
||||
|
||||
private GravityReceiver gravityReceiver;
|
||||
private double cameraPitch = 0;
|
||||
private Camera3D camera;
|
||||
|
|
@ -35,18 +35,20 @@ public partial class Player : CharacterBody3D
|
|||
{
|
||||
if (IsMultiplayerAuthority())
|
||||
{
|
||||
RPCNode rpc = RPCNode.Instance;
|
||||
|
||||
networkPhysicsSyncCounter += delta;
|
||||
if (networkPhysicsSyncCounter >= NetworkPhysicsSyncInterval)
|
||||
{
|
||||
networkPhysicsSyncCounter = 0;
|
||||
Rpc(nameof(RpcSyncPhysics), movementVelocity, gravityVelocity, GlobalRotation);
|
||||
rpc.Rpc(nameof(rpc.RpcSyncPlayerPhysics), GetMultiplayerAuthority(), MovementVelocity, GravityVelocity, GlobalRotation);
|
||||
}
|
||||
|
||||
networkSyncCounter += delta;
|
||||
if (networkSyncCounter >= NetworkSyncInterval)
|
||||
{
|
||||
networkSyncCounter = 0;
|
||||
Rpc(nameof(RpcSync), GlobalPosition, PlayerData.CurrentSector.Coordinates);
|
||||
rpc.Rpc(nameof(rpc.RpcSyncPlayer), GetMultiplayerAuthority(), GlobalPosition, PlayerData.CurrentSector.Coordinates);
|
||||
}
|
||||
|
||||
PlayerData.SetCoordinatesFromLocal(GlobalPosition);
|
||||
|
|
@ -57,8 +59,8 @@ public partial class Player : CharacterBody3D
|
|||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
Vector3 newGravityVelocity = gravityVelocity;
|
||||
Vector3 newMovementVelocity = movementVelocity;
|
||||
Vector3 newGravityVelocity = GravityVelocity;
|
||||
Vector3 newMovementVelocity = MovementVelocity;
|
||||
|
||||
if (!GameMenu.Visible && IsMultiplayerAuthority())
|
||||
{
|
||||
|
|
@ -76,8 +78,8 @@ public partial class Player : CharacterBody3D
|
|||
}
|
||||
}
|
||||
|
||||
gravityVelocity = newGravityVelocity;
|
||||
movementVelocity = newMovementVelocity;
|
||||
GravityVelocity = newGravityVelocity;
|
||||
MovementVelocity = newMovementVelocity;
|
||||
Velocity = newGravityVelocity + newMovementVelocity;
|
||||
|
||||
MoveAndSlide();
|
||||
|
|
@ -237,33 +239,4 @@ public partial class Player : CharacterBody3D
|
|||
|
||||
RotateObjectLocal(Vector3.Forward, rotateAmountZ);
|
||||
}
|
||||
|
||||
[Rpc(MultiplayerApi.RpcMode.AnyPeer)]
|
||||
public void RpcSync(Vector3 position, Vector3I sectorCoordinates)
|
||||
{
|
||||
Sector sector = GameManager.GameUniverse.GetSector(sectorCoordinates);
|
||||
if (sector == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
Vector3Dec newGlobal = PlayerData.CalculateGlobalCoordinates(sector.GlobalCenterCoordinates, position);
|
||||
PlayerData.SetCoordinatesFromGlobal(newGlobal);
|
||||
|
||||
if (PlayerData.CurrentSector.Coordinates != sectorCoordinates)
|
||||
{
|
||||
sector.AssignObject(PlayerData);
|
||||
}
|
||||
|
||||
GlobalPosition = position + PlayerData.SectorOffset;
|
||||
}
|
||||
|
||||
[Rpc(MultiplayerApi.RpcMode.AnyPeer)]
|
||||
public void RpcSyncPhysics(Vector3 _movementVelocity, Vector3 _gravityVelocity, Vector3 rotation)
|
||||
{
|
||||
movementVelocity = _movementVelocity;
|
||||
gravityVelocity = _gravityVelocity;
|
||||
|
||||
GlobalRotation = rotation;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue