PlayerIdentityComponent.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project. For complete copyright and license terms please see the LICENSE at the root of this distribution.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0 OR MIT
  5. *
  6. */
  7. #pragma once
  8. #include <MultiplayerSampleTypes.h>
  9. #include <PlayerIdentityBus.h>
  10. #include <Source/AutoGen/PlayerIdentityComponent.AutoComponent.h>
  11. #include <Atom/RPI.Public/ViewportContext.h>
  12. #include <AzCore/Component/TickBus.h>
  13. #include <AzFramework/Font/FontInterface.h>
  14. #if AZ_TRAIT_CLIENT
  15. namespace AZ::RPI
  16. {
  17. class ViewportContext;
  18. }
  19. #endif
  20. namespace MultiplayerSample
  21. {
  22. class PlayerIdentityComponent
  23. : public PlayerIdentityComponentBase
  24. #if AZ_TRAIT_CLIENT
  25. , AZ::TickBus::Handler
  26. #endif
  27. {
  28. public:
  29. AZ_MULTIPLAYER_COMPONENT(MultiplayerSample::PlayerIdentityComponent, s_playerIdentityComponentConcreteUuid, MultiplayerSample::PlayerIdentityComponentBase);
  30. static void Reflect(AZ::ReflectContext* context);
  31. void OnActivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  32. void OnDeactivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  33. private:
  34. #if AZ_TRAIT_CLIENT
  35. static constexpr float FontScale = 0.7f;
  36. // AZ::TickBus::Handler overrides...
  37. void OnTick(float deltaTime, AZ::ScriptTimePoint time) override;
  38. // Cache properties required for font rendering...
  39. AZ::RPI::ViewportContextPtr m_viewport;
  40. AzFramework::FontDrawInterface* m_fontDrawInterface = nullptr;
  41. AzFramework::TextDrawParameters m_drawParams;
  42. #endif
  43. };
  44. class PlayerIdentityComponentController
  45. : public PlayerIdentityComponentControllerBase
  46. , public PlayerIdentityRequestBus::Handler
  47. {
  48. public:
  49. explicit PlayerIdentityComponentController(PlayerIdentityComponent& parent);
  50. void OnActivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  51. void OnDeactivate(Multiplayer::EntityIsMigrating entityIsMigrating) override;
  52. #if AZ_TRAIT_SERVER
  53. void HandleRPC_AssignPlayerName(AzNetworking::IConnection* invokingConnection, const PlayerNameString& newPlayerName) override;
  54. void HandleRPC_ResetPlayerState(AzNetworking::IConnection* invokingConnection, const PlayerResetOptions& resetOptions) override;
  55. #endif
  56. // PlayerIdentityRequestBus overrides ...
  57. const char* GetPlayerIdentityName() override;
  58. private:
  59. AZ::Event<PlayerNameString>::Handler m_onAutomonousPlayerNameChanged{ [](const PlayerNameString& playerName)
  60. {
  61. PlayerIdentityNotificationBus::Broadcast(&PlayerIdentityNotifications::OnAutonomousPlayerNameChanged, playerName.c_str());;
  62. } };
  63. };
  64. }