Ported the local prediction player controller component
This commit is contained in:
@@ -1,114 +0,0 @@
|
||||
/*
|
||||
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
||||
* its licensors.
|
||||
*
|
||||
* For complete copyright and license terms please see the LICENSE at the root of this
|
||||
* distribution (the "License"). All use of this software is governed by the License,
|
||||
* or, if provided, by the license below or the license accompanying this file. Do not
|
||||
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AzCore/Time/ITime.h>
|
||||
#include <AzNetworking/ConnectionLayer/IConnection.h>
|
||||
|
||||
namespace Multiplayer
|
||||
{
|
||||
//! This is a strong typedef for representing the number of application frames since application start.
|
||||
AZ_TYPE_SAFE_INTEGRAL(ApplicationFrameId, uint32_t);
|
||||
static constexpr ApplicationFrameId InvalidApplicationFrameId = ApplicationFrameId{0xFFFFFFFF};
|
||||
|
||||
//! @class INetworkTime
|
||||
//! @brief This is an AZ::Interface<> for managing multiplayer specific time related operations.
|
||||
class INetworkTime
|
||||
{
|
||||
public:
|
||||
AZ_RTTI(INetworkTime, "{7D468063-255B-4FEE-86E1-6D750EEDD42A}");
|
||||
|
||||
INetworkTime() = default;
|
||||
virtual ~INetworkTime() = default;
|
||||
|
||||
//! Converts from an ApplicationFrameId to a corresponding TimeMs.
|
||||
//! @param frameId the ApplicationFrameId to convert to a TimeMs
|
||||
//! @return the TimeMs that corresponds to the provided ApplicationFrameId
|
||||
virtual AZ::TimeMs ConvertFrameIdToTimeMs(ApplicationFrameId frameId) const = 0;
|
||||
|
||||
//! Converts from a TimeMs to an ApplicationFrameId.
|
||||
//! @param timeMs the TimeMs to convert to an ApplicationFrameId
|
||||
//! @return the ApplicationFrameId that corresponds to the provided TimeMs
|
||||
virtual ApplicationFrameId ConvertTimeMsToFrameId(AZ::TimeMs timeMs) const = 0;
|
||||
|
||||
//! Returns true if the application frameId has been temporarily altered.
|
||||
//! @return true if the application frameId has been altered, false otherwise
|
||||
virtual bool IsApplicationFrameIdRewound() const = 0;
|
||||
|
||||
//! Retrieves the applications current frameId (may be rewound on the server during backward reconciliation).
|
||||
//! @return the applications current frameId
|
||||
virtual ApplicationFrameId GetApplicationFrameId() const = 0;
|
||||
|
||||
//! Retrieves the unaltered applications current frameId.
|
||||
//! @return the applications current frameId, unaltered by any scoped time instance
|
||||
virtual ApplicationFrameId GetUnalteredApplicationFrameId() const = 0;
|
||||
|
||||
//! Increments the applications current frameId.
|
||||
virtual void IncrementApplicationFrameId() = 0;
|
||||
|
||||
//! Synchronizes rewindable entity state for the current application time.
|
||||
virtual void SyncRewindableEntityState() = 0;
|
||||
|
||||
//! Get the controlling connection that may be currently altering global game time.
|
||||
//! Note this abstraction is required at a relatively high level to allow for 'don't rewind the shooter' semantics
|
||||
//! @return the ConnectionId of the connection requesting the rewind operation
|
||||
virtual AzNetworking::ConnectionId GetRewindingConnectionId() const = 0;
|
||||
|
||||
//! Get the controlling connection that may be currently altering global game time.
|
||||
//! Note this abstraction is required at a relatively high level to allow for 'don't rewind the shooter' semantics
|
||||
//! @param rewindConnectionId if this parameter matches the current rewindConnectionId, it will return the unaltered applicationFrameId
|
||||
//! @return the ApplicationFrameId taking into account the provided rewinding connectionId
|
||||
virtual ApplicationFrameId GetApplicationFrameIdForRewindingConnection(AzNetworking::ConnectionId rewindConnectionId) const = 0;
|
||||
|
||||
//! Alters the current ApplicationFrameId and binds that alteration to the provided ConnectionId.
|
||||
//! @param frameId the new ApplicationFrameId to use
|
||||
//! @param rewindConnectionId the rewinding ConnectionId
|
||||
virtual void AlterApplicationFrameId(ApplicationFrameId frameId, AzNetworking::ConnectionId rewindConnectionId) = 0;
|
||||
|
||||
AZ_DISABLE_COPY_MOVE(INetworkTime);
|
||||
};
|
||||
|
||||
// EBus wrapper for ScriptCanvas
|
||||
class INetworkTimeRequests
|
||||
: public AZ::EBusTraits
|
||||
{
|
||||
public:
|
||||
static const AZ::EBusHandlerPolicy HandlerPolicy = AZ::EBusHandlerPolicy::Single;
|
||||
static const AZ::EBusAddressPolicy AddressPolicy = AZ::EBusAddressPolicy::Single;
|
||||
};
|
||||
using INetworkTimeRequestBus = AZ::EBus<INetworkTime, INetworkTimeRequests>;
|
||||
|
||||
//! @class ScopedAlterTime
|
||||
//! @brief This is a wrapper that temporarily adjusts global program time for backward reconciliation purposes.
|
||||
class ScopedAlterTime final
|
||||
{
|
||||
public:
|
||||
inline ScopedAlterTime(ApplicationFrameId frameId, AzNetworking::ConnectionId connectionId)
|
||||
{
|
||||
INetworkTime* time = AZ::Interface<INetworkTime>::Get();
|
||||
m_previousApplicationFrameId = time->GetApplicationFrameId();
|
||||
m_previousRewindConnectionId = time->GetRewindingConnectionId();
|
||||
time->AlterApplicationFrameId(frameId, connectionId);
|
||||
}
|
||||
inline ~ScopedAlterTime()
|
||||
{
|
||||
INetworkTime* time = AZ::Interface<INetworkTime>::Get();
|
||||
time->AlterApplicationFrameId(m_previousApplicationFrameId, m_previousRewindConnectionId);
|
||||
}
|
||||
private:
|
||||
ApplicationFrameId m_previousApplicationFrameId = InvalidApplicationFrameId;
|
||||
AzNetworking::ConnectionId m_previousRewindConnectionId = AzNetworking::InvalidConnectionId;
|
||||
};
|
||||
}
|
||||
|
||||
AZ_TYPE_SAFE_INTEGRAL_SERIALIZEBINDING(Multiplayer::ApplicationFrameId);
|
||||
@@ -24,36 +24,31 @@ namespace Multiplayer
|
||||
AZ::Interface<INetworkTime>::Unregister(this);
|
||||
}
|
||||
|
||||
AZ::TimeMs NetworkTime::ConvertFrameIdToTimeMs([[maybe_unused]] ApplicationFrameId frameId) const
|
||||
{
|
||||
return AZ::TimeMs{0};
|
||||
}
|
||||
|
||||
ApplicationFrameId NetworkTime::ConvertTimeMsToFrameId([[maybe_unused]] AZ::TimeMs timeMs) const
|
||||
{
|
||||
return ApplicationFrameId{0};
|
||||
}
|
||||
|
||||
bool NetworkTime::IsApplicationFrameIdRewound() const
|
||||
bool NetworkTime::IsTimeRewound() const
|
||||
{
|
||||
return m_rewindingConnectionId != AzNetworking::InvalidConnectionId;
|
||||
}
|
||||
|
||||
ApplicationFrameId NetworkTime::GetApplicationFrameId() const
|
||||
HostFrameId NetworkTime::GetHostFrameId() const
|
||||
{
|
||||
return m_applicationFrameId;
|
||||
return m_hostFrameId;
|
||||
}
|
||||
|
||||
ApplicationFrameId NetworkTime::GetUnalteredApplicationFrameId() const
|
||||
HostFrameId NetworkTime::GetUnalteredHostFrameId() const
|
||||
{
|
||||
return m_unalteredFrameId;
|
||||
}
|
||||
|
||||
void NetworkTime::IncrementApplicationFrameId()
|
||||
void NetworkTime::IncrementHostFrameId()
|
||||
{
|
||||
AZ_Assert(!IsApplicationFrameIdRewound(), "Incrementing the global application frameId is unsupported under a rewound time scope");
|
||||
AZ_Assert(!IsTimeRewound(), "Incrementing the global application frameId is unsupported under a rewound time scope");
|
||||
++m_unalteredFrameId;
|
||||
m_applicationFrameId = m_unalteredFrameId;
|
||||
m_hostFrameId = m_unalteredFrameId;
|
||||
}
|
||||
|
||||
AZ::TimeMs NetworkTime::GetHostTimeMs() const
|
||||
{
|
||||
return m_hostTimeMs;
|
||||
}
|
||||
|
||||
void NetworkTime::SyncRewindableEntityState()
|
||||
@@ -66,14 +61,15 @@ namespace Multiplayer
|
||||
return m_rewindingConnectionId;
|
||||
}
|
||||
|
||||
ApplicationFrameId NetworkTime::GetApplicationFrameIdForRewindingConnection(AzNetworking::ConnectionId rewindConnectionId) const
|
||||
HostFrameId NetworkTime::GetHostFrameIdForRewindingConnection(AzNetworking::ConnectionId rewindConnectionId) const
|
||||
{
|
||||
return (IsApplicationFrameIdRewound() && (rewindConnectionId == m_rewindingConnectionId)) ? m_unalteredFrameId : m_applicationFrameId;
|
||||
return (IsTimeRewound() && (rewindConnectionId == m_rewindingConnectionId)) ? m_unalteredFrameId : m_hostFrameId;
|
||||
}
|
||||
|
||||
void NetworkTime::AlterApplicationFrameId(ApplicationFrameId frameId, AzNetworking::ConnectionId rewindConnectionId)
|
||||
void NetworkTime::AlterTime(HostFrameId frameId, AZ::TimeMs timeMs, AzNetworking::ConnectionId rewindConnectionId)
|
||||
{
|
||||
m_applicationFrameId = frameId;
|
||||
m_hostFrameId = frameId;
|
||||
m_hostTimeMs = timeMs;
|
||||
m_rewindingConnectionId = rewindConnectionId;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Source/NetworkTime/INetworkTime.h>
|
||||
#include <Include/INetworkTime.h>
|
||||
#include <AzCore/Component/Component.h>
|
||||
#include <AzCore/Console/IConsole.h>
|
||||
|
||||
@@ -28,22 +28,22 @@ namespace Multiplayer
|
||||
|
||||
//! INetworkTime overrides.
|
||||
//! @{
|
||||
AZ::TimeMs ConvertFrameIdToTimeMs(ApplicationFrameId frameId) const override;
|
||||
ApplicationFrameId ConvertTimeMsToFrameId(AZ::TimeMs timeMs) const override;
|
||||
bool IsApplicationFrameIdRewound() const override;
|
||||
ApplicationFrameId GetApplicationFrameId() const override;
|
||||
ApplicationFrameId GetUnalteredApplicationFrameId() const override;
|
||||
void IncrementApplicationFrameId() override;
|
||||
bool IsTimeRewound() const override;
|
||||
HostFrameId GetHostFrameId() const override;
|
||||
HostFrameId GetUnalteredHostFrameId() const override;
|
||||
void IncrementHostFrameId() override;
|
||||
AZ::TimeMs GetHostTimeMs() const override;
|
||||
void SyncRewindableEntityState() override;
|
||||
AzNetworking::ConnectionId GetRewindingConnectionId() const override;
|
||||
ApplicationFrameId GetApplicationFrameIdForRewindingConnection(AzNetworking::ConnectionId rewindConnectionId) const override;
|
||||
void AlterApplicationFrameId(ApplicationFrameId frameId, AzNetworking::ConnectionId rewindConnectionId) override;
|
||||
HostFrameId GetHostFrameIdForRewindingConnection(AzNetworking::ConnectionId rewindConnectionId) const override;
|
||||
void AlterTime(HostFrameId frameId, AZ::TimeMs timeMs, AzNetworking::ConnectionId rewindConnectionId) override;
|
||||
//! @}
|
||||
|
||||
private:
|
||||
|
||||
ApplicationFrameId m_applicationFrameId = ApplicationFrameId{0};
|
||||
ApplicationFrameId m_unalteredFrameId = ApplicationFrameId{0};
|
||||
HostFrameId m_hostFrameId = HostFrameId{ 0 };
|
||||
HostFrameId m_unalteredFrameId = HostFrameId{ 0 };
|
||||
AZ::TimeMs m_hostTimeMs = AZ::TimeMs{ 0 };
|
||||
AzNetworking::ConnectionId m_rewindingConnectionId = AzNetworking::InvalidConnectionId;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Source/NetworkTime/INetworkTime.h>
|
||||
#include <Include/INetworkTime.h>
|
||||
#include <AzNetworking/Serialization/ISerializer.h>
|
||||
#include <AzNetworking/ConnectionLayer/IConnection.h>
|
||||
#include <AzNetworking/Utilities/NetworkCommon.h>
|
||||
@@ -87,25 +87,25 @@ namespace Multiplayer
|
||||
|
||||
//! Returns what the appropriate current time is for this rewindable property.
|
||||
//! @return the appropriate current time is for this rewindable property
|
||||
ApplicationFrameId GetCurrentTimeForProperty() const;
|
||||
HostFrameId GetCurrentTimeForProperty() const;
|
||||
|
||||
//! Updates the latest value for this object instance, if frameTime represents a current or future time.
|
||||
//! Any attempts to set old values on the object will fail
|
||||
//! @param value the new value to set in the object history
|
||||
//! @param frameTime the time to set the value for
|
||||
void SetValueForTime(const BASE_TYPE& value, ApplicationFrameId frameTime);
|
||||
void SetValueForTime(const BASE_TYPE& value, HostFrameId frameTime);
|
||||
|
||||
//! Const value accessor, returns the correct value for the provided input time.
|
||||
//! @param frameTime the frame time to return the associated value for
|
||||
//! @return value given the current input time
|
||||
const BASE_TYPE& GetValueForTime(ApplicationFrameId frameTime) const;
|
||||
const BASE_TYPE& GetValueForTime(HostFrameId frameTime) const;
|
||||
|
||||
//! Helper method to compute clamped array index values accounting for the offset head index.
|
||||
AZStd::size_t GetOffsetIndex(AZStd::size_t absoluteIndex) const;
|
||||
|
||||
AZStd::array<BASE_TYPE, REWIND_SIZE> m_history;
|
||||
AzNetworking::ConnectionId m_owningConnectionId = AzNetworking::InvalidConnectionId;
|
||||
ApplicationFrameId m_headTime = ApplicationFrameId{0};
|
||||
HostFrameId m_headTime = HostFrameId{0};
|
||||
uint32_t m_headIndex = 0;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace Multiplayer
|
||||
inline RewindableObject<BASE_TYPE, REWIND_SIZE> &RewindableObject<BASE_TYPE, REWIND_SIZE>::operator =(const RewindableObject<BASE_TYPE, REWIND_SIZE>& rhs)
|
||||
{
|
||||
INetworkTime* networkTime = AZ::Interface<INetworkTime>::Get();
|
||||
SetValueForTime(rhs.GetValueForTime(networkTime->GetApplicationFrameId()), GetCurrentTimeForProperty());
|
||||
SetValueForTime(rhs.GetValueForTime(networkTime->GetHostFrameId()), GetCurrentTimeForProperty());
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@ namespace Multiplayer
|
||||
template <typename BASE_TYPE, AZStd::size_t REWIND_SIZE>
|
||||
inline BASE_TYPE& RewindableObject<BASE_TYPE, REWIND_SIZE>::Modify()
|
||||
{
|
||||
const ApplicationFrameId frameTime = GetCurrentTimeForProperty();
|
||||
const HostFrameId frameTime = GetCurrentTimeForProperty();
|
||||
if (frameTime < m_headTime)
|
||||
{
|
||||
AZ_Assert(false, "Trying to mutate a rewindable in the past");
|
||||
@@ -103,7 +103,7 @@ namespace Multiplayer
|
||||
template <typename BASE_TYPE, AZStd::size_t REWIND_SIZE>
|
||||
inline bool RewindableObject<BASE_TYPE, REWIND_SIZE>::Serialize(AzNetworking::ISerializer& serializer)
|
||||
{
|
||||
const ApplicationFrameId frameTime = GetCurrentTimeForProperty();
|
||||
const HostFrameId frameTime = GetCurrentTimeForProperty();
|
||||
BASE_TYPE value = GetValueForTime(frameTime);
|
||||
if (serializer.Serialize(value, "Element") && (serializer.GetSerializerMode() == AzNetworking::SerializerMode::WriteToObject))
|
||||
{
|
||||
@@ -113,14 +113,14 @@ namespace Multiplayer
|
||||
}
|
||||
|
||||
template <typename BASE_TYPE, AZStd::size_t REWIND_SIZE>
|
||||
inline ApplicationFrameId RewindableObject<BASE_TYPE, REWIND_SIZE>::GetCurrentTimeForProperty() const
|
||||
inline HostFrameId RewindableObject<BASE_TYPE, REWIND_SIZE>::GetCurrentTimeForProperty() const
|
||||
{
|
||||
INetworkTime* networkTime = AZ::Interface<INetworkTime>::Get();
|
||||
return networkTime->GetApplicationFrameIdForRewindingConnection(m_owningConnectionId);
|
||||
return networkTime->GetHostFrameIdForRewindingConnection(m_owningConnectionId);
|
||||
}
|
||||
|
||||
template <typename BASE_TYPE, AZStd::size_t REWIND_SIZE>
|
||||
inline void RewindableObject<BASE_TYPE, REWIND_SIZE>::SetValueForTime(const BASE_TYPE& value, ApplicationFrameId frameTime)
|
||||
inline void RewindableObject<BASE_TYPE, REWIND_SIZE>::SetValueForTime(const BASE_TYPE& value, HostFrameId frameTime)
|
||||
{
|
||||
if (frameTime < m_headTime)
|
||||
{
|
||||
@@ -155,7 +155,7 @@ namespace Multiplayer
|
||||
}
|
||||
|
||||
template <typename BASE_TYPE, AZStd::size_t REWIND_SIZE>
|
||||
inline const BASE_TYPE &RewindableObject<BASE_TYPE, REWIND_SIZE>::GetValueForTime(ApplicationFrameId frameTime) const
|
||||
inline const BASE_TYPE &RewindableObject<BASE_TYPE, REWIND_SIZE>::GetValueForTime(HostFrameId frameTime) const
|
||||
{
|
||||
if (frameTime > m_headTime)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user