Skip to content

Commit 3fe297a

Browse files
committed
Working basic client authoritative networking
1 parent 4c96b84 commit 3fe297a

36 files changed

+738
-2
lines changed

Runtime/Code/Player/Character/MovementSystem.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Code/Player/Character/MovementSystems.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Code/Player/Character/MovementSystems/BasicTestMovement.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Code.Player.Character.Net;
2+
using UnityEngine;
3+
4+
namespace Code.Player.Character.NetworkedMovement.BasicTest
5+
{
6+
public class BasicMovementInput : InputCommand
7+
{
8+
public Vector3 moveDirection;
9+
}
10+
}

Runtime/Code/Player/Character/MovementSystems/BasicTestMovement/BasicMovementInput.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
using Code.Player.Character.Net;
3+
using UnityEngine;
4+
5+
namespace Code.Player.Character.NetworkedMovement.BasicTest
6+
{
7+
public class BasicMovementState : StateSnapshot, IEquatable<BasicMovementState>
8+
{
9+
public Vector3 position;
10+
public Quaternion rotation;
11+
12+
public override string ToString()
13+
{
14+
return position.ToString() + " " + rotation.ToString();
15+
}
16+
17+
public bool Equals(BasicMovementState other)
18+
{
19+
return this.position == other.position && this.rotation == other.rotation;
20+
}
21+
}
22+
23+
24+
}

Runtime/Code/Player/Character/MovementSystems/BasicTestMovement/BasicMovementState.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using Code.Player.Character.Net;
3+
using Mirror;
4+
using UnityEngine;
5+
6+
namespace Code.Player.Character.NetworkedMovement.BasicTest
7+
{
8+
public class BasicTestMovement: NetworkedMovement<BasicMovementState, BasicMovementInput>
9+
{
10+
private Rigidbody rb;
11+
private Vector3 moveVector;
12+
13+
private void Awake()
14+
{
15+
rb = this.GetComponent<Rigidbody>();
16+
rb.isKinematic = true;
17+
}
18+
19+
public override void SetCurrentState(BasicMovementState state)
20+
{
21+
Debug.Log("setting state to" + state.lastProcessedCommand + " type" + state.GetType());
22+
var s = state as BasicMovementState;
23+
Debug.Log("state now "+ s.lastProcessedCommand);
24+
rb.MovePosition(s.position);
25+
rb.MoveRotation(s.rotation);
26+
}
27+
28+
public override BasicMovementState GetCurrentState(int commandNumber, double time)
29+
{
30+
return new BasicMovementState() { position = rb.position, rotation = rb.rotation, lastProcessedCommand = commandNumber, time = time};
31+
}
32+
33+
public override void Tick(BasicMovementInput command, bool replay)
34+
{
35+
Debug.Log("Ticking command" + command.commandNumber);
36+
var c = command as BasicMovementInput;
37+
rb.MovePosition(rb.position + c.moveDirection * Time.fixedDeltaTime * 5f);
38+
}
39+
40+
public override void Interpolate(float delta, BasicMovementState stateOld, BasicMovementState stateNew)
41+
{
42+
print("Interpolating with delta: " + delta);
43+
this.rb.position = Vector3.Lerp(stateOld.position, stateNew.position, delta);
44+
this.rb.rotation = Quaternion.Lerp(stateOld.rotation, stateNew.rotation, delta);
45+
}
46+
47+
public override BasicMovementInput GetCommand(int commandNumber)
48+
{
49+
Debug.Log("retrieving command" + moveVector);
50+
return new BasicMovementInput() { moveDirection = moveVector, commandNumber = commandNumber};
51+
}
52+
53+
private void Update()
54+
{
55+
float moveX = Input.GetAxisRaw("Horizontal");
56+
float moveZ = Input.GetAxisRaw("Vertical");
57+
this.moveVector = new Vector3(moveX, 0f, moveZ).normalized;
58+
}
59+
}
60+
}

Runtime/Code/Player/Character/MovementSystems/BasicTestMovement/BasicTestMovement.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using Code.Player.Character.Net;
2+
using Mirror;
3+
4+
namespace Code.Player.Character.NetworkedMovement.BasicTest
5+
{
6+
public class BasicTestMovementManager: AirshipMovementManager<BasicTestMovement, BasicMovementState, BasicMovementInput>
7+
{
8+
9+
public override void SendClientInputToServer(BasicMovementInput input)
10+
{
11+
this.RpcClientInputToServer(input);
12+
}
13+
14+
public override void SendClientSnapshotToServer(BasicMovementState snapshot)
15+
{
16+
this.RpcClientSnapshotToServer(snapshot);
17+
}
18+
19+
public override void SendServerSnapshotToClients(BasicMovementState snapshot)
20+
{
21+
this.RpcServerSnapshotToClients(snapshot);
22+
}
23+
24+
25+
[ClientRpc(channel = Channels.Unreliable)]
26+
private void RpcServerSnapshotToClients(BasicMovementState state)
27+
{
28+
this.OnClientReceiveSnapshot?.Invoke(state);
29+
}
30+
31+
[Command(channel = Channels.Unreliable)]
32+
private void RpcClientInputToServer(BasicMovementInput input)
33+
{
34+
this.OnServerReceiveInput?.Invoke(input);
35+
}
36+
37+
[Command(channel = Channels.Unreliable)]
38+
private void RpcClientSnapshotToServer(BasicMovementState state)
39+
{
40+
this.OnServerReceiveSnapshot?.Invoke(state);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)