浏览代码

refactor: Add tasks to TODOs and add comments

/main
Luke Stampfli 4 年前
当前提交
e7d31100
共有 10 个文件被更改,包括 85 次插入107 次删除
  1. 16
      Assets/BossRoom/Prefabs/Player.prefab
  2. 19
      Assets/BossRoom/Scripts/Server/ServerCharacterMovement.cs
  3. 17
      Assets/BossRoom/Scripts/Shared/NetworkCharacterState.cs
  4. 44
      Assets/BossRoom/Scripts/Client/ClientCharacterVisualization.cs
  5. 11
      Assets/BossRoom/Scripts/Client/ClientInput.cs.meta
  6. 36
      Assets/BossRoom/Scripts/Client/ClientCharacter.cs
  7. 49
      Assets/BossRoom/Scripts/Client/ClientInput.cs
  8. 0
      /Assets/BossRoom/Scripts/Client/ClientCharacterVisualization.cs.meta
  9. 0
      /Assets/BossRoom/Scripts/Server/ServerCharacterMovement.cs.meta
  10. 0
      /Assets/BossRoom/Scripts/Server/ServerCharacterMovement.cs

16
Assets/BossRoom/Prefabs/Player.prefab


m_Component:
- component: {fileID: 1935255767028652502}
m_Layer: 0
m_Name: Graphics
m_Name: Client_Visuals
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0

- component: {fileID: 4470766618279719912}
- component: {fileID: -3542424572169399493}
- component: {fileID: 6970334877957368017}
- component: {fileID: 4093145920273822261}
m_Layer: 0
m_Name: Player
m_TagString: Untagged

m_Height: 2
m_Direction: 1
m_Center: {x: 0, y: 1, z: 0}
--- !u!114 &4093145920273822261
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4600110157238723781}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9520a47fc61d5ab4ca99cdac2d574909, type: 3}
m_Name:
m_EditorClassIdentifier:
m_ClientVisuals: {fileID: 1935255767028652502}

19
Assets/BossRoom/Scripts/Server/ServerCharacterMovement.cs


PathFollowing = 1,
}
/// <summary>
/// Component responsible for moving a character on the server side based on inputs.
/// </summary>
[RequireComponent(typeof(NetworkCharacterState))] // TODO decouple this from network character state?
public class ServerMovement : NetworkedBehaviour
[RequireComponent(typeof(NetworkCharacterState))]
public class ServerCharacterMovement : NetworkedBehaviour
{
private NavMeshAgent navMeshAgent;
private NetworkCharacterState networkCharacterState;

private void SetMovementTarget(Vector3 position)
{
// Receive a target movement position and calculate the navmesh path for moving towards it.
movementState = MovementState.PathFollowing;
// Recalculate navigation path only on target change.
movementState = MovementState.PathFollowing;
}
private void Awake()

{
Movement();
}
// Send new position values to the client
networkCharacterState.NetworkPosition.Value = transform.position;
networkCharacterState.NetworkRotationY.Value = transform.rotation.eulerAngles.y;
}
private void Movement()

navMeshAgent.Move(movementVector);
transform.rotation = Quaternion.LookRotation(movementVector);
//navMeshAgent.CalculatePath(corners[corners.Length - 1], path);
navMeshAgent.CalculatePath(corners[corners.Length - 1], path);
}
}
}

17
Assets/BossRoom/Scripts/Shared/NetworkCharacterState.cs


namespace BossRoom.Shared
{
// RPCStateComponent from the GDD
/// <summary>
/// Contains all NetworkedVars and RPCs of a character. This component is present on both client and server objects.
/// </summary>
// TODO Should we use Unity events or c# events?
/// <summary>
/// Gets invoked when inputs are received from the client which own this networked character.
/// </summary>
/// <summary>
/// RPC to send inputs for this character from a client to a server.
/// </summary>
/// <param name="movementTarget">The position which this character should move towards.</param>
public void ServerRpcReceiveMovementInput(Vector3 position)
public void SendCharacterInputServerRpc(Vector3 movementTarget)
// Assumption that RPC is snaphshotted and buffered already here
OnReceivedClientInput?.Invoke(position);
OnReceivedClientInput?.Invoke(movementTarget);
}
}
}

44
Assets/BossRoom/Scripts/Client/ClientCharacterVisualization.cs


using BossRoom.Shared;
using MLAPI;
using UnityEngine;
namespace BossRoom.Client
{
/// <summary>
///
/// </summary>
[RequireComponent(typeof(NetworkCharacterState))]
public class ClientCharacterVisualization : NetworkedBehaviour
{
private NetworkCharacterState networkCharacterState;
/// <summary>
/// The GameObject which visually represents the character is a child object of the character GameObject. This needs to be the case to support host mode.
/// In host mode <see cref="MonoBehaviour.transform"/> is the transform which is relevant for gameplay.
/// <see cref="m_ClientVisuals"/> is the visual representation on the client side which has interpolated position values.
/// </summary>
[SerializeField] private Transform m_ClientVisuals;
/// <inheritdoc />
public override void NetworkStart()
{
if (!IsClient)
{
enabled = false;
}
}
void Awake()
{
networkCharacterState = GetComponent<NetworkCharacterState>();
}
void Update()
{
// TODO Needs core sdk support. This and rotation should grab the interpolated value of network position based on the last received snapshots.
m_ClientVisuals.position = networkCharacterState.NetworkPosition.Value;
m_ClientVisuals.rotation = Quaternion.Euler(0, networkCharacterState.NetworkRotationY.Value, 0);
}
}
}

11
Assets/BossRoom/Scripts/Client/ClientInput.cs.meta


fileFormatVersion: 2
guid: 331c67f15523ad7419792662b9768f44
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

36
Assets/BossRoom/Scripts/Client/ClientCharacter.cs


using BossRoom.Shared;
using MLAPI;
using UnityEngine;
namespace BossRoom.Client
{
[RequireComponent(typeof(NetworkCharacterState))]
public class ClientCharacter: NetworkedBehaviour
{
private NetworkCharacterState networkCharacterState;
[SerializeField]
private Transform clientInterpolatedObject;
public override void NetworkStart()
{
if (!IsClient)
{
enabled = false;
}
}
void Awake()
{
networkCharacterState = GetComponent<NetworkCharacterState>();
}
void Update()
{
// TODO Needs core sdk support. This and rotation should grab the interpolated value of network position based on the last received snapshots.
clientInterpolatedObject.position = networkCharacterState.NetworkPosition.Value;
clientInterpolatedObject.rotation = Quaternion.Euler(0, networkCharacterState.NetworkRotationY.Value, 0);
}
}
}

49
Assets/BossRoom/Scripts/Client/ClientInput.cs


using BossRoom.Shared;
using MLAPI;
using UnityEngine;
namespace BossRoom.Client
{
[RequireComponent(typeof(NetworkCharacterState))]
public class ClientInput : NetworkedBehaviour
{
private NetworkCharacterState networkCharacter;
public override void NetworkStart()
{
// TODO The entire disabling/enabling is still sketchy and the reason why this has to be a NetworkedBehaviour
if (!IsClient)
{
enabled = false;
}
}
void Awake()
{
networkCharacter = GetComponent<NetworkCharacterState>();
}
// Update is called once per frame
void FixedUpdate()
{
// EDU Multiplayer games poll update in fixed step because server processes game simulation in a fixed step as well
// TODO can we use new Unity input system which supports fixed update polling? Right now implementation is broken
// Is mouse button pressed (not checking for down for continuous input)
if (Input.GetMouseButton(0))
{
RaycastHit hit;
// TODO Camera.main is horrible in Unity < 2020.2
if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
{
// TODO Send reliable sequenced
// TODO Call syntax is still ugly
networkCharacter.InvokeServerRpc(networkCharacter.ServerRpcReceiveMovementInput, hit.point, "MLAPI_INTERNAL");
}
}
}
}
}

/Assets/BossRoom/Scripts/Client/ClientCharacter.cs.meta → /Assets/BossRoom/Scripts/Client/ClientCharacterVisualization.cs.meta

/Assets/BossRoom/Scripts/Server/ServerMovement.cs.meta → /Assets/BossRoom/Scripts/Server/ServerCharacterMovement.cs.meta

/Assets/BossRoom/Scripts/Server/ServerMovement.cs → /Assets/BossRoom/Scripts/Server/ServerCharacterMovement.cs

正在加载...
取消
保存